From 8d637962da7ab5cf1ba4d5c626c602d832118492 Mon Sep 17 00:00:00 2001 From: LouisSzeto Date: Thu, 14 Nov 2024 16:39:24 +0800 Subject: [PATCH] add examples --- .../01 Periods/99 Examples.html | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 03 Writing Algorithms/01 Key Concepts/05 Time Modeling/01 Periods/99 Examples.html diff --git a/03 Writing Algorithms/01 Key Concepts/05 Time Modeling/01 Periods/99 Examples.html b/03 Writing Algorithms/01 Key Concepts/05 Time Modeling/01 Periods/99 Examples.html new file mode 100644 index 0000000000..b7a1ef333b --- /dev/null +++ b/03 Writing Algorithms/01 Key Concepts/05 Time Modeling/01 Periods/99 Examples.html @@ -0,0 +1,73 @@ +

The following examples demonstrate some common practices for period time modeling.

+ +

Example 1: Tick Signal Daily Security Bar

+

This example shows using a tick/pointwise signal from Smart Insider Intention dataset to buy AAPL in daily resolution. You can count down the holding trade days per each AAPL trade bar received to liquidate the position after 2 trading days.

+
+
public class PeriodTimeModelingAlgorithm : QCAlgorithm
+{
+    private Symbol _aapl;
+    private Symbol _smartInsiderIntention;
+    // For counting down the holding days per opening position.
+    private int _holdDays = 0;
+
+    public override void Initialize()
+    {
+        SetStartDate(2016, 2, 1);
+        SetEndDate(2021, 3, 1);
+
+        // Subscribe in daily resolution can aids with counting down.
+        // We order with market on open order and will hold position for full day, so daily resolution is sufficient.
+        _aapl = AddEquity("AAPL", Resolution.Daily).Symbol;
+            
+        // Requesting insider trade intention as sentiment trading signal.
+        _smartInsiderIntention = AddData<SmartInsiderIntention>(_aapl).Symbol;
+    }
+    
+    public override void OnData(Slice slice)
+    {
+        // Buy Apple whenever we receive a buyback intention or transaction notification due to volatility.
+        if (slice.ContainsKey(_smartInsiderIntention))
+        {
+            // Hold the position for 2 days to fully digest the insider intention sentiment.
+            MarketOnOpenOrder(_aapl, 100);
+            _holdDays = 2;
+        }
+
+        // Count down the holding day per each daily trade bar received.
+        if (Portfolio[_aapl].Invested && slice.Bars.ContainsKey(_aapl) && _holdDays-- <= 0)
+        {
+            // Liquidate the position if 2 days were fully counted down.
+            Liquidate(_aapl);
+        }
+    }
+}
+
class PeriodTimeModelingAlgorithm(QCAlgorithm):
+    def initialize(self) :
+        self.set_start_date(2016, 2, 1)
+        self.set_end_date(2021, 3, 1)
+        
+        # Subscribe in daily resolution can aids with counting down.
+        # We order with market on open order and will hold position for full day, so daily resolution is sufficient.
+        self.aapl = self.add_equity("AAPL", Resolution.DAILY).symbol
+
+        # Requesting insider trade intention as sentiment trading signal.
+        self.smart_insider_intention = self.add_data(SmartInsiderIntention, self.aapl).symbol
+
+        # For counting down the holding days per opening position.
+        self.hold_days = 0
+
+    def on_data(self, slice: Slice):
+        # Buy Apple whenever we receive a buyback intention or transaction notification due to volatility.
+        if self.smart_insider_intention in slice:
+            # Hold the position for 2 days to fully digest the insider intention sentiment.
+            self.market_on_open_order(self.aapl, 100)
+            self.hold_days = 2
+
+        # Count down the holding day per each daily trade bar received.
+        if self.portfolio[self.aapl].invested and self.aapl in slice.bars:
+            self.hold_days -= 1
+
+            # Liquidate the position if 2 days were fully counted down.
+            if self.hold_days <= 0:
+                self.liquidate(self.aapl)
+
\ No newline at end of file