Overall Statistics
Total Trades
152
Average Win
1.76%
Average Loss
-1.04%
Compounding Annual Return
66.867%
Drawdown
12.800%
Expectancy
0.671
Net Profit
67.072%
Sharpe Ratio
1.887
Loss Rate
38%
Win Rate
62%
Profit-Loss Ratio
1.69
Alpha
1.298
Beta
-53.662
Annual Standard Deviation
0.232
Annual Variance
0.054
Information Ratio
1.818
Tracking Error
0.232
Treynor Ratio
-0.008
Total Fees
$0.00
from clr import AddReference
AddReference("System")
AddReference("QuantConnect.Algorithm")
AddReference("QuantConnect.Indicators")
AddReference("QuantConnect.Common")
from System import *
from QuantConnect import *
from QuantConnect.Algorithm import *
from QuantConnect.Indicators import *
import numpy as np
from datetime import timedelta, datetime
import decimal

class RegressionChannelAlgorithm(QCAlgorithm):

    def Initialize(self):

        self.SetCash(1000)
        self.SetStartDate(2018,4,9)
        self.SetEndDate(2019,4,9)
        self.symbols = ["EURNZD", "USDCAD", "NZDUSD", "CADSGD", "GBPCHF", "EURGBP"]
        slowperiod = 240
        self.SetWarmUp(slowperiod)
        for symbol in self.symbols:
            equity = self.AddForex(symbol, Resolution.Hour, Market.Oanda)
            self._spy = equity.Symbol
            #self._holdings = equity.Holdings
        self.ema1 = self.EMA("USDCAD", 10, Resolution.Daily) #[1],[3]
        self.ema2 = self.EMA("CADSGD", 10, Resolution.Daily)
        self.sma1 = IndicatorExtensions.Minus(self.ema1, self.ema2) 
        
        self.ema3 = self.EMA("EURNZD", 10, Resolution.Daily) #[0].[2]
        self.ema4 = self.EMA("NZDUSD", 10, Resolution.Daily)
        self.sma2 = IndicatorExtensions.Minus(self.ema3, self.ema4)
        
        self.ema5 = self.EMA("GBPCHF", 50, Resolution.Hour) #[4],[5]
        self.ema6 = self.EMA("EURGBP", 50, Resolution.Hour)
        self.sma3 = IndicatorExtensions.Minus(self.ema5, self.ema6)
        
        '''
        stockPlot = Chart("Trade Plot")
        stockPlot.AddSeries(Series("Buy", SeriesType.Scatter, 0)) 
        stockPlot.AddSeries(Series("Sell", SeriesType.Scatter, 0))
        stockPlot.AddSeries(Series("MinusSMA1", SeriesType.Line, 0))
        stockPlot.AddSeries(Series("lavern3", SeriesType.Line, 0))
        self.AddChart(stockPlot)
        '''
    def OnData(self, data):
        if self.IsWarmingUp: return
        if (not self.ema1.IsReady) or (not data.ContainsKey(self.symbols[0])) or (not data.ContainsKey(self.symbols[1])): return
        if (not self.ema2.IsReady) or ( not self.ema3.IsReady) or ( not self.ema4.IsReady) or ( not self.ema5.IsReady) or ( not self.ema6.IsReady): return 
        
        tolerance = decimal.Decimal(0.0100)
        tolerance2 = decimal.Decimal(0.0175)
        tolerance3 = decimal.Decimal(0.0070)
        lavern = (self.Securities["USDCAD"].Price - self.Securities["CADSGD"].Price)
        lavern2 = (self.Securities["EURNZD"].Price - self.Securities["NZDUSD"].Price)
        lavern3 = (self.Securities["GBPCHF"].Price - self.Securities["EURGBP"].Price)
        
        if not self.Portfolio[self.symbols[1]].Invested and lavern <= (self.sma1.Current.Value - tolerance):
            self.MarketOrder(self.symbols[1], 3000)
            self.MarketOrder(self.symbols[3], -3000)
            self.Plot("Trade Plot", "Buy", lavern)
        if self.Portfolio[self.symbols[1]].Invested and lavern >= (self.sma1.Current.Value + tolerance):
            self.MarketOrder(self.symbols[1], -3000)
            self.MarketOrder(self.symbols[3], 3000)
            self.Plot("Trade Plot", "Sell", lavern)
        
        if not self.Portfolio[self.symbols[0]].Invested and lavern2 <= (self.sma2.Current.Value - tolerance2):
            self.MarketOrder(self.symbols[0], -3000)
            self.MarketOrder(self.symbols[2], 3000)
            self.Plot("Trade Plot", "Buy", lavern2)
        if not self.Portfolio[self.symbols[0]].Invested and lavern2 >= (self.sma2.Current.Value + tolerance2):
            self.MarketOrder(self.symbols[0], 3000)
            self.MarketOrder(self.symbols[2], -3000)
            self.Plot("Trade Plot", "Sell", lavern2)
        if self.Portfolio[self.symbols[0]].Invested:
            if (self.sma2.Current.Value + 0.0005) >= lavern2 >= (self.sma2.Current.Value - 0.0005):
                self.Liquidate(self.symbols[0])
                self.Liquidate(self.symbols[2])
        self.Log("Unsettled Cash is: " + str(self.Portfolio.Cash))
        '''
        if not self.Portfolio[self.symbols[4]].Invested and lavern3 <= (self.sma3.Current.Value - tolerance3):
            self.MarketOrder(self.symbols[4], -3000)
            self.MarketOrder(self.symbols[5], 3000)
            self.Plot("Trade Plot", "Buy", lavern3)
        if not self.Portfolio[self.symbols[4]].Invested and lavern3 >= (self.sma3.Current.Value + tolerance3):
            self.MarketOrder(self.symbols[4], 3000)
            self.MarketOrder(self.symbols[5], -3000)
            self.Plot("Trade Plot", "Sell", lavern3)
        if self.Portfolio[self.symbols[4]].Invested:
            if (self.sma3.Current.Value + 0.0015) >= lavern3 >= (self.sma3.Current.Value - 0.0015):
                self.Liquidate(self.symbols[4])
                self.Liquidate(self.symbols[5])
        '''
        '''
        
        self.Plot("Trade Plot", "MinusSMA1", self.sma3.Current.Value)
        self.Plot("Trade Plot", "lavern3", lavern3)
        '''
    def OnEndOfDay(self):
        '''
        USD/CAD
        CAD/SGD
        
        EUR/GBP    0.05    -0.92    -0.91    -0.80    -0.91    -0.73    -0.46
        GBP/CHF    -0.43    -0.87    -0.92    -0.69    -0.92    -0.94    -0.69
        
        EUR/NZD    -0.59    -0.85    -0.88    -0.83    -0.67    -0.80    -0.69
        NZD/USD    -0.59    -0.85    -0.88    -0.83    -0.67    -0.80    -0.69
        self.Plot("Trade Plot", "USDCAD", self.Securities["USDCAD"].Price)
        #self.Plot("Trade Plot", "GBPAUD", self.Securities["GBPAUD"].Price)
        #if data[self._spy] is None: return
        #value = data[self.symbols[0]].Value
        '''