Overall Statistics
Total Trades
1
Average Win
0%
Average Loss
0%
Compounding Annual Return
0%
Drawdown
103.100%
Expectancy
0
Net Profit
-105.911%
Sharpe Ratio
-0.179
Probabilistic Sharpe Ratio
0.047%
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
-1.029
Beta
0.369
Annual Standard Deviation
5.592
Annual Variance
31.268
Information Ratio
-0.193
Tracking Error
5.595
Treynor Ratio
-2.708
Total Fees
$0.00
Estimated Strategy Capacity
$28000.00
Lowest Capacity Asset
EURUSD 5O
# region imports
from AlgorithmImports import *
# endregion

# ------------------------------------------------------------
SYMBOL = "EURUSD"
# ------------------------------------------------------------

class DeterminedAsparagusBee(QCAlgorithm):

    def Initialize(self):
        # Set dates
        self.SetStartDate(2020,1,1)       
        self.SetEndDate(2021,12,31)

        # Set cash
        self.SetCash(10000)

        # Add asset
        self.AddForex(SYMBOL, Resolution.Daily, Market.FXCM)

        # Set up EMA and MACD indicators
        self.macd = self.MACD(SYMBOL, 12, 26, 9)
        self.ema200 = self.EMA(SYMBOL, 200)

        self.SetWarmUp(200)

        self.buyTicket = None
        self.buystoplossTicket = None
        self.buyprofitTicket = None
        self.sellTicket = None
        self.selltakeprofitTicket = None
        self.sellstoplossTicket = None

    def OnData(self, data):

        openOrders = self.Transactions.GetOpenOrders()
        if len(openOrders)> 0:
            for x in openOrders:
                self.Transactions.CancelOrder(x.Id)

        if self.IsWarmingUp:
            return

        # Check holdings
        holdings = self.Portfolio[SYMBOL].Quantity

        # Set risk amount
        riskAmount = (self.Portfolio.TotalPortfolioValue / 100)

        # Calculate range of current price to EMA value
        emaRange = (abs(self.Securities[SYMBOL].Close - self.ema200.Current.Value))

        # Calculate position size based on risk amount and the range to EMA value
        positionSize = (riskAmount / emaRange)

        self.Log(holdings)
        self.Log(riskAmount)
        self.Log(emaRange)
        self.Log(positionSize)

        # Buy trade
        if holdings == 0:
            if self.Securities[SYMBOL].Low > self.ema200.Current.Value:
                if self.macd.Current.Value > self.macd.Signal.Current.Value:
                    if self.macd.Signal.Current.Value < 0:
                        self.buyPrice = self.Securities[SYMBOL].Close + 0.0003 # 3 pips to account for slippage
                        self.buyTicket = self.StopMarketOrder(SYMBOL, positionSize, self.buyPrice, tag="BUY ENTRY")
                        self.buystoplossPrice = self.buyPrice - emaRange - 0.0003 # 3 pips to account for slippage
                        self.buyprofitTarget1 = self.buyPrice + (1.5 * emaRange) - 0.003 # 3 pips to account for slippage

        # Sell trade
        if holdings == 0:
            if self.Securities[SYMBOL].Close < self.ema200.Current.Value:
                if self.macd.Current.Value < self.macd.Signal.Current.Value:
                    if self.macd.Signal.Current.Value > 0:
                        self.sellPrice = self.Securities[SYMBOL].Close - 0.0003 # 3 pips to account for slippage
                        self.sellTicket = self.StopMarketOrder(SYMBOL, -positionSize, self.sellPrice, tag="SELL ENTRY")
                        self.sellstoplossPrice = self.sellPrice + emaRange + 0.0003 # 3 pips to account for slippage
                        self.sellprofitTarget1 = self.sellPrice - (1.5 * emaRange) + 0.003 # 3 pips to account for slippage


    def OnOrderEvent(self, orderEvent: OrderEvent) -> None:
        if orderEvent.Status is not OrderStatus.Filled:
            return

        positionSize = orderEvent.FillQuantity

        # Create take profit and stop loss orders for BUY
        if self.buyTicket is not None and self.buyTicket.OrderId == orderEvent.OrderId:
            # Enter buy stop loss order
            self.buystoplossTicket = self.StopMarketOrder(SYMBOL, -positionSize, self.buystopLossPrice, tag="BUY STOP LOSS")

            # Enter buy take profit order
            self.buyprofitTicket = self.LimitOrder(SYMBOL, -positionSize, self.buyprofitTarget1, tag = "BUY TAKE PROFIT")

        # Create take profit and stop loss orders for SELL
        if self.sellTicket is not None and self.sellTicket.OrderId == orderEvent.OrderId:          
            # Enter sell stop loss order
            self.sellstoplossTicket = self.StopMarketOrder(SYMBOL, -positionSize, self.sellstoplossPrice, tag = "SELL STOP LOSS")

            # Enter sell take profit order
            self.selltakeprofitTicket = self.LimitOrder(SYMBOL, -positionSize, self.sellprofitTarget1, tag = "SELL TAKE PROFIT")

        # One order cancels the other when it gets filled for BUY
        if self.buystoplossTicket is not None and self.buystoplossTicket.OrderId == orderEvent.OrderId:
            self.buyprofitTicket.Cancel()
            self.buyTicket = None

        if self.buyprofitTicket is not None and self.buystoplossTicket.OrderId == orderEvent.OrderId:
            self.buystoplossTicket.Cancel()
            self.buyTicket = None

        # One order cancels the other when it gets filled for SELL
        if self.sellstoplossTicket is not None and self.sellstoplossTicket.OrderId == orderEvent.OrderId:
            self.selltakeprofitTicket.Cancel()
            self.sellTicket = None

        if self.selltakeprofitTicket is not None and self.selltakeprofitTicket.OrderId == orderEvent.OrderId:
            self.sellstoplossTicket.Cancel()
            self.sellTicket = None