Back

Could Use Some Help

Hi all,
I'm new to C# and relatively new to programming in general. I'm trying to build a simple algorithm to backtest a pairs trading idea.
Essentially I want to take two securities (let's say coke and pepsi) and find the ratio of the price of one to the other (I think I got that part down). After that I would like to find the average of that ratio at the close of the last 14 days. I tried using the SMA function but it appears to only work with securities and not with custom variables. I also need to find the 50 day standard deviation for the ratio of the prices of the two stocks. If someone could explain how to do this I would greatly appreciate it. The idea is to short the overpriced stock and buy the cheap stock when the ratio is 3 standard deviations above or below its 14-day average. I think I figured out how to execute the orders using if statements but I can't solve for z score of the ratio because I don't know how to find a 14-day mean or stdev.
Update Backtest








You can use SimpleMovingAverage class to find the SMA of anything you'd like! Just create a new instance of it and pass data points into the Update() method.

You could also compute this manually with a few lines:
Queue.Enqueue(x);
if (Queue.Count > 14) Queue.Dequeue();
var sma = Queue.Sum()/Queue.Count();


We also support Math.Numerics, so any functions you find in there you can use by adding the right "using" statement to the top of the file.
1

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.


Thanks for the quick reply. Like I said, I'm pretty new to programming so I don't know how to go about using the update method. Could you tell me specifically what to do in order to make my variable ratio (stock price 1 / stock price 2) compatible with the SMA class and STD class?
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