Overall Statistics
import decimal as d
import numpy as np

class MovingAverageCrossAlgorithm(QCAlgorithm):

    def Initialize(self):
        self.SetCash(100000)
        self.SetStartDate(2017,1,1)
        self.SetEndDate(2017,8,20)

        self.fast = dict()
        self.slow = dict()

        for ticker in ["AAPL", "TSLA", "BA", "FB"]:
		    symbol = self.AddEquity(ticker).Symbol
		    self.fast[symbol] = self.SMA(symbol, 20, Resolution.Daily)
		    self.slow[symbol] = self.SMA(symbol, 50, Resolution.Daily)


    def OnData(self, data):
    	
    	tolerance = 0.00015
    	
        for symbol in data.Keys:
        	
        	# Check whether the indicator that need more datapoint is ready 
            if not self.slow[symbol].IsReady:
                continue
        	
            fast = self.fast[symbol].Current.Value
            slow = self.slow[symbol].Current.Value
            quantity = self.Portfolio[symbol].Quantity
        
            # Go Long If MA20 > MA50
            if quantity <= 0 and fast > slow * d.Decimal(1 + tolerance):
                self.Log("BUY {0} for {1}".format(symbol, self.Securities[symbol].Price))
                self.SetHoldings(symbol, 0.24)

            # Go Short if MA20 < MA50
            if quantity > 0 and fast < slow:
            	self.Log("SELL {0} for {1}".format(symbol, self.Securities[symbol].Price))
            	self.Liquidate(symbol)