Overall Statistics
Total Trades
329
Average Win
2.96%
Average Loss
-0.09%
Compounding Annual Return
31.362%
Drawdown
4.300%
Expectancy
8.444
Net Profit
14.460%
Sharpe Ratio
3.209
Loss Rate
72%
Win Rate
28%
Profit-Loss Ratio
33.00
Alpha
0.481
Beta
-10.324
Annual Standard Deviation
0.086
Annual Variance
0.007
Information Ratio
2.976
Tracking Error
0.086
Treynor Ratio
-0.027
Total Fees
$361.88
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,9) #Set Start Date
        self.SetEndDate(2018,3,8)   #Set End Date
        self.SetCash(100000)             #Set Strategy Cash
        self.SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage)
        
        # Find more symbols here: http://quantconnect.com/data
        self.spy = self.AddEquity("SPY", Resolution.Minute).Symbol
        self.x = self.AddEquity("X", Resolution.Minute).Symbol
        self.tbf = self.AddEquity("TBF", Resolution.Minute).Symbol
        self.shak = self.AddEquity("SHAK", Resolution.Minute).Symbol
        self.hal = self.AddEquity("HAL", Resolution.Minute).Symbol
        
        # create exponential moving averages
        self.SPY = self.EMA("SPY", 18, Resolution.Daily);
        self.X = self.EMA("X", 15, Resolution.Daily);
        self.TBF = self.EMA("TBF", 21, Resolution.Daily);
        self.SHAK = self.EMA("SHAK", 50, Resolution.Daily);
        self.HAL = self.EMA("HAL", 21, Resolution.Daily);
        
        self.previous = None
    
    def OnData(self, data):

        #Define a small tolerance on our checks to avoid bouncing
        tolerance = 0.007;
        
        #SPY
        if self.Securities["SPY"].Price > self.SPY.Current.Value * d.Decimal(1 + tolerance):
            cash = self.Portfolio.Cash * d.Decimal(0.30)
            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")
            
        #X
        if self.Securities["X"].Price > self.X.Current.Value * d.Decimal(1 + tolerance):
            cash2 = self.Portfolio.Cash * d.Decimal(0.15)
            number_of_shares2 = (cash2/self.Securities["X"].Price)
            self.MarketOrder("X", number_of_shares2)

        if self.Securities["X"].Price < self.X.Current.Value * d.Decimal(1 - tolerance):
            self.Liquidate("X")
            
        #TBF
        if self.Securities["TBF"].Price > self.TBF.Current.Value * d.Decimal(1 + tolerance):
            cash3 = self.Portfolio.Cash * d.Decimal(0.34)
            number_of_shares3 = (cash3/self.Securities["TBF"].Price)
            self.MarketOrder("TBF", number_of_shares3)

        if self.Securities["TBF"].Price < self.TBF.Current.Value * d.Decimal(1 - tolerance):
            self.Liquidate("TBF")
        
        #SHAK
        if self.Securities["SHAK"].Price > self.SHAK.Current.Value * d.Decimal(1 + tolerance):
            cash4 = self.Portfolio.Cash * d.Decimal(0.07)
            number_of_shares4 = (cash4/self.Securities["SHAK"].Price)
            self.MarketOrder("SHAK", number_of_shares4)

        if self.Securities["SHAK"].Price < self.SHAK.Current.Value * d.Decimal(1 - tolerance):
            self.Liquidate("SHAK")
            
        #HAL
        if self.Securities["HAL"].Price > self.HAL.Current.Value * d.Decimal(1 + tolerance):
            cash5 = self.Portfolio.Cash * d.Decimal(0.12)
            number_of_shares5 = (cash5/self.Securities["HAL"].Price)
            self.MarketOrder("HAL", number_of_shares5)

        if self.Securities["HAL"].Price < self.HAL.Current.Value * d.Decimal(1 - tolerance):
            self.Liquidate("HAL")
        
        self.previous = self.Time