Overall Statistics Total Trades0Average Win0%Average Loss0%Compounding Annual Return0%Drawdown0%Expectancy0Net Profit0%Sharpe Ratio0Probabilistic Sharpe Ratio0%Loss Rate0%Win Rate0%Profit-Loss Ratio0Alpha0Beta0Annual Standard Deviation0Annual Variance0Information Ratio-2.396Tracking Error0.212Treynor Ratio0Total Fees\$0.00
import datetime
import numpy as np
import pandas as pd
import mlfinlab as ml
from scipy import stats
import math

class ModulatedHorizontalAutosequencers(QCAlgorithm):
index = 0
volume = None
volmet = {}

def Initialize(self):
self.SetStartDate(2020, 4, 20)  # Set Start Date
self.SetEndDate(2020, 9, 19)
self.SetCash(100000)  # Set Strategy Cash
self.start = datetime.date(2020, 3, 15)
self.end = datetime.date(2020, 3, 25)
self.features = ["open", "high", "low", "close", "volume"]
self.stockPlot = Chart('Volume Levels')

def Lbl(self, _z):
return _z + str( int( self.volmet[_z][0] - self.volmet[_z][1] ) )

def Norm(self, _s):
try:
if self.volmet[_s][0] == 0:
return round(math.log(100), 4)
return str(round(math.log(  100 * ( math.fabs(self.volume[_s].values[self.index] - self.volmet[_s][1] ) / self.volmet[_s][0] )),4))
except:
return round(math.log(100), 4)

def OnData(self, data):
if self.volume is not None:
if len(self.volmet.keys()) == 0:
self.volmet["close"] = (self.volume["close"].max(),self.volume["close"].min())
self.volmet["volume"] = (self.volume["volume"].max(), self.volume["volume"].min())
self.volmet["cum_ticks"] = (self.volume["cum_ticks"].max(), self.volume["cum_ticks"].min())
self.volmet["cum_dollar_value"] = (self.volume["cum_dollar_value"].max(),self.volume["cum_dollar_value"].min())

if self.index < len(self.volume.index):
self.Plot("Volume Levels", self.Lbl("close"),            self.Norm("close"))
self.Plot("Volume Levels", self.Lbl("volume"),           self.Norm("volume"))
self.Plot("Volume Levels", self.Lbl("cum_ticks"),        self.Norm("cum_ticks"))
self.Plot("Volume Levels", self.Lbl("cum_dollar_value"), self.Norm("cum_dollar_value"))
self.index += int((self.EndDate - self.StartDate).days / (6.5 * 5/7))

if len(self.Securities.Keys) > 0 and self.estado:
h1 = self.History([k.Value for k in self.Securities.Keys], self.end - self.start, Resolution.Tick)

data = h1#[h1.suspicious == False]
data = data[["lastprice", "quantity"]]
data = data.loc["SPY R735QTJ8XC9X"]

df = pd.DataFrame()
df["date_time"] = data.index.values
df["price"] = data.lastprice.values
df["volume"] = data.quantity.values

self.volume = ml.data_structures.get_volume_bars(df, threshold=20000, batch_size=1000000, verbose=False)
self.estado = False