Overall Statistics
Total Trades
0
Average Win
0%
Average Loss
0%
Compounding Annual Return
0%
Drawdown
0%
Expectancy
0
Net Profit
0%
Sharpe Ratio
0
Probabilistic Sharpe Ratio
0%
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0
Beta
0
Annual Standard Deviation
0
Annual Variance
0
Information Ratio
0
Tracking Error
0
Treynor Ratio
0
Total Fees
$0.00
Estimated Strategy Capacity
$0
Lowest Capacity Asset
from AlgorithmImports import *
class ThreeBarReversal(QCAlgorithm):
    
    tradeSignalLong = False
    tradeSignalShort = False
    
    def __init__(self):

        self.SetStartDate(2022, 3, 21)
        self.SetEndDate(2022, 3, 21)
        self.SetCash(100000)
        self.tolerance = 0.05
        self.risk = 0.005
        self.ticker = "QQQ"
    
    
    def Initialize(self):
        
        self.symbol = self.AddEquity(self.ticker, Resolution.Minute).Symbol
        self.window = RollingWindow[TradeBar](4)
        
        self.SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage)

        self.SetRiskManagement(MaximumDrawdownPercentPerSecurity(self.risk))

        self.SetWarmUp(4)


    def OnData(self, data):
        
        self.window.Add(data[self.symbol])

        if not self.window.IsReady:
            return

        barD = self.window[0]
        barC = self.window[1]
        barB = self.window[2]
        barA = self.window[3]
        
        #downtrend reversal tradeSignalLong lock logic
        #based on Tradingview QQQ 1m chart data 03/23/22 starting @ 0858
        if barA.Open > barA.Close                               \
            and barB.Open > barB.Close                          \
            and barC.Open > barC.Close                          \
            and barD.Open < barD.Close                          \
            and barA.High > barB.High > barC.High               \
            and barA.Low > barB.Low > barC.Low                  \
            and barA.Close - self.tolerance >= barB.Open        \
            and barB.Close - self.tolerance >= barC.Open        \
            and barC.High < barD.High                           \
            and barC.Low <= barD.Low                            \
            and bardD.Open <= barC.Open + self.tolerance        \
            and bardD.Open >= barC.Open - self.tolerance:       \
                self.tradeSignalLong = True
    
        if not self.Portfolio.Invested:
            if self.tradeSignalLong is True:
                self.SetHoldings(self.symbol, (1 - self.risk.Value))
                self.stopPrice = barC.Low
                self.tradeSignalLong = False

        elif self.Portfolio[self.symbol].IsLong:
            if self.Securities[self.ticker].Price < self.stopPrice or barD.Open > barD.Close:
                self.Liquidate()
        
        
        """
        ***FOR REFERENCE ONLY***
        if barA.Open > barA.Close   \   #establish barA is bearish
            and barB.Open > barB.Close  \  #establish barB is bearish
            and barC.Open > barC.Close  \  #establish barC is bearish
            and barD.Open < barD.Close  \  #establish barD is bullish
            and barA.High > barB.High > barC.High   \   #establish 'lower highs' are in play
            and barA.Low > barB.Low > barC.Low  \   #establish 'lower lows' are in play
            and barA.Close - self.tolerance >= barB.Open    \   #establish downtrend is strong (w/tolerance)
            and barB.Close - self.tolerance >= barC.Open    \   #establish downtrend is strong (w/tolerance)
            and barC.High < barD.High   \   #establish reversal of 'lower highs' is in play
            and barC.Low <= barD.Low    \   #establish reversal of 'lower lows' is in play
            and bardD.Open <= barC.Open + self.tolerance    \   #establish that bullish bar open is equal to bearish bar close (w/ tolerance)
            and bardD.Open >= barC.Open - self.tolerance    \   #establish that bullish bar open is equal to bearish bar close (w/ tolerance)
        #try incorporating engulphing bullish candle where barD.Close > barC.High
        """