Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for multiple market closes in date #8509

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 @@ -36,7 +36,7 @@ public class BasicTemplateFuturesWithExtendedMarketDailyAlgorithm : BasicTemplat
/// <summary>
/// Data Points count of all timeslices of algorithm
/// </summary>
public override long DataPoints => 14713;
public override long DataPoints => 14181;

/// <summary>
/// This is used by the regression test system to indicate what the expected statistics are from running the algorithm
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ public class ContinuousFutureRolloverDailyExchangeTimeZoneAheadOfDataRegressionA
/// <summary>
/// Data Points count of all timeslices of algorithm
/// </summary>
public override long DataPoints => 1002;
public override long DataPoints => 1014;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ public class ContinuousFutureRolloverDailyExchangeTimeZoneSameAsDataRegressionAl
/// <summary>
/// Data Points count of all timeslices of algorithm
/// </summary>
public override long DataPoints => 995;
public override long DataPoints => 1007;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,25 @@ public class FutureMarketOpenAndCloseWithExtendedMarketRegressionAlgorithm : Fut
{
protected override bool ExtendedMarketHours => true;
protected override List<DateTime> AfterMarketOpen => new List<DateTime>() {
new DateTime(2020, 02, 04, 18, 0, 0), // Tuesday
new DateTime(2020, 02, 05, 18, 0, 0),
new DateTime(2020, 02, 06, 18, 0, 0),
new DateTime(2020, 02, 09, 18, 0, 0),
new DateTime(2020, 02, 10, 18, 0, 0),
new DateTime(2020, 02, 11, 18, 0, 0)
new DateTime(2020, 02, 04, 0, 0, 0), // Tuesday
new DateTime(2020, 02, 05, 0, 0, 0),
new DateTime(2020, 02, 06, 0, 0, 0),
new DateTime(2020, 02, 07, 0, 0, 0),
new DateTime(2020, 02, 09, 18, 0, 0), // sunday
new DateTime(2020, 02, 10, 0, 0, 0),
new DateTime(2020, 02, 11, 0, 0, 0),
new DateTime(2020, 02, 12, 0, 0, 0)
};
protected override List<DateTime> BeforeMarketClose => new List<DateTime>()
{
new DateTime(2020, 02, 04, 17, 0, 0),
new DateTime(2020, 02, 05, 17, 0, 0),
new DateTime(2020, 02, 06, 17, 0, 0),
new DateTime(2020, 02, 07, 17, 0, 0),
new DateTime(2020, 02, 10, 17, 0, 0),
new DateTime(2020, 02, 11, 17, 0, 0)
new DateTime(2020, 02, 04, 0, 0, 0),
new DateTime(2020, 02, 05, 0, 0, 0),
new DateTime(2020, 02, 06, 0, 0, 0),
new DateTime(2020, 02, 07, 0, 0, 0),
new DateTime(2020, 02, 07, 17, 0, 0), // friday
new DateTime(2020, 02, 10, 0, 0, 0),
new DateTime(2020, 02, 11, 0, 0, 0),
new DateTime(2020, 02, 12, 0, 0, 0)
};

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public override void Initialize()
SetStartDate(2013, 10, 06);
SetEndDate(2013, 10, 14);

Settings.DailyConsolidationUseExtendedMarketHours = true;
var es = AddSecurity(SecurityType.Future, "ES", extendedMarketHours: ExtendedMarketHours);

_expectedOpensQueue = new Queue<DateTime>(ExpectedOpens);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,24 @@ public class FutureMarketOpenConsolidatorWithExtendedMarketRegressionAlgorithm :
protected override bool ExtendedMarketHours => true;
protected override List<DateTime> ExpectedOpens => new List<DateTime>(){
new DateTime(2013, 10, 06, 18, 0, 0), // Sunday
new DateTime(2013, 10, 07, 18, 0, 0),
new DateTime(2013, 10, 08, 18, 0, 0),
new DateTime(2013, 10, 09, 18, 0, 0),
new DateTime(2013, 10, 10, 18, 0, 0),
// market is open for the whole day, so goes from midnight to midnight
new DateTime(2013, 10, 07, 0, 0, 0),
new DateTime(2013, 10, 08, 0, 0, 0),
new DateTime(2013, 10, 09, 0, 0, 0),
new DateTime(2013, 10, 10, 0, 0, 0),
new DateTime(2013, 10, 11, 0, 0, 0),
new DateTime(2013, 10, 13, 18, 0, 0),
new DateTime(2013, 10, 14, 0, 0, 0),
};
protected override List<DateTime> ExpectedCloses => new List<DateTime>(){
new DateTime(2013, 10, 07, 17, 0, 0),
new DateTime(2013, 10, 08, 17, 0, 0),
new DateTime(2013, 10, 09, 17, 0, 0),
new DateTime(2013, 10, 10, 17, 0, 0),
new DateTime(2013, 10, 11, 17, 0, 0),
new DateTime(2013, 10, 14, 17, 0, 0),
new DateTime(2013, 10, 07, 0, 0, 0),
new DateTime(2013, 10, 08, 0, 0, 0),
new DateTime(2013, 10, 09, 0, 0, 0),
new DateTime(2013, 10, 10, 0, 0, 0),
new DateTime(2013, 10, 11, 0, 0, 0),
new DateTime(2013, 10, 11, 17, 0, 0), // friday
new DateTime(2013, 10, 14, 0, 0, 0),
new DateTime(2013, 10, 15, 0, 0, 0),
};

/// <summary>
Expand Down
80 changes: 80 additions & 0 deletions Algorithm.CSharp/HSIFutureDailyRegressionAlgorithm.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* 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;

namespace QuantConnect.Algorithm.CSharp
{
/// <summary>
/// Regression algorithm using and testing HSI futures and index
/// </summary>
public class HSIFutureDailyRegressionAlgorithm : HSIFutureHourRegressionAlgorithm
{
/// <summary>
/// The data resolution
/// </summary>
protected override Resolution Resolution => Resolution.Daily;

/// <summary>
/// Data Points count of all timeslices of algorithm
/// </summary>
public override long DataPoints => 177;

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

/// <summary>
/// Final status of the algorithm
/// </summary>
public override 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 override Dictionary<string, string> ExpectedStatistics => new Dictionary<string, string>
{
{"Total Orders", "15"},
{"Average Win", "0%"},
{"Average Loss", "-0.33%"},
{"Compounding Annual Return", "-55.187%"},
{"Drawdown", "2.400%"},
{"Expectancy", "-1"},
{"Start Equity", "100000"},
{"End Equity", "97610"},
{"Net Profit", "-2.390%"},
{"Sharpe Ratio", "-15.799"},
{"Sortino Ratio", "-19.207"},
{"Probabilistic Sharpe Ratio", "0%"},
{"Loss Rate", "100%"},
{"Win Rate", "0%"},
{"Profit-Loss Ratio", "0"},
{"Alpha", "0"},
{"Beta", "0"},
{"Annual Standard Deviation", "0.029"},
{"Annual Variance", "0.001"},
{"Information Ratio", "-15.544"},
{"Tracking Error", "0.029"},
{"Treynor Ratio", "0"},
{"Total Fees", "$600.00"},
{"Estimated Strategy Capacity", "$0"},
{"Lowest Capacity Asset", "HSI VL6DN7UV65S9"},
{"Portfolio Turnover", "1590.77%"},
{"OrderListHash", "42cd8e3b58361b181c911a603f69d2f7"}
};
}
}
210 changes: 210 additions & 0 deletions Algorithm.CSharp/HSIFutureHourRegressionAlgorithm.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
/*
* 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;
using System.Linq;
using QuantConnect.Data;
using QuantConnect.Interfaces;
using QuantConnect.Securities;
using System.Collections.Generic;
using QuantConnect.Securities.Future;
using QuantConnect.Data.UniverseSelection;

namespace QuantConnect.Algorithm.CSharp
{
/// <summary>
/// Regression algorithm using and testing HSI futures and index
/// </summary>
public class HSIFutureHourRegressionAlgorithm : QCAlgorithm, IRegressionAlgorithmDefinition
{
private int _symbolChangeEvent;
private Symbol _contractSymbol;
private Symbol _index;
private Symbol _futureSymbol;

/// <summary>
/// The data resolution
/// </summary>
protected virtual Resolution Resolution => Resolution.Hour;

/// <summary>
/// Initialize your algorithm and add desired assets.
/// </summary>
public override void Initialize()
{
SetStartDate(2013, 10, 20);
SetEndDate(2013, 10, 30);

SetAccountCurrency("HKD");
SetTimeZone(TimeZones.HongKong);

UniverseSettings.Resolution = Resolution;
_index = AddIndex("HSI", Resolution, market: Market.HKFE).Symbol;
var future = AddFuture(Futures.Indices.HangSeng, Resolution);
future.SetFilter(TimeSpan.Zero, TimeSpan.FromDays(182));
_futureSymbol = future.Symbol;

var seeder = new FuncSecuritySeeder(GetLastKnownPrices);
SetSecurityInitializer(security => seeder.SeedSecurity(security));
}

/// <summary>
/// OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
/// </summary>
/// <param name="data">Slice object keyed by symbol containing the stock data</param>
public override void OnData(Slice slice)
{
foreach (var changedEvent in slice.SymbolChangedEvents.Values)
{
Debug($"{Time} - SymbolChanged event: {changedEvent}");
if (Time.TimeOfDay != TimeSpan.Zero)
{
throw new RegressionTestException($"{Time} unexpected symbol changed event {changedEvent}!");
}
_symbolChangeEvent++;
}

if (!Portfolio.Invested)
{
foreach (var chain in slice.FutureChains)
{
// find the front contract expiring no earlier than in 90 days
var contract = (
from futuresContract in chain.Value.OrderBy(x => x.Expiry)
select futuresContract
).FirstOrDefault();

// if found, trade it
if (contract != null)
{
_contractSymbol = contract.Symbol;
MarketOrder(_contractSymbol, 1);
}
}
}
else
{
Liquidate();
}
}

public override void OnEndOfAlgorithm()
{
if (_symbolChangeEvent != 1)
{
throw new RegressionTestException($"Got no expected symbol changed event count {_symbolChangeEvent}!");
}

// Get the margin requirements
var buyingPowerModel = Securities[_contractSymbol].BuyingPowerModel;
var futureMarginModel = buyingPowerModel as FutureMarginModel;
if (buyingPowerModel == null)
{
throw new RegressionTestException($"Invalid buying power model. Found: {buyingPowerModel.GetType().Name}. Expected: {nameof(FutureMarginModel)}");
}
var initialOvernight = futureMarginModel.InitialOvernightMarginRequirement;
var maintenanceOvernight = futureMarginModel.MaintenanceOvernightMarginRequirement;
var initialIntraday = futureMarginModel.InitialIntradayMarginRequirement;
var maintenanceIntraday = futureMarginModel.MaintenanceIntradayMarginRequirement;

var lastDataFuture = Securities[_futureSymbol].GetLastData();
if (lastDataFuture == null || (lastDataFuture.EndTime - lastDataFuture.Time) != TimeSpan.FromHours(Resolution == Resolution.Hour ? 1 : 7.25)
|| lastDataFuture.EndTime.Date != lastDataFuture.Time.Date)
{
throw new RegressionTestException($"Unexpected data for symbol {_futureSymbol}!");
}

var lastDataIndex = Securities[_index].GetLastData();
if (lastDataIndex == null || (lastDataIndex.EndTime - lastDataIndex.Time) != TimeSpan.FromHours(Resolution == Resolution.Hour ? 1 : 6.5)
|| lastDataFuture.EndTime.Date != lastDataFuture.Time.Date)
{
throw new RegressionTestException($"Unexpected data for symbol {_index}!");
}
}

public override void OnSecuritiesChanged(SecurityChanges changes)
{
foreach (var addedSecurity in changes.AddedSecurities)
{
if (addedSecurity.Symbol.SecurityType == SecurityType.Future
&& !addedSecurity.Symbol.IsCanonical()
&& !addedSecurity.HasData)
{
throw new RegressionTestException($"Future contracts did not work up as expected: {addedSecurity.Symbol}");
}
}
}

/// <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 };

/// <summary>
/// Data Points count of all timeslices of algorithm
/// </summary>
public virtual long DataPoints => 823;

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

/// <summary>
/// Final status of the algorithm
/// </summary>
public virtual 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 virtual Dictionary<string, string> ExpectedStatistics => new Dictionary<string, string>
{
{"Total Orders", "57"},
{"Average Win", "1.83%"},
{"Average Loss", "-1.31%"},
{"Compounding Annual Return", "-99.930%"},
{"Drawdown", "23.000%"},
{"Expectancy", "-0.572"},
{"Start Equity", "100000"},
{"End Equity", "80330"},
{"Net Profit", "-19.670%"},
{"Sharpe Ratio", "-1.298"},
{"Sortino Ratio", "-1.254"},
{"Probabilistic Sharpe Ratio", "1.073%"},
{"Loss Rate", "82%"},
{"Win Rate", "18%"},
{"Profit-Loss Ratio", "1.40"},
{"Alpha", "0"},
{"Beta", "0"},
{"Annual Standard Deviation", "0.772"},
{"Annual Variance", "0.596"},
{"Information Ratio", "-1.288"},
{"Tracking Error", "0.772"},
{"Treynor Ratio", "0"},
{"Total Fees", "$2280.00"},
{"Estimated Strategy Capacity", "$120000000.00"},
{"Lowest Capacity Asset", "HSI VL6DN7UV65S9"},
{"Portfolio Turnover", "7099.25%"},
{"OrderListHash", "174bdb031f17212dc9d92372f4fb75c2"}
};
}
}
Loading
Loading