Overall Statistics
Total Trades
5
Average Win
4.38%
Average Loss
-0.67%
Compounding Annual Return
21.979%
Drawdown
11.700%
Expectancy
2.781
Net Profit
10.277%
Sharpe Ratio
1.707
Loss Rate
50%
Win Rate
50%
Profit-Loss Ratio
6.56
Alpha
0.272
Beta
-3.181
Annual Standard Deviation
0.122
Annual Variance
0.015
Information Ratio
1.542
Tracking Error
0.122
Treynor Ratio
-0.065
Total Fees
$10.18
import clr
clr.AddReference("System")
clr.AddReference("QuantConnect.Algorithm")
clr.AddReference("QuantConnect.Indicators")
clr.AddReference("QuantConnect.Common")
from System import *
from QuantConnect import *
from QuantConnect.Algorithm import *
from QuantConnect.Indicators import *
import decimal as d
import math
import numpy as np
import pandas as pd
import statistics
from datetime import datetime, timedelta

class MovingAverageCrossAlgorithm(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(2017,9,8) #Set Start Date
        self.SetEndDate(2018,3,6)   #Set End Date
        self.SetCash(100000)             #Set Strategy Cash
        
        # Find more symbols here: http://quantconnect.com/data
        self.spy = self.AddEquity("SPY", Resolution.Minute).Symbol

        # create a 21 day exponential moving average
        self.SPY = self.EMA("SPY", 252, Resolution.Daily);
        
        self.previous = None
    
    def OnData(self, data):
        
        #Define a small tolerance on our checks to avoid bouncing
        tolerance = 0.003;
        
        #SPY
        if self.Securities["SPY"].Price > self.SPY.Current.Value * d.Decimal(1 + tolerance):
            cash = self.Portfolio.Cash
            number_of_shares = (cash/self.Securities["SPY"].Price)
            self.MarketOrder("SPY", number_of_shares)
            
        if self.Securities["SPY"].Price < self.SPY.Current.Value * d.Decimal(1 - tolerance):
            self.Liquidate("SPY")
            
        self.previous = self.Time