Overall Statistics
Total Trades
458
Average Win
9.44%
Average Loss
-4.90%
Compounding Annual Return
-43.499%
Drawdown
78.200%
Expectancy
0.060
Net Profit
-44.937%
Sharpe Ratio
0.426
Probabilistic Sharpe Ratio
18.200%
Loss Rate
64%
Win Rate
36%
Profit-Loss Ratio
1.92
Alpha
0.555
Beta
4.701
Annual Standard Deviation
2.049
Annual Variance
4.199
Information Ratio
0.395
Tracking Error
2.037
Treynor Ratio
0.186
Total Fees
$572.76
from math import floor, ceil
from datetime import datetime

class NZDUSD_10amBuyer(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2020, 1, 5)  # Set Start Date
        
        self.SetCash(1000)  # Set Strategy Cash
        
        self.margin = self.Portfolio.MarginRemaining
        
        self.SetBrokerageModel(BrokerageName.FxcmBrokerage, AccountType.Margin)
        self.ATRfactor = float(self.GetParameter("ATR factor"))
        self.RRR = float(self.GetParameter("RRR"))
        
        self.AddForex("NZDUSD", Resolution.Minute)
        
        self.Schedule.On(self.DateRules.Every(DayOfWeek.Monday, DayOfWeek.Tuesday, DayOfWeek.Wednesday, DayOfWeek.Thursday, DayOfWeek.Friday), self.TimeRules.At(10, 00), self.TimeToBuy)
        
        self.atr = AverageTrueRange(14)
        self.RegisterIndicator("NZDUSD", self.atr, timedelta(minutes=15)) #MovingAverageType.Simple
        self.SetWarmup(20)
        
        self.leverage = self.Securities["NZDUSD"].Leverage
        self.buypwr = self.margin * self.leverage
        
        self.Debug("-------------")
        self.Debug("Leverage is: " + str(self.leverage))
        self.Debug("Margin is: " + str(self.margin))
        self.Debug("Self calculated buypwr: " + str(self.buypwr))
        self.Debug("RRR is: " + str(self.RRR))
        self.Debug("ATRfactor is: " + str(self.ATRfactor))
        self.Debug("-------------")
        
        
    #Enters buy order every weekday at 10am NY time
    def TimeToBuy(self):
        
        if self.Portfolio.Invested:
            return
        
        leverage = 0.02 * self.Securities["NZDUSD"].Close / (self.atr.Current.Value * self.ATRfactor)
        self.Debug("Calculated leverage: " + str(leverage))
        quantity = round((leverage * 1000)/self.Securities["NZDUSD"].Close, -3)
        self.Debug("Calculated quantity: " + str(quantity))
        if leverage > 30: #maximum leverage is 50, above that orders get invalid
            self.Debug("Leverage too high, return")
            return
        
        
        self.MarketOrder("NZDUSD", quantity)        #ATR factor = 1.5            #RRR = 2
        self.StopMarketOrder("NZDUSD", -quantity, round(self.Securities["NZDUSD"].Close - (self.atr.Current.Value * self.ATRfactor), 5))
        self.LimitOrder("NZDUSD", -quantity, round(self.Securities["NZDUSD"].Close + (self.atr.Current.Value * self.ATRfactor)*self.RRR, 5))
        
        
        
    def OnOrderEvent(self, orderEvent):
        
        if orderEvent.StopPrice != None or orderEvent.LimitPrice != None:
            #If stoploss or limit order was filled, cancel the other one and in case anything spills over Liquidate that
            if orderEvent.Status == OrderStatus.Filled:
                
                self.CancelledOrders = self.Transactions.CancelOpenOrders("NZDUSD")
                self.Liquidate("NZDUSD")
                
        elif orderEvent.Status == OrderStatus.Invalid:
            self.CancelledOrders = self.Transactions.CancelOpenOrders("NZDUSD")
            self.Liquidate("NZDUSD")