Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
* limitations under the License.
*/

using System;
using System.Collections.Generic;
using QuantConnect.Data.Market;
using QuantConnect.Interfaces;
Expand All @@ -31,6 +30,8 @@ public class ConsolidateDifferentTickTypesRegressionAlgorithm : QCAlgorithm, IRe
private bool _thereIsAtLeastOneTradeBar;
private bool _thereIsAtLeastOneQuoteBar;

private int _consolidationCount;

public override void Initialize()
{
SetStartDate(2013, 10, 06);
Expand All @@ -56,6 +57,12 @@ public void OnTradeTickMaxCount(TradeBar tradeBar)
public void OnQuoteTickMaxCount(QuoteBar quoteBar)
{
_thereIsAtLeastOneQuoteBar = true;

// Let's shortcut to reduce regression test duration: algorithms using tick data are too long
if (++_consolidationCount >= 1000)
{
Quit();
}
}

public void OnQuoteTick(Tick tick)
Expand Down Expand Up @@ -112,7 +119,7 @@ public override void OnEndOfAlgorithm()
/// <summary>
/// Data Points count of all timeslices of algorithm
/// </summary>
public long DataPoints => 2857175;
public long DataPoints => 12190;

/// <summary>
/// Data Points count of the algorithm history
Expand Down
40 changes: 27 additions & 13 deletions Algorithm.CSharp/HistoryTickRegressionAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@
* limitations under the License.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using QuantConnect.Data;
using QuantConnect.Data.Market;
using QuantConnect.Interfaces;

Expand All @@ -31,22 +29,38 @@ public class HistoryTickRegressionAlgorithm : QCAlgorithm, IRegressionAlgorithmD

public override void Initialize()
{
SetStartDate(2013, 10, 11);
SetEndDate(2013, 10, 11);
SetStartDate(2013, 10, 12);
SetEndDate(2013, 10, 13);

_symbol = AddEquity("SPY", Resolution.Tick).Symbol;
}

public override void OnEndOfAlgorithm()
{
var history = History<Tick>(_symbol, StartDate, EndDate, Resolution.Tick);
var quotes = history.Where(x => x.TickType == TickType.Quote).ToList();
var trades = history.Where(x => x.TickType == TickType.Trade).ToList();
var tradesCount = 0;
var quotesCount = 0;

if (quotes.Count == 0 || trades.Count == 0)
foreach (var point in History<Tick>(_symbol, StartDate.AddDays(-1), StartDate, Resolution.Tick))
{
if (point.TickType == TickType.Trade)
{
tradesCount++;
}
else if (point.TickType == TickType.Quote)
{
quotesCount++;
}

if (tradesCount > 0 && quotesCount > 0)
{
// We already found at least one tick of each type, we can exit the loop
break;
}
}

if (quotesCount == 0 || tradesCount == 0)
{
throw new RegressionTestException("Expected to find at least one tick of each type (quote and trade)");
}

Quit();
}

/// <summary>
Expand All @@ -62,12 +76,12 @@ public override void OnEndOfAlgorithm()
/// <summary>
/// Data Points count of all timeslices of algorithm
/// </summary>
public long DataPoints => 2741747;
public long DataPoints => 0;

/// <summary>
/// Data Points count of the algorithm history
/// </summary>
public int AlgorithmHistoryDataPoints => 2741732;
public int AlgorithmHistoryDataPoints => 9;

/// <summary>
/// Final status of the algorithm
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,19 @@ public override void Initialize()
"Tick history call with symbol array with explicit tick resolution");

var history = History<Tick>(spy, TimeSpan.FromHours(12));
if (history.Count() == 0)
if (!history.Any())
{
throw new RegressionTestException("On history call with implicit tick resolution: history returned no results");
}

history = History<Tick>(spy, TimeSpan.FromHours(12), Resolution.Tick);
if (history.Count() == 0)
if (!history.Any())
{
throw new RegressionTestException("On history call with explicit tick resolution: history returned no results");
}

// We already tested what we wanted to test, we can quit now
Quit();
}

private void AssertThatHistoryThrowsForTickResolution(Action historyCall, string historyCallDescription)
Expand Down Expand Up @@ -82,12 +85,12 @@ private void AssertThatHistoryThrowsForTickResolution(Action historyCall, string
/// <summary>
/// Data Points count of all timeslices of algorithm
/// </summary>
public long DataPoints => 7682413;
public long DataPoints => 0;

/// <summary>
/// Data Points count of the algorithm history
/// </summary>
public int AlgorithmHistoryDataPoints => 2736238;
public int AlgorithmHistoryDataPoints => 12;

/// <summary>
/// Final status of the algorithm
Expand Down
50 changes: 25 additions & 25 deletions Algorithm.CSharp/RegressionAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class RegressionAlgorithm : QCAlgorithm, IRegressionAlgorithmDefinition
public override void Initialize()
{
SetStartDate(2013, 10, 07);
SetEndDate(2013, 10, 11);
SetEndDate(2013, 10, 08);

SetCash(10000000);

Expand Down Expand Up @@ -83,7 +83,7 @@ public override void OnData(Slice slice)
/// <summary>
/// Data Points count of all timeslices of algorithm
/// </summary>
public long DataPoints => 16896623;
public long DataPoints => 6879791;

/// <summary>
/// Data Points count of the algorithm history
Expand All @@ -100,34 +100,34 @@ public override void OnData(Slice slice)
/// </summary>
public Dictionary<string, string> ExpectedStatistics => new Dictionary<string, string>
{
{"Total Orders", "583"},
{"Average Win", "0.00%"},
{"Average Loss", "0.00%"},
{"Compounding Annual Return", "-0.451%"},
{"Drawdown", "0.000%"},
{"Expectancy", "-0.964"},
{"Total Orders", "119"},
{"Average Win", "0%"},
{"Average Loss", "0%"},
{"Compounding Annual Return", "0%"},
{"Drawdown", "0%"},
{"Expectancy", "0"},
{"Start Equity", "10000000"},
{"End Equity", "9999422.04"},
{"Net Profit", "-0.006%"},
{"Sharpe Ratio", "-33.13"},
{"Sortino Ratio", "-33.13"},
{"Probabilistic Sharpe Ratio", "0.023%"},
{"Loss Rate", "99%"},
{"Win Rate", "1%"},
{"Profit-Loss Ratio", "4.14"},
{"Alpha", "-0.01"},
{"Beta", "-0"},
{"End Equity", "9999850.26"},
{"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", "-8.922"},
{"Tracking Error", "0.223"},
{"Treynor Ratio", "95.517"},
{"Total Fees", "$580.00"},
{"Estimated Strategy Capacity", "$39000000.00"},
{"Information Ratio", "0"},
{"Tracking Error", "0"},
{"Treynor Ratio", "0"},
{"Total Fees", "$119.00"},
{"Estimated Strategy Capacity", "$26000000.00"},
{"Lowest Capacity Asset", "AIG R735QTJ8XC9X"},
{"Portfolio Turnover", "0.16%"},
{"Portfolio Turnover", "0.08%"},
{"Drawdown Recovery", "0"},
{"OrderListHash", "f63bd4cd4e69db4d2b3ba5cc0e3bd284"}
{"OrderListHash", "8ac2506392feb9423f1a970846e70982"}
};
}
}
15 changes: 12 additions & 3 deletions Algorithm.CSharp/TickDataFilteringAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using QuantConnect.Interfaces;
using QuantConnect.Securities;
using QuantConnect.Securities.Interfaces;
using System;
using System.Collections.Generic;

namespace QuantConnect.Algorithm.CSharp
Expand All @@ -31,6 +32,8 @@ namespace QuantConnect.Algorithm.CSharp
/// <meta name="tag" content="ticks event" />
public class TickDataFilteringAlgorithm : QCAlgorithm, IRegressionAlgorithmDefinition
{
private DateTime _orderTime;

/// <summary>
/// Initialize the tick filtering example algorithm
/// </summary>
Expand Down Expand Up @@ -63,6 +66,12 @@ public override void OnData(Slice slice)
if (!Portfolio.Invested)
{
SetHoldings("SPY", 1);
_orderTime = Time;
}
// Let's shortcut to reduce regression test duration
else if (Time - _orderTime > TimeSpan.FromMinutes(5))
{
Quit();
}
}

Expand All @@ -79,7 +88,7 @@ public override void OnData(Slice slice)
/// <summary>
/// Data Points count of all timeslices of algorithm
/// </summary>
public long DataPoints => 707410;
public long DataPoints => 26983;

/// <summary>
/// Data Points count of the algorithm history
Expand All @@ -103,7 +112,7 @@ public override void OnData(Slice slice)
{"Drawdown", "0%"},
{"Expectancy", "0"},
{"Start Equity", "25000"},
{"End Equity", "25003.46"},
{"End Equity", "25009.41"},
{"Net Profit", "0%"},
{"Sharpe Ratio", "0"},
{"Sortino Ratio", "0"},
Expand All @@ -121,7 +130,7 @@ public override void OnData(Slice slice)
{"Total Fees", "$1.00"},
{"Estimated Strategy Capacity", "$0"},
{"Lowest Capacity Asset", "SPY R735QTJ8XC9X"},
{"Portfolio Turnover", "99.58%"},
{"Portfolio Turnover", "99.55%"},
{"Drawdown Recovery", "0"},
{"OrderListHash", "96e039d2b3ee8fccf0e161367ee78629"}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,20 @@ public override void Initialize()

// Requesting history for SPY and IBM (separately) with tick resolution
var spyHistory = History<Tick>(spy, TimeSpan.FromDays(1), Resolution.Tick);
if (spyHistory.Count() == 0)
if (!spyHistory.Any())
{
throw new RegressionTestException("SPY tick history is empty");
}

var ibmHistory = History<Tick>(ibm, TimeSpan.FromDays(1), Resolution.Tick);
if (ibmHistory.Count() == 0)
if (!ibmHistory.Any())
{
throw new RegressionTestException("IBM tick history is empty");
}

// Requesting history for SPY and IBM (together) with tick resolution
var spyIbmHistory = History<Tick>(new [] { spy, ibm }, TimeSpan.FromDays(1), Resolution.Tick);
if (spyIbmHistory.Count() == 0)
if (!spyIbmHistory.Any())
{
throw new RegressionTestException("Compound SPY and IBM tick history is empty");
}
Expand All @@ -77,7 +77,7 @@ public override void Initialize()
/// <summary>
/// Data Points count of the algorithm history
/// </summary>
public int AlgorithmHistoryDataPoints => 5978528;
public int AlgorithmHistoryDataPoints => 32;

/// <summary>
/// Final status of the algorithm
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ def initialize(self):
# Tick consolidators with max count
self.consolidate(TradeBar, equity.symbol, 10, TickType.TRADE, lambda trade_bar: self.on_trade_tick_max_count(trade_bar))
self._there_is_at_least_one_trade_bar = False

self.consolidate(QuoteBar, equity.symbol, 10, TickType.QUOTE, lambda quote_bar: self.on_quote_tick_max_count(quote_bar))
self._there_is_at_least_one_quote_bar = False


self._consolidation_count = 0

def on_trade_tick_max_count(self, trade_bar):
self._there_is_at_least_one_trade_bar = True
if type(trade_bar) != TradeBar:
Expand All @@ -47,6 +49,11 @@ def on_quote_tick_max_count(self, quote_bar):
if type(quote_bar) != QuoteBar:
raise AssertionError(f"The type of the bar should be Quote, but was {type(quote_bar)}")

self._consolidation_count += 1
# Let's shortcut to reduce regression test duration: algorithms using tick data are too long
if self._consolidation_count >= 1000:
self.quit()

def on_quote_tick(self, tick):
self.there_is_at_least_one_quote_tick = True
if tick.tick_type != TickType.QUOTE:
Expand All @@ -63,7 +70,7 @@ def on_end_of_algorithm(self):

if not self.there_is_at_least_one_trade_tick:
raise AssertionError(f"There should have been at least one tick in OnTradeTick() method, but there wasn't")

if not self._there_is_at_least_one_trade_bar:
raise AssertionError("There should have been at least one bar in OnTradeTickMaxCount() method, but there wasn't")

Expand Down
23 changes: 16 additions & 7 deletions Algorithm.Python/HistoryTickRegressionAlgorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,24 @@
class HistoryTickRegressionAlgorithm(QCAlgorithm):

def initialize(self):
self.set_start_date(2013, 10, 11)
self.set_end_date(2013, 10, 11)
self.set_start_date(2013, 10, 12)
self.set_end_date(2013, 10, 13)

self._symbol = self.add_equity("SPY", Resolution.TICK).symbol

def on_end_of_algorithm(self):
history = list(self.history[Tick](self._symbol, timedelta(days=1), Resolution.TICK))
quotes = [x for x in history if x.tick_type == TickType.QUOTE]
trades = [x for x in history if x.tick_type == TickType.TRADE]
trades_count = 0
quotes_count = 0
for point in self.history[Tick](self._symbol, timedelta(days=1), Resolution.TICK):
if point.tick_type == TickType.TRADE:
trades_count += 1
elif point.tick_type == TickType.QUOTE:
quotes_count += 1

if not quotes or not trades:
if trades_count > 0 and quotes_count > 0:
# We already found at least one tick of each type, we can exit the loop
break

if trades_count == 0 or quotes_count == 0:
raise AssertionError("Expected to find at least one tick of each type (quote and trade)")

self.quit()
Loading
Loading