Overall Statistics
Total Trades
90
Average Win
4.04%
Average Loss
-4.32%
Compounding Annual Return
-11.908%
Drawdown
27.400%
Expectancy
-0.032
Net Profit
-22.472%
Sharpe Ratio
-0.705
Loss Rate
50%
Win Rate
50%
Profit-Loss Ratio
0.94
Alpha
0.038
Beta
-8.168
Annual Standard Deviation
0.132
Annual Variance
0.017
Information Ratio
-0.827
Tracking Error
0.132
Treynor Ratio
0.011
Total Fees
$0.00
# https://quantpedia.com/Screener/Details/100
from QuantConnect.Data import SubscriptionDataSource
from QuantConnect.Python import PythonData
from datetime import date, timedelta, datetime
import decimal
import numpy as np
from sklearn import datasets, linear_model
from QuantConnect.Python import PythonQuandl

class TradeSpreadAlgorithm(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2016, 1, 1)
        self.SetEndDate(2018, 1, 1)
        self.SetCash(2000)
        self.Forex = self.AddForex("NZDUSD", Resolution.Daily, Market.Oanda)
        self.Forex = self.AddForex("NZDSGD", Resolution.Daily, Market.Oanda)
        
        '''history = self.History(["NZDSGD", "NZDUSD"], 200 + 1)

        # prints out the tail of the dataframe
        self.Log(str(history.loc["EURUSD"].tail()))
        self.Log(str(history.loc["NZDUSD"].tail()))

        for index, row in history.loc["EURUSD"].iterrows():
            self.fast.Update(index, row["close"])
            self.slow.Update(index, row["close"])
            '''
        self.SetWarmup(timedelta(20))  
        
        self.nzdema = self.EMA("NZDSGD", 15)
        self.usdema = self.EMA("NZDUSD", 15)
        self.usdsgdEMA = IndicatorExtensions.Minus(self.nzdema, self.usdema) # Creating the EMA on 2 MOVING AVERAGES
        #self.usdsgdsma = IndicatorExtensions.SMA(self.usdsgdEMA, 21) #creating a 20 period sma out of my ema 
        #self.PlotIndicator("Special Ind", self.usdsgdEMA, self.usdsgdsma)
        overlayPlot = Chart("Overlay Plot")
        overlayPlot.AddSeries(Series("usdsgdEMA Plot", SeriesType.Line, 0))
        overlayPlot.AddSeries(Series("Buy", SeriesType.Scatter, 0))
        overlayPlot.AddSeries(Series("Sell", SeriesType.Scatter, 0))
        overlayPlot.AddSeries(Series("NZDSGD", SeriesType.Line, 1))
        overlayPlot.AddSeries(Series("NZDUSD", SeriesType.Line, 1))
        overlayPlot.AddSeries(Series("liquidated_buy", SeriesType.Scatter, 0))
        overlayPlot.AddSeries(Series("liquidated_sell", SeriesType.Scatter, 0))
        self.AddChart(overlayPlot)
        
    def OnData(self, data):
        forex = ["NZDUSD", "NZDSGD"]
        for symbol in forex:
            if not self.usdsgdEMA.IsReady: 
                return
            # get the indicator value 
        if self.IsWarmingUp: return
        buy_signal_triggered, sell_signal_triggered = False, False
        liquidate_buy, liquidate_sell = False, False
        price = self.Securities["NZDSGD"].Price - self.Securities["NZDUSD"].Price
        tolerance = decimal.Decimal(0.0007);
        
        sgdSpread = False
        usdSpread = False
        if self.Securities["NZDUSD"].AskPrice - self.Securities["NZDUSD"].BidPrice <= 0.0005:
                usdSpread = True
        if self.Securities["NZDSGD"].AskPrice - self.Securities["NZDSGD"].BidPrice <= 0.0005:
                sgdSpread = True
        #spread = self.Securities["NZDUSD"].AskPrice - self.Securities["NZDUSD"].BidPrice
        self.Debug("the price is " + str(price) + " and the USDSGDEMA is " + str(self.usdsgdEMA.Current.Value * (1 + tolerance)))
        self.Debug("the DELTA is " + str(price - (self.usdsgdEMA.Current.Value * (1 + tolerance))))
        self.Log("FAST {0} READY. Samples: {1}".format("IS" if self.nzdema.IsReady else "IS NOT", self.nzdema.Samples))
        self.Log("SLOW {0} READY. Samples: {1}".format("IS" if self.usdema.IsReady else "IS NOT", self.usdema.Samples))
        #self.Quit(f'{history}')
        if sgdSpread and usdSpread:
        
            if price >= self.usdsgdEMA.Current.Value * (1 + tolerance) and not (self.Portfolio["NZDSGD"].IsShort and self.Portfolio["NZDUSD"].IsLong):
                self.SetHoldings("NZDSGD", -5)
                self.SetHoldings("NZDUSD", 5)
                buy_signal_triggered = True
                self.Debug("Order Purchased")
            elif price <= self.usdsgdEMA.Current.Value * (1 - tolerance) and not (self.Portfolio["NZDSGD"].IsLong and self.Portfolio["NZDUSD"].IsShort):#
                self.SetHoldings("NZDSGD", 5)
                self.SetHoldings("NZDUSD", -5)
                sell_signal_triggered = True
                self.Debug("Order Purchased")
        if self.Portfolio["NZDSGD"].IsShort and self.Portfolio["NZDUSD"].IsLong and price < self.usdsgdEMA.Current.Value:
            self.Liquidate()
            liquidate_buy = True
        if self.Portfolio["NZDSGD"].IsLong and self.Portfolio["NZDUSD"].IsShort and price > self.usdsgdEMA.Current.Value:
            self.Liquidate()
            liquidate_sell = True
        
        if buy_signal_triggered:
                self.Plot("Overlay Plot", "Buy", price)
        elif sell_signal_triggered:
                self.Plot("Overlay Plot", "Sell", price)
        if liquidate_buy:
            self.Plot("Overlay Plot", "liquidated_buy", price)
        elif liquidate_sell:
            self.Plot("Overlay Plot", "liquidated_sell", price)
        self.Plot("Overlay Plot", "NZDSGD", self.Securities["NZDSGD"].Price)
        self.Plot("Overlay Plot", "NZDUSD", self.Securities["NZDUSD"].Price)   
        self.Plot("Overlay Plot", "usdsgdEMA Plot", self.usdsgdEMA.Current.Value)