hi guys,
im looking to implement a pairs trading strategy. to do this i need the rolling 20 day ratio of 2 prices, and to take the SMA of that. ive gotten the prices into a rolling window and created a third rolling window for the ratio of the two, however when i try to use this ratio as input for an SMA, im told that this input can only be a symbol. what is the recommended way to do this? example code only gives a way to add indicator values to a rollingwindow, where as im trying to do the reverse.
here's the relevant code:
//warmup
var asset1 = History<TradeBar>(_btceur, 100, Resolution.Daily);
var asset2 = History<TradeBar>(_btcusdt, 100, Resolution.Daily);
var ratio = new RollingWindow<decimal>(100);
var _window = new RollingWindow<TradeBar>(100);
var _window2 = new RollingWindow<TradeBar>(100);
foreach (var bar in btceurTrades)
{
_window.Add(bar);
}
foreach (var bar in btcusdtTrades)
{
_window2.Add(bar);
}
foreach (var bar in btcusdtTrades)
{
ratio.Add(_window[0].Close/_window2[0].Close);
}
var sma = SMA(ratio, 20);
sma.Updated += (sender, updated) => _smaWin.Add(updated);
var _smaWin = new RollingWindow<IndicatorDataPoint>(20);
thanks,
mischa
M. A. Wiedouw
just noticed that you can't edit posts 😊
in the code block, btceurtrades should be asset1 and btcusdttrades should be asset 2.
Derek Melchin
Hi M. A. Wiedouw,
To get an SMA of the price ratios, create a manual SMA indicator and update it with the ratio. See the attached backtest for reference and consider wrapping the logic inside a custom indicator.
Best,
Derek Melchin
Want to invest in QuantConnect as we build the Linux of quant finance? Checkout our Wefunder campaign to join the revolution.
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.
M. A. Wiedouw
thanks derek, looks like im able to get the desired sma for 100 samples now, however im still having trouble accessing and updating it in OnData, the error thrown is (even on the IsReady line)
Runtime Error: Object reference not set to an instance of an object.
M. A. Wiedouw
btw i declared it to be public in the following way so shouldnt it be accessible in ondata?:
Derek Melchin
Hi M. A. Wiedouw,
See the attached backtest for an example of updating the ratio in OnData.
We can access the _sma in OnData if it's private or public. There are many great resources online that explain the difference between the private and public keywords.
Best,
Derek Melchin
Want to invest in QuantConnect as we build the Linux of quant finance? Checkout our Wefunder campaign to join the revolution.
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.
M. A. Wiedouw
hi derek,
thanks for your help so far, i tested your code and it works. however what i want to achieve in the end is a chain of indicators that feed into eachother and i couldn't figure out how to do this. i found a way to do it in the documentation but it seems there's a bug in it atm. anyway, the full idea for what i want to achieve can be summarized as seen below in custom indicator code. could this be achieved in this way or should it be done differently? if i understood correctly, to update this chain, i would only have to update btcusdt and btceur, which would happen with a history request to warm up and then automatically every time new data comes in since it's a symbol?
Louis Szeto
Hi M. A. Wiedouw
Please follow the docs here to combine indicators.
Best
Louis
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.
M. A. Wiedouw
hi louis,
as far as i understand, since i need the sma of the ratio of two prices, i have to convert their symbols with the identity function so i can use them as input for the .of method. the documentation states:
The Of extension feeds an indicator's .Current.Value into the input of another indicator. The first argument of the IndicatorExtensions.Of method must be a manual indicator with no automatic updates. If you pass an indicator that has automatic updates as the argument, that first indicator is updated twice. The first update is from the security data and the second update is from the IndicatorExtensions class.
since the symbols at the beginning of the indicator chain are automatically updated, should i not update them manually? i have to say i don't completely understand this section of the docs.
thanks
Derek Melchin
Hi M.A. Wiedouw,
The implementation above is correct. Since it uses two Identity indicators with automatic updates, the entire indicator chain updates correctly with each time step. There is no need to manually update each Identity indicator.
The Of methods are correct. The indicators on the left-hand side of the operations are manual indicators and the indicators on the right-hand side of the operations are automatically updated.
See the attached backtest for reference.
Best,
Derek Melchin
Want to invest in QuantConnect as we build the Linux of quant finance? Checkout our Wefunder campaign to join the revolution.
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.
M. A. Wiedouw
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.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!