Overall Statistics
Total Trades
1
Average Win
0%
Average Loss
0%
Compounding Annual Return
42.194%
Drawdown
26.700%
Expectancy
0
Net Profit
42.194%
Sharpe Ratio
1.218
Probabilistic Sharpe Ratio
50.987%
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0.406
Beta
0.017
Annual Standard Deviation
0.337
Annual Variance
0.113
Information Ratio
0.432
Tracking Error
0.355
Treynor Ratio
24.315
Total Fees
$30.16
Estimated Strategy Capacity
$400000.00
import numpy as np
import pandas as pd
from collections import deque


class PensiveYellowGreenGoat(QCAlgorithm):

    
    highestsymbolprice = 0
    lowestsymbolprice  = 0
    def Initialize(self):
        self.SetStartDate(2019, 1, 1) # Set Start Date
        self.SetEndDate(2020, 1, 1)
        self.SetCash(100000)  # Set Strategy Cash
        
        # add RUBY stock 
        self.symbol = self.AddEquity("RUBY", Resolution.Daily, Market.USA).Symbol
        self.bbars = 15
        
        #revolving list of to hold highs
        self.dtrhigh = deque(maxlen = self.bbars)
        
        #revolving list to hold close price
        self.dtrclose = deque(maxlen = self.bbars)
        
        #revolving list to hold low price
        self.dtrlow = deque(maxlen = self.bbars)
        
        # Warm up
        history = self.History(self.symbol, self.bbars, Resolution.Daily)
        if history.empty or 'close' not in history.columns:
            return
        history = history.loc[self.symbol]
        for time, row in history.iterrows():
            self.dtrhigh.append(row['high'])
            self.dtrclose.append(row['close'])
            self.dtrlow.append(row['low'])
      

        
    def OnData(self, data):
        # get current high price, close price and low price of Ruby
        current_price = self.Securities[self.symbol.Value].Close
        currenthigh_price = self.Securities[self.symbol.Value].High
        currentlow_price = self.Securities[self.symbol.Value].Low
        
        #Adding data to the revolving list
        self.dtrhigh.append(currenthigh_price)
        self.dtrclose.append(current_price)
        self.dtrlow.append(currentlow_price)
     
        
        
        ### Traling Stop loss
        if self.Portfolio.Invested:
            if self.isLong:
              
                #if condStopProfit:
                if current_price > self.highestsymbolprice:
                    self.highestsymbolprice = current_price
                    updateFields = UpdateOrderFields()
                    updateFields.StopPrice = self.highestsymbolprice * 0.98
                    self.trailingstop = updateFields.StopPrice
                    if current_price <= self.trailingstop:
                        self.Liquidate(self.symbol.Value)
                        #self.Log(f"{self.Time} Long Position Trailing Stop Profit at {current_price}")
               
            else:
             
                if current_price < self.lowestsymbolprice:
                    self.lowestsymbolprice = current_price
                    updateFields = UpdateOrderFields()
                    updateFields.StopPrice = self.lowestsymbolprice * 1.02
                    self.trailingstop = updateFields.StopPrice 
                    if current_price >= self.trailingstop:
                        self.Liquidate(self.symbol.Value)
                        #self.Log(f"{self.Time} Short Position Trailing Stop Profit at {current_price}")
                        
                    
               
                
      
        if not self.Portfolio.Invested: 
            # If the high 3 bars ago is less than the current low, and the current low is less than the previous bar high, and the current bar high is 
            # less than the high 2 bars ago and the previous bar high is less than the high 2 bars ago,buy long at the next market open.
           
            if  self.dtrhigh[11] < self.dtrlow[14] and self.dtrlow[14] < self.dtrhigh[13] and self.dtrhigh[14] < self.dtrhigh[12] and self.dtrhigh[13] < self.dtrhigh[12]:   
                self.SetHoldings(self.symbol.Value, 1)
                # get buy-in price for trailing stop loss/profit
                self.buyInPrice = current_price
                # entered long position
                self.isLong = True
                        
                #self.Log(f"{self.Time} Entered Long Position at {current_price}")
            
               
            # If the low 3 bars ago is great than the current high, and the current high is greater than the previous bar low, and the current bar low is 
            # greater than the low 2 bars ago and the previous bar low is greater than the low 2 bars ago,sell short at the next market open.
            if self.dtrlow[11] > self.dtrhigh[14] and self.dtrhigh[14] > self.dtrlow[13]  and self.dtrlow[14] > self.dtrlow[12] and self.dtrlow[13] > self.dtrlow[12]:
                self.SetHoldings(self.symbol.Value, -1)
                # get sell-in price for trailing stop loss/profit
                self.sellInPrice = current_price
                # entered short position
                self.isLong = False
                        
                #self.Log(f"{self.Time} Entered Short Position at {current_price}")