Trading bot scope builder

Engineering note · Grid basket lifecycle

An MT5 Grid EA Needs One Basket State Before It Needs More Inputs

Define one two-sided basket, one-time grid levels, basket-profit closure and restart recovery before accepting a configurable MT5 grid EA.

7 minute read

Buyer question

What exactly counts as one basket when buy and sell grids run at the same time?

Failure pattern

A crossed level fires again after a tick, restart or partial close because used levels were only held in memory.

Acceptance rule

One basket owns its levels, closes as one unit and resets only after owned exposure is confirmed empty.

Inputs do not define the lifecycle

Independent lot sizes, grid distances and a basket-profit target make an EA configurable. They do not define how the EA behaves when price crosses one level twice, when only part of the basket closes or when MT5 restarts with exposure still open.

The smallest useful model gives every trading cycle one basket identity. Each accepted order belongs to that basket, each level moves from unused to used once, and the profit target moves the whole basket into a closing state before any new entry path can run.

Without that state, the code is forced to guess from position count and current price. Those facts are evidence, but they do not explain whether a level has already fired or whether a close-and-reset sequence is still in progress.

One basket owns every transition

The state must answer which positions belong to the basket, which levels have already activated, whether the target has locked new entries and whether the previous cycle is fully closed. Rebuild those answers before the first post-restart trading decision.

StateAllowed behavior
IDLENo owned basket exists. The EA may create the first buy and sell level once.
BUILDINGOne basket owns every submitted level and records each accepted order identity.
ACTIVEThe EA may add only the next unused level on each side when its distance rule is met.
TARGET_REACHEDThe basket-profit condition is true. New entries are locked before closure starts.
CLOSINGOwned positions and orders are being closed and reconciled. No new basket may start.
RESET_READYThe old basket is confirmed empty. One clean reset may return the EA to IDLE.

What the buyer should freeze

  1. 1

    Ownership

    Freeze symbol, Magic Number, order types and the treatment of manual or foreign positions.

  2. 2

    Level identity

    Give every buy and sell level a stable index and record when the broker accepts its order.

  3. 3

    Distance basis

    State whether spacing uses points, ticks, price units or another normalized value for the named symbol.

  4. 4

    Basket profit

    Define the account currency, included costs and exact owned positions used in the target calculation.

  5. 5

    Close precedence

    Lock new entries before closing starts and define how rejected or partial closes are reconciled.

  6. 6

    Restart source

    Name whether used levels come from persisted state, owned orders and history, or a documented combination.

Three acceptance cases

These cases define a working basket without promising a trading result. They are small enough to inspect and strict enough to expose the most expensive lifecycle mistakes.

Test 1

One-time level activation

Input: Price crosses the same grid level several times while the basket remains active.

Pass: That level creates no more than one owned order. Later valid unused levels remain available.

Test 2

Basket target and reset

Input: Combined profit reaches the written basket target while both buy and sell positions exist.

Pass: Entries lock, every owned position closes, the empty state is confirmed and exactly one fresh cycle becomes available.

Test 3

Restart with open levels

Input: MT5 restarts after several levels have activated but before the basket target is reached.

Pass: The EA rebuilds ownership and used-level state from the agreed evidence without duplicating an order or resetting the basket.

What these tests do not prove

A correct basket lifecycle does not prove profit, drawdown, challenge passing or favorable live execution. It proves that the written grid, ownership, close and restart rules lead to observable, repeatable software behavior.

Broker stop levels, hedging permissions, minimum volume, symbol digits and execution costs remain separate inputs. Freeze the exact MT5 build, account mode, symbol and broker rules before implementation.

Free next step

Write the basket before listing every input

Use the free scope builder to record ownership, level activation, basket closure, reset behavior and the three pass cases. That gives a developer one bounded lifecycle to quote and build.

Trading risk disclosure

Futures and forex trading contains substantial risk and is not for every investor. An investor could potentially lose all or more than the initial investment. Risk capital is money that can be lost without jeopardizing one's financial security or lifestyle. Only risk capital should be used for trading, and only those with sufficient risk capital should consider trading. Past performance is not necessarily indicative of future results.

Hypothetical performance disclosure

Hypothetical performance results have many inherent limitations. No representation is being made that any account will or is likely to achieve profits or losses similar to those shown. There are frequently sharp differences between hypothetical performance results and the actual results subsequently achieved by any particular trading program. Hypothetical results are generally prepared with the benefit of hindsight, do not involve financial risk, and cannot completely account for the impact of financial risk in actual trading. Market conditions, the ability to withstand losses, and adherence to a trading program can all materially affect actual results.

NinjaTrader trademark disclosure

NinjaTrader® is a registered trademark of NinjaTrader Group, LLC. No NinjaTrader company has any affiliation with the owner, developer, or provider of the products or services described herein, or any interest, ownership or otherwise, in any such product or service, or endorses, recommends or approves any such product or service.

Testimonials, when shown, may not represent the experience of other clients and are not a guarantee of future performance or success. Virtual-currency trading carries additional risks. See the CFTC customer advisories. Read the full financial risk disclaimer.