Overall Statistics
import numpy as np

class BasicTemplateAlgorithm(QCAlgorithm):
    def Initialize(self):
        self.SetStartDate(2013,10, 7)
        self.SetEndDate(2015,10,11)
        self.SetCash(100000)
        self.AddEquity("TSLA", Resolution.Hour)
        self.ema_slow_period = 50
        self.ema_slow        = None
        self.ema_fast_period = 20
        self.ema_fast        = None

        self.slices_since_last_order = 0
        self.last_direction = 0
        
        avgCross = Chart("Average Cross")

        ## Indexing at 0 ensures both Series show up and are overlaid on the same plot
        avgCross.AddSeries(Series("FastMA", SeriesType.Line, 0))
        avgCross.AddSeries(Series("SlowMA", SeriesType.Line, 0))
        ## If you wanted to separate the two series, then you can index one of the series
        ## above at 1

        self.AddChart(avgCross)
        
    def OnData(self, data):
        last = float(data.Bars['TSLA'].Close)
        if not self.ema_fast or not self.ema_slow:
            self.ema_fast = last
            self.ema_slow = last
        else:
            w_slow = 2 / (self.ema_slow_period + 1)
            self.ema_slow = last * w_slow + self.ema_slow * (1 - w_slow)

            w_fast = 2 / (self.ema_fast_period + 1)
            self.ema_fast = last * w_fast + self.ema_fast * (1 - w_fast)

        self.Plot("Average Cross", "SlowMA", self.ema_slow)
        self.Plot("Average Cross", "FastMA", self.ema_fast)
        
        if self.ema_slow < self.ema_fast and self.last_direction != 1:
            self.last_direction = 1
            self.SetHoldings("TSLA", 1)
        elif self.ema_slow > self.ema_fast and self.last_direction != -1:
            self.last_direction = -1
            self.SetHoldings("TSLA", -1)