Hello, I tried to add a SMA indicator to my algorith, however the error "AttributeError : attribute is read-only" comes up and I don't know how to fix the problem.
Any help would be appreciated thank you.
from clr import AddReference
AddReference("System")
AddReference("QuantConnect.Algorithm")
AddReference("QuantConnect.Common")
from System import *
from QuantConnect import *
from QuantConnect.Algorithm import *
from QuantConnect.Indicators import *
from QuantConnect.Data.Market import TradeBar
class RollingWindowAlgorithm(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2004, 1, 1) #Set Start Date
self.SetEndDate(2017, 12, 12) #Set End Date
self.SetCash(1000000) #Set Strategy Cash
self.SetBrokerageModel(BrokerageName.FxcmBrokerage)
self.AddForex("EURUSD", Resolution.Daily)
self.window = RollingWindow[QuoteBar](2)
# create a bollinger band
self.Bolband = self.BB("EURUSD", 20, 0.7, MovingAverageType.Simple, Resolution.Daily)
self.Bolband.Updated += self.BolbandUpdated
self.BolbandWin = RollingWindow[IndicatorDataPoint](5)
# create a simple moving average
self.SMA = self.SMA("EURUSD", 50, Resolution.Daily)
self.SMA.Updated += self.SMAUpdated
self.SMAWin = RollingWindow[IndicatorDataPoint](5)
# set warmup period
self.SetWarmUp(20)
def BolbandUpdated(self, sender, updated):
'''Adds updated values to rolling window'''
self.BolbandWin.Add(updated)
def SMAUpdated(self, sender, updated):
self.SMAWin.Add(updated)
def OnData(self, data):
self.window.Add(data["EURUSD"])
if not (self.window.IsReady and self.BolbandWin.IsReady): return
holdings = self.Portfolio["EURUSD"].Quantity
price = self.window[0].Close
previousPrice = self.window[1].Close
Buy_below_lowerband = (previousPrice <= self.Bolband.LowerBand.Current.Value) and (price > self.Bolband.LowerBand.Current.Value)
Liquidate_at_middleband_from_lowerband = (previousPrice > self.Bolband.MiddleBand.Current.Value and price == self.Bolband.MiddleBand.Current.Value)
Go_Long = (previousPrice < self.SMA.Current.Value and price == self.SMA.Current.Value)
Go_Short = (previousPrice > self.SMA.Current.Value and price == self.SMA.Current.Value)
if holdings <= 0:
if Buy_below_lowerband:
self.SetHoldings("EURUSD", 1.0)
if previousPrice == self.SMA.Current.Value and price < self.SMA.Current.Value:
self.Liquidate()
self.SetHoldings("EURUSD", -1.0)
if Go_Long:
self.Liquidate()
elif Liquidate_at_middleband_from_lowerband:
self.Liquidate()
if previousPrice >= self.Bolband.UpperBand.Current.Value and price < self.Bolband.UpperBand.Current.Value:
self.SetHoldings("EURUSD", -1.0)
if previousPrice == self.SMA.Current.Value and price > self.SMA.Current.Value:
self.Liquidate()
self.SetHoldings("EURUSD", 1.0)
if Go_Short:
self.Liquidate
elif Liquidate_at_middleband_from_lowerband:
self.Liquidate()
if holdings > 0:
if previousPrice >= self.Bolband.UpperBand.Current.Value and price < self.Bolband.UpperBand.Current.Value:
self.SetHoldings("EURUSD", -1.0)
if previousPrice == self.SMA.Current.Value and price > self.SMA.Current.Value:
self.Liquidate()
self.SetHoldings("EURUSD", 1.0)
if Go_Short:
self.Liquidate
elif Liquidate_at_middleband_from_lowerband:
self.Liquidate()
if Buy_below_lowerband:
self.SetHoldings("EURUSD", 1.0)
if previousPrice == self.SMA.Current.Value and price < self.SMA.Current.Value:
self.Liquidate()
self.SetHoldings("EURUSD", -1.0)
if Go_Long:
self.Liquidate
elif Liquidate_at_middleband_from_lowerband:
self.Liquidate()
Jing Wu
Please avoid using the LEAN method to name the variable. self.SMA is the indicator method. You can use self.rsi. It would be better to attach the algorithm instead of copy and paste the entire code.
Alex Haseldine
Ok thank you for your help :)
Alex Haseldine
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!