RealtimeErrorHandling

View as Markdown

Definition

Defines the behavior of a strategy when a strategy generated order is returned from the broker’s server in a “Rejected” state. Default behavior is to stop the strategy, cancel any remaining working orders, and then close any open positions managed by the strategy by submitting one “Close” order for each unique position.

  • Setting this property value to IgnoreAllErrors or IgnoreAllErrorsNoAlert can have serious adverse effects on a running strategy unless you have programmed your own order rejection handling in the OnOrderUpdate() method. Users are responsible for monitoring the Log tab for error messages and managing them as needed.

  • User defined rejection handling is advanced and should ONLY be addressed by experienced programmers.

Property Value

An enum value determining how the strategy behaves. Default value is set to RealtimeErrorHandling.StopCancelClose. Possible values include:

PropertyDescription
RealtimeErrorHandling.IgnoreAllErrorsIgnores any order errors received by the strategy, continues running, and sends an alert.
RealtimeErrorHandling.IgnoreAllErrorsNoAlertIgnores any order errors received by the strategy, continues running, and does not send an alert.
RealtimeErrorHandling.StopCancelCloseDefault behavior of a strategy.
RealtimeErrorHandling.StopCancelCloseIgnoreRejectsWill perform default behavior on all errors except order rejections.

This property should ONLY be set from the OnStateChange() method during State.SetDefaults or State.Configure.

Syntax

RealtimeErrorHandling

Examples

1private Order stopLossOrder = null;
2private Order entryOrder = null;
3
4protected override void OnStateChange()
5{
6 if (State == State.Configure)
7 {
8 RealtimeErrorHandling = RealtimeErrorHandling.IgnoreAllErrors;
9 }
10}
11
12protected override void OnBarUpdate()
13{
14 if (entryOrder == null && Close[0] > Open[0])
15 EnterLong("myEntryOrder");
16
17 if (stopLossOrder == null)
18 stopLossOrder = ExitLongStopMarket(Position.AveragePrice - 10 * TickSize, "myStopLoss", "myEntryOrder");
19}
20
21protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice,
22 OrderState orderState, DateTime time, ErrorCode error, string nativeError)
23{
24 // Assign stopLossOrder in OnOrderUpdate() to ensure the assignment occurs when expected.
25 // This is more reliable than assigning Order objects in OnBarUpdate,
26 // as the assignment is not guaranteed to be complete if it is referenced immediately after submitting
27 if (order.Name == "myStopLoss" && orderState == OrderState.Filled)
28 stopLossOrder = order;
29
30 if (stopLossOrder != null && stopLossOrder == order)
31 {
32 // Rejection handling
33 if (order.OrderState == OrderState.Rejected)
34 {
35 // Stop loss order was rejected !!!!
36 // Do something about it here
37 }
38 }
39}