Overall Statistics Total Trades6Average Win2.18%Average Loss-0.40%Compounding Annual Return151.843%Drawdown1.700%Expectancy4.128Net Profit8.343%Sharpe Ratio13.68Probabilistic Sharpe Ratio97.906%Loss Rate20%Win Rate80%Profit-Loss Ratio5.41Alpha1.494Beta0.018Annual Standard Deviation0.111Annual Variance0.012Information Ratio0.715Tracking Error0.203Treynor Ratio85.323Total Fees\$15.11
import pandas as pd
import numpy as np
from io import StringIO

class RedditStockSentiment(QCAlgorithm):

def Initialize(self):
self.SetStartDate(2020,8, 1)
self.SetEndDate(2020, 9, 1)
self.SetCash(100000)
self.tickers = ["HD"]
self.investP = 1/len(self.tickers)
for stock in self.tickers:

self.SetBenchmark("HD")

self.df = self.df.drop([0, 1, 7, 8, 14, 15, 21, 22, 28, 29]) #Drops all weekend data

self.Schedule.On(self.DateRules.EveryDay(),
self.TimeRules.At(8, 30),
self.runDaily) #Trade once a day at 8:30 am

self.trafficValues = RollingWindow[int](5) #one trading week of foot traffic data
self.smaSlope = RollingWindow[int](2) #two day rollingwindow for SMA

def OnData(self, data):

return

for row in self.df.itertuples(): #Chooses data corresponding to today
count = int(row)
traffic = int(row)
break
else:
continue

currentSMA = sum(self.trafficValues)/5 #Calculates the current SMA with a 5 day lookback

try:
slope = (self.smaSlope - self.smaSlope)/1 #Calculates the slope of the SMA  (SMA0-SMA1)/(DateToday-DateYesterday)
except:
slope = 0 #If error make slope 0. Only error if rolling window is missing data point

self.Debug("Count: " + str(count) + " Day: " + str(self.tradingday) + " Slope: " + str(slope))

if count == self.tradingday & count > 4 & (count != 7 or 14 or 21 or 28): #Double checking for current day, checking if atleast a week has passed for foot traffic data rolling window to fill, double checks to make sure we are not trading on weekend
if(slope > 0) and not self.Portfolio["HD"].IsLong: #If slope is positive long
self.SetHoldings("HD", self.investP, True)
elif(slope < 0) and not self.Portfolio["HD"].IsShort: #If slope is negative short
self.SetHoldings("HD", -(self.investP*0.4), True) #40% leverage
elif(slope == 0): #If slope is 0 then liquidate. (Also known as the Flat insight)
self.Liquidate("HD")

self.trade = True