> For the complete documentation index, see [llms.txt](https://docs.itradingbot.net/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.itradingbot.net/uk/ai-strategy-builder/order-grid.md).

# Order-grid calculations

> Knowledge package 0.6.1, checked 22 September 2026, build 03613bfa. If the copy you are reading does not carry that line, you have a cached or older revision: fetch this page again.

This page describes the reviewed **ordinary BACKTEST**. It distinguishes the first entry, additional limit orders and signal-driven averaging. Use [main settings constraints](https://1830394604-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F6lHX8ZJXkBHNJALLsrDj%2Fuploads%2Fal06lQ1MzU9ddPZ2qxML%2Fitb-ai-0.6.1-en-main-settings-constraints.json?alt=media) for input bounds and [signal passports](/uk/ai-strategy-builder/signal-passports.md) for when a condition qualifies.

## Inputs and units

The first order is a quote-currency amount in this profile. A percentage-of-balance first order is not encoded by the adapter. Leverage is a separate setting; do not silently multiply the user's First Order value before inserting it in the form.

`in_count` is the number of **additional** orders, from 0 to 50. Zero means no additional grid levels. With additional orders enabled, SOS / `in_step_perc` is 0.1–10%, DSOS / `in_dynamic_step` is greater than 0 and at most 10, and Martingale / `in_martin_gale` is 0.5–10. The source range checks for these three grid parameters are inactive when `in_count=0`; the fields still need explicit finite values in a completed card.

Initial deposit is 10–10,000,000 and First Order is 5–1,000,000 in quote-currency units. Both commission fields are percentages from 0 to 1 inclusive. The general leverage domain is 1–125; a particular instrument can allow less. These are general input domains, not evidence of available balance or exchange acceptance.

## Additional limit orders

Let P0 and Q0 be the executed first-entry price and quantity. Let s be SOS in percentage points, d be DSOS, m be the quantity multiplier, and b=+1 for Long or −1 for Short. For additional order i, beginning at 1:

```
previousDeviation = 0 before the first additional order
distance_i = abs(previousDeviation) + s * d^(i-1)
rawPrice_i = P0 * (1 - b * distance_i / 100)
P_i = round_price(rawPrice_i)
Q_i = round_limit_quantity(P_i, previous_rounded_quantity * m)
previousDeviation = 100 * (P_i - P0) / P0
previous_rounded_quantity = Q_i
```

Price rounding feeds back into the **next** level's distance. Quantity rounding also feeds into the next order. Do not calculate every level independently from an unrounded geometric series if you need to reproduce this ordinary limit ladder.

Ignoring rounding only, total percentage distance is `s * (1 + d + ... + d^(i-1))`. When d=1 it becomes i\*s. This is a useful explanation of spacing, not a substitute for the rounded recurrence.

The reviewed price helper rounds to the nearest price tick and then to the price precision. Its Python rounding behavior matters at ties and binary floating-point boundaries. For positive quantities, quantity/step is truncated down and then rounded to the quantity precision. LIMIT and MARKET can use different quantity steps and limits.

**Equal quantities are rejected.** If the multiplier m is not 1 and, after rounding, the first-order quantity and every additional-order quantity come out equal, the reviewed ordinary BACKTEST rejects the whole deal. The arithmetic reference does not reproduce this refusal. When the first order is small relative to the quantity step, use m = 1 or a larger first order; the validator reports the mismatch before a link is generated.

After quantity rounding, minimum/maximum quantity and minimum notional are checked. A failing quantity is not automatically raised to the minimum, and the helper does not silently skip a level. Use actual instrument filters supplied by the panel; do not invent ticks, precision or notional limits.

The [independent arithmetic reference](https://1830394604-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F6lHX8ZJXkBHNJALLsrDj%2Fuploads%2F8vAZta0LMzqw6uPUiVYU%2Fitb-ai-0.6.1-en-reference-ordinary_grid.py.txt?alt=media) computes hypothetical additional LIMIT levels with explicit filters and a quantity-weighted entry price. It has no execution, account, market-data or order-submission code. It rejects unusable reference inputs and does not reproduce every runtime failure path. It does not certify the frontend preview or every other robot engine.

## Averaging by signal

In `AVERAGE_BY_SIGNAL`, an eligible card with action `AVG` requests averaging. The next averaging index i must be within the configured additional-order count. For the reviewed path:

```
step_i = s * d^(i-1)
threshold_i = round_price(last_actual_entry_price * (1 - b * step_i / 100))
requested_quantity_i = round_market_quantity(threshold_i, Q0 * m^i)
```

Here the threshold is relative to the **last actual entry or averaging fill**, and quantity derives from Q0 with a final MARKET-quantity rounding. This is different from the accumulated LIMIT recurrence.

The signal, side, threshold and task state must all qualify. PRICE\_LIMIT filters are evaluated for entry only; they do not gate signal averaging in the reviewed ordinary calculation. The averaging threshold and the EDOP gate are compared with the close of the processed bar. In the reviewed ordinary calculation, a queued market action uses the open of the next processed OHLC item. The threshold is therefore not a promised fill price; the next processed item need not be the next wall-clock second if data is missing.

## Average price, TP and SL

For same-side entry fills with quantities Qj and prices Pj, gross average entry price is `sum(Pj*Qj)/sum(Qj)`. Fees are not part of this gross weighted-price equation.

Take Profit uses the current average entry: Long `average*(1+TP/100)`, Short `average*(1-TP/100)`. In the reviewed ordinary calculation it is rebuilt as the position changes. Stop Limit / SL stays anchored to the first entry: Long `firstEntry*(1-SL/100)`, Short `firstEntry*(1+SL/100)`. Do not move SL to the average price merely because an averaging fill occurred.

The general TP and SL domain is 0–50%. Zero disables the corresponding rule. These formulas identify reference levels. They do not establish a universal exchange order type, tick rounding or guaranteed fill at that price.

## EDOP and the Deviation field

`close_by_signal_only_profit_perc` is a percentage from **−1 inclusive to 10 exclusive**. The special value −1 bypasses the extra price gate. For other values g, the reviewed EDOP check requires Long price `>= average*(1+g/100)` or Short price `<= average*(1-g/100)`. ED completion bypasses this profit/deviation restriction.

That check compares the close of the processed bar with the gross average. It does not subtract commissions or multiply by leverage, and the subsequent fill can occur at another price. “Profit-only” in the interface is not a guarantee of positive net account return. Do not infer a different range from an error-message example.

## Before interpreting a calculation

The on-screen grid preview and an ordinary BACKTEST result can differ by one price tick because they use different rounding paths. Treat the preview as an aid and verify the actual calculated entries. The arithmetic reference on this page follows the reviewed ordinary BACKTEST path; it does not describe live-robot error handling. TP and SL reference prices are not rounded to the tick in the reviewed ordinary calculation.

Verify the displayed settings, instrument filters, actual fees, selected history and date interpretation. Inspect entry, additional fills, average price and the recorded exit reason on several trades. Increasing order quantities changes exposure; an arithmetically valid ladder does not establish that the deposit can support it or that the strategy will be profitable.

This package provides settings and a testable hypothesis. Calculation results, margin/liquidation behavior, partial fills and numerical parity must be assessed from an actual run; the local reference does not simulate them.
