diff --git a/03 Writing Algorithms/22 Trading and Orders/10 Order Errors/99 Examples.html b/03 Writing Algorithms/22 Trading and Orders/10 Order Errors/99 Examples.html new file mode 100644 index 0000000000..8a64870c5d --- /dev/null +++ b/03 Writing Algorithms/22 Trading and Orders/10 Order Errors/99 Examples.html @@ -0,0 +1,68 @@ +

The following examples demonstrate common practices for handling order errors.

+ +

Example 1: Pre-Open Order Error

+

The following algorithm simulate extended market hour trading on Tradier Brokerage. We place a market-on-open order at 7:30am and expect the order cannot submit to the broker since it is not supported, resulting into an error.

+
+
public class OrderErrorsAlgorithm : QCAlgorithm
+{
+    private Symbol _spy;
+
+    public override void Initialize()
+    {
+        SetStartDate(2022, 1, 1);
+        SetEndDate(2022, 1, 5);
+        // Simulate Tradier brokerage, which does not support the market-on-open orders.
+        SetBrokerageModel(BrokerageName.TradierBrokerage, AccountType.Cash);
+
+        // Request extended market hour SPY data for trading.
+        _spy = AddEquity("SPY", extendedMarketHours: true).Symbol;
+
+        // Set a scheduled event to trade 2 hours pre-open to place the market-on-open-order.
+        Schedule.On(
+            DateRules.EveryDay(_spy),
+            TimeRules.BeforeMarketOpen(_spy, 120),
+            OpenPosition
+        );
+    }
+
+    private void OpenPosition()
+    {
+        // Buy on market open will result in an error since Tradier does not support it.
+        MarketOnOpenOrder(_spy, 10);
+    }
+
+    // It will be triggered on live trading.
+    public override void OnBrokerageMessage(BrokerageMessageEvent message)
+    {
+        if (message.Type == BrokerageMessageType.Error)
+        {
+            Log($"{Time}: {message.Type}: Message: {message.Message}");
+        }
+    }
+}
+
class OrderErrorsAlgorithm(QCAlgorithm):
+    def initialize(self) -> None:
+        self.set_start_date(2022, 1, 1)
+        self.set_end_date(2022, 1, 5)
+        # Simulate Tradier brokerage, which does not support the market-on-open orders.
+        self.set_brokerage_model(BrokerageName.TRADIER_BROKERAGE, AccountType.CASH)
+
+        # Request extended market hour SPY data for trading.
+        self.spy = self.add_equity("SPY", extended_market_hours=True).symbol
+
+        # Set a scheduled event to trade 2 hours pre-open to place the market-on-open-order.
+        self.schedule.on(
+            self.date_rules.every_day(self.spy),
+            self.time_rules.before_market_open(self.spy, 120),
+            self.open_position
+        )
+
+    def open_position(self) -> None:
+        # Buy on market open will result in an error since Tradier does not support it.
+        self.market_on_open_order(self.spy, 10)
+
+    # It will be triggered on live trading.
+    def on_brokerage_message(self, message: BrokerageMessageEvent) -> None: 
+        if message.type == BrokerageMessageType.ERROR:
+            self.log(f"{self.time}: {message.type}: Message: {message.message}")
+