Reusable Order Management for Single and Multiple Expert Positions
Summary
The article shows how to move repeated order lookup and state management out of an expert advisor and into reusable functions. For a single position, the example scans open orders for a matching symbol and magic number, then stores the selected order’s ticket, type, size, prices, and other properties in global variables. The trading logic can then use those values to decide whether to close or open a position based on MACD signals.
The article describes extending this approach to experts that can hold one position of each type or many positions, including arrays of order properties and a total count of matching orders. It also gives an example of displaying the profit or loss of an expert’s open positions. A timing table reports that the versions using helper functions took longer in tests than their counterparts without them, illustrating a speed cost for simpler source code. The approach is limited by its assumptions: the single-order function stores one matching order, so it does not represent multiple simultaneous positions.
Key ideas
- A reusable initialization function can scan orders by symbol and magic number and store the selected position’s properties.
- The single-position example uses stored order details to keep MACD-based entry and exit logic separate from order searching.
- Multiple-position experts can store matching order properties in arrays and track the number of orders found.
- The reported tests show longer runtimes for the versions using helper functions, trading execution speed for more compact code.
Tags
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.