Overall Statistics
Total Trades
92
Average Win
2.70%
Average Loss
-1.12%
Compounding Annual Return
0.416%
Drawdown
25.100%
Expectancy
0.553
Net Profit
0.165%
Sharpe Ratio
0.158
Probabilistic Sharpe Ratio
26.093%
Loss Rate
55%
Win Rate
45%
Profit-Loss Ratio
2.42
Alpha
0.069
Beta
0.305
Annual Standard Deviation
0.279
Annual Variance
0.078
Information Ratio
0.245
Tracking Error
0.519
Treynor Ratio
0.144
Total Fees
$3015.20
Estimated Strategy Capacity
$430000.00
Lowest Capacity Asset
BTCUSD XJ
from System.Drawing import Color

class TestAlgorithm(QCAlgorithm):


    def Initialize(self):
        self.SetStartDate(2021,4,1)
        self.SetEndDate(2021,8,23)
        self.SetCash("BTC", 1)
        self.btc = self.AddCrypto("BTCUSD", Resolution.Hour, Market.GDAX).Symbol
        self.SetBrokerageModel(BrokerageName.GDAX, AccountType.Cash)
        self.SetBenchmark("BTCUSD")
    
        self.entryTicket = None
        
        self.SetWarmUp(25)
        self.smafast = self.SMA("BTCUSD", 9, Resolution.Daily)
        self.smaslow = self.SMA("BTCUSD", 25, Resolution.Daily)
        self.rsi = self.RSI("BTCUSD", 25, Resolution.Hour)
        
    
        stockPlot = Chart("Trade Plot")
        stockPlot.AddSeries(Series('Buy', SeriesType.Scatter, '$',
                                Color.Green, ScatterMarkerSymbol.Triangle))
        stockPlot.AddSeries(Series('Buy 2', SeriesType.Scatter, '$',
                                Color.Black, ScatterMarkerSymbol.Triangle))  
        stockPlot.AddSeries(Series('Buy 3', SeriesType.Scatter, '$',
                                Color.Cyan, ScatterMarkerSymbol.Triangle))  
        stockPlot.AddSeries(Series('Sell', SeriesType.Scatter, '$',
                                Color.Red, ScatterMarkerSymbol.TriangleDown))
        stockPlot.AddSeries(Series('Liquidate Signal', SeriesType.Scatter, '$',
                                Color.Blue, ScatterMarkerSymbol.Diamond))
        stockPlot.AddSeries(Series('Liquidate Trend', SeriesType.Scatter, '$',
                                Color.Pink, ScatterMarkerSymbol.Diamond))
        stockPlot.AddSeries(Series('Price', SeriesType.Line, 0))
        stockPlot.AddSeries(Series('smafast', SeriesType.Line, 1))   
        stockPlot.AddSeries(Series('smaslow', SeriesType.Line, 1)) 
        stockPlot.AddSeries(Series('rsi', SeriesType.Line, 2)) 

        self.AddChart(stockPlot)
    
    def OnData(self, data):
        if self.IsWarmingUp: return
        price = self.Securities["BTCUSD"].Price
        tolerance = 0.00015
        
        self.Plot("Trade Plot", "Price", price)
        self.Plot("Trade Plot", "smafast", self.smafast.Current.Value)
        self.Plot("Trade Plot", "smaslow", self.smaslow.Current.Value)
        self.Plot("Trade Plot", "rsi", self.rsi.Current.Value)

        self.trendup = self.smafast.Current.Value > self.smaslow.Current.Value *(1 + tolerance)
        self.trenddown = self.smafast.Current.Value < self.smaslow.Current.Value
    
    
       #Long Signal
        if self.trendup == True and self.rsi.Current.Value < 25 and not self.Portfolio["BTCUSD"].IsLong:
                self.entryTicket = self.MarketOrder("BTCUSD", 0.1, False, "Long One")
                self.Debug("Market Order Fill Price: {0}".format(self.entryTicket.AverageFillPrice))
                self.Plot("Trade Plot", "Buy", price)
                #self.Log("BUY  >> {0}".format(self.Securities["BTCUSD"].Price))
                        
                        
        #Liquidate Signal        
        if self.Portfolio["BTCUSD"].IsLong and self.trendup == True and self.rsi.Current.Value > 60:
            self.Liquidate()
            self.Plot("Trade Plot", "Liquidate Signal", price)
                    
        #Liquidate Trend
        if self.Portfolio["BTCUSD"].IsLong and self.trendup == False:
                self.Liquidate()
                self.Plot("Trade Plot", "Liquidate Trend", price)
                
        #Short Signal
        ...

    def OnOrderEvent(self, orderEvent):
        price = self.Securities["BTCUSD"].Price
        #order = self.Transactions.GetOrderById(orderEvent.OrderId)
        if self.entryTicket is not None and orderEvent.Status == OrderStatus.Filled:   
            if self.Securities["BTCUSD"].Price < self.entryTicket.AverageFillPrice:    
                if self.trendup == True and self.rsi.Current.Value < 25:
                    self.entryTicket2 = self.MarketOrder("BTCUSD", 0.2, False, "Long Two")
                    #self.Debug("Market Order Fill Price: {0}".format(self.entryTicket.AverageFillPrice))
                    self.Plot("Trade Plot", "Buy 2", price)
                    self.Log("BUY  >> {0}".format(self.Securities["BTCUSD"].Price))
                    #self.entryTicket2 = None