Back

Runtime Error: Value cannot be null. Parameter name: key (Open Stacktrace)

Hi ! The code builds, but I'm getting a Runtime error during the backtest, it says : 

Runtime Error: Value cannot be null.  Parameter name: key (Open Stacktrace)

 

namespace QuantConnect.Algorithm.CSharp
{

public class MultipleCrypto : QCAlgorithm
{
// Define variable
// USER VARIABLES
private int startingCash = 2000;
private int maxPosition = 1000;
private int minPosition = 500;

Resolution res = Resolution.Hour;

int fastPeriod = 12;
int slowPeriod =26;
int sigPeriod = 9;
public decimal usd;

Dictionary <string,SymbolData> Data = new Dictionary <string,SymbolData>();

List <string> CryptoSymbols = new List<string>
{
"BTCUSD",
"ETHUSD",
"LTCUSD"
};
int numberOfSymbols => CryptoSymbols.Count;


// INITIALIZE
public override void Initialize()
{
SetStartDate(2017, 1, 1);
SetEndDate(2018, 1, 1);
SetCash(startingCash);

foreach (var symbol in CryptoSymbols) // adds each crypto to the Dictionary Data
{
var crypto = AddCrypto(symbol, res);
Data.Add(symbol, new SymbolData(crypto.Symbol, crypto.BaseCurrencySymbol));
}

foreach (var key in Data) // assign the Macd to the symbol
{
var symbolData = key.Value;
symbolData.Macd = MACD(symbolData.Symbol, fastPeriod, slowPeriod, sigPeriod, MovingAverageType.Exponential, res);
}

SetBrokerageModel(BrokerageName.GDAX, AccountType.Cash);

}

public override void OnData(Slice data)
{
foreach(var symbolData in Data.Values)
{
if(!symbolData.Macd.IsReady) {return;}
if (symbolData.Macd.IsReady)
{
usd = Portfolio.CashBook["USD"].Amount;

if(!Portfolio[symbolData.Symbol].Invested && usd > minPosition)
{
if (symbolData.Macd.Fast > symbolData.Macd.Slow)
{
decimal quantity = Math.Round(Math.Min( usd/numberOfSymbols, maxPosition) / data[symbolData.Symbol].Price, 2);
MarketOrder(symbolData.Symbol, quantity);
}
}
if(Portfolio[symbolData.Symbol].Invested)
{
var holding = Portfolio.CashBook[symbolData.BaseSymbol].Amount;
if (symbolData.Macd.Fast < symbolData.Macd.Slow)
{
MarketOrder(symbolData.Symbol, -holding);
}
}
}
}

}
public class SymbolData
{
public Symbol Symbol;
public string BaseSymbol;
public MovingAverageConvergenceDivergence Macd;
public SymbolData(Symbol symbol, string BaseSymbol)
{
Symbol = symbol;
BaseSymbol = BaseSymbol;
}
}
}
}

 

 

 

 

 

 

 

 

 

Update Backtest







0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


up any fix ? 

0

Update Backtest





0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Loading...

This discussion is closed