Overall Statistics
Total Trades
7
Average Win
0%
Average Loss
0%
Compounding Annual Return
0.538%
Drawdown
27.100%
Expectancy
0
Net Profit
1.627%
Sharpe Ratio
0.095
Probabilistic Sharpe Ratio
2.960%
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0.009
Beta
0.092
Annual Standard Deviation
0.132
Annual Variance
0.017
Information Ratio
-0.13
Tracking Error
0.204
Treynor Ratio
0.136
Total Fees
$0.00
class CryptoRaptor(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2017, 4, 1)  # Set Start Date
        self.SetEndDate(2020, 4, 1)
        self.SetCash(100000)  # Set Strategy Cash
        
        self.short_period = 12
        self.long_period = 21
        
        self.stopLossPercent = 0.05
        
        self.ticket_per_symbol = {}
        
        self.rsi_per_symbol = {}
        self.short_ema_per_symbol = {}        
        self.long_ema_per_symbol = {}
        
        self.buy_signal = 6.0
        
        for ticker in ['BTCUSD', 'LTCUSD', 'EOSUSD', 'ETPUSD', 'TRXUSD', 'XTZUSD', 'VSYUSD']: 
            self.AddCrypto(ticker, Resolution.Hour, Market.Bitfinex)
            
            #Add the RSI Indicator 
            rsi = self.RSI(ticker, self.short_period,  MovingAverageType.Simple, Resolution.Daily)
            self.rsi_per_symbol[ticker] = rsi
            
            #Add the EMA Indicator 
            short_ema = self.EMA(ticker, self.short_period, Resolution.Daily)
            self.short_ema_per_symbol[ticker] = short_ema            
            
            long_ema = self.EMA(ticker, self.short_period, Resolution.Daily)
            self.long_ema_per_symbol[ticker] = long_ema
            
            stockPlot = Chart('{0} Trade Plot'.format(ticker))
            # On the Trade Plotter Chart we want 3 series: trades and price:
            stockPlot.AddSeries(Series('Buy', SeriesType.Scatter, 0))
            stockPlot.AddSeries(Series('EMA Short', SeriesType.Line, 0))
            stockPlot.AddSeries(Series('EMA Long', SeriesType.Line, 0))
            stockPlot.AddSeries(Series('Price', SeriesType.Line, 0))
            self.AddChart(stockPlot)
        
        self.Schedule.On(self.DateRules.Every(DayOfWeek.Monday, DayOfWeek.Thursday, DayOfWeek.Wednesday, DayOfWeek.Thursday, DayOfWeek.Friday), \
            self.TimeRules.At(16, 0), \
            Action(self.chart))
    
    def chart(self): 
        for symbol in list(self.rsi_per_symbol.keys()): 
            close_price = self.Securities[symbol].Close
            
            #Charting
            self.Plot('{0} Trade Plot'.format(symbol), 'Price', close_price)
            
            if symbol in self.short_ema_per_symbol: 
                ema = self.short_ema_per_symbol[symbol]
                
                if ema.IsReady: 
                    ema_value = ema.Current.Value
                    
                    self.Plot('{0} Trade Plot'.format(symbol), 'EMA Short', ema_value)
            
            if symbol in self.long_ema_per_symbol: 
                ema = self.long_ema_per_symbol[symbol]
                
                if ema.IsReady: 
                    ema_value = ema.Current.Value
                    
                    self.Plot('{0} Trade Plot'.format(symbol), 'EMA Long', ema_value)
                    
    def OnOrderEvent(self, orderEvent):
        order = self.Transactions.GetOrderById(orderEvent.OrderId)
        if orderEvent.Status == OrderStatus.Filled: 
            self.Log("{0}: {1}: {2}".format(self.Time, order.Type, orderEvent))

    def OnData(self, data):
        for symbol in list(self.rsi_per_symbol.keys()): 
            open_price = self.Securities[symbol].Open
            close_price = self.Securities[symbol].Close
            
            if self.Portfolio[symbol].Invested: 
                self.UpdateBuy(symbol)
                continue

                        
            if symbol in self.rsi_per_symbol: 
                rsi = self.rsi_per_symbol[symbol]
                
                if rsi.IsReady:
                    # get the current RSI value
                    rsi_value = rsi.Current.Value
                    
                    #self.Debug("Crypto Pair {0}: RSI: {1}".format(symbol, rsi_value))
                    
                    if rsi_value > 0 and rsi_value < 30: 
                        #self.Debug("RSI Buy Signal")
                        self.OpenBuy(symbol)
            
            if symbol in self.short_ema_per_symbol: 
                ema = self.short_ema_per_symbol[symbol]
                
                if ema.IsReady: 
                    ema_value = ema.Current.Value
                    
                    #self.Debug("Crypto Pair {0}: EMA: {1}".format(symbol, ema_value))
                    #self.Debug((100 - ((close_price / ema_value) * 100)))
                    if (100 - ((close_price / ema_value) * 100)) > self.buy_signal: 
                        continue
                        #self.Debug("EMA Buy Signal")
                        #self.OpenBuy(symbol)
            
    
    def OpenBuy(self, symbol): 
        # Calculate the fee adjusted quantity of shares with given buying power
        target = (1 / len(self.rsi_per_symbol.keys())) * 0.1
        quantity = self.CalculateOrderQuantity(symbol, target)
        
        #Calculate the stop price
        close_price = self.Securities[symbol].Close;
        stop_price  = round(close_price * (1 - self.stopLossPercent), 3)
                                
        self.Debug("Open Buy Order for {0} on {1}. SL: {2}".format(symbol, close_price, stop_price))
        #self.Buy(symbol, quantity)
        
        ticket = self.StopMarketOrder(symbol, quantity, stop_price)
        self.ticket_per_symbol[symbol] = ticket
    
    def UpdateBuy(self, symbol): 
        
        if symbol in self.ticket_per_symbol: 
            ticket = self.ticket_per_symbol[symbol]
            
            
            close_price = self.Securities[symbol].Close
            new_stop_price = round(close_price * (1 - self.stopLossPercent), 3)
            old_stop_price = ticket.Get(OrderField.StopPrice)
            
            if new_stop_price > old_stop_price: 
                self.Debug("try update")
                
                updateSettings = UpdateOrderFields()
                updateSettings.StopPrice = new_stop_price
                updateSettings.Tag = "Stop Price updated"
                response = ticket.Update(updateSettings)
                
                if response.IsSuccess:
                    self.Debug("Stop Loss for {0} updated from {1} to {2}".format(symbol, old_stop_price, new_stop_price))