Overall Statistics
Total Trades
67
Average Win
6.61%
Average Loss
-4.39%
Compounding Annual Return
-18.528%
Drawdown
49.500%
Expectancy
-0.127
Net Profit
-33.722%
Sharpe Ratio
-0.22
Loss Rate
65%
Win Rate
35%
Profit-Loss Ratio
1.51
Alpha
-1.381
Beta
64.278
Annual Standard Deviation
0.454
Annual Variance
0.206
Information Ratio
-0.264
Tracking Error
0.454
Treynor Ratio
-0.002
Total Fees
$258.41
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")
        avgCross.AddSeries(Series("FastMA", SeriesType.Line, 1))
        avgCross.AddSeries(Series("SlowMA", SeriesType.Line, 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)