You should be familiar with creating trade systems before starting autotrading.
A trade system provides Auto Trade with entry and exit signals and the price and size of orders. Auto Trade places, modifies, and cancels orders according to trade system parameters.
Auto Trade also monitors order state (partial fills, open and closed positions, etc.) and communicates this information to the trade system. Auto Trade and trade systems are always in sync.
A trade system evaluates trade signals and price and size functions continuously in real time according to the frequency that you set.
Trade systems and Auto Trade are allowed to execute one entry order per bar, even if the entry signal may be true multiple times within the bar.
When using Auto Trade, we recommend that you do not use end-of-bar recalculation mode due to potential differences between Trade System and Auto Trade behavior. You are encouraged to use on-every-tick recalculation mode.
For example, if entry is placed at the end of the bar and a fill is received in the next bar, the exit is not placed until the next bar out because the trade system calculates entry at the same bar it received a fill.
CQG supports three ways of sequencing parent entry and exit orders per bar, which can be specified in trade systems (on the Entry tabbed window in the Allow field):
• No selection (default) = allows only one entry and one exit per bar
• Multiple entries before exit (Pyramid) = allows entry positions to accumulate without being closed with the exit signal.
If this option is not selected, Auto Trade does not place new entry parent orders until the entry order is fully exited, even if trading signal is true. Auto Trade does continue placing child orders in the case of partial executions.
• Entry on exit = allows entry and exit to occur on the same bar
If the signal is true, the trade system tells Auto Trade to place an order with a certain price and size. If market conditions permit, an order is executed. While the order is working, a trade system may prompt the modification of size or price. If the signal is no longer true prior to the order being filled, the auto trade order is cancelled.
Auto Trade places child entry and exit orders in sequence.
Auto Trade places exit orders only up to the size of the already filled entry positions. For example: if order size = 10 and partial fill = 5, Auto Trade places an exit order for 5 contracts. If there are multiple exit orders, the size of all individual exit orders is modified such that it is not larger than what was already filled.
Typically, trade systems have multiple entry and exit orders for different purposes to manage risk and maximize profit potential.
One entry order can be matched by multiple exit orders such as a standard exit, stop loss exit, profit exit, etc. So, the cumulative size of exit orders would likely be greater than the size of the entry order because they are not expected to be executed simultaneously.
Note: Some exit signals do not work correctly with Auto Trade unless they are wrapped inside SValPropagator. Examples:
LocalHour(@)=14 => SValPropagator(LocalHour(@)=14
LocalMinute(@)=0 => SValPropagator(LocalMinute(@)=0
BarIx(@,EndOfDay)=2 => SValPropagator(BarIx(@,EndOfDay)=2).
DomBid (@,3,Single,On_Every_Change) => (SValPropagator(DomBid(@,3,Single,On_Every_Change))
DomAsk(@,3,Single,On_Every_Change)=> (SValPropagator(DomAsk(@,3,Single,On_Every_Change))
In the case of multiple exit orders with a cumulative size greater than the entry size, Auto Trade may overfill the entry order if more than one exit condition is met.
This can happen in fast moving markets where exit orders are placed relatively close to each other.
You can avoid this scenario by selecting the Restrict Exits Total Size to Open Position check box on the Exits panel of Formula Builder.
This option instructs Auto Trade to only work exits closest to the market up to the size of the total open position. Orders closer to the market are given higher priority when distributing the open position size between exit orders. The sum of working exit sizes is calculated separately for orders above market and for orders below the market to allow for profit taking opportunities and stop loss protection to be active at all times.
This example below demonstrates the logic:
Assume that we have a long trade with open position equal to 12 and six exits, three of which (A, B, C) are profit-taking limits and the other three (D, E, F) are stops. Let's assume their size formulas are the following:
A = OpenPosition / 2 = 6
B = OpenPosition / 3 = 4
C = OpenPosition / 2 = 6
D = OpenPosition / 2 = 6
E = OpenPosition / 2 = 6
F = OpenPosition / 2 = 6
At some point their price formulas yield values like this: A > B > C > Market > D > E > F.
Auto Trade places full exit C order (OpenPosition / 2 = 6), full exit B order (OpenPosition / 3 = 4), and only a part of exit A order ( OpenPosition / 6 = 2), even though exit A size formula is OpenPosition / 2. This is because the Open Position is fully covered (6+4+2=12). As for stops, the Auto Trade only places orders D and E even though exit F may also be active (6+6=12 to fully cover Open Position).
Later the values for price formulas change, so that B > C > A > Market > F > E > D.
In this case, Auto Trade places a full order A (OpenPosition / 2), keeps order C (OpenPosition / 2), and cancels the order B, since A and C orders are sufficient to cover the open position completely. As for orders below market, Auto Trade now has orders F and E working, and cancels the order D. If at any time the market price moves suddenly in any direction, the total size of executed exits does not exceed the Auto Trade open position.
Alternatively, you can avoid this scenario by formulating a trade system that explicitly instructs Auto Trade to work only one exit order at the time, based on order price closest to the market. Sample code:
A sell stop to exit a long trade that combines two stops into one, so that only one stop order works at a time:
a1:=( Low(@)[-1] WHEN BarsSinceEntry(@,0,All,ThisTradeOnly) = 0)-TickStop* TSize(@);
b1:=EntryPrice(@,0,All,ThisTradeOnly)-TickStop* TSize(@);
c1:=Max(a1,b1);
Where
a1 = low of the entry bar,
b1 = entry price less a set number of ticks
c1 = uses the higher of the 2 stops as its price.
Type this code in the Price window for Exits, and set the Signal value to 1.
You can use five order types with trade systems and Auto Trade: market, limit, stop, limit stop, and pegging. A pegging order results from a formula using the functions DOM Bid and DOM Ask.
Price can be specified as a bar value, study value, or custom function:
Open(@) = market order at the open of the next bar
Close (@) = market order at the current market price
BHI(@,Sim,20,2.00) =limit order priced at Bollinger Band high
BLO(@,Sim,20,2.00) = limit order priced at Bollinger Band low
CLO(Low(@),LookBack) = limit order priced at Channel low
OpenPositionAverageEntryPrice(@,ThisTradeOnly) - Dollar2Price(@,300) / OpenPositionSize(@,ThisTradeOnly)
OpenPositionAverageEntryPrice(@,ThisTradeOnly) - ticksize(@) *1.5
The size of an order can be a fixed size or a function/formula that evaluates order size based on specific conditions. For example:
IF(EntryProfit(@,0,Closed,ThisTradeOnly) < 0, (OpenPositionSize(@,All)[-1] *2 when BarsSinceExit(@,0,All,All)=0),2)).
BarIx(@,EndOfSession)=0 = exit is triggered on the last bar of the trading session
OptimalF (@,95000,20,2,All,0) = Ralph Vince’s OptimalF study calculates optimal initial position size
OpenPositionSize(@) and OpenPositionEntrySize(@)
OpenPositionEntrySize is equal to the sum of all entry order fills, while OpenPositionSize is OpenPositionEntrySize minus the sum of all exit order fills. The table below illustrates the distinction:
Entry order fill |
Exit order fill |
OpenPositionEntrySize |
OpenPositionSize |
5 |
- |
5 |
5 |
5 |
- |
10 |
10 |
- |
3 |
10 |
7 |
- |
3 |
10 |
4 |
5 |
- |
15 |
9 |
- |
3 |
15 |
6 |
- |
6 |
0 |
0 |
In case of partial fills, you are advised to use OpenPostionEntrySize rather than OpenPositionSize(@) formula to avoid unexpected results.
IF(EntryProfit(@,0,Closed,ThisTradeOnly) < 0, (OpenPositionSize(@,All)[-1] *2 when BarsSinceExit(@,0,All,All)=0),2)
• Backtesting results are evaluated at bar OHLC prices and so exclude signals that may have been true during the bars as in real time execution.
• A trade system’s backtesting PNL results exclude trades where entry and exit signals are realized within the same bar because the system is not able to ascertain the order of the orders. Such trades, however, are correctly executed in real-time by Auto Trade. As such, backtesting results represent a more conservative estimate of the potential of the trading system.
• You may have to modify trade system execution parameters in order to use a backtesting trade system for autotrading. Backtesting logic evaluates signals and assumes executions using historical OHLC values only, while autotrading works in real-time, potentially reacting to every tick change.
For example, consider a system that triggers an order at the cross of two moving averages and attempts to trade at the close of the bar, where signal was true.
In backtesting to evaluate historical performance of the system, you would set a signal without offset and set price to Close(@).
To achieve the same result in the autotrading, you should offset the same signal by -1 and set trading price Open(@).
This instructs Auto Trade to wait until the next bar open to execute on the signal.
Network delays are indicated in milliseconds on the Auto Trade title bar.
In the case of a network disconnection, you receive a warning message notifying you of the connectivity issues. New positions are not initiated, but Auto Trade continues working open orders. You can manage these situations using a back-up trading system or by working with your broker directly. When Auto Trade restarts, you are prompted to check positions.
If Auto Trade is disconnected from the trading gateway during a trade, you must manually restart all systems once connectivity has been re-established.
• If Remember History was off at the time of the disconnect, then Auto Trade starts a new trade (unless it has associated external trades) if turned on.
• If Remember History was on at the time of the disconnect, then orders retain their state from the moment of disconnection, and Auto Trade continues working those orders.