Overall Statistics
Total Trades
36
Average Win
0.00%
Average Loss
0.00%
Compounding Annual Return
-0.119%
Drawdown
0.300%
Expectancy
0.105
Net Profit
-0.018%
Sharpe Ratio
-0.015
Probabilistic Sharpe Ratio
33.061%
Loss Rate
42%
Win Rate
58%
Profit-Loss Ratio
0.89
Alpha
0.015
Beta
-0.033
Annual Standard Deviation
0.008
Annual Variance
0
Information Ratio
-3.732
Tracking Error
0.123
Treynor Ratio
0.004
Total Fees
$0.00
Estimated Strategy Capacity
$13000000.00
import numpy as np
from collections import deque
from datetime import timedelta
import decimal as d

class BreakoutExample(QCAlgorithm):
    
        
    class MacdState:
        ''' This class is used to save the state of the MACD values with each bar'''
        def __init__(self, macd):
            if not macd.IsReady:
                self.Macd = 0.0
                self.Fast = 0.0
                self.Slow = 0.0
                self.Signal = 0.0
            else:
                self.Macd = macd.Current.Value
                self.Fast = macd.Fast.Current.Value
                self.Slow = macd.Slow.Current.Value
                self.Signal = macd.Signal.Current.Value



    def Initialize(self):
        # Set the cash for backtest
        self.SetCash(100000)
        
        
        
        
        
        # Start and end dates for backtest
        self.SetStartDate(2021,3,1)
        self.SetEndDate(2021,4,25)
        
        self.Hist = 100     
        
        # Add asset
        self.symbol = self.AddForex("AUDUSD", Resolution.Minute).Symbol
        
        
        #Self.addForex("AUDUSD",Resolution.Minute)
        
       # Schedule function 5 minutes after every market open
        self.Schedule.On(self.DateRules.EveryDay(self.symbol), \
                        self.TimeRules.AfterMarketOpen(self.symbol, 5), \
                        Action(self.EveryMarketOpen))
        # create a 15 day exponential moving average
        self.fast = self.EMA(self.symbol, 9, Resolution.Minute);
        self.slow = self.EMA(self.symbol, 50, Resolution.Minute);

        # create a 30 day exponential moving average
        self.__macd = self.MACD("AUDUSD", 12, 26, 9, MovingAverageType.Exponential, Resolution.Minute)
        self.__atr = self.ATR("AUDUSD", 14)
        
        
        self.__previous = datetime.min
        self.PlotIndicator("MACD", True, self.__macd, self.__macd.Signal)
        self.PlotIndicator("AUDUSD", self.__macd.Fast, self.__macd.Slow)
        
        self.closeval = self.History(self.symbol, 1, Resolution.Minute)["close"]
      
        self.openprice = self.History(self.symbol, 1, Resolution.Minute)["open"]
      
        self.lowval = self.History(self.symbol, 1, Resolution.Minute)["low"]
        
        self.macd = self.MACD("AUDUSD", 12, 26, 9, MovingAverageType.Exponential)
    
        self.macd.Updated += self.macdUpdated
        
        self.SubscriptionManager
        
        
        self.macdHist = deque([self.MacdState(self.macd)], self.Hist)
        
        fiveMinuteConsolidator = QuoteBarConsolidator(timedelta(minutes=5))
 
        # attach our event handler. the event handler is a function that will
        # be called each time we produce a new consolidated piece of data.
        fiveMinuteConsolidator.DataConsolidated += self.FiveMinuteBarHandler

        # this call adds our 30 minute consolidator to
        # the manager to receive updates frnom the engine
        self.SubscriptionManager.AddConsolidator("AUDUSD", fiveMinuteConsolidator)
        

    def FiveMinuteBarHandler(self, sender, consolidated):
        '''This is our event handler for our 5 minute trade bar defined above in Initialize(). So each time the
        consolidator produces a new 5 minute bar, this function will be called automatically. The 'sender' parameter
         will be the instance of the IDataConsolidator that invoked the event, but you'll almost never need that!'''

    
       

    def macdUpdated(self, sender, updated):
        # Save the current state of the macd values by instantiating a new MacdState
        #  object and adding it to the deque
        self.macdHist.appendleft(self.MacdState(self.macd))

      
    def OnData(self, data):
        
      if not self.Portfolio.Invested:
        price = data[self.symbol].Close
      
        self.Buy("AUDUSD", 1000)
        self.LimitOrder(self.symbol, -1000, price + 0.0008)
        self.StopMarketOrder(self.symbol, -1000, price - .0005)
    
      fxOpen = data['AUDUSD'].Open          ## Market Open FX Rate
      fxClose = data['AUDUSD'].Close        ## Market Close FX Rate
    
    ## If you are subscribed to more than one Forex or Futures data stream then you can
    ## access the QuoteBar dictionary and then subset this for your desired Forex symbol
      fxQuoteBars = data.QuoteBars
      audusdQuoteBar = fxQuoteBars['AUDUSD']     ## audusd QuoteBar
      fxOpen = audusdQuoteBar.Open               ## Market Open FX Rate
      fxClose = audusdQuoteBar.Close             ## Market Close FX Rate
        

 
    def EveryMarketOpen(self):
            
      self.Plot("Data Chart", self.symbol, self.Securities[self.symbol].Close) 
     
      MACDdifference = self.__macd.Current.Value - self.__macd.Signal.Current.Value
      
      
      
      if self.fast > self.slow and MACDdifference > 0 and self.macdHist[2].Macd > self.__macd.Current.Value and self.__macd.Current.Value > 0.0001 and self.openprice > self.fast and self.closeval > self.fast and self.openprice < self.fast and self.__atr > 0.00025:   
          if  not (MACDdifference < 0 and self.macdHist[1] < self.__macd.Current.Value and self.macdHist[2] < self.__macd.Current.Value): 
            if (self.Securities["AUDUSD"].Price - self.fast) > .0003: 
             self.SetHoldings("AUDUSD",10000)