Skip to main content
An indicator can send orders through TradingApi, a field of Indicator of type ITradingAPI (namespace VolSysAPI.Trading). It sends them to the account selected on the chart, for the symbol of the chart, and the orders are shown and filled like the ones of the trading panel.
TradingApi is [DesktopOnly]: on the server it is null, and a package which uses it is calculated on the desktop, see Desktop only features. Check it for null before using it.

Where the orders are accepted

Automatic trading is meant to test a strategy, not to trade a real account. Every request is checked against the account of the chart and the phase of the calculation: A refused request returns -1 (or false) and the reason. Other reasons are a chart without a symbol (Trading symbol not found), a chart still loading (Chart is not initialized yet), a limit or stop order without a price, a quantity of zero, and a bracket without quantity or distance.

Backtesting

A backtest is the history of the chart calculated again on a backtesting account: the orders sent during the calculation are filled on the bars, and the result is a report.
1

Prepare the indicator

Add it to the chart and enable its trading parameters. Nothing happens yet: the orders of the history are refused on the account of the chart.
2

Execute Backtest

Right-click the chart and choose Autotrading > Execute Backtest. Deepchart creates a backtesting account, switches trading and the historical fills on, and loads the chart again. During this load VAn.ChartPhase has the Backtesting flag, OnClose(isRt: false) is called for every bar, and every order is accepted and filled.
3

Read the report

When the history is done the performance report opens. Autotrading > Show Report opens it again, Save Report writes it to a file, Reset Backtest drops the account and gives the trading panel back.
In the indicator, the backtest is recognised from the phase:
A change of a parameter calculates the indicator again from the first bar. On the chart of a backtest the orders of this recalculation are accepted only while the account is still the backtesting one: run Execute Backtest again to be sure of what the report contains.

Simulation in realtime

Select a simulation account in the trading panel of the chart. The orders sent in realtime (isRt is true) are accepted; the ones of the history are refused. Right after the load, while the first batch of realtime updates is applied, the phase still has RealtimeFlushing and an order is refused too: the indicator sees the reason in the log and the next signal goes through. The history of a chart on a simulation account is not a backtest. An indicator which sends orders only when isRt || isBacktest is true behaves the same on both accounts, and never sends the orders of the history twice after a recalculation.

The order request

InsertOrder(OrderInsertRequest request, out string reason) returns the id of the order, or -1 with the reason.

Brackets

A bracket is an order attached to the entry, created when the entry is filled. The brackets of one entry are OCO: a target which is filled reduces the quantity of the stop, a stop which is filled cancels the targets. A bracket can also be added to an existing order with InsertBracket(orderId, bracket, out reason) and moved with ModifyBracket(orderId, bracketId, ticks, price, out reason).

Positions and orders

An OrderStatusInfo has the OrderId, the OriginalOrderId the order was inserted with (a provider can change the id), the PendingQuantity and FilledQuantity, the OrderType and the OrderStatus: Submitted, PendingRequest, PendingCancel, PendingModify, Filled, Canceled, Internal (a bracket not yet sent), Failed, Waiting (an OCO order waiting on the server).

Changing and closing

Writing a trading indicator

  • Decide on OnClose. The open bar changes with every trade: a signal taken from it in realtime would not exist in the history.
  • Trade the history only in a backtest. if (!isRt && !VAn.ChartPhase.HasFlag(ChartPhaseEnum.Backtesting)) return; keeps a recalculation after a parameter change from sending the orders of the history again.
  • Check the return value. Log the reason with VAn.AddToLog and, when TradingApi is null, say it in StatusMessage from OnLoad.
  • Keep the id of your pending entry and remove it with RemoveOrder. CancelAllOrders would also cancel the brackets of the position.
  • Read the position before entering. PositionNetQuantity tells whether to add, to reverse or to do nothing.
  • Use the time of the bars, BarVars[i].exchDt, for a time filter. The indicator must not read the clock.
The Moving Average Cross Trading example puts all of this together.