Overall Statistics Total Trades6Average Win5.43%Average Loss-2.89%Compounding Annual Return6.503%Drawdown13.000%Expectancy0.920Net Profit7.908%Sharpe Ratio0.543Probabilistic Sharpe Ratio30.129%Loss Rate33%Win Rate67%Profit-Loss Ratio1.88Alpha0.04Beta0.135Annual Standard Deviation0.139Annual Variance0.019Information Ratio-0.635Tracking Error0.3Treynor Ratio0.559Total Fees$6.00Estimated Strategy Capacity$2500000.00
### <summary>
### Simple SMA Strategy intended to provide a minimal algorithm example using
### one indicator with the most basic plotting
### </summary>
from datetime import timedelta

class SMAAlgorithm(QCAlgorithm):

# 2 - Cycle through stocks
# 3 - Cycle through list adding each equity
# 3 - Create an indicator dict like backtrader

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.'''

# Set our main strategy parameters
self.SetStartDate(2020,1,1)    # Set Start Date
#self.SetEndDate(2018,1,1)      # Set End Date
self.SetCash(10000)            # Set Strategy Cash

SMA_Period    = 14                # SMA Look back period
self.SMA_OB   = 75                # SMA Overbought level
self.SMA_OS   = 50                # SMA Oversold level
self.Allocate = 0.20              # Percentage of captital to allocate

self.Equities = ["FSLR", "FB"]

self.Indicators = dict()
self.Charts = dict()
self.Consolidators = dict()

# Find more symbols here: http://quantconnect.com/data
for Symbol in self.Equities:
self.Consolidators[Symbol] = dict()

# Each Equity requires its own consoilidator! See:
# https://www.quantconnect.com/forum/discussion/1936/multiple-consolidators/p1
# https://www.quantconnect.com/forum/discussion/1587/multiple-symbol-indicator-values-in-consolidated-bar-handler/p1
# ------------------------
# Create our consolidators

# Register our Handlers
self.Consolidators[Symbol]['onDayCon'].DataConsolidated += self.onDay
self.Consolidators[Symbol]['minutesCon'].DataConsolidated += self.minutes20

self.Indicators[Symbol] = dict()
self.Indicators[Symbol]['SMA'] = dict()
self.Indicators[Symbol]['Ichimoku'] = dict()

self.Indicators[Symbol]['SMA']['SMA200'] = self.SMA(Symbol, 200, Resolution.Daily)
self.Indicators[Symbol]['SMA']['SMA50'] = self.SMA(Symbol, 50, Resolution.Daily)
self.Indicators[Symbol]['Ichimoku'] = self.ICHIMOKU(Symbol,9, 26, 26, 52, 26, 26, Resolution.Daily)
# Register the indicaors with our stock and consolidator

self.RegisterIndicator(Symbol, self.Indicators[Symbol]['SMA']['SMA50'], self.Consolidators[Symbol]['onDayCon'])
self.RegisterIndicator(Symbol, self.Indicators[Symbol]['SMA']['SMA200'], self.Consolidators[Symbol]['onDayCon'])
self.RegisterIndicator(Symbol, self.Indicators[Symbol]['Ichimoku'], self.Consolidators[Symbol]['onDayCon'])

# Finally add our consolidators to the subscription

self.Charts[Symbol] = dict()
# Plot the SMA

# Create a custom volume chart
VolChartName = Symbol+" Volume"
self.Charts[Symbol]['VOL'] = Chart(VolChartName, ChartType.Stacked)

# Ensure that the Indicator has enough data before trading.

self.SetWarmUp(timedelta(days= 200))
self.dayCount = 0
self.countMinutes = 0

def onDay(self,sender,bar):

# Make sure we are not warming up
if self.IsWarmingUp: return
Symbol = str(bar.get_Symbol())

# Loop through our equities
for Symbol in self.Equities:

Close= bar.Close
Volume = bar.Volume
SMA200 = self.Indicators[Symbol]['SMA']['SMA200'].Current.Value
SMA50 = self.Indicators[Symbol]['SMA']['SMA50'].Current.Value
tenkan = self.Indicators[Symbol]['Ichimoku'].Tenkan.Current.Value
kijun = self.Indicators[Symbol]['Ichimoku'].Kijun.Current.Value
senkouA = self.Indicators[Symbol]['Ichimoku'].SenkouA.Current.Value
senkouB = self.Indicators[Symbol]['Ichimoku'].SenkouB.Current.Value

#self.Debug("{}: Close: {} SMA: {}".format(Symbol, Close, SMA200))

if bar.Close >= bar.Open:
else:
self.Plot(Symbol+" Volume", 'Selling Volume', Volume)

# Determine our entry and exit conditions
# Do it here to avoid long lines later
Long_Con1 = SMA200 < SMA50
Long_Cond2 = True
Exit_Con1 = SMA200 > SMA50
Exit_Cond2 = True

if not self.Securities[Symbol].Invested:
# If not, the long conditions
if all([Long_Con1, Long_Cond2]):
self.SetHoldings(Symbol, self.Allocate)
else:

if all([Exit_Con1, Exit_Cond2]):
# Sell!
self.Liquidate(Symbol)

if self.dayCount> 3 : return

self.Debug(" onDay")
Symbol = str(bar.get_Symbol())
self.Debug(Symbol)
self.Debug(bar.Close)
self.dayCount  = self.dayCount +  1

def minutes20(self,sender,bar):
if self.IsWarmingUp: return

#debug
if self.countMinutes> 3 : return

self.Debug(" 20Minutes")
Symbol = str(bar.get_Symbol())
self.Debug(Symbol)
self.Debug(bar.Close)
self.countMinutes  = self.countMinutes +  1

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

Arguments:
data: Slice object keyed by symbol containing the stock data
'''

# Make sure we are not warming up
if self.IsWarmingUp: return