Overall Statistics
Total Trades
967
Average Win
0.49%
Average Loss
-1.23%
Compounding Annual Return
-61.474%
Drawdown
87.000%
Expectancy
-0.160
Net Profit
-59.718%
Sharpe Ratio
-0.36
Probabilistic Sharpe Ratio
5.945%
Loss Rate
40%
Win Rate
60%
Profit-Loss Ratio
0.40
Alpha
-0.225
Beta
-0.501
Annual Standard Deviation
0.863
Annual Variance
0.745
Information Ratio
-0.5
Tracking Error
0.967
Treynor Ratio
0.621
Total Fees
$102.76
class TwoEMAstrategyForex(QCAlgorithm):

    

    def Initialize(self):
        self.SetStartDate(2020, 1, 15)  # Set Start Date
        self.SetCash(1000)  # Set Strategy Cash
        
        self.SetBrokerageModel(BrokerageName.FxcmBrokerage, AccountType.Margin)
        
        self.currency_pair = self.GetParameter("currency_pair") #grab currency pair from Algorithm Parameters
        self.AddForex(self.currency_pair, Resolution.Minute)
        self.Consolidate(self.currency_pair, timedelta(minutes=15), self.OnDataConsolidated)
        self.closeWindow = RollingWindow[float](2) #create a rolling window to store
        self.SetWarmUp(200)
        self.RRR = float(self.GetParameter("RRR")) #Reward Risk ratio
        self.risk = float(self.GetParameter("risk")) # % of account to risk at SL
        self.distance = float(self.GetParameter("deltaDistance"))
        
        

        
        self.emaFast = ExponentialMovingAverage(9)
        self.RegisterIndicator(self.currency_pair, self.emaFast, timedelta(minutes=15))
        self.emaSlow = ExponentialMovingAverage(21)
        self.RegisterIndicator(self.currency_pair, self.emaSlow, timedelta(minutes=15))
        self.emaTrend = ExponentialMovingAverage(200)
        self.RegisterIndicator(self.currency_pair, self.emaTrend, timedelta(minutes=15))
        
        self.long = False


    def OnData(self, data):

        pass        

    def OnDataConsolidated(self, bar):
        
        self.currentBar = bar #this is the current quote bar
        
        self.closedBar = float((self.currentBar).Close)
        #self.Debug(str((self.currentBar).Close))
        
        
        if not self.emaSlow.IsReady or not self.emaFast.IsReady or not self.emaTrend.IsReady:
            pass
        
        #check if 9EMA > 21EMA 
        if self.emaFast.Current.Value > self.emaSlow.Current.Value:
            
            #check if price is above 200EMA
            #if self.closedBar > self.emaTrend.Current.Value:
            
                #check if last was short
            if self.long == False:
        
    
               #Calcualte Quantity to buy
            
            
                        #self.Portfolio.MarginRemaining
                self.quantity = ((float(self.Portfolio.MarginRemaining) * self.risk)/self.distance)
                self.MarketOrder(self.currency_pair, self.quantity)
                self.StopMarketOrder(self.currency_pair, self.quantity * -1, self.closedBar - self.distance)
                self.LimitOrder(self.currency_pair, self.quantity * -1, self.closedBar + self.distance*self.RRR)
                
                    
            #signal last was long        
            self.long = True
                
        #check if 9EMA < 21EMA    
        elif self.emaFast.Current.Value < self.emaSlow.Current.Value:
            
            #check if price is below 200EMA
            #if self.closedBar < self.emaTrend.Current.Value:
                
                #check if last was long
            if self.long == True:
            
                        #self.Portfolio.MarginRemaining
                self.quantity = ((float(self.Portfolio.MarginRemaining) * self.risk)/self.distance)
                self.MarketOrder(self.currency_pair, self.quantity * -1)
                self.StopMarketOrder(self.currency_pair, self.quantity, self.closedBar + self.distance)
                self.LimitOrder(self.currency_pair, self.quantity, self.closedBar - self.distance*self.RRR)
                
            #signal last was short        
            self.long = False
        
        
        self.closeWindow.Add(str((self.currentBar).Close))