Overall Statistics
Total Trades
151
Average Win
0.17%
Average Loss
-0.19%
Compounding Annual Return
-20.591%
Drawdown
6.400%
Expectancy
-0.328
Net Profit
-5.132%
Sharpe Ratio
-3.093
Probabilistic Sharpe Ratio
1.124%
Loss Rate
64%
Win Rate
36%
Profit-Loss Ratio
0.87
Alpha
-0.207
Beta
0.218
Annual Standard Deviation
0.054
Annual Variance
0.003
Information Ratio
-4.303
Tracking Error
0.081
Treynor Ratio
-0.765
Total Fees
$0.00
Estimated Strategy Capacity
$7900000.00
Lowest Capacity Asset
EURNZD 8G
class HyperActiveApricotFalcon(QCAlgorithm):

    def Initialize(self):
        #General Information
        self.SetStartDate(2021, 6, 27)
        #self.SetEndDate(2021, 7, 31)
        self.SetCash(100000) 
        self.pair = 'EURNZD'
        self.forex = self.AddForex(self.pair, Resolution.Minute, Market.Oanda).Symbol
        self.quantity = 100000
        
        # Set Take Profit and Stop Loss Here
        self.tp = 0.002
        self.sl = 0.0015
        
        # Long / Short - True = Live
        self.Long = True
        self.Short = False
        
        # Set number of open positions allowed at a time
        self.numtrades = 10
        
        ''' Takeprofit and stoploss not working '''
        self.takeprofit = []
        self.stoploss = []
        self.numshares = []
        self.takeprofitpos = {}
        self.stoplosspos = {}
        
        # Indicators
        self.rsi = RelativeStrengthIndex(14, MovingAverageType.Wilders)
        self.macdfiveminute = MovingAverageConvergenceDivergence(12, 26, 9, MovingAverageType.Exponential)
        # self.macdonehour = MovingAverageConvergenceDivergence(12, 26, 9, MovingAverageType.Exponential)
        self.macdonehour = self.MACD(self.forex, 12, 26, 9, MovingAverageType.Exponential, Resolution.Hour)
        self.atr = AverageTrueRange(14, MovingAverageType.Wilders)
        self.emafast = ExponentialMovingAverage(9)
        self.emaslow = ExponentialMovingAverage(50)

        # One Hour Consolidator and Indicator Registrations
        oneHourConsolidator = QuoteBarConsolidator(timedelta(minutes=60))
        oneHourConsolidator.DataConsolidated += self.OneHourBarHandler
        self.SubscriptionManager.AddConsolidator(self.pair, oneHourConsolidator)
        # self.RegisterIndicator(self.pair, self.macdonehour, oneHourConsolidator)
        
        # Five Minute Consolidator and Indicator Registrations
        fiveMinuteConsolidator = QuoteBarConsolidator(timedelta(minutes=5))
        fiveMinuteConsolidator.DataConsolidated += self.FiveMinuteBarHandler
        self.SubscriptionManager.AddConsolidator(self.pair, fiveMinuteConsolidator)
        self.RegisterIndicator(self.pair, self.rsi, fiveMinuteConsolidator)
        # self.RegisterIndicator(self.pair, self.macdonehour, oneHourConsolidator)
        self.RegisterIndicator(self.pair, self.atr, fiveMinuteConsolidator)
        self.RegisterIndicator(self.pair, self.macdfiveminute, fiveMinuteConsolidator)#.Updated += self.FiveMinuteBarHandler
        self.RegisterIndicator(self.pair, self.emafast, fiveMinuteConsolidator)
        self.RegisterIndicator(self.pair, self.emaslow, fiveMinuteConsolidator)
        
        self.macdLastHourWindow = RollingWindow[float](2)
        self.macdHourSignal = RollingWindow[float](2)
        
        self.Schedule.On(self.DateRules.Every(DayOfWeek.Friday), self.TimeRules.BeforeMarketClose(self.pair), self.WeekendLiquidation)
        
        self.fiveminbaropen = 0
        self.SetWarmUp(50)
        
        self.lastfiveminutemacdvalues = []
        self.lastonehourmacdvalues = []
        self.removekeys = []
        
        self.counter = 0
        self.tpsl = {}
        
        self.macdLastFiveBar = None
        
        self.SLTicket = None
        self.TPTicket = None
        self.entryTicket = None
        
    def OnData(self, data):
        if self.Time.day == 9:
            curTime = self.Time
            pass

    def OneHourBarHandler(self, sender, consolidated):
        self.macdLastHourWindow.Add(self.macdonehour.Current.Value)
        self.macdHourSignal.Add(self.macdonehour.Signal.Current.Value)
        
    def FiveMinuteBarHandler(self, sender, consolidated):
        if not self.macdonehour.IsReady:
            return
        
        if self.macdLastFiveBar == None or self.macdLastHourWindow.Count <= 1:
            self.macdLastFiveBar = self.macdfiveminute.Current.Value
            return
        
        Close = (consolidated.Bid.Close+consolidated.Ask.Close)/2
        Open = (consolidated.Bid.Open+consolidated.Ask.Open)/2
        Low = (consolidated.Bid.Low+consolidated.Ask.Low)/2
        High = (consolidated.Bid.High+consolidated.Ask.High)/2
        Price = consolidated.Price
            
        # Limit number of open trades
        if len(self.tpsl) >= self.numtrades:
            return
        
        emaFast = self.emafast.Current.Value
        emaSlow = self.emaslow.Current.Value
        rsiValue = self.rsi.Current.Value
        macdFive = self.macdfiveminute.Current.Value

        
        # Entry Long
        if self.Long and Close > emaFast and Open > emaFast and Close < Open and emaSlow < emaFast and rsiValue < 63 and rsiValue > 55:
            self.GoLong(Close)
                                            
        # Entry Short
        # elif self.Short and Close < emaFast and Open < emaFast and Close > Open and emaSlow > emaFast and rsiValue > 38 and rsiValue < 45:
        #     self.GoShort(Close)

        # Record MACD values to compare at next datapoint
        self.macdLastFiveBar = self.macdfiveminute.Current.Value
        
        # Order Tickets
        # self.entryTicket = None
        # self.SLTicket = None
        # self.TPTicket = None
        
    def GoLong(self, Close):
        FiveMACDdifference = self.macdfiveminute.Current.Value - self.macdfiveminute.Signal.Current.Value
        HourMACDdifference = self.macdLastHourWindow[0] - self.macdHourSignal[0]
        if self.entryTicket == None and self.macdfiveminute.Current.Value > .00005 and self.macdfiveminute.Current.Value > self.macdLastFiveBar and self.macdLastHourWindow[0] > self.macdLastHourWindow[self.macdLastHourWindow.Count-1] and self.atr.Current.Value > .00025 and self.macdfiveminute.Current.Value - self.macdfiveminute.Signal.Current.Value > 0 and self.macdLastHourWindow[0] - self.macdHourSignal[0] > -.0001:
            self.MadeEntry()
            self.BuyPrice = Close
            self.SLPrice = self.BuyPrice - .0015
            self.TPPrice = self.BuyPrice + .002
            self.entryTicket = self.LimitOrder(self.pair, self.quantity, self.BuyPrice, str(self.macdfiveminute.Current.Value) + " " + str(self.macdLastFiveBar))
            self.Debug(f"1 Hour MACD Difference : {HourMACDdifference}" + " " + str(self.Time))
            self.Debug(f"1 Hour MACD Value : {self.macdonehour.Current.Value}")
            self.Debug(f"1 Hour MACD Signal : {self.macdonehour.Signal.Current.Value}")
            self.Debug(f"1 Hour MACD Previous : {self.macdLastHourWindow[self.macdLastHourWindow.Count-1]}")
            # Enter stop loss order
            self.SLTicket = self.StopMarketOrder( self.pair, -self.quantity, self.SLPrice, "SLTicket")
            # Enter limit order
            self.TPTicket = self.LimitOrder( self.pair, -self.quantity, self.TPPrice, "TPTicket")
        else:
            self.NoEntry()
                        
    def GoShort(self, Close):
        FiveMACDdifference = self.macdfiveminute.Current.Value - self.macdfiveminute.Signal.Current.Value
        HourMACDdifference = self.macdonehour.Current.Value - self.macdonehour.Signal.Current.Value
        if self.macdfiveminute.Current.Value < -.00005:
            # MACD Current > MACD 1 Bar Ago ( 5 minute and 1 hour )
            if self.macdfiveminute.Current.Value < self.macdLastFiveBar and self.macdonehour.Current.Value < self.macdLastHourWindow[self.macdLastHourWindow.Count-1]:
                # ATR > .00025
                if self.atr.Current.Value > .00025:
                    # MACD Difference < 0 ( 5 minute and 1 hour )
                    if self.macdfiveminute.Current.Value - self.macdfiveminute.Signal.Current.Value < 0 and self.macdonehour.Current.Value - self.macdonehour.Signal.Current.Value < 0:
                        self.BuyPrice = Close
                        self.SLPrice = self.BuyPrice + .0015
                        self.TPPrice = self.BuyPrice - .002
                        self.entryTicket = self.LimitOrder(self.pair, -self.quantity, self.BuyPrice, str(self.macdfiveminute.Current.Value) + " " + str(self.macdLastFiveBar))
                        self.Debug(f"1 Hour MACD Difference : {HourMACDdifference}")
                        self.Debug(f"1 Hour MACD Value : {self.macdonehour.Current.Value}")
                        self.Debug(f"1 Hour MACD Signal : {self.macdonehour.Signal.Current.Value}")
                        self.Debug(f"1 Hour MACD Previous : {self.macdLastHourWindow[self.macdLastHourWindow.Count-1]}")
                        # Enter stop loss order
                        self.SLTicket = self.StopMarketOrder( self.pair, self.quantity, self.SLPrice)
                        # Enter limit order
                        self.TPTicket = self.LimitOrder( self.pair, self.quantity, self.TPPrice)
    
    def WeekendLiquidation(self):
        self.Liquidate()
        self.tpsl = {}
        if self.entryTicket != None and self.entryTicket.Status in [OrderStatus.Filled, OrderStatus.Canceled, ]:
            self.entryTicket = None
        
    def MadeEntry(self):
        self.Debug("ENTRY APPROVED ON " + str(self.Time))
        FiveMACDdifference = self.macdfiveminute.Current.Value - self.macdfiveminute.Signal.Current.Value
        self.Debug(f"5 Min MACD Difference : {FiveMACDdifference}")
        self.Debug(f"5 Min MACD Value : {self.macdfiveminute.Current.Value}")
        self.Debug(f"5 Min MACD Signal : {self.macdfiveminute.Signal.Current.Value}")
        
    def NoEntry(self):
        self.Debug("NO ENTRY ON " + str(self.Time) + ". ENTRY TICKET " + str(self.entryTicket))
        FiveMACDdifference = self.macdfiveminute.Current.Value - self.macdfiveminute.Signal.Current.Value
        self.Debug(f"5 Min MACD Difference : {FiveMACDdifference}")
        self.Debug(f"5 Min MACD Value : {self.macdfiveminute.Current.Value}")
        self.Debug(f"5 Min MACD Signal : {self.macdfiveminute.Signal.Current.Value}")
        HourMACDdifference = self.macdonehour.Current.Value - self.macdonehour.Signal.Current.Value
        self.Debug(f"1 Hour MACD Difference : {HourMACDdifference}")
        self.Debug(f"1 Hour MACD Value : {self.macdonehour.Current.Value}")
        self.Debug(f"1 Hour MACD Signal : {self.macdonehour.Signal.Current.Value}")
        self.Debug(f"1 Hour MACD Previous : {self.macdLastHourWindow[self.macdLastHourWindow.Count-1]}")

    def OnOrderEvent(self, orderEvent):
        if orderEvent.Status != OrderStatus.Filled:
            return
        order = self.Transactions.GetOrderById(orderEvent.OrderId)
        
        # if self.entryTicket.Status == OrderStatus.Filled:
        #     self.SLTicket = self.StopMarketOrder( self.pair, self.quantity, self.SLPrice)
        #     # Enter limit order
        #     self.TPTicket = self.LimitOrder( self.pair, self.quantity, self.TPPrice)
        
        # if self.entryTicket != None and self.entryTicket.Status == OrderStatus.Filled and not self.SLTicket == None and (self.SLTicket.Status == OrderStatus.Filled or self.TPTicket.Status == OrderStatus.Filled):
        #     self.Transactions.CancelOpenOrders(order.Symbol)
        
        
        # self.Transactions.CancelOpenOrders(order.Symbol)
        self.asd = self.SLTicket
        if self.SLTicket == None: 
            return
        a = self.SLTicket
        b = orderEvent.OrderId
        c = self.SLTicket.OrderId
        d = self.Portfolio[orderEvent.Symbol].Invested
        
        if self.SLTicket != None and orderEvent.OrderId == self.SLTicket.OrderId and not self.Portfolio[orderEvent.Symbol].Invested:
            self.SLTicket.Cancel()
            self.TPTicket.Cancel()
            self.SLTicket = None
            self.TPTicket = None
            self.entryTicket = None
        elif self.TPTicket != None and orderEvent.OrderId == self.TPTicket.OrderId and not self.Portfolio[orderEvent.Symbol].Invested:
            self.SLTicket.Cancel()
            self.TPTicket.Cancel()
            self.SLTicket = None
            self.TPTicket = None
            self.entryTicket = None
        
        # if order.Type == OrderType.Limit and self.SLTicket != None:
        #     self.SLTicket.Cancel()
        #     self.SLTicket = None
        #     self.TPTicket = None
        # if order.Type == OrderType.StopMarket and self.TPTicket != None:
        #     self.TPTicket.Cancel()
        #     self.SLTicket = None
        #     self.TPTicket = None
        # self.Cancel(orderEvent.OrderId)
    
    def Cancel(self, id):
        '''cancel one order if the other was filled'''
        if self.TPTicket is not None and id == self.TPTicket.OrderId:
            self.stopLoss.Cancel()
        elif self.SLTicket is not None and id == self.SLTicket.OrderId:
            self.takeProfit.Cancel()
        else:
            return
        self.TPTicket = None
        self.SLTicket = None
        self.entryTicket = None