-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into feature-ib-tier-fee-model
- Loading branch information
Showing
11 changed files
with
595 additions
and
12 deletions.
There are no files selected for viewing
166 changes: 166 additions & 0 deletions
166
Algorithm.CSharp/ConsolidateHourBarsIntoDailyBarsRegressionAlgorithm.cs
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 |
---|---|---|
@@ -0,0 +1,166 @@ | ||
/* | ||
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals. | ||
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
using QuantConnect.Data; | ||
using QuantConnect.Indicators; | ||
using QuantConnect.Interfaces; | ||
using System; | ||
using System.Collections.Generic; | ||
using QuantConnect.Data.Market; | ||
|
||
namespace QuantConnect.Algorithm.CSharp | ||
{ | ||
/// <summary> | ||
/// This regression algorithm asserts the consolidated US equity daily bars from the hour bars exactly matches | ||
/// the daily bars returned from the database | ||
/// </summary> | ||
public class ConsolidateHourBarsIntoDailyBarsRegressionAlgorithm : QCAlgorithm, IRegressionAlgorithmDefinition | ||
{ | ||
private Symbol _spy; | ||
private RelativeStrengthIndex _rsi; | ||
private RelativeStrengthIndex _rsiTimeDelta; | ||
private Dictionary<DateTime, decimal> _values = new(); | ||
private int _count; | ||
private bool _indicatorsCompared; | ||
|
||
public override void Initialize() | ||
{ | ||
SetStartDate(2020, 5, 1); | ||
SetEndDate(2020, 6, 5); | ||
|
||
_spy = AddEquity("SPY", Resolution.Hour).Symbol; | ||
|
||
// We will use these two indicators to compare the daily consolidated bars equals | ||
// the ones returned from the database. We use this specific type of indicator as | ||
// it depends on its previous values. Thus, if at some point the bars received by | ||
// the indicators differ, so will their final values | ||
_rsi = new RelativeStrengthIndex("FIRST", 15, MovingAverageType.Wilders); | ||
RegisterIndicator(_spy, _rsi, Resolution.Daily, selector: (bar) => | ||
{ | ||
var tradeBar = (TradeBar)bar; | ||
return (tradeBar.Close + tradeBar.Open) / 2; | ||
}); | ||
|
||
// We won't register this indicator as we will update it manually at the end of the | ||
// month, so that we can compare the values of the indicator that received consolidated | ||
// bars and the values of this one | ||
_rsiTimeDelta = new RelativeStrengthIndex("SECOND" ,15, MovingAverageType.Wilders); | ||
} | ||
|
||
public override void OnData(Slice slice) | ||
{ | ||
if (IsWarmingUp) return; | ||
|
||
if (slice.ContainsKey(_spy) && slice[_spy] != null) | ||
{ | ||
if (Time.Month == EndDate.Month) | ||
{ | ||
var history = History(_spy, _count, Resolution.Daily); | ||
foreach (var bar in history) | ||
{ | ||
var time = bar.EndTime.Date; | ||
var average = (bar.Close + bar.Open) / 2; | ||
_rsiTimeDelta.Update(bar.EndTime, average); | ||
if (_rsiTimeDelta.Current.Value != _values[time]) | ||
{ | ||
throw new RegressionTestException($"Both {_rsi.Name} and {_rsiTimeDelta.Name} should have the same values, but they differ. {_rsi.Name}: {_values[time]} | {_rsiTimeDelta.Name}: {_rsiTimeDelta.Current.Value}"); | ||
} | ||
} | ||
_indicatorsCompared = true; | ||
Quit(); | ||
} | ||
else | ||
{ | ||
_values[Time.Date] = _rsi.Current.Value; | ||
|
||
// Since the symbol resolution is hour and the symbol is equity, we know the last bar received in a day will | ||
// be at the market close, this is 16h. We need to count how many daily bars were consolidated in order to know | ||
// how many we need to request from the history | ||
if (Time.Hour == 16) | ||
{ | ||
_count++; | ||
} | ||
} | ||
} | ||
} | ||
|
||
public override void OnEndOfAlgorithm() | ||
{ | ||
if (!_indicatorsCompared) | ||
{ | ||
throw new RegressionTestException($"Indicators {_rsi.Name} and {_rsiTimeDelta.Name} should have been compared, but they were not. Please make sure the indicators are getting SPY data"); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// This is used by the regression test system to indicate if the open source Lean repository has the required data to run this algorithm. | ||
/// </summary> | ||
public bool CanRunLocally { get; } = true; | ||
|
||
/// <summary> | ||
/// This is used by the regression test system to indicate which languages this algorithm is written in. | ||
/// </summary> | ||
public List<Language> Languages { get; } = new() { Language.CSharp, Language.Python }; | ||
|
||
/// <summary> | ||
/// Data Points count of all timeslices of algorithm | ||
/// </summary> | ||
public long DataPoints => 290; | ||
|
||
/// <summary> | ||
/// Data Points count of the algorithm history | ||
/// </summary> | ||
public int AlgorithmHistoryDataPoints => 20; | ||
|
||
/// <summary> | ||
/// Final status of the algorithm | ||
/// </summary> | ||
public AlgorithmStatus AlgorithmStatus => AlgorithmStatus.Completed; | ||
|
||
/// <summary> | ||
/// This is used by the regression test system to indicate what the expected statistics are from running the algorithm | ||
/// </summary> | ||
public Dictionary<string, string> ExpectedStatistics => new Dictionary<string, string> | ||
{ | ||
{"Total Orders", "0"}, | ||
{"Average Win", "0%"}, | ||
{"Average Loss", "0%"}, | ||
{"Compounding Annual Return", "0%"}, | ||
{"Drawdown", "0%"}, | ||
{"Expectancy", "0"}, | ||
{"Start Equity", "100000"}, | ||
{"End Equity", "100000"}, | ||
{"Net Profit", "0%"}, | ||
{"Sharpe Ratio", "0"}, | ||
{"Sortino Ratio", "0"}, | ||
{"Probabilistic Sharpe Ratio", "0%"}, | ||
{"Loss Rate", "0%"}, | ||
{"Win Rate", "0%"}, | ||
{"Profit-Loss Ratio", "0"}, | ||
{"Alpha", "0"}, | ||
{"Beta", "0"}, | ||
{"Annual Standard Deviation", "0"}, | ||
{"Annual Variance", "0"}, | ||
{"Information Ratio", "-5.215"}, | ||
{"Tracking Error", "0.159"}, | ||
{"Treynor Ratio", "0"}, | ||
{"Total Fees", "$0.00"}, | ||
{"Estimated Strategy Capacity", "$0"}, | ||
{"Lowest Capacity Asset", ""}, | ||
{"Portfolio Turnover", "0%"}, | ||
{"OrderListHash", "d41d8cd98f00b204e9800998ecf8427e"} | ||
}; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,135 @@ | ||
/* | ||
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals. | ||
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
using System.Collections.Generic; | ||
using System.Linq; | ||
using QuantConnect.Algorithm.Framework.Portfolio; | ||
using QuantConnect.Data; | ||
using QuantConnect.Data.UniverseSelection; | ||
using QuantConnect.Interfaces; | ||
using QuantConnect.Orders.Slippage; | ||
using QuantConnect.Securities; | ||
|
||
namespace QuantConnect.Algorithm.CSharp | ||
{ | ||
/// <summary> | ||
/// Example algorithm implementing VolumeShareSlippageModel. | ||
/// </summary> | ||
public class VolumeShareSlippageModelAlgorithm : QCAlgorithm, IRegressionAlgorithmDefinition | ||
{ | ||
private List<Symbol> _longs = new(); | ||
private List<Symbol> _shorts = new(); | ||
|
||
public override void Initialize() | ||
{ | ||
SetStartDate(2020, 11, 29); | ||
SetEndDate(2020, 12, 2); | ||
// To set the slippage model to limit to fill only 30% volume of the historical volume, with 5% slippage impact. | ||
SetSecurityInitializer((security) => security.SetSlippageModel(new VolumeShareSlippageModel(0.3m, 0.05m))); | ||
|
||
// Create SPY symbol to explore its constituents. | ||
var spy = QuantConnect.Symbol.Create("SPY", SecurityType.Equity, Market.USA); | ||
|
||
UniverseSettings.Resolution = Resolution.Daily; | ||
// Add universe to trade on the most and least weighted stocks among SPY constituents. | ||
AddUniverse(Universe.ETF(spy, universeFilterFunc: Selection)); | ||
} | ||
|
||
private IEnumerable<Symbol> Selection(IEnumerable<ETFConstituentUniverse> constituents) | ||
{ | ||
var sortedByDollarVolume = constituents.OrderBy(x => x.Weight).ToList(); | ||
// Add the 10 most weighted stocks to the universe to long later. | ||
_longs = sortedByDollarVolume.TakeLast(10) | ||
.Select(x => x.Symbol) | ||
.ToList(); | ||
// Add the 10 least weighted stocks to the universe to short later. | ||
_shorts = sortedByDollarVolume.Take(10) | ||
.Select(x => x.Symbol) | ||
.ToList(); | ||
|
||
return _longs.Union(_shorts); | ||
} | ||
|
||
public override void OnData(Slice slice) | ||
{ | ||
// Equally invest into the selected stocks to evenly dissipate capital risk. | ||
// Dollar neutral of long and short stocks to eliminate systematic risk, only capitalize the popularity gap. | ||
var targets = _longs.Select(symbol => new PortfolioTarget(symbol, 0.05m)).ToList(); | ||
targets.AddRange(_shorts.Select(symbol => new PortfolioTarget(symbol, -0.05m)).ToList()); | ||
|
||
// Liquidate the ones not being the most and least popularity stocks to release fund for higher expected return trades. | ||
SetHoldings(targets, liquidateExistingHoldings: true); | ||
} | ||
|
||
/// <summary> | ||
/// This is used by the regression test system to indicate if the open source Lean repository has the required data to run this algorithm. | ||
/// </summary> | ||
public bool CanRunLocally { get; } = true; | ||
|
||
/// <summary> | ||
/// This is used by the regression test system to indicate which languages this algorithm is written in. | ||
/// </summary> | ||
public List<Language> Languages { get; } = new() { Language.CSharp, Language.Python }; | ||
|
||
/// <summary> | ||
/// Data Points count of all timeslices of algorithm | ||
/// </summary> | ||
public long DataPoints => 1035; | ||
|
||
/// <summary> | ||
/// Data Points count of the algorithm history | ||
/// </summary> | ||
public int AlgorithmHistoryDataPoints => 0; | ||
|
||
/// <summary> | ||
/// Final status of the algorithm | ||
/// </summary> | ||
public AlgorithmStatus AlgorithmStatus => AlgorithmStatus.Completed; | ||
|
||
/// <summary> | ||
/// This is used by the regression test system to indicate what the expected statistics are from running the algorithm | ||
/// </summary> | ||
public Dictionary<string, string> ExpectedStatistics => new Dictionary<string, string> | ||
{ | ||
{"Total Orders", "4"}, | ||
{"Average Win", "0%"}, | ||
{"Average Loss", "0%"}, | ||
{"Compounding Annual Return", "20.900%"}, | ||
{"Drawdown", "0%"}, | ||
{"Expectancy", "0"}, | ||
{"Start Equity", "100000"}, | ||
{"End Equity", "100190.84"}, | ||
{"Net Profit", "0.191%"}, | ||
{"Sharpe Ratio", "9.794"}, | ||
{"Sortino Ratio", "0"}, | ||
{"Probabilistic Sharpe Ratio", "0%"}, | ||
{"Loss Rate", "0%"}, | ||
{"Win Rate", "0%"}, | ||
{"Profit-Loss Ratio", "0"}, | ||
{"Alpha", "0.297"}, | ||
{"Beta", "-0.064"}, | ||
{"Annual Standard Deviation", "0.017"}, | ||
{"Annual Variance", "0"}, | ||
{"Information Ratio", "-18.213"}, | ||
{"Tracking Error", "0.099"}, | ||
{"Treynor Ratio", "-2.695"}, | ||
{"Total Fees", "$4.00"}, | ||
{"Estimated Strategy Capacity", "$4400000000.00"}, | ||
{"Lowest Capacity Asset", "GOOCV VP83T1ZUHROL"}, | ||
{"Portfolio Turnover", "4.22%"}, | ||
{"OrderListHash", "9d2bd0df7c094c393e77f72b7739bfa0"} | ||
}; | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
Algorithm.Python/ConsolidateHourBarsIntoDailyBarsRegressionAlgorithm.py
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 |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals. | ||
# Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from AlgorithmImports import * | ||
|
||
### <summary> | ||
### This regression algorithm asserts the consolidated US equity daily bars from the hour bars exactly matches | ||
### the daily bars returned from the database | ||
### </summary> | ||
class ConsolidateHourBarsIntoDailyBarsRegressionAlgorithm(QCAlgorithm): | ||
def initialize(self): | ||
self.set_start_date(2020, 5, 1) | ||
self.set_end_date(2020, 6, 5) | ||
|
||
self.spy = self.add_equity("SPY", Resolution.HOUR).symbol | ||
|
||
# We will use these two indicators to compare the daily consolidated bars equals | ||
# the ones returned from the database. We use this specific type of indicator as | ||
# it depends on its previous values. Thus, if at some point the bars received by | ||
# the indicators differ, so will their final values | ||
self._rsi = RelativeStrengthIndex("First", 15, MovingAverageType.WILDERS) | ||
self.register_indicator(self.spy, self._rsi, Resolution.DAILY, selector= lambda bar: (bar.close + bar.open) / 2) | ||
|
||
# We won't register this indicator as we will update it manually at the end of the | ||
# month, so that we can compare the values of the indicator that received consolidated | ||
# bars and the values of this one | ||
self._rsi_timedelta = RelativeStrengthIndex("Second", 15, MovingAverageType.WILDERS) | ||
self._values = {} | ||
self.count = 0; | ||
self._indicators_compared = False; | ||
|
||
def on_data(self, data: Slice): | ||
if self.is_warming_up: | ||
return | ||
|
||
if data.contains_key(self.spy) and data[self.spy] != None: | ||
if self.time.month == self.end_date.month: | ||
history = self.history[TradeBar](self.spy, self.count, Resolution.DAILY) | ||
for bar in history: | ||
time = bar.end_time.strftime('%Y-%m-%d') | ||
average = (bar.close + bar.open) / 2 | ||
self._rsi_timedelta.update(bar.end_time, average) | ||
if self._rsi_timedelta.current.value != self._values[time]: | ||
raise Exception(f"Both {self._rsi.name} and {self._rsi_timedelta.name} should have the same values, but they differ. {self._rsi.name}: {self._values[time]} | {self._rsi_timedelta.name}: {self._rsi_timedelta.current.value}") | ||
self._indicators_compared = True | ||
self.quit() | ||
else: | ||
time = self.time.strftime('%Y-%m-%d') | ||
self._values[time] = self._rsi.current.value | ||
|
||
# Since the symbol resolution is hour and the symbol is equity, we know the last bar received in a day will | ||
# be at the market close, this is 16h. We need to count how many daily bars were consolidated in order to know | ||
# how many we need to request from the history | ||
if self.time.hour == 16: | ||
self.count += 1 | ||
|
||
def on_end_of_algorithm(self): | ||
if not self._indicators_compared: | ||
raise Exception(f"Indicators {self._rsi.name} and {self._rsi_timedelta.name} should have been compared, but they were not. Please make sure the indicators are getting SPY data") |
Oops, something went wrong.