eb12c8fa65
* Seed runtime-added currency conversion rates immediately Fixes the spurious 'The conversion rate for <currency> is not available' runtime error caused by a two-path seeding asymmetry. The setup path (BaseSetupHandler.SetupCurrencyConversions) wires up a currency's conversion feed AND seeds its rate via history/last-known-price so the rate is non-zero right away. The runtime path (UniverseSelection.EnsureCurrencyDataFeeds, invoked during universe selection / SetCash mid-run) only created the conversion subscription and left the rate at 0 until the first bar of the pair arrived. Any conversion in that gap (classically a midnight scheduled SetHoldings firing before the day's first conversion-pair bar) threw. EnsureCurrencyDataFeeds now seeds newly introduced, still-zero-rate conversion securities and calls cash.Update(), mirroring the setup path. Seeding is gated behind a seedNewCurrencies flag (default true) so the setup caller, which performs its own optionally white-listed seeding, can opt out and not regress white-list semantics. SeedSecurities degrades gracefully when no history/data is available, leaving the rate at 0 as before, so live mode and no-history scenarios are safe. Adds a regression test exercising the runtime path. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * Make runtime currency seeding robust and fix regression expectation CI failures from the runtime currency-conversion seeding change: 1. AlgorithmWarmupTests.WarmUpInternalSubscriptions threw ArgumentNullException because the new EnsureCurrencyDataFeeds seeding path ran GetLastKnownPrices in a stub where the conversion security lacked SymbolProperties. Pre-seeding is best-effort and must never break the algorithm, so wrap it in try/catch and degrade gracefully (leave the rate at 0, the pre-fix behavior) - matching the documented intent. The first conversion-pair bar still updates the rate. 2. ScheduledUniverseSelectionModelRegressionAlgorithm (C# + Python) asserted AlgorithmHistoryDataPoints == 0. The algorithm runtime-adds Forex pairs (EURGBP -> GBP cash) via scheduled universe selection; the fix now correctly seeds that runtime currency's conversion rate with a last-known-price history request (deterministically 50 points). The old 0 reflected the buggy unseeded behavior, so update the expectation to 50. No other statistics changed. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * Seed runtime added currency conversion rates * Seed currencies with no new conversion feed and dedup the seeding helper --------- Co-authored-by: Martin-Molinero <Martin-Molinero@users.noreply.github.com> Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
245 lines
9.5 KiB
C#
245 lines
9.5 KiB
C#
/*
|
|
* 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.Collections.Generic;
|
|
using System.Linq;
|
|
using QuantConnect.Algorithm.Framework.Alphas;
|
|
using QuantConnect.Algorithm.Framework.Portfolio;
|
|
using QuantConnect.Algorithm.Framework.Selection;
|
|
using QuantConnect.Data.UniverseSelection;
|
|
using QuantConnect.Orders;
|
|
using QuantConnect.Interfaces;
|
|
|
|
namespace QuantConnect.Algorithm.CSharp
|
|
{
|
|
/// <summary>
|
|
/// Regression algorithm for testing <see cref="ScheduledUniverseSelectionModel"/> scheduling functions
|
|
/// </summary>
|
|
public class ScheduledUniverseSelectionModelRegressionAlgorithm : QCAlgorithm, IRegressionAlgorithmDefinition
|
|
{
|
|
public override void Initialize()
|
|
{
|
|
UniverseSettings.Resolution = Resolution.Hour;
|
|
|
|
// Order margin value has to have a minimum of 0.5% of Portfolio value, allows filtering out small trades and reduce fees.
|
|
// Commented so regression algorithm is more sensitive
|
|
//Settings.MinimumOrderMarginPortfolioPercentage = 0.005m;
|
|
|
|
SetStartDate(2017, 01, 01);
|
|
SetEndDate(2017, 02, 01);
|
|
|
|
// selection will run on mon/tues/thurs at 00:00/12:00
|
|
SetUniverseSelection(new ScheduledUniverseSelectionModel(
|
|
DateRules.Every(DayOfWeek.Monday, DayOfWeek.Tuesday, DayOfWeek.Thursday),
|
|
TimeRules.Every(TimeSpan.FromHours(12)),
|
|
SelectSymbols
|
|
));
|
|
|
|
SetAlpha(new ConstantAlphaModel(InsightType.Price, InsightDirection.Up, TimeSpan.FromDays(1)));
|
|
SetPortfolioConstruction(new EqualWeightingPortfolioConstructionModel());
|
|
}
|
|
|
|
private IEnumerable<Symbol> SelectSymbols(DateTime dateTime)
|
|
{
|
|
Log($"SelectSymbols() {Time}");
|
|
if (dateTime.DayOfWeek == DayOfWeek.Monday || dateTime.DayOfWeek == DayOfWeek.Tuesday)
|
|
{
|
|
yield return QuantConnect.Symbol.Create("SPY", SecurityType.Equity, Market.USA);
|
|
}
|
|
else if (dateTime.DayOfWeek == DayOfWeek.Wednesday)
|
|
{
|
|
// given the date/time rules specified in Initialize, this symbol will never be selected (not invoked on wednesdays)
|
|
yield return QuantConnect.Symbol.Create("AAPL", SecurityType.Equity, Market.USA);
|
|
}
|
|
else
|
|
{
|
|
yield return QuantConnect.Symbol.Create("IBM", SecurityType.Equity, Market.USA);
|
|
}
|
|
|
|
if (dateTime.DayOfWeek == DayOfWeek.Tuesday || dateTime.DayOfWeek == DayOfWeek.Thursday)
|
|
{
|
|
yield return QuantConnect.Symbol.Create("EURUSD", SecurityType.Forex, Market.Oanda);
|
|
}
|
|
else if (dateTime.DayOfWeek == DayOfWeek.Friday)
|
|
{
|
|
// given the date/time rules specified in Initialize, this symbol will never be selected (every 6 hours never lands on hour==1)
|
|
yield return QuantConnect.Symbol.Create("EURGBP", SecurityType.Forex, Market.Oanda);
|
|
}
|
|
else
|
|
{
|
|
yield return QuantConnect.Symbol.Create("NZDUSD", SecurityType.Forex, Market.Oanda);
|
|
}
|
|
}
|
|
|
|
// some days of the week have different behavior the first time -- less securities to remove
|
|
private readonly HashSet<DayOfWeek> _seenDays = new HashSet<DayOfWeek>();
|
|
public override void OnSecuritiesChanged(SecurityChanges changes)
|
|
{
|
|
Console.WriteLine($"{Time}: {changes}");
|
|
|
|
switch (Time.DayOfWeek)
|
|
{
|
|
case DayOfWeek.Monday:
|
|
ExpectAdditions(changes, "SPY", "NZDUSD");
|
|
if (_seenDays.Add(DayOfWeek.Monday))
|
|
{
|
|
ExpectRemovals(changes, null);
|
|
}
|
|
else
|
|
{
|
|
ExpectRemovals(changes, "EURUSD", "IBM");
|
|
}
|
|
break;
|
|
|
|
case DayOfWeek.Tuesday:
|
|
ExpectAdditions(changes, "EURUSD");
|
|
if (_seenDays.Add(DayOfWeek.Tuesday))
|
|
{
|
|
ExpectRemovals(changes, "NZDUSD");
|
|
}
|
|
else
|
|
{
|
|
ExpectRemovals(changes, "NZDUSD");
|
|
}
|
|
break;
|
|
|
|
case DayOfWeek.Wednesday:
|
|
// selection function not invoked on wednesdays
|
|
ExpectAdditions(changes, null);
|
|
ExpectRemovals(changes, null);
|
|
break;
|
|
|
|
case DayOfWeek.Thursday:
|
|
ExpectAdditions(changes, "IBM");
|
|
ExpectRemovals(changes, "SPY");
|
|
break;
|
|
|
|
case DayOfWeek.Friday:
|
|
// selection function not invoked on fridays
|
|
ExpectAdditions(changes, null);
|
|
ExpectRemovals(changes, null);
|
|
break;
|
|
}
|
|
}
|
|
|
|
public override void OnOrderEvent(OrderEvent orderEvent)
|
|
{
|
|
Debug($"{Time}: {orderEvent}");
|
|
}
|
|
|
|
private void ExpectAdditions(SecurityChanges changes, params string[] tickers)
|
|
{
|
|
if (tickers == null && changes.AddedSecurities.Count > 0)
|
|
{
|
|
throw new RegressionTestException($"{Time}: Expected no additions: {Time.DayOfWeek}");
|
|
}
|
|
if (tickers == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
foreach (var ticker in tickers)
|
|
{
|
|
if (changes.AddedSecurities.All(s => s.Symbol.Value != ticker))
|
|
{
|
|
throw new RegressionTestException($"{Time}: Expected {ticker} to be added: {Time.DayOfWeek}");
|
|
}
|
|
}
|
|
}
|
|
|
|
private void ExpectRemovals(SecurityChanges changes, params string[] tickers)
|
|
{
|
|
if (tickers == null && changes.RemovedSecurities.Count > 0)
|
|
{
|
|
throw new RegressionTestException($"{Time}: Expected no removals: {Time.DayOfWeek}");
|
|
}
|
|
|
|
if (tickers == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
foreach (var ticker in tickers)
|
|
{
|
|
if (changes.RemovedSecurities.All(s => s.Symbol.Value != ticker))
|
|
{
|
|
throw new RegressionTestException($"{Time}: Expected {ticker} to be removed: {Time.DayOfWeek}");
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <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 => 987;
|
|
|
|
/// <summary>
|
|
/// Data Points count of the algorithm history
|
|
/// </summary>
|
|
public int AlgorithmHistoryDataPoints => 10;
|
|
|
|
/// <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", "59"},
|
|
{"Average Win", "0.28%"},
|
|
{"Average Loss", "-0.20%"},
|
|
{"Compounding Annual Return", "75.392%"},
|
|
{"Drawdown", "1.100%"},
|
|
{"Expectancy", "0.749"},
|
|
{"Start Equity", "100000"},
|
|
{"End Equity", "105049.17"},
|
|
{"Net Profit", "5.049%"},
|
|
{"Sharpe Ratio", "7.229"},
|
|
{"Sortino Ratio", "10.917"},
|
|
{"Probabilistic Sharpe Ratio", "96.421%"},
|
|
{"Loss Rate", "27%"},
|
|
{"Win Rate", "73%"},
|
|
{"Profit-Loss Ratio", "1.39"},
|
|
{"Alpha", "0.477"},
|
|
{"Beta", "0.042"},
|
|
{"Annual Standard Deviation", "0.067"},
|
|
{"Annual Variance", "0.004"},
|
|
{"Information Ratio", "3.991"},
|
|
{"Tracking Error", "0.084"},
|
|
{"Treynor Ratio", "11.625"},
|
|
{"Total Fees", "$35.53"},
|
|
{"Estimated Strategy Capacity", "$2600000.00"},
|
|
{"Lowest Capacity Asset", "EURUSD 8G"},
|
|
{"Portfolio Turnover", "90.30%"},
|
|
{"Drawdown Recovery", "3"},
|
|
{"OrderListHash", "9af211e68f600642a2aaa58f3bec6380"}
|
|
};
|
|
}
|
|
}
|