Overall Statistics
Total Trades
1356
Average Win
0.04%
Average Loss
-0.05%
Compounding Annual Return
-14.626%
Drawdown
8.000%
Expectancy
-0.334
Net Profit
-7.687%
Sharpe Ratio
-4.878
Probabilistic Sharpe Ratio
0.000%
Loss Rate
62%
Win Rate
38%
Profit-Loss Ratio
0.77
Alpha
-0.116
Beta
-0.01
Annual Standard Deviation
0.025
Annual Variance
0.001
Information Ratio
-2.625
Tracking Error
0.193
Treynor Ratio
11.643
Total Fees
$0.00
from datetime import timedelta

import QuantConnect
from QuantConnect import Resolution
from QuantConnect.Algorithm import QCAlgorithm
from QuantConnect.Orders import OrderStatus
from QuantConnect import Chart, SeriesType
from QuantConnect import Series


class MultidimensionalTransdimensionalCoreWave(QCAlgorithm):

    __risk = 0.2
    __pair = "EURUSD"

    __takeProfit = None
    __stopLoss = None

    __ema = None
    __lastTrade = "short"

    def Initialize(self):

        self.SetStartDate(2020, 4, 19)                          # Set Start Date
        self.SetCash(1000)                                      # Set Strategy Cash
        self.AddForex(self.__pair, Resolution.Hour)             # The pair we trade

        self.__ema = self.EMA(self.__pair, 20, Resolution.Hour) # The exponential moving average we use

        chart = Chart("Chart")
        candles = Series("Candles", SeriesType.Candle)
        ema = Series("EMA", SeriesType.Line)
        buy = Series("Buy", SeriesType.Scatter)
        sell = Series("Sell", SeriesType.Scatter)

        chart.AddSeries(candles)
        chart.AddSeries(ema)
        chart.AddSeries(buy)
        chart.AddSeries(sell)
        self.AddChart(chart)

    def OnData(self, data):
        '''OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
            Arguments:
                data: Slice object keyed by symbol containing the stock data
        '''

        # Wait till ema is initialized
        if not self.__ema.IsReady: return
        
        pairBar = data[self.__pair]
        self.Plot("Chart", "Candles", pairBar.Close)
        self.Plot("Chart", "EMA", self.__ema.Current.Value)

        if pairBar.Close > self.__ema.Current.Value and self.__lastTrade is not "long":
            
            self.Liquidate(self.__pair)
            self.SetHoldings(self.__pair, self.__risk, True)
            holdings = self.Portfolio[self.__pair].Quantity

            self.__takeProfit = self.LimitOrder(self.__pair, -holdings, pairBar.Close + 0.00050)
            self.__stopLoss = self.StopMarketOrder(self.__pair, -holdings, pairBar.Close - 0.00020)

            self.__lastTrade = "long"
            self.Plot("Chart","Buy", pairBar.Close)
            
            self.Log("Long : " + str(pairBar.Close) + " " + str(self.__ema.Current.Value))

        if pairBar.Close < self.__ema.Current.Value and self.__lastTrade is not "short":
            
            self.Liquidate(self.__pair)
            self.SetHoldings(self.__pair, -self.__risk, True)
            holdings = self.Portfolio[self.__pair].Quantity

            self.__stopLoss = self.LimitOrder(self.__pair, holdings, pairBar.Close + 0.00020)
            self.__takeProfit = self.StopMarketOrder(self.__pair, holdings, pairBar.Close - 0.00050)

            self.__lastTrade = "short"
            self.Plot("Chart", "Sell", pairBar.Close)
            
            self.Log("Short : " + str(pairBar.Close) + " " + str(self.__ema.Current.Value))

    def OnOrderEvent(self, orderEvent: QuantConnect.Orders.OrderEvent) -> None:
        super().OnOrderEvent(orderEvent)

        if orderEvent is None: return
        if orderEvent.Status is OrderStatus.Canceled: return
        if self.__stopLoss is None or self.__takeProfit is None: return

        self.Log("Order event " + str(orderEvent.OrderId) + " incoming")

        # Cancel stops loss and take profits if one of them was triggered
        if self.__stopLoss.OrderId is orderEvent.OrderId:
            self.__takeProfit.Cancel()
            self.Log("Take Profit " + str(self.__takeProfit.OrderId) + " was canceled")

        if self.__takeProfit.OrderId is orderEvent.OrderId:
            self.__stopLoss.Cancel()
            self.Log("Stop loss " + str(self.__stopLoss.OrderId) + " was canceled")