Overall Statistics
Total Trades
12
Average Win
0%
Average Loss
0.00%
Compounding Annual Return
-0.034%
Drawdown
0.000%
Expectancy
-1
Net Profit
-0.004%
Sharpe Ratio
-6.877
Probabilistic Sharpe Ratio
0.003%
Loss Rate
100%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0
Beta
0
Annual Standard Deviation
0
Annual Variance
0
Information Ratio
-3.563
Tracking Error
0.17
Treynor Ratio
2.684
Total Fees
$0.00
class MovingAverageCrossAlgorithm(QCAlgorithm):

    def Initialize(self):
        '''Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.'''

        self.SetStartDate(2020, 10, 25)    #Set Start Date
           
        self.SetCash(1000)             #Set Strategy Cash
        # Find more symbols here: http://quantconnect.com/data
        self.symbol = 'EURUSD'
        self.AddForex(self.symbol, Resolution.Hour)

        # create a 15 day exponential moving average
        self.fast = self.EMA(self.symbol, 15, Resolution.Hour)

        # create a 30 day exponential moving average
        self.slow = self.EMA(self.symbol, 30, Resolution.Hour)
        
        self.firstRun = True

        self.previous = None
        stockPlot = Chart('Trade Plot')
      
        # Import the necessary module before using Custom color
        from System.Drawing import Color
        stockPlot.AddSeries(Series('Price', SeriesType.Candle, '$', Color.Green))
        stockPlot.AddSeries(Series('Buy', SeriesType.Scatter, '$', Color.Red, ScatterMarkerSymbol.Triangle))
        stockPlot.AddSeries(Series('Sell', SeriesType.Scatter, '$', Color.Blue, ScatterMarkerSymbol.TriangleDown))
        stockPlot.AddSeries(Series('fastEma', SeriesType.Line, '$', Color.Orange))
        stockPlot.AddSeries(Series('slowEma', SeriesType.Line, '$', Color.Black))
        self.AddChart(stockPlot)


    def OnData(self, data):
        '''OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.'''
        
        if not self.slow.IsReady:
            return

        # define a small tolerance on our checks to avoid bouncing
        tolerance = 0.00015

        holdings = self.Portfolio[self.symbol].Quantity
        close = self.Securities[self.symbol].Close
        
        self.emaSlowValue = self.slow.Current.Value
        self.emaFastValue = self.fast.Current.Value
        
        if self.firstRun == True:
            self.isLong =  self.emaFastValue < self.emaSlowValue
            self.firstRun == False
        
        self.Plot('Trade Plot', 'Price', data[self.symbol].Price)
        self.Plot('Trade Plot', 'fastEma', self.emaFastValue)
        self.Plot('Trade Plot', 'slowEma', self.emaSlowValue)
        positionSize = 1.0
        
        if not self.Portfolio.Invested:
            
            if self.fast.Current.Value > self.slow.Current.Value * (1 + tolerance) and self.isLong:
                self.Log("BUY >> {0}".format(self.Securities[self.symbol].Price))
                
                self.MarketOrder(self.symbol, positionSize)
    
                self.LimitOrder(self.symbol, -positionSize, 1.01*close)
                self.StopMarketOrder(self.symbol, -positionSize, 0.995*close)
                
                self.Plot('Trade Plot', 'Buy', data[self.symbol].Close)
                
            if (self.fast.Current.Value < self.slow.Current.Value * (1 + tolerance) and not self.isLong):
                self.Log("SELL >> {0}".format(self.Securities[self.symbol].Price))
                
                self.Log("Take profit ORDER PRICE >> {0}".format(0.990*close))
                self.Log("Stop Loss ORDER PRICE >> {0}".format(1.005*close))
                
                self.MarketOrder(self.symbol, -positionSize)
                
                self.LimitOrder(self.symbol, positionSize, 0.990*close)
                self.StopLimitOrder(self.symbol, positionSize, 1.005*close, 1.006*close)
                
                self.Plot('Trade Plot', 'Sell', data[self.symbol].Close)
    
    def OnOrderEvent(self, orderEvent):

        order = self.Transactions.GetOrderById(orderEvent.OrderId)
        
        ### Cancel remaining order if limit order or stop loss order is executed
        if order.Status == OrderStatus.Filled:
            self.Log("{0}: {1}: {2}".format(self.Time, order.Type, orderEvent))
            self.isLong = self.emaFastValue > self.emaSlowValue
            self.Log(">>> IS LONG VAR: " + str(self.isLong))
            if order.Type == OrderType.Limit or OrderType.StopLimit:
                self.Transactions.CancelOpenOrders(order.Symbol)
            
            if order.Status == OrderStatus.Canceled:
                self.Log(str(orderEvent))