Overall Statistics
Total Trades
10
Average Win
4.04%
Average Loss
-3.18%
Compounding Annual Return
13.170%
Drawdown
6.800%
Expectancy
0.816
Net Profit
13.170%
Sharpe Ratio
1.215
Probabilistic Sharpe Ratio
56.508%
Loss Rate
20%
Win Rate
80%
Profit-Loss Ratio
1.27
Alpha
0.114
Beta
-0.012
Annual Standard Deviation
0.092
Annual Variance
0.008
Information Ratio
-0.994
Tracking Error
0.147
Treynor Ratio
-9.123
Total Fees
$17.13
Estimated Strategy Capacity
$420000000.00
import numpy as np
import pandas as pd
from collections import deque


class PensiveYellowGreenGoat(QCAlgorithm):

    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("SPY", Resolution.Daily, Market.USA).Symbol
        self.bbars = 4
        
        #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)

        # 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.dtrlow[3] < self.dtrhigh[0] < self.dtrlow[1] < self.dtrlow[2]:    
            self.SetHoldings(self.symbol, 0)
               
        # 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[3] > self.dtrhigh[0] > self.dtrlow[1] > self.dtrlow[2]:
            self.SetHoldings(self.symbol, 1.0)