-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7b66b18
commit 778ef9f
Showing
4 changed files
with
272 additions
and
225 deletions.
There are no files selected for viewing
100 changes: 56 additions & 44 deletions
100
03 Writing Algorithms/26 Scheduled Events/02 Create Scheduled Events.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,70 @@ | ||
<p>To create a Scheduled Event, call the <code class="csharp">Schedule.On</code><code class="python">schedule.on</code> method. The method expects a <code>DateRules</code> object, a <code>TimeRules</code> object, and a function to execute. The following examples demonstrate some common Scheduled Events.</p> | ||
|
||
<h4>Schedule Events Before Market Open</h4> | ||
<p>You may want to train a model or fetch historical data before the market opens. The following example demonstrates how to set a Scheduled Event for 10 minutes before the market opens. <br></p> | ||
<p>You may want to train a model or fetch historical data before the market opens. The following example demonstrates how to set a Scheduled Event for 10 minutes before the market opens.</p> | ||
<div class="section-example-container"> | ||
<pre class="csharp">// Call the BeforeMarketOpen method 10 minutes before open every day SPY is trading. | ||
var symbol = QuantConnect.Symbol.Create("SPY", SecurityType.Equity, Market.USA); | ||
Schedule.On( | ||
DateRules.EveryDay(symbol), | ||
TimeRules.AfterMarketOpen(symbol, -10), | ||
BeforeMarketOpen | ||
);</pre> | ||
<pre class="python"># Call the _before_market_open method 10 minutes before open every day SPY is trading. | ||
symbol = Symbol.create('SPY', SecurityType.EQUITY, Market.USA) | ||
self.schedule.on( | ||
self.date_rules.every_day(symbol), | ||
self.time_rules.after_market_open(symbol, -10), | ||
self._before_market_open | ||
)</pre> | ||
<pre class="csharp">public override void Initialize() | ||
{ | ||
// Call the BeforeMarketOpen method 10 minutes before open every day SPY is trading. | ||
var symbol = QuantConnect.Symbol.Create("SPY", SecurityType.Equity, Market.USA); | ||
Schedule.On( | ||
DateRules.EveryDay(symbol), | ||
TimeRules.BeforeMarketOpen(symbol, 10), | ||
BeforeMarketOpen | ||
); | ||
}</pre> | ||
<pre class="python">def initialize(self): | ||
# Call the _before_market_open method 10 minutes before open every day SPY is trading. | ||
symbol = Symbol.create('SPY', SecurityType.EQUITY, Market.USA) | ||
self.schedule.on( | ||
self.date_rules.every_day(symbol), | ||
self.time_rules.before_market_open(symbol, 10), | ||
self._before_market_open | ||
)</pre> | ||
</div> | ||
|
||
<h4>Schedule Events on the Last Trading Day of the Week</h4> | ||
<p>You may want to rebalance your portfolio on the last trading day of each week and factor in market holidays. The following example demonstrates how to set a Scheduled Event for the last trading day of each week 30 minutes before the market closes.</p> | ||
<div class="section-example-container"> | ||
<pre class="csharp">// Call the Rebalance method 30 minutes before the end of the SPY trading week. | ||
var symbol = QuantConnect.Symbol.Create("SPY", SecurityType.Equity, Market.USA); | ||
Schedule.On( | ||
DateRules.WeekEnd(symbol), | ||
TimeRules.BeforeMarketClose(symbol, 30), | ||
Rebalance | ||
);</pre> | ||
<pre class="python"># Call the _rebalance method 30 minutes before the end of the SPY trading week. | ||
symbol = Symbol.create('SPY', SecurityType.EQUITY, Market.USA) | ||
self.schedule.on( | ||
self.date_rules.week_end(symbol), | ||
self.time_rules.before_market_close(symbol, 30), | ||
self._rebalance | ||
)</pre> | ||
<pre class="csharp">public override void Initialize() | ||
{ | ||
// Call the Rebalance method 30 minutes before the end of the SPY trading week. | ||
var symbol = QuantConnect.Symbol.Create("SPY", SecurityType.Equity, Market.USA); | ||
Schedule.On( | ||
DateRules.WeekEnd(symbol), | ||
TimeRules.BeforeMarketClose(symbol, 30), | ||
Rebalance | ||
); | ||
}</pre> | ||
<pre class="python">def initialize(self): | ||
# Call the _rebalance method 30 minutes before the end of the SPY trading week. | ||
symbol = Symbol.create('SPY', SecurityType.EQUITY, Market.USA) | ||
self.schedule.on( | ||
self.date_rules.week_end(symbol), | ||
self.time_rules.before_market_close(symbol, 30), | ||
self._rebalance | ||
)</pre> | ||
</div> | ||
|
||
<h4>Schedule Events on Regular Intervals Throughout the Trading Day</h4> | ||
<p>You may want to perform some action on a regular interval through each trading day. The following example demonstrates how to set a Scheduled Event for every 30 minutes through the trading day for SPY.</p> | ||
<div class="section-example-container"> | ||
<pre class="csharp">// Every day SPY is trading, call the 'SomeAction' method every 30 minutes. | ||
var symbol = QuantConnect.Symbol.Create("SPY", SecurityType.Equity, Market.USA); | ||
Schedule.On( | ||
DateRules.EveryDay(symbol), | ||
TimeRules.Every(TimeSpan.FromMinutes(30)), | ||
SomeAction | ||
);</pre> | ||
<pre class="python"># Every day SPY is trading, call the '_some_action' method every 30 minutes. | ||
symbol = Symbol.create('SPY', SecurityType.EQUITY, Market.USA) | ||
self.schedule.on( | ||
self.date_rules.every_day(symbol), | ||
self.time_rules.every(timedelta(minutes=30)), | ||
self._some_action | ||
)</pre> | ||
</div> | ||
<pre class="csharp">public override void Initialize() | ||
{ | ||
// Every day SPY is trading, call the 'SomeAction' method every 30 minutes. | ||
var symbol = QuantConnect.Symbol.Create("SPY", SecurityType.Equity, Market.USA); | ||
Schedule.On( | ||
DateRules.EveryDay(symbol), | ||
TimeRules.Every(TimeSpan.FromMinutes(30)), | ||
SomeAction | ||
); | ||
}</pre> | ||
<pre class="python">def initialize(self): | ||
# Every day SPY is trading, call the '_some_action' method every 30 minutes. | ||
symbol = Symbol.create('SPY', SecurityType.EQUITY, Market.USA) | ||
self.schedule.on( | ||
self.date_rules.every_day(symbol), | ||
self.time_rules.every(timedelta(minutes=30)), | ||
self._some_action | ||
)</pre> | ||
</div> |
44 changes: 24 additions & 20 deletions
44
03 Writing Algorithms/26 Scheduled Events/06 Remove Scheduled Events.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,29 @@ | ||
<p>If you no longer need a Scheduled Event in your algorithm, remove it so your algorithm doesn't execute unnecessary functions. To remove a Scheduled Event, save a reference to the Scheduled Event when you create it and then call the <code class="csharp">Remove</code><code class="python">remove</code> method to remove it.</p> | ||
|
||
<div class="section-example-container"> | ||
<pre class="csharp">// Remove a scheduled event by keeping a copy of the object on creation. | ||
var symbol = QuantConnect.Symbol.Create("SPY", SecurityType.Equity, Market.USA); | ||
var scheduledEvent = Schedule.On( | ||
DateRules.EveryDay(symbol), | ||
TimeRules.AfterMarketOpen(symbol, 10), | ||
TenMinutesAfterOpen | ||
); | ||
<pre class="csharp">private ScheduledEvent _scheduledEvent; | ||
|
||
// Remove the scheduled event using the object reference. | ||
Schedule.Remove(scheduledEvent); | ||
</pre> | ||
<pre class="python"># Remove a scheduled event by keeping a copy of the object on creation. | ||
symbol = Symbol.create('SPY', SecurityType.EQUITY, Market.USA) | ||
scheduled_event = self.schedule.on( | ||
self.date_rules.every_day(symbol), | ||
self.time_rules.after_market_open(symbol, 10), | ||
self._ten_minutes_after_open | ||
) | ||
public override void Initialize() | ||
{ | ||
// Remove a scheduled event by keeping a copy of the object on creation. | ||
var symbol = QuantConnect.Symbol.Create("SPY", SecurityType.Equity, Market.USA); | ||
_scheduledEvent = Schedule.On( | ||
DateRules.EveryDay(symbol), | ||
TimeRules.AfterMarketOpen(symbol, 10), | ||
TenMinutesAfterOpen | ||
) | ||
}; | ||
|
||
# Remove the scheduled event using the object reference. | ||
self.schedule.remove(scheduled_event)</pre> | ||
</div> | ||
// Remove the scheduled event using the object reference in any method | ||
Schedule.Remove(_scheduledEvent);</pre> | ||
<pre class="python">def initialize(self): | ||
# Remove a scheduled event by keeping a copy of the object on creation. | ||
symbol = Symbol.create('SPY', SecurityType.EQUITY, Market.USA) | ||
self.scheduled_event = self.schedule.on( | ||
self.date_rules.every_day(symbol), | ||
self.time_rules.after_market_open(symbol, 10), | ||
self._ten_minutes_after_open) | ||
|
||
# Remove the scheduled event using the object reference in any method | ||
self.schedule.remove(self.scheduled_event)</pre> | ||
</div> |
Oops, something went wrong.