Skip to content

Commit

Permalink
Fixed universe subscriptions with Forex
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanoRaggi authored and mchandschuh committed Dec 28, 2015
1 parent 7b04eaa commit a29b78c
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 18 deletions.
15 changes: 8 additions & 7 deletions Common/Securities/Cash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,20 +120,21 @@ public void SetAmount(decimal amount)
}

/// <summary>
/// Ensures that we have a data feed to conver this currency into the base currency.
/// Ensures that we have a data feed to convert this currency into the base currency.
/// This will add a subscription at the lowest resolution if one is not found.
/// </summary>
/// <param name="securities">The security manager</param>
/// <param name="subscriptions">The subscription manager used for searching and adding subscriptions</param>
/// <param name="marketHoursDatabase">A security exchange hours provider instance used to resolve exchange hours for new subscriptions</param>
public void EnsureCurrencyDataFeed(SecurityManager securities, SubscriptionManager subscriptions, MarketHoursDatabase marketHoursDatabase)
/// <returns>Returns the added currency security if needed, otherwise null</returns>
public Security EnsureCurrencyDataFeed(SecurityManager securities, SubscriptionManager subscriptions, MarketHoursDatabase marketHoursDatabase)
{
if (Symbol == CashBook.AccountCurrency)
{
SecuritySymbol = QuantConnect.Symbol.Empty;
_isBaseCurrency = true;
ConversionRate = 1.0m;
return;
return null;
}

if (subscriptions.Count == 0)
Expand All @@ -149,13 +150,13 @@ public void EnsureCurrencyDataFeed(SecurityManager securities, SubscriptionManag
if (config.Symbol.Value == normal)
{
SecuritySymbol = config.Symbol;
return;
return null;
}
if (config.Symbol.Value == invert)
{
SecuritySymbol = config.Symbol;
_invertRealTimePrice = true;
return;
return null;
}
}

Expand All @@ -180,8 +181,8 @@ public void EnsureCurrencyDataFeed(SecurityManager securities, SubscriptionManag
var security = new Forex.Forex(exchangeHours, this, config, 1m);
SecuritySymbol = config.Symbol;
securities.Add(config.Symbol, security);
Log.Trace("Cash.EnsureCurrencyDataFeed(): Adding " + symbol.ToString() + " for cash " + Symbol + " currency feed");
return;
Log.Trace("Cash.EnsureCurrencyDataFeed(): Adding " + symbol.Value + " for cash " + Symbol + " currency feed");
return security;
}
}

Expand Down
11 changes: 9 additions & 2 deletions Common/Securities/CashBook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,19 @@ public void Add(string symbol, decimal quantity, decimal conversionRate)
/// <param name="securities">The SecurityManager for the algorithm</param>
/// <param name="subscriptions">The SubscriptionManager for the algorithm</param>
/// <param name="marketHoursDatabase">A security exchange hours provider instance used to resolve exchange hours for new subscriptions</param>
public void EnsureCurrencyDataFeeds(SecurityManager securities, SubscriptionManager subscriptions, MarketHoursDatabase marketHoursDatabase)
/// <returns>Returns a list of added currency securities</returns>
public List<Security> EnsureCurrencyDataFeeds(SecurityManager securities, SubscriptionManager subscriptions, MarketHoursDatabase marketHoursDatabase)
{
var addedSecurities = new List<Security>();
foreach (var cash in _currencies.Values)
{
cash.EnsureCurrencyDataFeed(securities, subscriptions, marketHoursDatabase);
var security = cash.EnsureCurrencyDataFeed(securities, subscriptions, marketHoursDatabase);
if (security != null)
{
addedSecurities.Add(security);
}
}
return addedSecurities;
}

/// <summary>
Expand Down
10 changes: 10 additions & 0 deletions Engine/DataFeeds/UniverseSelection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,16 @@ public SecurityChanges ApplyUniverseSelection(Universe universe, DateTime dateTi
}
}

// Add currency data feeds that weren't explicitly added in Initialize
if (additions.Count > 0)
{
var addedSecurities = _algorithm.Portfolio.CashBook.EnsureCurrencyDataFeeds(_algorithm.Securities, _algorithm.SubscriptionManager, MarketHoursDatabase.FromDataFolder());
foreach (var security in addedSecurities)
{
_dataFeed.AddSubscription(universe, security, dateTimeUtc, _algorithm.EndDate.ConvertToUtc(_algorithm.TimeZone));
}
}

// return None if there's no changes, otherwise return what we've modified
return additions.Count + removals.Count != 0
? new SecurityChanges(additions, removals)
Expand Down
2 changes: 0 additions & 2 deletions Engine/Setup/BacktestingSetupHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,6 @@ public bool Setup(IAlgorithm algorithm, IBrokerage brokerage, AlgorithmNodePacke
algorithm.Schedule.SetEventSchedule(realTimeHandler);
//Initialise the algorithm, get the required data:
algorithm.Initialize();
//Add currency data feeds that weren't explicity added in Initialize
algorithm.Portfolio.CashBook.EnsureCurrencyDataFeeds(algorithm.Securities, algorithm.SubscriptionManager, MarketHoursDatabase.FromDataFolder());
}
catch (Exception err)
{
Expand Down
5 changes: 0 additions & 5 deletions Engine/Setup/BrokerageSetupHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -335,11 +335,6 @@ public bool Setup(IAlgorithm algorithm, IBrokerage brokerage, AlgorithmNodePacke
return false;
}

Log.Trace("BrokerageSetupHandler.Setup(): Ensuring currency data feeds present...");

// call this after we've initialized everything from the brokerage since we may have added some holdings/currencies
algorithm.Portfolio.CashBook.EnsureCurrencyDataFeeds(algorithm.Securities, algorithm.SubscriptionManager, MarketHoursDatabase.FromDataFolder());

//Set the starting portfolio value for the strategy to calculate performance:
StartingPortfolioValue = algorithm.Portfolio.TotalPortfolioValue;
StartingDate = DateTime.Now;
Expand Down
2 changes: 0 additions & 2 deletions Engine/Setup/ConsoleSetupHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,6 @@ public bool Setup(IAlgorithm algorithm, IBrokerage brokerage, AlgorithmNodePacke
algorithm.Initialize();
//Set the time frontier of the algorithm
algorithm.SetDateTime(algorithm.StartDate.ConvertToUtc(algorithm.TimeZone));
//Add currency data feeds that weren't explicity added in Initialize
algorithm.Portfolio.CashBook.EnsureCurrencyDataFeeds(algorithm.Securities, algorithm.SubscriptionManager, MarketHoursDatabase.FromDataFolder());

//Construct the backtest job packet:
backtestJob.PeriodStart = algorithm.StartDate;
Expand Down

0 comments on commit a29b78c

Please sign in to comment.