Overall Statistics
Total Trades
49
Average Win
0.00%
Average Loss
0.00%
Compounding Annual Return
0.002%
Drawdown
0.000%
Expectancy
0.035
Net Profit
0.001%
Sharpe Ratio
0.267
Probabilistic Sharpe Ratio
32.925%
Loss Rate
62%
Win Rate
38%
Profit-Loss Ratio
1.76
Alpha
0
Beta
0
Annual Standard Deviation
0
Annual Variance
0
Information Ratio
-1.278
Tracking Error
0.174
Treynor Ratio
0.342
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, 8, 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
        
        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):
                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)):
                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:
            if order.Type == OrderType.Limit or order.Type == OrderType.StopLimit or order.Type == OrderType.StopMarket:
                self.Transactions.CancelOpenOrders(order.Symbol)