src/Deepchart/Trading/MovingAverageCross.cs · Calculated on: desktop only
A fast and a slow moving average. When they cross on a closed bar, the indicator sends an entry order with its brackets: stop, break-even, trailing, up to two targets. Every option of the order is a parameter.
What it shows
TradingApi: an entry with brackets, the position, a pending order, the reason of a refusal.- Where the orders are accepted: a backtest, or a simulation account in realtime.
- A trading indicator which gives the same result after every recalculation.
- Nested parameter classes, each one a group in the settings window.
Before you run it
1
Enable the trading
Trading > Entry > Enabled is off by default: the indicator starts as two plain averages. Turn it on and choose the order type, the stop and the targets.2
Backtest
Right-click the chart: Autotrading > Execute Backtest. The chart is loaded again on a backtesting account, the orders of every cross are filled on the bars, and the report opens.
3
Or realtime on a simulation account
Select a SIM account in the trading panel of the chart. From then on every cross of a closed bar sends an order. The crosses of the history are skipped.
The cross
The averages are two running sums, as in Simple Moving Average.OnClose compares them and keeps the side of the last closed bar:
lastSide: there is no cross until a bar on the other side. OnEnd updates the points of the open bar but takes no signal from it.
When to trade
The entry
Before the order, the indicator removes a limit entry of the previous cross still waiting, reads the position, and closes the opposite one whenClose the opposite position is on:
The brackets
The stop takes the whole quantity and carries the break-even and trailing options. Each target takes its own part and can move the stop to break-even when it is filled:
Distances are ticks from the fill price. The brackets of one entry are OCO: a target which is filled reduces the stop, the stop which is filled cancels the targets.
OnSet checks that the targets do not exceed the entry quantity, and OnLoad shows the problem in StatusMessage.
Things to notice
TradingApiisnullon the server.OnLoadsays so inStatusMessageinstead of failing.- The pending entry is removed by its id with
RemoveOrder.CancelAllOrderswould also cancel the brackets of the open position. - The time filter reads
bar.exchDt, the exchange time of the bar, never the clock: the indicator must give the same result for the same bars. - Every parameter group is a class with
[VolCustom(PropertyIndex = ...)]on its properties: the order inside the group is yours.
Try
- Add a unit for the stop and the targets: fixed ticks, or the range of the bar of the cross plus some ticks.
- Draw a marker on the bar of every order sent, colored by side.
- Move the stop yourself with
ModifyBracketwhen a condition of your own is met, instead of the break-even option.