Overall Statistics
Total Trades
71
Average Win
17.40%
Average Loss
-4.85%
Compounding Annual Return
160.454%
Drawdown
47.300%
Expectancy
2.337
Net Profit
2759.819%
Sharpe Ratio
1.825
Loss Rate
27%
Win Rate
73%
Profit-Loss Ratio
3.59
Alpha
0.992
Beta
0.355
Annual Standard Deviation
0.635
Annual Variance
0.403
Information Ratio
0.795
Tracking Error
0.866
Treynor Ratio
3.264
Total Fees
$762.40
from math import ceil,floor
from datetime import datetime
import pandas as pd
import numpy as np
from sklearn.linear_model import LinearRegression


class TrendFollowingAlgorithm(QCAlgorithm):
    

    def Initialize(self):
        self.SetStartDate(2015, 1, 1)  
        self.SetEndDate(2018, 7, 2)
        self.SetCash(10000)            
        self.AddEquity("SPY", Resolution.Minute)
        self.load_symbols()
        self.SetBenchmark("DGAZ")
        
        for symbol in self.symbols:
            symbol.weight = 0
            symbol.stopprice = None
            
        
        self.Schedule.On(self.DateRules.EveryDay("SPY"), self.TimeRules.AfterMarketOpen("SPY", 5), Action(self.exit))
        self.Schedule.On(self.DateRules.EveryDay("SPY"), self.TimeRules.AfterMarketOpen("SPY", 30), Action(self.symbol_weighting))
        self.Schedule.On(self.DateRules.EveryDay("SPY"), self.TimeRules.AfterMarketOpen("SPY", 31), Action(self.trade))
        #self.Schedule.On(self.DateRules.EveryDay("SPY"), self.TimeRules.AfterMarketOpen("SPY", 120), Action(self.exit))
        #self.Schedule.On(self.DateRules.EveryDay("SPY"), self.TimeRules.AfterMarketOpen("SPY", 150), Action(self.symbol_weighting))
        #self.Schedule.On(self.DateRules.EveryDay("SPY"), self.TimeRules.AfterMarketOpen("SPY", 151), Action(self.trade))
        #self.Schedule.On(self.DateRules.EveryDay("SPY"), self.TimeRules.AfterMarketOpen("SPY", 250), Action(self.exit))
        #self.Schedule.On(self.DateRules.EveryDay("SPY"), self.TimeRules.AfterMarketOpen("SPY", 280), Action(self.symbol_weighting))
        #self.Schedule.On(self.DateRules.EveryDay("SPY"), self.TimeRules.AfterMarketOpen("SPY", 281), Action(self.trade))
        #self.Schedule.On(self.DateRules.EveryDay("SPY"), self.TimeRules.AfterMarketOpen("SPY", 350), Action(self.exit))
        #self.Schedule.On(self.DateRules.EveryDay("SPY"), self.TimeRules.AfterMarketOpen("SPY", 380), Action(self.symbol_weighting))
        #self.Schedule.On(self.DateRules.EveryDay("SPY"), self.TimeRules.AfterMarketOpen("SPY", 381), Action(self.trade))
        
    def OnData(self, data):
        pass   
        
    def symbol_weighting(self):
        for symbol in self.symbols:
             hist1 = self.History(self.symbols, 120, Resolution.Minute)
             hist2 = self.History(self.symbols, 15, Resolution.Daily)
             hist3 = self.History(self.symbols, 120, Resolution.Minute)
             hist4 = self.History(self.symbols, 80, Resolution.Daily)
             hist5 = self.History(self.symbols, 60, Resolution.Daily)
             hist6 = self.History(self.symbols, 9, Resolution.Daily)
             mean_price1 = (hist1.loc[symbol.Value]['close']).mean()
             mean_price2 = (hist2.loc[symbol.Value]['close']).mean()
             mean_price3 = (hist3.loc[symbol.Value]['close']).mean()
             mean_price4 = (hist4.loc[symbol.Value]['close']).mean()
             mean_price5 = (hist3.loc[symbol.Value]['close']).mean()
             mean_price6 = (hist6.loc[symbol.Value]['close']).mean()
             delta1 = ((hist2.loc[symbol.Value]['close']).mean()) - ((hist1.loc[symbol.Value]['close']).mean())
             delta2 = ((hist3.loc[symbol.Value]['close']).mean()) - ((hist4.loc[symbol.Value]['close']).mean())
             if mean_price2 > (mean_price4 * 1.6) and symbol.weight > 0:
                  self.Liquidate(symbol)
                  symbol.weight = 0
             elif mean_price1 > (mean_price2 * 1.09) and symbol.weight > 0:
                   #if mean_price3 > mean_price4 and symbol.weight > 0:
                      #symbol.weight = 0
                       self.Liquidate(symbol)
                       symbol.weight = 0
             elif mean_price3 < (mean_price4 * .4) and symbol.weight > 0:
                    self.Liquidate(symbol)
                    symbol.weight = 0
             #elif mean_price1 < (mean_price5 * .3) and symbol.weight > 0:
             #       self.Liquidate(symbol)
             #       symbol.weight = 0        
             #elif mean_price3 > (mean_price4 * 1.2):
             #       self.Liquidate(symbol)
             #       symbol.weight = 0
             elif mean_price1 < (mean_price2 * .9) and symbol.weight == 0:
                   if mean_price1 > (mean_price2 * .85) and symbol.weight == 0:
                       if mean_price1 < mean_price4 and symbol.weight == 0 :
                       #if mean_price3 > (mean_price4 * .75) and symbol.weight == 0:
                           #if mean_price3 < (mean_price4 * 1.25) and symbol.weight ==0:
                               #if mean_price3 < mean_price4 and symbol.weight == 0:
                               #if mean_price2 > (mean_price4 * -1.2): 
                               symbol.weight = 1
             elif mean_price2 > (mean_price4 * 1.5) and symbol.weight == 0:
                   if mean_price6 > mean_price2 and symbol.weight == 0:
                       symbol.weight = 1
             
    def trade(self):    
        for symbol in self.symbols:
            if symbol.weight == 0:
                self.Liquidate(symbol)
            elif symbol.weight != 0:
                  self.SetHoldings(symbol, symbol.weight)
            else:
                 self.Liquidate(symbol)
        
    def exit(self):
        hist1 = self.History(self.symbols, 120, Resolution.Minute)
        hist2 = self.History(self.symbols, 15, Resolution.Daily)
        for symbol in self.symbols:
            mean_price1 = (hist1.loc[symbol.Value]['close']).mean()
            mean_price2 = (hist2.loc[symbol.Value]['close']).mean()
            stoploss = abs(1.4)  # percent change per period
            
            if symbol.weight > 0 and symbol.stopprice is not None:
                #symbol.stopprice = max(mean_price / stoploss, symbol.stopprice)
                if mean_price1 < symbol.stopprice:
                    symbol.weight = 0
                    self.Liquidate(symbol)
            
            if symbol.weight > 0 and symbol.stopprice is None:
                symbol.stopprice = (mean_price2 / stoploss)
                if mean_price1 < symbol.stopprice:
                    symbol.weight = 0
                    self.Liquidate(symbol)
                
            #elif symbol.weight < 0 and symbol.stopprice is not None:
            #      symbol.stopprice = min(mean_price * stoploss, symbol.stopprice)
            #      if mean_price > symbol.stopprice:
            #         symbol.weight = 0
            #         self.Liquidate(symbol)
            
            #elif symbol.weight < 0 and symbol.stopprice is None:
            #      symbol.stopprice = (mean_price * stoploss)
            #      if mean_price > symbol.stopprice:
            #         symbol.weight = 0
            #         self.Liquidate(symbol)
            
            else:
                 symbol.stopprice = None         

    def load_symbols(self) :
        syl_list = [
            # Equity
            #'SPY',
            #'DIA',
            #'VOO',
            'DGAZ',
        ]
        
        self.symbols = []
        for i in syl_list:
            self.symbols.append(self.AddEquity(i, Resolution.Minute).Symbol)