Hello,

So, The following algorithm is trying to trade based on following conditions:

Long: if the price breaks above the 20 SMA and slope of 200 SMA is positive

Sell: if the price breaks below the 20 SMA

The stop loss is set to 1%

Questions:

1. how can I eliminate the fee structure when I have rolling window?

2. how does the rolling window indicator data points affected my returns? I am only using the first two data points but if I change `self.smaWin = RollingWindow[IndicatorDataPoint](2)` to `self.smaWin = RollingWindow[IndicatorDataPoint](200)` the results are worse. Is it because it will first fill up the window with 200 points and then try to perform everything afterwards? Specifically, my slope will be performed after 200 hours instead of 2 hours?

3. I followed https://www.quantconnect.com/forum/discussion/4497/adding-a-sma-indicator/p1 where line 53, he is getting the price, but I get `

Runtime Error: AttributeError : 'NoneType' object has no attribute 'Price'
at OnData in main.py:line 71
:: price = self.window[0].Price
AttributeError : 'NoneType' object has no attribute 'Price'

4. The drawdown in Overview is Max drawdown?

Thanks

# https://www.quantconnect.com/forum/discussion/4497/adding-a-sma-indicator/p1

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

### <summary>
### Using rolling windows for efficient storage of historical data; which automatically clears after a period of time.
### </summary>
### <meta name="tag" content="using data" />
### <meta name="tag" content="history and warm up" />
### <meta name="tag" content="history" />
### <meta name="tag" content="warm up" />
### <meta name="tag" content="indicators" />
### <meta name="tag" content="rolling windows" />
class RollingWindowAlgorithm(QCAlgorithm):

def Initialize(self):
'''Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.'''

self.SetStartDate(2019,5,15) #Set Start Date
self.SetCash(1000) #Set Strategy Cash
# Find more symbols here: http://quantconnect.com/data
self.AddEquity("SPY", Resolution.Hour)

self.Securities["SPY"].FeeModel = ConstantFeeModel(0)
# Creates a Rolling Window indicator to keep the 2 TradeBar
self.window = RollingWindow[TradeBar](1) # For other security types, use QuoteBar

# Creates an indicator and adds to a rolling window when it is updated
self.EMA("SPY", 200, Resolution.Hour).Updated += self.SmaUpdated
self.smaWin = RollingWindow[IndicatorDataPoint](2)

# create a 20 day exponential moving average
# Creates an indicator and adds to a rolling window when it is updated
self.fast = self.EMA("SPY", 20, Resolution.Hour)
self.fast.Updated += self.FastUpdated
self.fastWin = RollingWindow[IndicatorDataPoint](20)


def SmaUpdated(self, sender, updated):
'''Adds updated values to rolling window'''
self.smaWin.Add(updated)

def FastUpdated(self, sender, updated):
'''Adds updated values to rolling window'''
self.fastWin.Add(updated)


def OnData(self, data):
'''OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.'''

# Add SPY TradeBar in rollling window
self.window.Add(data["SPY"])

# Wait for windows to be ready.
if not (self.window.IsReady and self.smaWin.IsReady): return

currSma = self.smaWin[0] # Current SMA had index zero.
pastSma = self.smaWin[1]
slope = currSma.Value - pastSma.Value

holdings = self.Portfolio["SPY"].Quantity
price = self.window[0].Price
long_condition = price > self.fast.Current.Value and slope > 0
# we only want to go long if we're currently short or flat
if holdings <= 0:
if long_condition:
quantity = self.CalculateOrderQuantity("SPY", 1.0)
self.MarketOrder("SPY", quantity)
self.StopMarketOrder("SPY", -quantity, self.Securities["SPY"].Price*0.99)



# we only want to liquidate if we're currently long
# if the fast is less than the slow we'll liquidate our long
if holdings > 0 and price < self.fast.Current.Value:
self.Liquidate("SPY")

Author