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.904
Tracking Error
0.066
Treynor Ratio
0
Total Fees
$0.00
Estimated Strategy Capacity
$0
class SacrificeScotFree(QCAlgorithm):

    def Initialize(self):
        
        # Set params for data and brokerage
        # -----------------------------------
        self.symbol           = "EURUSD"
        #self.symbol           = ["EURUSD", "USDJPY", "EURGBP", "EURCHF", "USDCAD", "USDCHF", "AUDUSD","NZDUSD"]
        self.SetBrokerageModel(BrokerageName.OandaBrokerage)
        
        #for i in range(len(self.symbol)):
        
        self.AddForex(self.symbol) #, Resolution.Hour, 
        
        self.ADXtresh             = 20   # threshold line for ADX
        self.SetStartDate(2018, 5, 1)  # Set Start Date 
        self.SetEndDate(2018, 9, 30) 
        self.SetCash(100000)     # Set Strategy Cash
        self.Securities[self.symbol].SetDataNormalizationMode(DataNormalizationMode.Raw)
        self.symbolDataBySymbol = {}
         
        ######### ADD WINDOWS ##################
           
        self.window_hr =  RollingWindow[QuoteBar](20)    # History hour bars #For other security types, use QuoteBar
        self.windowmin =  RollingWindow[QuoteBar](20)  # History 5 min bars
        self.window_4hr = RollingWindow[QuoteBar](20)   # History 4 hour bars
        
        #self.Consolidate(self.symbol,timedelta(hours=1),lambda x:self.HourBarHandler.Add(x))
        #self.Consolidate(self.symbol,timedelta(minutes=5),lambda x:self.HourBarHandler.Add(x))
        #self.Consolidate(self.symbol,timedelta(hours=4),lambda x:self.window_4hr.Add(x))
        
        self._macdWin2 = RollingWindow[IndicatorDataPoint](20)
        self._macdWin = RollingWindow[IndicatorDataPoint](20)
        self.BB_4Hr_Win = RollingWindow[IndicatorDataPoint](20)
        self.BB_Min_Win = RollingWindow[IndicatorDataPoint](20)
        self.historyADXindicator =  RollingWindow[IndicatorDataPoint](20)
            
        def OnSecuritiesChanged(self, algorithm, changes):
        
            for security in changes.AddedSecurities:
                symbol = security.Symbol
                if symbol not in self.symbolDataBySymbol:
                    self.symbolDataBySymbol[symbol] = SymbolData(algorithm, symbol) ##add periodsself.barHistoryWindow
            
            for security in changes.RemovedSecurities:
                symbol = security.Symbol
                if symbol not in self.symbolDataBySymbol:
                    symbolData = self.symbolDataBySymbol.pop(symbol, None)
                    if symbolData != None:
                        algorithm.SubscriptionManager.RemoveConsolidator(symbol, symbolData.fiveMinutesConsolidate)
                        algorithm.SubscriptionManager.RemoveConsolidator(symbol, symbolData.HourConsolidate)
                        algorithm.SubscriptionManager.RemoveConsolidator(symbol, symbolData.FourHoursConsolidate)
         
        def InitIndicators(self, algorithm):
    
            self.algorithm = algorithm
            
            ## ------REGISTER HANDLER-----FIVE MINS CONSOLIDATOR -------##
            self.fiveMinutesConsolidator = QuoteBarConsolidator(timedelta(minutes=5))
            self.fiveMinutesConsolidator.DataConsolidated += self.fiveMinutesBarHandler #create function for this
            algorithm.SubscriptionManager.AddConsolidator(self.symbol, self.fiveMinutesConsolidator)
            
             ## ------REGISTER HANDLER-----ONE HOUR CONSOLIDATOR -------##
            self.HourConsolidator = QuoteBarConsolidator(timedelta(minutes=60)) #BarPeriod = TimeSpan.FromMinutes(10)
            self.HourConsolidator.DataConsolidated += self.HourBarHandler #create function for this
            algorithm.SubscriptionManager.AddConsolidator(self.symbol, self.HourConsolidator)
            
            ## ------REGISTER HANDLER-----FOUR HOURS CONSOLIDATOR -------##
            self.FourHoursConsolidator = QuoteBarConsolidator(timedelta(hours=4))
            self.FourHoursConsolidator.DataConsolidated += self.FourHoursBarHandler #create function for this
            algorithm.SubscriptionManager.AddConsolidator(self.symbol, self.FourHoursConsolidator)
            
            self.BB_HR        = self.BB(self.symbol, 20, 2, MovingAverageType.Exponential, Resolution.Hour),
            self.BB_4HR       = self.BB(self.symbol, 20, 2, MovingAverageType.Exponential, Resolution.Hour),
            self.BB_min       = self.BB(self.symbol, 20, 2, MovingAverageType.Exponential, Resolution.Minute),
            self.MACD_min     = self.MACD(self.symbol, 12, 20, 4, MovingAverageType.Exponential,Resolution.Minute),
            self.MACD_hour    = self.MACD(self.symbol, 12, 20, 4, MovingAverageType.Exponential,Resolution.Hour),
            self.ADX_hr          = self.ADX(self.symbol,14, Resolution.Hour),
            self.RSI_hr          = self.RSI(self.symbol,self.RSI_Period,Resolution.Hour)
            
            ## ------REGISTER INDICATORS TO HANDLER -------##
            ## Register macd 5 Min
            
            algorithm.RegisterIndicator(self.symbol, self.MACD_min, self.fiveMinutesConsolidator)
            self.MACD_min.Updated +=self.Macd_Min_Updated
            
            ## Register BB 5 Min
            
            algorithm.RegisterIndicator(self.symbol, self.BB_min ,self.fiveMinutesConsolidator)
            self.BB_min.Updated +=self.BB_Min_Updated  #create function for this
            
            ## Register macd 1 hr
            
            algorithm.RegisterIndicator(self.symbol, self.MACD_hour, self.fiveMinutesConsolidator)
            self.MACD_hour.Updated +=self.MACD_hour_Updated   #create function for this
            
            ## Register BB 4hr
            
            algorithm.RegisterIndicator(self.symbol, self.BB_4HR, self.fiveMinutesConsolidator)
            self.BB_4HR.Updated +=self.BB_4Hr_Updated  #create function for this
        
    def ScheduleRoutines(self):
          
        self.Schedule.On(self.DateRules.EveryDay(self.symbol),\
                         self.TimeRules.At(9, 0), Action(self._sessionOn))
                         
        self.Schedule.On(self.DateRules.EveryDay(self.symbol),\
                         self.TimeRules.At(23, 0), Action(self._sessionOff))
    
  
    def Indicators_Ready(self):
        return ( self.window_hr.IsReady and \
                 self.windowmin.IsReady and \
                 self.window_4hr.IsReady and \
                 self.BB_Min_Win.IsReady and \
                 self.BB_4Hr_Win.IsReady and \
                 self._macdWin.IsReady and \
                 self._macdWin2.IsReady and \
                 self.historyADXindicator.IsReady)
                 
   
    def BuyDetected(self):            
        
        bbUpperBand_5Min  = self.BB_min.UpperBand.Current.Value
        bbLowerBand_5Min  = self.BB_min.LowerBand.Current.Value
        bbUpperBand_Hour  = self.BB_HR.UpperBand.Current.Value
        bbLowerBand_Hour  = self.BB_HR.LowerBand.Current.Value
        bbUpperBand_4H  = self.BB_4HR.UpperBand.Current.Value
        bbLowerBand_4H  = self.BB_4HR.LowerBand.Current.Value
        MACD_5min  = self.MACD_min.LowerBand.Current.Value
        MACD_hour  = self.MACD_hour.LowerBand.Current.Value
        self.diplus = self.ADX_hr.PositiveDirectionalIndex.Current.Value
        self.diminus = self.ADX_hr.NegativeDirectionalIndex.Current.Value
        self.adx = self.ADX_hr.Current.Value
        
        # ----------------------------------------------------
        if self.fxClose_4hr >= bbUpperBand_4H or fxClose_hr >= bbUpperBand_Hour:
            if self.diplus > self.ADXtresh and self.fxClose00 >= bbLowerBand_5Min: 
                return True
            else:
                return False

            
    def SellDetected(self):            

        bbUpperBand_5Min  = self.BB_min.UpperBand.Current.Value
        bbLowerBand_5Min  = self.BB_min.LowerBand.Current.Value
        bbUpperBand_Hour  = self.BB_HR.UpperBand.Current.Value
        bbLowerBand_Hour  = self.BB_HR.LowerBand.Current.Value
        bbUpperBand_4H  = self.BB_4HR.UpperBand.Current.Value
        bbLowerBand_4H  = self.BB_4HR.LowerBand.Current.Value
        MACD_5min  = self.MACD_min.LowerBand.Current.Value
        MACD_hour  = self.MACD_hour.LowerBand.Current.Value
        self.diplus = self.ADX_hr.PositiveDirectionalIndex.Current.Value
        self.diminus = self.ADX_hr.NegativeDirectionalIndex.Current.Value
        self.adx = self.ADX_hr.Current.Value

        if self.fxClose_4hr <= bbLowerBand_4H or fxClose_hr <= bbLowerBand_Hour:
            if self.diminus > self.ADXtresh and self.fxClose00 <= bbUpperBand_5Min:
                return True
            else:
                return False
                        
    # ============================================================================
    # Logic to open new positions. Called whenever we want to open a new position.
    # ============================================================================
    def OpenNewBuyPositions(self):            
            
            self.SetHoldings(self.symbol, 0.25) # 1 means 100%
            
            #self.SetInitialStops()
    
    def OpenNewSellPositions(self):            
            
            self.SetHoldings(self.symbol, -0.25) 
            
            #self.SetInitialStopsSells()
                
    def OnData(self, data):
        self.Indicators_Ready()
        
        if not data.Bars.ContainsKey(self.symbol) or data[self.symbol] is None:
            self.Debug('ccccccccccccccccc') 
            return
        
        ''''if data.ContainsKey(self.symbol) and data[self.symbol] is not None:
            self.Debug('dddddddddddddddddddddddd')
            #self.closeWindows[symbol].Add(data[symbol].Close)
    
        self.window_hr[self.symbol].Add(data[self.symbol].Close)
        self.windowmin[self.symbol].Add(data[self.symbol].Close)
        self.window_4hr[self.symbol].Add(data[self.symbol].Close)
            
        self.Debug('eeeeeeeeeeeeeeeeeeeeeeeee') 
        
        self.fxClose_hr=self.window_hr[0].Close # price in four hour
        self.fxClose00=self.windowmin[0].Close #Price in 5min
        self.fxClose_4hr=self.window_4hr[0].Close # price in four hour'''
          
        if not self.Portfolio.Invested and self._session():
            if self.BuyEntrySignalDetected():
                self.OpenNewBuyPositions()
            elif self.SellEntrySignalDetected():
                 self.OpenNewSellPositions()     
        
    def _sessionOn(self):
        self._session = True

    def _sessionOff(self):
        self._session = False