Overall Statistics
Total Trades
0
Average Win
0%
Average Loss
0%
Compounding Annual Return
0%
Drawdown
0%
Expectancy
0
Net Profit
0%
Sharpe Ratio
0
Probabilistic Sharpe Ratio
0%
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0
Beta
0
Annual Standard Deviation
0
Annual Variance
0
Information Ratio
-2.396
Tracking Error
0.212
Treynor Ratio
0
Total 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.AddEquity("SPY", Resolution.Hour)
        self.start = datetime.date(2020, 3, 15)
        self.end = datetime.date(2020, 3, 25)
        self.features = ["open", "high", "low", "close", "volume"]
        self.estado = True
        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_buy_volume"] = (self.volume["cum_buy_volume"].max(),self.volume["cum_buy_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_buy_volume"),   self.Norm("cum_buy_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.Debug(self.volume.head(10).to_string())
            self.Debug("....  .... ....")
            self.Debug(self.volume.tail(10).to_string())
            self.estado = False