# Grid Trading

All Grid Trading API endpoints require authentication. Please create OKX API keys to interact with these functions successfully. Accounts must be funded or collateralized for any trades to successfully post.

## Grid Trading

### Place Grid Algo Order

{% hint style="info" %}
Please refer to the official OKX API Docs for required parameters.

<https://www.okx.com/docs-v5/en/#rest-api-grid-trading-place-grid-algo-order>
{% endhint %}

#### Function Name:

```javascript
placeGridAlgoOrder()
```

#### Usage:

```javascript
import { createExchange } from "./exchanges/exchange.js";

  let myOkxAccount = createExchange({
      exchange: "okx",
      authenticate: true,
      key: "myKeys",
      secret: "mySecret",
      passphrase: "myPassphrase",
      label: "okx",
      marginType: "usdt"
});

async function placeGridAlgoOrder(exchange, options) {
    try {
        let result = await exchange.placeGridAlgoOrder(options);
        console.log(result);
    } catch (error) {
        console.log(error.message);
    }
}

const result = await placeGridAlgoOrder(myOkxAccount,
{
    instId: "BTC-USDT",
    algoOrdType: "grid",
    maxPx: "5000",
    minPx: "400",
    gridNum: "10",
    runType: "1",
    quoteSz: "25"
})
```

### Amend Grid Algo Order

{% hint style="info" %}
Please refer to the official OKX API Docs for required parameters.

<https://www.okx.com/docs-v5/en/#rest-api-grid-trading-amend-grid-algo-order>
{% endhint %}

#### Function Name:

```javascript
amendGrid()
```

#### Usage:

```javascript
import { createExchange } from "./exchanges/exchange.js";

  let myOkxAccount = createExchange({
      exchange: "okx",
      authenticate: true,
      key: "myKeys",
      secret: "mySecret",
      passphrase: "myPassphrase",
      label: "okx",
      marginType: "usdt"
});

async function amendGrid(exchange, options) {
    try {
        let result = await exchange.amendGrid(options);
        console.log(result);
    } catch (error) {
        console.log(error.message);
    }
}

const result = await amendGrid(myOkxAccount,
{
    algoId:"448965992920907776",
    instId:"BTC-USDT-SWAP",
    slTriggerPx:"1200",
    tpTriggerPx:""
})
```

### Stop Grid Algo Order

{% hint style="info" %}
Please refer to the official OKX API Docs for required parameters.

<https://www.okx.com/docs-v5/en/#rest-api-grid-trading-stop-grid-algo-order>
{% endhint %}

#### Function Name:

```javascript
stopGrid()
```

#### Usage:

```javascript
import { createExchange } from "./exchanges/exchange.js";

  let myOkxAccount = createExchange({
      exchange: "okx",
      authenticate: true,
      key: "myKeys",
      secret: "mySecret",
      passphrase: "myPassphrase",
      label: "okx",
      marginType: "usdt"
});

async function stopGrid(exchange, options) {
    try {
        let result = await exchange.stopGrid(options);
        console.log(result);
    } catch (error) {
        console.log(error.message);
    }
}

const result = await stopGrid(myOkxAccount,
{
    [
    {
        algoId:"198273485",
        instId:"BTC-USDT",
        stopType:"1",
        algoOrdType:"grid"
    }
    ]
})
```

### Get Grid Algo Order List

{% hint style="info" %}
Please refer to the official OKX API Docs for required parameters.

<https://www.okx.com/docs-v5/en/#rest-api-grid-trading-get-grid-algo-order-list>
{% endhint %}

#### Function Name:

```javascript
getPendingGridOrders()
```

#### Usage:

```javascript
import { createExchange } from "./exchanges/exchange.js";

  let myOkxAccount = createExchange({
      exchange: "okx",
      authenticate: true,
      key: "myKeys",
      secret: "mySecret",
      passphrase: "myPassphrase",
      label: "okx",
      marginType: "usdt"
});

async function getPendingGridOrders(exchange, options) {
    try {
        let result = await exchange.getPendingGridOrders(options);
        console.log(result);
    } catch (error) {
        console.log(error.message);
    }
}

const result = await getPendingGridOrders(myOkxAccount,
{
        algoOrdType:"grid"
})
```

### Get Grid Algo Order History

{% hint style="info" %}
Please refer to the official OKX API Docs for required parameters.

<https://www.okx.com/docs-v5/en/#rest-api-grid-trading-get-grid-algo-order-history>
{% endhint %}

#### Function Name:

```javascript
getGridOrderHistory()
```

#### Usage:

```javascript
import { createExchange } from "./exchanges/exchange.js";

  let myOkxAccount = createExchange({
      exchange: "okx",
      authenticate: true,
      key: "myKeys",
      secret: "mySecret",
      passphrase: "myPassphrase",
      label: "okx",
      marginType: "usdt"
});

async function getGridOrderHistory(exchange, options) {
    try {
        let result = await exchange.getGridOrderHistory(options);
        console.log(result);
    } catch (error) {
        console.log(error.message);
    }
}

const result = await getGridOrderHistory(myOkxAccount,
{
        algoOrdType:"grid"
})
```

### Get Grid Algo Order Details

{% hint style="info" %}
Please refer to the official OKX API Docs for required parameters.

<https://www.okx.com/docs-v5/en/#rest-api-grid-trading-get-grid-algo-order-details>
{% endhint %}

#### Function Name:

```javascript
getGridOrderDetails()
```

#### Usage:

```javascript
import { createExchange } from "./exchanges/exchange.js";

  let myOkxAccount = createExchange({
      exchange: "okx",
      authenticate: true,
      key: "myKeys",
      secret: "mySecret",
      passphrase: "myPassphrase",
      label: "okx",
      marginType: "usdt"
});

async function getGridOrderDetails(exchange, options) {
    try {
        let result = await exchange.getGridOrderDetails(options);
        console.log(result);
    } catch (error) {
        console.log(error.message);
    }
}

const result = await getGridOrderDetails(myOkxAccount,
{
        algoId: "448965992920907776",
        algoOrdType: "grid"
})
```

### Get Grid Algo Sub Orders

{% hint style="info" %}
Please refer to the official OKX API Docs for required parameters.

<https://www.okx.com/docs-v5/en/#rest-api-grid-trading-get-grid-algo-sub-orders>
{% endhint %}

#### Function Name:

```javascript
getGridSubOrders()
```

#### Usage:

```javascript
import { createExchange } from "./exchanges/exchange.js";

  let myOkxAccount = createExchange({
      exchange: "okx",
      authenticate: true,
      key: "myKeys",
      secret: "mySecret",
      passphrase: "myPassphrase",
      label: "okx",
      marginType: "usdt"
});

async function getGridSubOrders(exchange, options) {
    try {
        let result = await exchange.getGridSubOrders(options);
        console.log(result);
    } catch (error) {
        console.log(error.message);
    }
}

const result = await getGridSubOrders(myOkxAccount,
{
        algoId: "448965992920907776",
        algoOrdType: "grid"
})
```

### Get Grid Algo Order Positions

{% hint style="info" %}
Please refer to the official OKX API Docs for required parameters.

<https://www.okx.com/docs-v5/en/#rest-api-grid-trading-get-grid-algo-order-positions>
{% endhint %}

#### Function Name:

```javascript
getGridPositions()
```

#### Usage:

```javascript
import { createExchange } from "./exchanges/exchange.js";

  let myOkxAccount = createExchange({
      exchange: "okx",
      authenticate: true,
      key: "myKeys",
      secret: "mySecret",
      passphrase: "myPassphrase",
      label: "okx",
      marginType: "usdt"
});

async function getGridPositions(exchange, options) {
    try {
        let result = await exchange.getGridPositions(options);
        console.log(result);
    } catch (error) {
        console.log(error.message);
    }
}

const result = await getGridPositions(myOkxAccount,
{
        algoId: "448965992920907776",
        algoOrdType: "contract_grid"
})
```

### Spot Grid Withdraw Income

{% hint style="info" %}
Please refer to the official OKX API Docs for required parameters.

<https://www.okx.com/docs-v5/en/#rest-api-grid-trading-spot-moon-grid-withdraw-income>
{% endhint %}

#### Function Name:

```javascript
spotGridWithdraw()
```

#### Usage:

```javascript
import { createExchange } from "./exchanges/exchange.js";

  let myOkxAccount = createExchange({
      exchange: "okx",
      authenticate: true,
      key: "myKeys",
      secret: "mySecret",
      passphrase: "myPassphrase",
      label: "okx",
      marginType: "usdt"
});

async function spotGridWithdraw(exchange, options) {
    try {
        let result = await exchange.spotGridWithdraw(options);
        console.log(result);
    } catch (error) {
        console.log(error.message);
    }
}

const result = await spotGridWithdraw(myOkxAccount,
{
    algoId:"448965992920907776"
})
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.compendium.finance/pendax/using-pendax-sdk/okx-functions/grid-trading.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
