Overall Statistics
Total Trades
4257
Average Win
0.06%
Average Loss
-0.05%
Compounding Annual Return
-100.000%
Drawdown
27.300%
Expectancy
-0.286
Net Profit
-27.277%
Sharpe Ratio
-1.472
Probabilistic Sharpe Ratio
0.000%
Loss Rate
68%
Win Rate
32%
Profit-Loss Ratio
1.20
Alpha
-1.325
Beta
0.819
Annual Standard Deviation
0.679
Annual Variance
0.462
Information Ratio
-2.151
Tracking Error
0.649
Treynor Ratio
-1.221
Total Fees
$6497.38
Estimated Strategy Capacity
$740000.00
Lowest Capacity Asset
MSFT R735QTJ8XC9X
Portfolio Turnover
73306.55%
from AlgorithmImports import *
import numpy as np

class BasicAlgo(QCAlgorithm):
    def Initialize(self):
        self.SetStartDate(2022, 7, 29)
        self.SetEndDate(2022, 8, 3)
        self.SetCash(100000)


        # Define the symbol and "type" of our generic data:
        self.symbol = self.AddEquity("MSFT", Resolution.Second).Symbol
        self.SetBenchmark(self.symbol)

        self.td = float(self.GetParameter("tick_amount"))
        self.Debug('Tick amount: ' + str(self.td) + ' ')
        self.window_size_p = int(float(self.GetParameter("window_size_p")))
        self.window_size_n = int(float(self.GetParameter("window_size_n")))

        renkoClose = ClassicRenkoConsolidator(self.td, evenBars = False)
        renkoClose.DataConsolidated += self.HandleRenkoClose

        self.SubscriptionManager.AddConsolidator(self.symbol, renkoClose)

        self.tick_seq = []
        self.postive_c = 0
        self.negative_c = 0
        self.close = 0
        self.max = float('-inf')
        self.min = float('inf')

        self.tradingEnabled = False

    def OnData(self, slice):
        pass

    def HandleRenkoClose(self, sender, data):
        self.tradingEnabled = True
        self.Debug(f"RENKO CLOSE - {data.Time} - {data.Open} {data.Close}")
        if data.Open < data.Close:
            self.tick_seq.append(1)
        else:
            self.tick_seq.append(-1)

        if self.tradingEnabled:
            if self.Portfolio.Invested:
                if data.Close > self.max:
                    self.max = data.Close
                if data.Close < self.min:
                    self.min = data.Close 

                if self.Portfolio[self.symbol].IsShort:
                    self.negative_c +=1
                    self.Debug('Negative Counter' + str(self.negative_c))
                    if self.tick_seq[-1] == 1 or self.negative_c == self.window_size_n :
                        self.SetHoldings(self.symbol, 0)
                        self.Debug('Short')
                        self.Debug('Finish')
                        self.Debug('Trade time: ' + str(self.Time))
                        self.Debug('Price ' + str(round(data.Close,3))+ '     ')
                        self.Debug('Min ' + str(round(self.min,3))+ ' ')
                        self.Debug('Max ' + str(round(self.max,3))+ ' ')
                        self.max = float('-inf')
                        self.min = float('inf')
                        self.negative_c = 0

                if self.Portfolio[self.symbol].IsLong: 
                    self.postive_c +=1
                    self.Debug('Positive Counter' + str(self.postive_c))
                    if self.tick_seq[-1] == -1 or self.postive_c == self.window_size_p:
                        self.Liquidate()
                        self.Debug('Long ')
                        self.Debug('Finish')
                        self.Debug('Trade time: ' + str(self.Time))
                        self.Debug('Price ' + str(round(data.Close,3))+ '     ')
                        self.Debug('Min ' + str(round(self.min,3))+ ' ')
                        self.Debug('Max ' + str(round(self.max,3))+ ' ')
                        self.max = float('-inf')
                        self.min = float('inf')
                        self.postive_c = 0


            if not self.Portfolio.Invested:
                if self.tick_seq[-1]==-1:
                    self.SetHoldings(self.symbol,-1)                           
                    self.Debug('Short')
                    self.Debug('Start ')
                    self.Debug('Trade time: ' + str(self.Time))
                    self.Debug('Price ' + str(round(data.Close,3))+ '      ')
                    self.Debug('Min 000000 ')
                    self.Debug('Max 000000 ')
                    self.negative_c = 1

                if self.tick_seq[-1]==1:
                    self.SetHoldings(self.symbol,1)
                    self.Debug('Long ')
                    self.Debug('Start ')
                    self.Debug('Trade time: ' + str(self.Time))
                    self.Debug('Price ' + str(round(data.Close,3))+ '      ')
                    self.Debug('Min 000000 ')
                    self.Debug('Max 000000 ')
                    self.postive_c = 1
        

    def OnEndOfAlgorithm(self):
        self.Debug(str(self.tick_seq))
        self.ExitPositions()
    def ExitPositions(self):
        if self.Portfolio[self.symbol].IsShort:
            self.SetHoldings(self.symbol, 0)
            self.Debug('Short')
            self.Debug('Finish')
            self.Debug('Trade time: ' + str(self.Time))
            self.Debug('Price ' + str(round(self.close,3))+ '     ')
            self.Debug('Min ' + str(round(self.min,3))+ ' ')
            self.Debug('Max ' + str(round(self.max,3))+ ' ')
        if self.Portfolio[self.symbol].IsLong:
            self.Liquidate()
            self.Debug('Long ')
            self.Debug('Finish')
            self.Debug('Trade time: ' + str(self.Time))
            self.Debug('Price ' + str(round(self.close,3))+ '     ')
            self.Debug('Min ' + str(round(self.min,3))+ ' ')
            self.Debug('Max ' + str(round(self.max,3))+ ' ')
            self.max = float('-inf')
            self.min = float('inf')



class Bitstamp(PythonData):
    def GetSource(self, config, date, isLiveMode):
        source = " https://www.dropbox.com/scl/fi/vpj9sl8tn9is874bibi1k/DATA_PROB.csv?rlkey=2jx70njirja8kpxecxnuk23ts&dl=1"
        return SubscriptionDataSource(source, SubscriptionTransportMedium.RemoteFile)

    def Reader(self, config, line, date, isLiveMode):
        
        if not line.strip():
            return None

        coin = Bitstamp()
        coin.Symbol = config.Symbol

        # Example Line Format:
        # Date      Open   High    Low     Close   Volume (BTC)    Volume (Currency)   Weighted Price
        # 2011-09-13 5.8    6.0     5.65    5.97    58.37138238,    346.0973893944      5.929230648356
        if not line[0].isdigit():
            return None

        data = line.split(',')

        # If value is zero, return None
        coin.Value = float(data[4])
        if coin.Value == 0:
            return None

        coin.Time = datetime.strptime(data[0], "%Y-%m-%d %H:%M:%S") 
        coin["Close"] = coin.Value

        return coin