Back

"Runtime Error: An item with the same key has already been added." for Forex symbols with built-in Alphas using MeanVarianceOptimization

So, for the following code:

class ForexTemplateAlgorithm(QCAlgorithm):

def Initialize(self):
self.SetCash(100000)
self.SetStartDate(2018,1,1)
self.SetEndDate(datetime.now() - timedelta(1))

self.signs = [
"USDCAD",
"EURUSD",
"USDCHF",
"EURGBP",
"GBPUSD",
"USDJPY",
]

self.symbols = [Symbol.Create(x, SecurityType.Forex, Market.Oanda) for x in self.signs]
self.UniverseSettings.Resolution = Resolution.Daily
self.AddUniverseSelection(ManualUniverseSelectionModel(self.symbols))

self.SetAlpha(RsiAlphaModel())
self.SetAlpha(EmaCrossAlphaModel())
self.SetAlpha(MacdAlphaModel())
self.SetPortfolioConstruction(MeanVarianceOptimizationPortfolioConstructionModel())
self.SetRiskManagement(MaximumDrawdownPercentPerSecurity(0.02))
self.SetExecution(ImmediateExecutionModel())

I get the following errors:
`Runtime Error: An item with the same key has already been added. Key: 11/03/2017 20:00:00 (Open Stacktrace)`

I am assuming it's because I'm trying to analyze Forex data but the MeanVarianceOptimizationPortfolioConstructionModel is calculating returns on 252 trading days as opposed to daily for Forex as shown in this code: 

https://github.com/QuantConnect/Lean/blob/master/Algorithm.Framework/Portfolio/MeanVarianceOptimizationPortfolioConstructionModel.py#L173


My question is, what should I do to modify the returns formula for the MeanVarianceOptimizationPortfolioConstructionModel code so it's compatible with Forex. Do I calculate returns over 365 days instead?

Thank you for your help! Best wishes.

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.


Hi Yaz,

There may possibly be an issue with the C# version of MeanVarianceOptimization PCM. We've created a GitHub issue regarding this which you can follow for updates.

For now, you can run your algorithm by copying over the python version of the PCM into your project and importing it. MeanVarianceOptimization PCM requires insights with magnitude to create portfolio targets. So we need to make sure to use an alpha model which emits insights with magnitude. For example, we can use a constant alpha model which emits upwards insights with magnitude 0.01.

self.SetAlpha(ConstantAlphaModel(InsightType.Price, InsightDirection.Up, timedelta(1), 0.01, 0.05))

Best
Rahul

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.


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