Overall Statistics
Total Trades
1395
Average Win
0.01%
Average Loss
0.00%
Compounding Annual Return
9.982%
Drawdown
0.400%
Expectancy
0.111
Net Profit
0.261%
Sharpe Ratio
2.917
Probabilistic Sharpe Ratio
64.742%
Loss Rate
63%
Win Rate
37%
Profit-Loss Ratio
1.99
Alpha
-0.375
Beta
0.023
Annual Standard Deviation
0.024
Annual Variance
0.001
Information Ratio
-41.77
Tracking Error
0.466
Treynor Ratio
3.091
Total Fees
$874.63
Estimated Strategy Capacity
$5300000.00
Lowest Capacity Asset
COMPUSDT 18N
from AlgorithmImports import *
import numpy as np
from System.Drawing import Color
from datetime import datetime, timedelta
import os
import pandas as pd
from io import StringIO


# Custom fee implementation

class CustomFeeModel:
    def GetOrderFee(self, parameters):
        #fee = max(1, parameters.Security.Price
        #         * parameters.Order.AbsoluteQuantity
        #         * 0.00075)
        fee = parameters.Security.Price * parameters.Order.AbsoluteQuantity * 0.00075
        feeUSD = OrderFee(CashAmount(fee, 'USD'))
        return feeUSD
        
        
class BasicTemplateAlgorithm(QCAlgorithm):
    '''Basic template algorithm simply initializes the date range and cash'''
    
    def ThirtyMinuteBarHandler(self, sender, bar):
        '''This is our event handler for our 30-minute trade bar defined above in Initialize(). So each time the consolidator produces a new 30-minute bar, this function will be called automatically. The sender parameter will be the instance of the IDataConsolidator that invoked the event '''
        #self.Debug(str(self.Time) + " " + str(bar))
        return
        self.Data[bar.Symbol.Value].macd.Update(bar.Time, bar.Close)
        self.Data[bar.Symbol.Value].Bars.Add(bar)
        if not self.Data[bar.Symbol.Value].macd.IsReady: 
            return
        return
        if self.Portfolio[bar.Symbol].Quantity == 0 and self.Data[bar.Symbol.Value].macd.Current.Value > self.Data[bar.Symbol.Value].macd.Signal.Current.Value:  
                tt = float(bar.Close)
                tt = float(600/tt)
                self.Buy(bar.Symbol,tt)
                self.Debug(bar.Symbol)
                self.Debug(f"MACD VALUE : {self.Data[bar.Symbol.Value].macd.Current.Value}")
                self.Debug(f"SIGNAL VALUE :{self.Data[bar.Symbol.Value].macd.Signal.Current.Value}")
        elif self.Portfolio[bar.Symbol].Quantity > 0 and self.Data[bar.Symbol.Value].macd.Current.Value < self.Data[bar.Symbol.Value].macd.Signal.Current.Value:
                self.Liquidate()        
                self.Debug("Liquidating ")
                self.Debug(f"MACD VALUE : {self.Data[bar.Symbol.Value].macd.Current.Value}")
                self.Debug(f"SIGNAL VALUE :{self.Data[bar.Symbol.Value].macd.Signal.Current.Value}")


    

                
    def Initialize(self):
        '''Initialise the daa and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.'''
        
        self.UseLocalData = False
        self.UseLocalLogs = False
      
        
         # This is the period of bars we'll be creating
        BarPeriod = TimeSpan.FromMinutes(10)
        # This is the period of our sma indicators
        SimpleMovingAveragePeriod = 10
        # This is the number of consolidated bars we'll hold in symbol data for reference
        RollingWindowSize = 30
        
        # This is the period of bars we'll be creating
        BarPeriodData = TimeSpan.FromMinutes(10)
        
        
        self.SetStartDate(2021,8,3)  #Set Start Date
        self.SetEndDate(2021,8,12)    #Set End Date
        self.SetCash(100000)
        self.SetCash('USDT',100000)           #Set Strategy Cash
       
        if self.UseLocalData:
            self.SetCash(1000000)
        
        self.SetTimeZone("Europe/Berlin")   
        # self.SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage, AccountType.Margin)
        # self.SetBenchmark("BTCUSDT")
        
        self.DisableBuyFeatureActivated = 1
        self.DisableBuy = 0
        self.DisableBuyCount = 0
        self.DisableBuyDuration = 120
        
        # Holds all of our data keyed by each symbol
        self.Data = {}
        # Contains all of our equity symbols
        #EquitySymbols = ["BTCUSD","ETHUSD","LTCUSD","XRPUSD"] 
        EquitySymbols = ["BTCUSDT","ETHUSDT","BNBUSDT","NEOUSDT","LTCUSDT","ADAUSDT",
"XRPUSDT",
"EOSUSDT",
"IOTAUSDT",
"XLMUSDT",
"ONTUSDT",
"TRXUSDT",
"ETCUSDT",
"ICXUSDT",
"NULSUSDT",
"LINKUSDT",
"WAVESUSDT",
"BTTUSDT",
"HOTUSDT",
"ZILUSDT",
"BATUSDT",
"DASHUSDT",
"NANOUSDT",
"OMGUSDT",
"THETAUSDT",
"MATICUSDT",
"ATOMUSDT",
"TFUELUSDT",
"ONEUSDT",
"ALGOUSDT",
"DOGEUSDT",
"DUSKUSDT",
"ANKRUSDT",
"DOCKUSDT",
"WANUSDT",
"CVCUSDT",
"BANDUSDT",
"XTZUSDT",
"ARPAUSDT",
"IOTXUSDT",
"BCHUSDT",
"HIVEUSDT",
"ARDRUSDT",
"LRCUSDT",
"COMPUSDT",
"ZENUSDT",
"SNXUSDT",
#"ETHUPUSDT",
#"ADAUPUSDT",
"SXPUSDT",
"AUDUSDT",
"BLZUSDT",
"IRISUSDT",
"SRMUSDT",
"ANTUSDT",
"SANDUSDT",
"OCEANUSDT",
"NMRUSDT",
"DOTUSDT",
"RSRUSDT",
"PAXGUSDT",
"BZRXUSDT",
"SUSHIUSDT",
"UMAUSDT",
"BELUSDT",
"ALPHAUSDT",
"AUDIOUSDT",
"DNTUSDT",
"SHIBUSDT",
"ATAUSDT",
"MKRUSDT",
"UTKUSDT"
]

        #EquitySymbols = ["BTCUSD","ETHUSD","LTCUSD","BALUSD","DAIUSD","KNCUSD","OXTUSD"] 
        #EquitySymbols = ["BTCUSD","ETHUSD","LTCUSD","BALUSD","DAIUSD","KNCUSD","OXTUSD","ALGOUSD","ATOMUSD","LINKUSD","LRCUSD","ZRXUSD","XRPUSD"]  
        
        #EquitySymbols = ["BTCUSD"] 
        
        
        # initialize our equity data
        #equity = self.AddData(CustomDropboxOHLC, "SPY", Resolution.Minute)
        for symbol in EquitySymbols:
            if self.UseLocalData:
                equity = self.AddData(CustomDropboxOHLC, symbol, Resolution.Minute)
            else:
                equity = self.AddCrypto(symbol,Resolution.Minute,Market.Binance)
            self.Data[symbol] = SymbolData(self,equity.Symbol, BarPeriodData, RollingWindowSize)
        
            # Consolidate 1min symbil -> 5min Bars
            #####self.Consolidate(symbol, timedelta(minutes=5), self.OnDataConsolidatedHandler)
            
            if self.UseLocalData == True:
                FiveMinuteConsolidator = self.ResolveConsolidator(symbol,timedelta(minutes=5))
            else:
                FiveMinuteConsolidator = TradeBarConsolidator(timedelta(minutes=5))
            FiveMinuteConsolidator.DataConsolidated += self.OnDataConsolidatedHandler
            self.SubscriptionManager.AddConsolidator(symbol, FiveMinuteConsolidator)

           
            # Assigning securities custom fee models:
            self.Securities[symbol].SetFeeModel(CustomFeeModel())
            
        #self.AddData(CHBacktest,"CH")
        self.SetBenchmark("BTCUSDT")

        csv = self.Download("https://www.dropbox.com/s/c139ya4wkovjg8x/cryptohopper_trade_history.csv.csv?dl=1")
        self.dfCH = pd.read_csv(StringIO(csv)) 
        
        
        
        # loop through all our symbols and request data subscriptions and initialize indicator
        for symbol, symbolData in self.Data.items():
            # define the indicator
            
            symbolData.macd = MovingAverageConvergenceDivergence(12, 26, 9, MovingAverageType.Exponential)
            
            if self.UseLocalData == True:
                thirtyMinuteConsolidator = self.ResolveConsolidator(symbol,timedelta(minutes=15))
            else:
                thirtyMinuteConsolidator = QuoteBarConsolidator(timedelta(minutes=15))
            self.SubscriptionManager.AddConsolidator(symbolData.Symbol, thirtyMinuteConsolidator)
            thirtyMinuteConsolidator.DataConsolidated += self.ThirtyMinuteBarHandler
            self.RegisterIndicator(symbol, symbolData.macd, thirtyMinuteConsolidator)
            
            #Trend Indicators for disable Buy
            #QuantConnect.Indicators.MomentumPercent
            # define a 10-period RSI indicator with indicator constructor
            symbolData.rocp_hr = self.ROCP(symbolData.Symbol,5,Resolution.Hour)
            symbolData.rocp_day = self.ROCP(symbolData.Symbol,3,Resolution.Daily)
            symbolData.rocp_min = self.ROCP(symbolData.Symbol,30,Resolution.Minute)
            
            #ScalpIndicators for BuySignal
            symbolData.macd_Buy = MovingAverageConvergenceDivergence(12, 26, 9, MovingAverageType.Exponential)
            if self.UseLocalData == True:
                fiveMinuteConsolidator = self.ResolveConsolidator(symbol,timedelta(minutes=5))
            else:
                fiveMinuteConsolidator = TradeBarConsolidator(timedelta(minutes=5))
            self.SubscriptionManager.AddConsolidator(symbolData.Symbol, fiveMinuteConsolidator)
            self.RegisterIndicator(symbolData.Symbol, symbolData.macd_Buy, fiveMinuteConsolidator)
            symbolData.ATR = AverageTrueRange(14)
            self.RegisterIndicator(symbolData.Symbol, symbolData.ATR, fiveMinuteConsolidator)
            symbolData.ADX = AverageDirectionalIndex(14)
            self.RegisterIndicator(symbolData.Symbol, symbolData.ADX, Resolution.Hour)
            symbolData.SMA5 = SimpleMovingAverage(5)
            self.RegisterIndicator(symbolData.Symbol, symbolData.SMA5, Resolution.Minute)
            
            symbolData.PaintDetail = 0
            symbolData.PaintDetailStart = datetime(2021,4,1)
            symbolData.PaintDetailEnd = datetime(2021,4,28)
            
                #symbolData.stockPlot = Chart("TradePlot" + symbol)
                #symbolData.stockPlot.AddSeries(Series("Price", SeriesType.Line,'$', Color.Green))
                #symbolData.stockPlot.AddSeries(Series("Buy", SeriesType.Scatter, '$', Color.Blue, ScatterMarkerSymbol.Triangle))
                #symbolData.stockPlot.AddSeries(Series("SellPos", SeriesType.Scatter, '$', Color.Green, ScatterMarkerSymbol.TriangleDown))
                #symbolData.stockPlot.AddSeries(Series("SellNeg", SeriesType.Scatter, '$', Color.Red, ScatterMarkerSymbol.TriangleDown))
                #if symbolData.PaintDetail == 1:
                    #symbolData.candels_series = Series('Candle', SeriesType.Candle)
                #    symbolData.candels_series = Series('Candle', SeriesType.Line)
                    
                #    symbolData.stockPlot.AddSeries(symbolData.candels_series)
            
            if symbol == "BTCUSD":
                
                #symbolData.stockPlot.AddSeries(Series("Buy", SeriesType.Bar))
                #symbolData.stockPlot.AddSeries(Series("Sell", SeriesType.Bar))
                
                symbolData.stockPlot2 = Chart("SignalPlot" + symbol)
                symbolData.stockPlot2.AddSeries(Series("MACD", SeriesType.Line))
                symbolData.stockPlot2.AddSeries(Series("ROCPERC", SeriesType.Line))
                self.AddChart(symbolData.stockPlot2)
            
            
            #self.AddChart(symbolData.stockPlot)
            symbolData.lastPrice = 0

            if self.UseLocalLogs:
                ss = f"Cryptohopper_API_datalogs/{symbol}-data.csv"
                #symbolData.filesym = #open (os.path.join(Globals.DataFolder,ss),"w")
                #symbolData.filesym.write("Time,Open,High,Low,Close,Buy,Sell\r\n")
                
                symbolData.filesym.write("Time,Open,High,Low,Close,InactiveBuy,LocalInactive,StopBuyStart,BuyNextRise,Buy,TrailArm,StopLoss,TrailingStop,TakeProfit,NegCorr\r\n")
                symbolData.filesym.close
          
        
                
        
        self.Log("Start: ")
        
        # define our daily macd(12,26) with a 9 day signal
        # self.__macd = self.MACD("BTCUSD", 12, 26, 9, MovingAverageType.Exponential, Resolution.Hour)
        #self.__macd = self.macd
        self.__previous = datetime.min
        self.__previouscross = 0
        #self.macdwindow = RollingWindow[float](10)
        
       
        # '''
        sPlot = Chart('Strategy Equity')
        # sPlot.AddSeries(Series('Signal', SeriesType.Line, 2))
        #sPlot.AddSeries(Series('MCrossing', SeriesType.Bar, 2))
        
        self.AddChart(sPlot)
        
        #sPlot2 = Chart('BTCUSD')
        #self.AddChart(sPlot2)
        # ''' 
        
        # if self.ShowTicker > 0:
            # assets = Chart('ticker')
            # assets.AddSeries(Series('Open', SeriesType.Scatter))
            # assets.AddSeries(Series('High', SeriesType.Scatter))
            # assets.AddSeries(Series('Low', SeriesType.Scatter))
            # assets.AddSeries(Series('Close', SeriesType.Scatter))
            # self.AddChart(assets)

        # assets = Chart('Analyse1')
        # assets.AddSeries(Series('MCrossing', SeriesType.Line))
        # self.AddChart(assets)

        
        #self.PlotIndicator("MACD", True, self.__macd, self.__macd.Signal)
        # self.PlotIndicator("BTCUSD", self.__macd.Fast, self.__macd.Slow)
        
        
    def Checkpoint(self, data):
        return
        pass
        bars = data.Bars
        bar = data["BTCUSD"]
        # Some custom charts so better see model performance over time (and see if our training is even progressing)    
        #  self.Plot('Strategy Equity','Signal', bar.Open)
        
        if self.ShowTicker > 0:
            self.Plot('ticker', "Open", bar.Open)
            self.Plot('ticker', "High", bar.High)
            self.Plot('ticker', "Low", bar.Low)
            self.Plot('ticker', "Close", bar.Close)
            
        # self.Plot('Analyse1', "MCrossing", self.CrossingData)
         
        i = float(self.CrossingData)
        if (i != 0):
            self.Plot('Strategy Equity', 'MCrossing', i) # self.CrossingData)
            self.Log("CrossingData inside checkpoint: %s"%self.CrossingData)
        
        # Once file io, perform model checkpointing
        # pass
        
    def PosSell(self, symbol, symbolData, data, price):
                winning = self.Portfolio[symbol].UnrealizedProfit
                winpercent = (self.Portfolio[symbol].UnrealizedProfit+(self.Portfolio[symbol].AveragePrice*self.Portfolio[symbol].Quantity))/(self.Portfolio[symbol].AveragePrice*self.Portfolio[symbol].Quantity)
                symbolData.winpercent = winpercent
                
                #chekc if minimale haltezeit
                if symbolData.SellDuration > symbolData.SellMinDuration: 
                    
                    self.Liquidate(symbol)
                    
                    self.Debug(f"SELL symbol: {symbol}, SELL, {symbolData.SellReason}, winpercratio: {winpercent}, win: {winning}, Price: {price}, Min: {symbolData.SellActMinimum}, Max: {symbolData.SellActMaximum}, Duration: {symbolData.SellDuration}")
                    #if symbol == "BTCUSD":
                    if 1 > 0:
                        if winning > 0:
                            self.Plot("TradePlot" + symbol, "SellPos", self.Portfolio[symbol].Price * 1.05)
                        else:
                            self.Plot("TradePlot" + symbol, "SellNeg", self.Portfolio[symbol].Price * 1.05)
                    symbolData.SellDuration = 0
                    symbolData.SellActive = 0
                    
        
            
        
    def OnDataConsolidatedHandler(self, sender, bar):
        return
        self.Data[bar.Symbol.Value].Bars.Add(bar)
        self.Data[bar.Symbol.Value].New_Consolidate_bar = 1
        return

    
    def CalcSignalRise(self, symbol, symbolData):
        if symbolData.SignalRiseFeatureActivated == 0: return 0
        i=0
        checklength = 10
        risecounter = 0
        lowcounter = 0
        windowcount = symbolData.ClosePriceSeries.Count 
        if windowcount < checklength +1 : return 0
        while i < checklength:
            
            if symbolData.ClosePriceSeries[i] > symbolData.ClosePriceSeries[i+1]: risecounter = risecounter + 1
            if symbolData.ClosePriceSeries[i] < symbolData.ClosePriceSeries[i+1]: lowcounter =  lowcounter + 1
            i = i + 1

        returncounter = 0
        if risecounter > checklength/3*2 : returncounter = risecounter
        if lowcounter > checklength/3*2 : returncounter = lowcounter * -1
        return returncounter

    def CalcOverallSignalRise(self,symbol,symbolData,Data,data):
        if symbolData.OverallSignalRiseFeatureActivated == 0: return 0
        anzahlsymbol = 0
        nr_rise= 0
        nr_low = 0

        for symbol1 in Data.keys():
            anzahlsymbol = anzahlsymbol + 1
            if data.ContainsKey(symbol1):
                
                symbolData1 = Data[symbol1]
                CurrentRise = self.CalcSignalRise(symbol1,symbolData1)
                if CurrentRise > 0: nr_rise =+ 1
                if CurrentRise < 0: nr_low =+ 1
        returncounter = 0
        if anzahlsymbol > 1:
            #if nr_rise > anzahlsymbol/2: returncounter = nr_rise
            #if nr_low > anzahlsymbol/2: returncounter = nr_low
            if nr_rise > nr_low: returncounter = nr_rise + 1
            if nr_rise < nr_low: returncounter = nr_low * -1
        return returncounter


        


        
    def OnData(self, data):
        
        '''OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.

        Arguments:
            data: Slice object keyed by symbol containing the stock data
        ''' 
        
        #backtest

        #if not data.ContainsKey("CH"):return
        #if not self.dfCH: return
        nr = -1
        for x in self.dfCH["Date"]:
            nr = nr + 1
            t1 = datetime.strptime(x, "%Y-%m-%d %H:%M:%S") #+  timedelta(hours=-2)
            t1 = t1 + timedelta(seconds =  30)  #time wo order gegeben wurde ungefähr
            if t1 >= self.Time and t1 < self.Time + timedelta(minutes = 1):
                symb = self.dfCH["Pair"][nr]
                typus = self.dfCH["Type"][nr]
                amount = float(self.dfCH["Order Amount"][nr]) #anzahl shares
                ordervalue = self.dfCH["Order Value"][nr] #kosten position
                orderrate = self.dfCH["Order Rate"][nr] #preis
                #if symb == symbol and typus == "sell":
                if typus == "sell":
                    #self.PosSell(symbol, symbolData, data, price)
                    symbol = symb
                    self.Liquidate(symbol)
                    self.Debug(f"Sell symbol: {symbol}")
                #if symb == symbol and typus == "buy":
                if typus == "buy":
                    symbol = symb
                    #self.Plot("TradePlot" + symbol, "Buy", price * 0.9)
                    price = 0
                    priceclose = 0
                    if data.ContainsKey(symbol):
                        price = data[symbol].Open
                        priceclose = data[symbol].Close
                        
                    priceCH = orderrate
            
                    self.Debug(f"BUY symbol: {symbol}, BUY, priceCH: {orderrate}, priceQC: {price}, amount: {amount}, costsCH: {ordervalue}, Portfolio Cash: {self.Portfolio.Cash} ")
                    self.Debug(f"time: {self.Time}, QC Open: {price},   Close: {priceclose}, timeCH: {x}, CH: {orderrate}")
                    tt = float(600/orderrate)
                    if self.Securities.ContainsKey(symbol):
                        self.Buy(symbol,amount)
                        self.BuyLog = "BUY"
                    else:
                        self.Debug(f"symbol {symbol} not in list")

        ###########################                          
        return
    



        #check if disable buy
        if self.DisableBuyFeatureActivated == 1:

            if self.DisableBuy == 0:
                if self.Portfolio.TotalUnrealizedProfit < 0: 
                    self.DisableBuy = 1
                    self.DisableBuyCount = 0
                    self.Debug("Start DisableBuy")
            
            if self.DisableBuy == 0:
                i = 0
                n = 0
                for symbol in self.Data.keys():
                    i = i + 1
                    if self.Portfolio[symbol].UnrealizedProfit < 0: n = n + 1
                if n > (i/2):
                    self.DisableBuy = 1
                    self.Debug(f"Start DisableBuy {self.Time}")
                
                
            if self.DisableBuy == 1:
                self.DisableBuyCount = self.DisableBuyCount + 1
                if self.DisableBuyCount > self.DisableBuyDuration:
                    self.DisableBuy = 0
                    self.DisableBuyCount = 0
                    self.Debug("DisableBuy End")
        #else:
        #    self.DisableBuy = 0
                    
        
        # loop through each symbol in our structure
        #quickcheck = data.ContainsKey("BTCUSD")
        #quickcheck = data.ContainsKey("ETHUSD")
        price = 0
        for symbol in self.Data.keys():
            if data.ContainsKey(symbol):
                
                price = data[symbol].Close
                symbolData = self.Data[symbol]
                symbolData.New_Consolidate_bar = 0
                #if self.UseLocalData == True:
                #    price = data[symbol].Close
                #    symbolData = self.Data[symbol]
                #    symbolData.New_Consolidate_bar = 0
                #else:
                #    symbolData = self.Data[symbol]
                #    fxQuoteBars = data.QuoteBars
                #    currentQuoteBar = fxQuoteBars[symbol]     ## EURUSD QuoteBar
                #    fxOpen = currentQuoteBar.Open               ## Market Open FX Rate
                #    symbolData.New_Consolidate_bar = 0
                #    price = currentQuoteBar.Close
                
                
                
                self.BuyLog = ""
                self.SellLog = ""   
                #self.Debug(f"Price: {price}")
                
                # this check proves that this symbol was JUST updated prior to this OnData function being called
                #if symbolData.New_Consolidate_bar == 1:
                
                #if 1 > 0:
                if price > 0:    
                    price = symbolData.SMA5.Current.Value
                    #price = symbolData.Bars[0].Close   ##consolidate 5 min bar
                    symbolData.ClosePriceSeries.Add(price)

                    #calculate rise and fall
                    SignalRiseFall = self.CalcSignalRise(symbol,symbolData) 
                    AllSignalsRiseFall = self.CalcOverallSignalRise(symbol,symbolData,self.Data,data)
                    if symbolData.OverallSignalRiseFeatureActivated == 0: AllSignalsFall = 1
                    if symbolData.SignalRiseFeatureActivated == 0: SignalRiseFall = 1
                    #self.Debug(f"localsignalrise: {SignalRiseFall} overall: {AllSignalsRiseFall}")

                
                    if symbolData.PaintDetail == 1:
                        if self.Time > symbolData.PaintDetailStart and self.Time < symbolData.PaintDetailEnd:
                            tradeBar = currentQuoteBar
                            #symbolData.candels_series.AddPoint(tradeBar.EndTime - timedelta(seconds=59), tradeBar.Open)
                            #symbolData.candels_series.AddPoint(tradeBar.EndTime - timedelta(seconds=58), tradeBar.High)
                            #symbolData.candels_series.AddPoint(tradeBar.EndTime - timedelta(seconds=57), tradeBar.Low)
                            #symbolData.candels_series.AddPoint(tradeBar.EndTime, tradeBar.Close)
                            self.Plot("TradePlot" + symbol, "Candle", tradeBar.Close)
                        
                    #price = data.Bars[symbol].Close
                    
                    #calculate lower resolution for paint chart
                    symbolData.price_sum = symbolData.price_sum + price
                    symbolData.price_sum_nr = symbolData.price_sum_nr + 1
                    if symbolData.price_sum_nr > symbolData.price_average:
                        avprice = symbolData.price_sum/symbolData.price_sum_nr
                        self.Plot("TradePlot" + symbol, "Price", avprice)
                        if symbol == "BTCUSDT" :
                        
                            self.Plot("SignalPlot" + symbol, "MACD", symbolData.macd.Current.Value)
                            self.Plot("SignalPlot" + symbol, "ROCPERC", symbolData.rocp_min.Current.Value)
                        symbolData.price_sum = 0
                        symbolData.price_sum_nr = 0

                                     

                    

    
    def OnEndOfAlgorithm(self):
        self.Liquidate()
        
        
        
class SymbolData(object):

    def __init__(self, algo, symbol, barPeriod, windowSize):
        self.Symbol = symbol
        # The period used when population the Bars rolling window
        self.BarPeriod = barPeriod
        self.algo = algo
        RollingWindowSize = 30
        # A rolling window of data, data needs to be pumped into Bars by using Bars.Update( tradeBar ) and can be accessed like:
        # mySymbolData.Bars[0] - most first recent piece of data
        # mySymbolData.Bars[5] - the sixth most recent piece of data (zero based indexing)
        self.Bars = RollingWindow[IBaseDataBar](windowSize)
        self.ClosePriceSeries = RollingWindow[float](RollingWindowSize)
        for i in range(RollingWindowSize): self.ClosePriceSeries.Add(0)
        # The simple moving average indicator for our symbol
        self.macd = None
        self.stockPlot = None
        self.lastPrice = 0
        
        #check general rise fall in symbol and all symbols
        self.OverallSignalRiseFeatureActivated = 1
        self.SignalRiseFeatureActivated = 1
        
        #parameters for decrease nr of points in graphs
        self.price_sum = 0
        self.price_sum_nr = 0
        self.price_average = 2000   #2400
        
        self.Local_DisableBuyFeatureActivated = 1
        self.Local_DisableBuy = 0
        self.Local_DisableBuy_MaxChange = -6
        self.Local_DisableBuyDuration = 180
        self.Local_DisableBuyCount = 0
        
        self.TakeProfit = float(algo.GetParameter("TakeProfit"))
        #self.TakeProfit = 1.5
        
        self.TrailingStopBuy_Percentage = 0.2
        self.TrailingStopBuy_Active = 0
        self.TrailingStopBuy_ActMinimum = 0
        self.TrailingStopBuy_ActMaximum = 0
        self.TrailingStopBuy_Duration = 0
        
        self.NextStepRise_Active = 0
        self.NextStepRise_ActMinimum = 0
        self.NextStepRise_ActMaximum = 0
        self.NextStepRise_Duration = 0
        self.NextStepRise_Percentage = 0.3
        self.NextStepRise_MaxDuration = 5
        
        self.SellMaxDuration = 30
        self.SellMinDuration = 5
        self.SellDuration = 0
        self.SellActive = 0
        self.SellStopLoss  = float(algo.GetParameter("SellStopLoss"))
        #self.SellStopLoss = 0.3  # 0.3
        self.SellActMimimum = 0
        self.SellActMaximum = 0
        self.SellTrailStopArm  = float(algo.GetParameter("SellTrailStopArm"))
        #self.SellTrailStopArm = 1  #1.5
        self.SellTrailStop  = float(algo.GetParameter("SellTrailStop"))
        #self.SellTrailStop = 2 #0.6
        self.SellTrailStopArmActive = 0
        self.SellReason = ""
        
        
        self.BuyAmountUSD = 700
        if self.algo.UseLocalData:
            self.BuyAmountUSD = self.BuyAmountUSD * 100
        
    def ResetallBuyvars(self):
        self.TrailingStopBuy_Active = 0
        self.TrailingStopBuy_ActMinimum = 0
        self.TrailingStopBuy_ActMaximum = 0
        self.TrailingStopBuy_Duration = 0
        self.NextStepRise_Active = 0
        self.NextStepRise_ActMinimum = 0
        self.NextStepRise_ActMaximum = 0
        self.NextStepRise_Duration = 0
        
    def ResetallSellvars(self):
        self.SellDuration = 0
        self.SellActive = 0
        self.SellActMinimum = 0
        self.SellActMaximum = 0
        self.SellReason = ""
        self.SellTrailStopArmActive = 0
        
        
    New_Consolidate_bar = 0   
  
    # Returns true if all the data in this instance is ready (indicators, rolling windows, ect...)
    def IsReady(self):
        return self.Bars.IsReady and self.macd.IsReady

    # Returns true if the most recent trade bar time matches the current time minus the bar's period, this
    # indicates that update was just called on this instance
    def WasJustUpdated(self, current):
        return self.Bars.Count > 0 and self.Bars[0].Time == current - self.BarPeriod 

class CHBacktest(PythonData):

    def GetSource(self, config, date, isLiveMode):
        symbol = config.Symbol.Value
        source = "https://www.dropbox.com/s/c139ya4wkovjg8x/cryptohopper_trade_history.csv.csv?dl=1"
        
        return SubscriptionDataSource(source, SubscriptionTransportMedium.RemoteFile)

    def Reader(self, config, line, date, isLiveMode):
             
        
        if not (line.strip() and line[0].isdigit()): 
        
            return None

        # New GoldPhys object
        CH = CHBacktest()
        CH.Symbol = config.Symbol

        try:
            # Example File Format:
            # Date,       Open       High        Low       Close     Volume      
            # 2011-09-13  7792.9    7799.9     7722.65    7748.7    116534670    
            data = line.split(',')
            
            #ohlc.Time = datetime.fromtimestamp(int(data[0]))
            CH.Time = datetime.strptime(data[1], "%Y-%m-%d %H:%M:%S")
            CH.Value = float(data[12])
            CH.BackSymbol = data[4]
            CH["Type"] = data[5]

        except:
            return None

        return CH   



class CustomDropboxOHLC(PythonData):
    #'''IGLN.L Custom Data Class'''

    #def Initialize(self):
        #self.algo = algo

    
    def GetSource(self, config, date, isLiveMode):
        symbol = config.Symbol.Value
        source = ""
        #old with dropbox
        #if symbol == "BTCUSD": source = "https://www.dropbox.com/s/ye269gk4o4zz2ts/Binance_BTCUSDT_minute_reverse.csv?dl=1"
        #if symbol == "ETHUSD": source = "https://www.dropbox.com/s/asncbfd0mpbidtr/Binance_ETHUSDT_minute_reversed.csv?dl=1"
        #if symbol == "LTCUSD": source = "https://www.dropbox.com/s/84wszmz71pncegk/Binance_LTCUSDT_minute_reversed.csv?dl=1"
        #if symbol == "XRPUSD": source = "https://www.dropbox.com/s/xgtm3t3oq5hyqt6/Binance_XRPUSDT_minute_reversed.csv?dl=1"
        if symbol == "BTCUSD": source = os.path.join(Globals.DataFolder,"Binance_BTCUSDT_minute_reversed.csv")
        if symbol == "ETHUSD": source = os.path.join(Globals.DataFolder,"Binance_ETHUSDT_minute_reversed.csv")
        if symbol == "LTCUSD": source = os.path.join(Globals.DataFolder,"Binance_LTCUSDT_minute_reversed.csv")
        if symbol == "XRPUSD": source = os.path.join(Globals.DataFolder,"Binance_XRPUSDT_minute_reversed.csv")
        #if symbol == "SPY": source = os.path.join(Globals.DataFolder,"Binance_BTCUSDT_minute_reversed.csv")
        

      
        return SubscriptionDataSource(source, SubscriptionTransportMedium.LocalFile)


    def Reader(self, config, line, date, isLiveMode):
             
        
        if not (line.strip() and line[0].isdigit()): 
        
            return None

        # New GoldPhys object
        ohlc = CustomDropboxOHLC()
        ohlc.Symbol = config.Symbol

        try:
            # Example File Format:
            # Date,       Open       High        Low       Close     Volume      
            # 2011-09-13  7792.9    7799.9     7722.65    7748.7    116534670    
            data = line.split(',')
            
            #ohlc.Time = datetime.fromtimestamp(int(data[0]))
            ohlc.Time = datetime.strptime(data[1], "%Y-%m-%d %H:%M:%S")

            ohlc.Value = float(data[6])
            ohlc["Open"] = float(data[3])
            ohlc["High"] = float(data[4])
            ohlc["Low"] = float(data[5])
            ohlc["Close"] = float(data[6])
            ohlc["Volumen"] = float(data[8])
            ohlc["tradecount"] = float(data[9])
            
            
        

        except ValueError:
                # Do nothing
                return None

        return ohlc