Overall Statistics
Total Trades
158
Average Win
3.38%
Average Loss
-2.69%
Compounding Annual Return
4.920%
Drawdown
7.400%
Expectancy
0.183
Net Profit
39.652%
Sharpe Ratio
0.651
Loss Rate
48%
Win Rate
52%
Profit-Loss Ratio
1.26
Alpha
0.196
Beta
-9.694
Annual Standard Deviation
0.062
Annual Variance
0.004
Information Ratio
0.393
Tracking Error
0.062
Treynor Ratio
-0.004
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(2012, 1, 1)
        self.SetEndDate(2018, 12, 10)
        self.SetCash(2000)
        slow_period = 250
        self.SetWarmup(slow_period)  
        self.first = True
        self.Forex = self.AddForex("NZDUSD", Resolution.Daily, Market.Oanda)
        self.Forex = self.AddForex("NZDSGD", Resolution.Daily, Market.Oanda)
        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.first and not self.IsWarmingUp:
                self.first = False
        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.0020);
        
        sgdSpread = False
        usdSpread = False
        if self.Securities["NZDUSD"].AskPrice - self.Securities["NZDUSD"].BidPrice <= 0.0003:
                usdSpread = True
        if self.Securities["NZDSGD"].AskPrice - self.Securities["NZDSGD"].BidPrice <= 0.00038:
                sgdSpread = True
        #spread = self.Securities["NZDUSD"].AskPrice - self.Securities["NZDUSD"].BidPrice
        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
            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
        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)