Overall Statistics
Total Trades
311
Average Win
0.05%
Average Loss
-0.05%
Compounding Annual Return
0.426%
Drawdown
1.400%
Expectancy
0.036
Net Profit
0.284%
Sharpe Ratio
0.169
Probabilistic Sharpe Ratio
20.580%
Loss Rate
45%
Win Rate
55%
Profit-Loss Ratio
0.87
Alpha
0.005
Beta
0.036
Annual Standard Deviation
0.018
Annual Variance
0
Information Ratio
0.941
Tracking Error
0.047
Treynor Ratio
0.086
Total Fees
$0.00
Estimated Strategy Capacity
$5100000.00
Lowest Capacity Asset
USDJPY 8G
from System.Drawing import Color
import math

class HyperActiveRedBear(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2021, 2, 6)  # Set Start Date
        self.SetCash(100000)  # Set Strategy Cash
        self.SetBrokerageModel(BrokerageName.OandaBrokerage, AccountType.Margin)
        self.Pair = self.AddForex('USDJPY', Resolution.Minute, Market.Oanda, leverage=50)
        self.Pip = self.Pair.SymbolProperties.MinimumPriceVariation
        self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.At(18, 0), Action(self.CalcHL))
        
        self.Data = {'High': 0, 'Low': 9999}
        self.Entry = {'Long': None, 'Short': None}
        
        self.Closed = True
        self.Long = False
        self.Short = False
        self.TakeProfit1 = True
        self.TakeProfit2 = True
        self.StopLoss = None
        self.Size = None

        stockPlot = Chart('Trade Plot')
        stockPlot.AddSeries(Series('Buy', SeriesType.Scatter, "$", Color.Green, ScatterMarkerSymbol.Triangle))
        stockPlot.AddSeries(Series('Sell', SeriesType.Scatter, "$", Color.Red, ScatterMarkerSymbol.TriangleDown))
        stockPlot.AddSeries(Series('Liquidate', SeriesType.Scatter, "$", Color.Blue, ScatterMarkerSymbol.Diamond))
        self.AddChart(stockPlot)

    def OnData(self, data):
        self.Data['High'] = data['USDJPY'].High if data['USDJPY'].High > self.Data['High'] else self.Data['High']
        self.Data['Low'] = data['USDJPY'].Low if data['USDJPY'].Low < self.Data['Low'] else self.Data['Low']
        self.Plot("Trade Plot", 'Close', data['USDJPY'].Close)
        
        if self.Entry['Long'] == None or self.Entry['Short'] == None:
            return
        
        if self.Closed:
            return
        
        if self.Long == False and self.Short == False:
            # check to enter trade
            if data['USDJPY'].High > self.Entry['Long'] and self.Closed != True:
                # self.Size = self.Entry['Long'] / (self.Pip * 25) * 0.08 * self.Portfolio.Cash
                self.Size = self.Portfolio.MarginRemaining
                self.LimitOrder('USDJPY', self.Size, self.Entry['Long'], tag='Enter Long')
                self.Plot('Trade Plot', 'Buy', self.Entry['Long'])
                self.StopLoss = self.Entry['Long'] - (self.Pip * 25)
                self.Long = True
                return
            #if data['USDJPY'].Low < self.Entry['Short']:
                # self.Size = self.Entry['Short'] / (self.Pip * 25) * 0.08 * self.Portfolio.Cash
                #self.Size = self.Portfolio.MarginRemaining
                #self.LimitOrder('USDJPY', -self.Size, self.Entry['Short'], tag='Enter Short')
                #self.Plot('Trade Plot', 'Sell', self.Entry['Short'])
                #self.StopLoss = self.Entry['Short'] + (self.Pip * 25)
                #self.Short = True
                #return
        elif self.Long:
            if self.TakeProfit1 and data['USDJPY'].High > self.Entry['Long'] + (self.Pip * 15):
                # order to sell a third and raise stop loss
                size = math.ceil(self.Size / 3)
                self.LimitOrder('USDJPY', -size, self.Entry['Long'] + (self.Pip * 15), tag='Take Profit 1 Long')
                self.Size = self.Size - size
                self.TakeProfit1 = False
                self.StopLoss = self.Entry['Long']
                return
            elif self.TakeProfit2 and data['USDJPY'].High > self.Entry['Long'] + (self.Pip * 30):
                # order to sell another third and raise stop loss
                size = math.ceil(self.Size / 2)
                self.LimitOrder('USDJPY', -size, self.Entry['Long'] + (self.Pip * 30), tag='Take Profit 2 Long')
                self.Size = self.Size - size
                self.TakeProfit2 = False
                self.StopLoss = self.Entry['Long'] + (self.Pip * 15)
                return
            elif self.TakeProfit1 == False and self.TakeProfit2 == False and data['USDJPY'].High > self.Entry['Long'] + (self.Pip * 50):
                # order to sell the rest
                self.LimitOrder('USDJPY', -self.Size, self.Entry['Long'] + (self.Pip * 50), tag='Take Profit 3 Long')
                self.Plot('Trade Plot', 'Liquidate', self.StopLoss)
                self.Reset()
                return
            elif data['USDJPY'].Low < self.StopLoss and self.Closed != True:
                # sell all of it
                self.LimitOrder('USDJPY', -self.Size, self.StopLoss, tag='Stop Loss Long')
                self.Plot('Trade Plot', 'Liquidate', self.StopLoss)
                self.Reset()
                return
        elif self.Short:
            if self.TakeProfit1 and data['USDJPY'].Low < self.Entry['Short'] - (self.Pip * 15):
                # order to sell a third and raise stop loss
                size = math.ceil(self.Size / 3)
                self.LimitOrder('USDJPY', size, self.Entry['Short'] - (self.Pip * 15), tag='Take Profit 1 Short')
                self.Size = self.Size - size
                self.TakeProfit1 = False
                self.StopLoss = self.Entry['Short']
                return
            elif self.TakeProfit2 and data['USDJPY'].Low < self.Entry['Short'] - (self.Pip * 30):
                # order to sell another third and raise stop loss
                size = math.ceil(self.Size / 2)
                self.LimitOrder('USDJPY', size, self.Entry['Short'] - (self.Pip * 30), tag='Take Profit 2 Short')
                self.Size = self.Size - size
                self.TakeProfit2 = False
                self.StopLoss = self.Entry['Short'] - (self.Pip * 15)
                return
            elif self.TakeProfit1 == False and self.TakeProfit2 == False and data['USDJPY'].Low < self.Entry['Short'] - (self.Pip * 50):
                # order to sell the rest
                self.LimitOrder('USDJPY', self.Size, self.Entry['Short'] - (self.Pip * 50), tag='Take Profit 3 Short')
                self.Plot('Trade Plot', 'Liquidate', self.StopLoss)
                self.Reset()
                return
            elif data['USDJPY'].High > self.StopLoss and self.Closed == False:
                # sell all of it
                self.LimitOrder('USDJPY', self.Size, self.StopLoss, tag='Stop Loss Short')
                self.Plot('Trade Plot', 'Liquidate', self.StopLoss)
                self.Reset()
                return
        
    def CalcHL(self):
        self.Debug('Running CalcHL')
        if self.Data['High'] == 0 or self.Data['Low'] == 9999:
            self.Entry['Long'] = None
            self.Entry['Short'] = None
            self.Closed = False
            self.Data = {'High': 0, 'Low': 9999}
            return
        else:
            self.Entry['Long'] = self.Data['High'] + (self.Pip * 7)
            self.Entry['Short'] = self.Data['Low'] - (self.Pip * 7)
            self.Data = {'High': 0, 'Low': 9999}
            self.Closed = False
            return
        
    def Reset(self):
        self.Closed = True
        self.Long = False
        self.Short = False
        self.TakeProfit1 = True
        self.TakeProfit2 = True
        self.StopLoss = None
        self.Size = None