Back

SMA and ratio python

Hi, I am new to python and Quantconnect. I got soem questions in my development project.

First, I wonder whether we can divide two equities in directly in Initializer.

Second, it is possible to calculate the SMA of the ratio by using self.SMA(self.ratio, .....)

Thank you. Below is the code

class SMATrendAlgo(QCAlgorithm):

    def Initialize(self):
        self.period = 20
        self.SetStartDate(2010, 01, 01)  #Set Start Date
        self.SetEndDate(2015, 01, 01)    #Set End Date
        self.SetCash(100000)             #Set Strategy Cash
        self.spy = self.AddEquity("SPY", Resolution.Daily).value
        self.tlt = self.AddEquity("TLT", Resolution.Daily).value
        self.ratio = self.spy/self.tlt
        self.sma20 = self.SMA(self.ratio, 20, Resolution.Daily)
Update Backtest








In Lean/QuantConnect, indicators are composable - meaning they can be chained together to create unique combinations much like lego blocks. This is how your should define the ratio moving average:

# Create Identity indicators
spyId = self.Identity("SPY")
tltId = self.Identity("TLT")

# Compose them into the ratio indicator
# and the SMA of the ratio
ratio = IndicatorExtensions.Over(spyId, tltId)
self.sma20 = IndicatorExtensions.SMA(ratio, 20)

# Plot them for visual sanity check
self.PlotIndicator("Ratio", ratio, self.sma20)
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.


Thanks a lot Alexandre. I wonder how we can get the 1day lagged value instead of current value. 

0

You can use the Delay Of an Identity:

spyId = self.Identity("SPY")
spyDelay = IndicatorExtensions.Of(Delay(1), spyId)
self.PlotIndicator("SPY", spyId, spyDelay)
tltId = self.Identity("TLT")
tltDelay = IndicatorExtensions.Of(Delay(1), tltId)
self.PlotIndicator("TLT", tltId, tltDelay)
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