From b8badc7f275e0f76236f1ef3c8f076e469ce457a Mon Sep 17 00:00:00 2001 From: LouisSzeto Date: Mon, 2 Dec 2024 12:12:51 +0800 Subject: [PATCH 1/2] add example --- .../10 Order Errors/99 Examples.html | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 03 Writing Algorithms/22 Trading and Orders/10 Order Errors/99 Examples.html 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..00d27a49c6 --- /dev/null +++ b/03 Writing Algorithms/22 Trading and Orders/10 Order Errors/99 Examples.html @@ -0,0 +1,68 @@ +

The following examples demonstrate some common practices for handling order error.

+ +

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 market on open order.
+        SetBrokerageModel(BrokerageName.TradierBrokerage, AccountType.Cash);
+
+        // Request extended market hour SPY data for trading.
+        _spy = AddEquity("SPY", extendedMarketHours: true).Symbol;
+
+        // Set a schedule 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, which should will result in 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 market on open order.
+        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 schedule 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, which should will result in 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}")
+
\ No newline at end of file From 675b2601bf33a87cac96cb01b72b4ccd10109c37 Mon Sep 17 00:00:00 2001 From: Alexandre Catarino Date: Mon, 2 Dec 2024 22:35:51 +0000 Subject: [PATCH 2/2] Updates Text --- .../10 Order Errors/99 Examples.html | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) 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 index 00d27a49c6..8a64870c5d 100644 --- 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 @@ -1,4 +1,4 @@ -

The following examples demonstrate some common practices for handling order error.

+

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.

@@ -11,13 +11,13 @@

Example 1: Pre-Open Order Error

{ SetStartDate(2022, 1, 1); SetEndDate(2022, 1, 5); - // Simulate tradier brokerage, which does not support market on open order. + // 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 schedule event to trade 2 hours pre-open to place the market-on-open-order. + // 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), @@ -27,7 +27,7 @@

Example 1: Pre-Open Order Error

private void OpenPosition() { - // Buy on market open, which should will result in error since Tradier does not support it. + // Buy on market open will result in an error since Tradier does not support it. MarketOnOpenOrder(_spy, 10); } @@ -44,13 +44,13 @@

Example 1: Pre-Open Order Error

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 market on open order. + # 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 schedule event to trade 2 hours pre-open to place the market-on-open-order. + # 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), @@ -58,11 +58,11 @@

Example 1: Pre-Open Order Error

) def open_position(self) -> None: - # Buy on market open, which should will result in error since Tradier does not support it. + # 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}") - \ No newline at end of file +