# Trading Examples

### Limit order vs. market order

When you open or decrease a position, you choose how the order is executed:

| Type                | `isLimitOrder` | `isIocOrder` | Behavior                                                                                                                             |
| ------------------- | -------------- | ------------ | ------------------------------------------------------------------------------------------------------------------------------------ |
| **Market order**    | `false`        | —            | Execute immediately at market. Use **slippage** (`pricesSlippage`, `collateralSlippage`) to bound worst price.                       |
| **Limit order**     | `true`         | `false`      | Stored as a pending order; executes when the price condition is met. Typically use **zero or small slippage** (`pricesSlippage: 0`). |
| **IOC limit order** | `true`         | `true`       | Fill only if the limit can be met immediately; otherwise cancel (no partial fill).                                                   |

* **Market**: best for “get in/out now”; you accept some slippage.
* **Limit**: best when you want a specific price; order stays pending until the price condition is met.
* **IOC limit**: “fill at my price or not at all” in one block.

***

### ZLP / SLP: Open position (market order)

Execute immediately with slippage protection:

```typescript
const tx = await zlpAPI.openPositionV2(
  'usdc',             // collateral token
  'btc',              // index token
  BigInt(1000000),    // size
  BigInt(100000),     // collateral amount
  ['coinObjectId'],   // coin objects
  true,               // long position
  BigInt(50000),      // reserve amount
  30000,              // index price (reference)
  1.5,                // collateral price (reference)
  false,              // isLimitOrder: false = market order
  false,              // isIocOrder (ignored for market)
  0.003,              // pricesSlippage: 0.3% for market
  0.5,                // collateralSlippage: 50% for market
  BigInt(500),        // relayer fee
  'referralAddress',
  'senderAddress'
)
```

***

### ZLP / SLP: Open position (limit order)

Pending order at your limit price; use zero or small slippage:

```typescript
const tx = await zlpAPI.openPositionV2(
  'usdc',
  'btc',
  BigInt(1000000),
  BigInt(100000),
  ['coinObjectId'],
  true,                // long
  BigInt(50000),
  30000,               // your limit price (index)
  1.5,                 // collateral price
  true,                // isLimitOrder: true = limit order
  false,               // isIocOrder: false = pending until price is met
  0,                   // pricesSlippage: 0 for limit (exact price)
  0.5,                 // collateralSlippage
  BigInt(500),
  'referralAddress',
  'senderAddress'
)
```

***

### ZLP / SLP: Open position (IOC limit order)

Fill only if the limit can be met immediately; otherwise the order does not execute:

```typescript
const tx = await zlpAPI.openPositionV2(
  'usdc',
  'btc',
  BigInt(1000000),
  BigInt(100000),
  ['coinObjectId'],
  true,
  BigInt(50000),
  30000,
  1.5,
  true,                // isLimitOrder: true
  true,                // isIocOrder: true = fill now or cancel
  0,
  0.5,
  BigInt(500),
  'referralAddress',
  'senderAddress'
)
```
