Overall Statistics
Total Trades
1
Average Win
0%
Average Loss
0%
Compounding Annual Return
-6.313%
Drawdown
13.100%
Expectancy
0
Net Profit
-5.823%
Sharpe Ratio
-0.631
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
-0.135
Beta
5.334
Annual Standard Deviation
0.078
Annual Variance
0.006
Information Ratio
-0.836
Tracking Error
0.078
Treynor Ratio
-0.009
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
from decimal 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(2018, 1, 1)
        self.SetEndDate(2018, 12, 1)
        self.SetCash(100000)
        # import the custom data 
        self.Forex = self.AddForex("AUDUSD", Resolution.Daily, Market.Oanda)
        self.Forex = self.AddForex("USDSGD", Resolution.Daily, Market.Oanda)
        
        #self.Data = self.AddData(Quandl, "BCIW/_DXY", Resolution.Daily)
        #self.dxy_sma = SimpleMovingAverage(25)
        # initialize the indicator with the history request
        #dxy_smaHistory = self.History("BCIW/_DXY", 25*10, Resolution.Daily)
        #audsgdHistory = self.History(("AUDUSD", "USDSGD"), 25*10, Resolution.Daily)
        
        '''for tuple in dxy_smaHistory.loc["BCIW/_DXY"].itertuples():
           self.dxy_sma.Update(tuple.Index, tuple.value)'''
        
        self.audsgd_sma = SimpleMovingAverage(25)
        prices = self.History(["AUDUSD", "USDSGD"], 200, Resolution.Daily)
        self.Debug(str(prices))
        
        self.Debug(str(self.audsgd_sma.Current.Value))
        self.Debug(str(self.History))
        
    def OnData(self, data):
        if not self.Portfolio.Invested:
            self.SetHoldings("AUDUSD", 1)  
        '''
        prices = whathist.close.unstack(level=0).dropna()
        hist_20days = hist[-20:]
        price = (hist_20days["AUDUSD"] - hist_20days["USDSGD"]).dropna()
        for index, value in price.items():
            self.audsgd_sma.Update(index, value) 
        
        
         
        # create the SMA of 2 correlated pairs = aud price - sgd price
        self.SpreadSMA = SimpleMovingAverage(20)
        hist = self.History(["AUDUSD", "USDSGD"], 400, Resolution.Daily)["value"].unstack(level=0).dropna()
        hist_20days = hist[-20:]
        price = (hist_20days["AUDUSD"] - hist_20days["USDSGD"]).dropna()
        for index, value in price.items():
            self.priceSMA.Update(index, value) 
       
        
          # Add the spread plot and mark the long/short spread point
        spreadPlot = Chart("Spread Plot")
        spreadPlot.AddSeries(Series("Spread", SeriesType.Line, 0))
        spreadPlot.AddSeries(Series("Long Spread Trade", SeriesType.Scatter, 0))
        spreadPlot.AddSeries(Series("Short Spread Trade", SeriesType.Scatter, 0))
        self.AddChart(spreadPlot)'''
        
        
    '''
    def OnData(self, data):
        if not (data.ContainsKey("WTI") and data.ContainsKey("BRENT")): return
        self.Plot("Spread Plot", "Spread", data["WTI"].Price - data["BRENT"].Price)
        
        self.SpreadSMA.Update(self.Time, data["WTI"].Price - data["BRENT"].Price) 
        if not self.SpreadSMA.IsReady: return
        spread = self.Securities["WTI"].Price - self.Securities["BRENT"].Price 
        fair_value =self.Securities["WTI"].Price - Decimal(self.regr.predict(self.Securities["WTI"].Price)[0])      
        
        if spread > self.SpreadSMA.Current.Value and not (self.Portfolio["WTI"].IsShort and self.Portfolio["BRENT"].IsLong):
            self.SetHoldings("WTI", -0.5)
            self.SetHoldings("BRENT", 0.5)
            self.Plot("Spread Plot", "Long Spread Trade", data["WTI"].Price - data["BRENT"].Price)


        elif spread < self.SpreadSMA.Current.Value and not (self.Portfolio["WTI"].IsLong and self.Portfolio["BRENT"].IsShort):
            self.SetHoldings("WTI", 0.5)
            self.SetHoldings("BRENT", -0.5)
            self.Plot("Spread Plot", "Short Spread Trade", data["WTI"].Price - data["BRENT"].Price)
            
        if self.Portfolio["WTI"].IsShort and self.Portfolio["BRENT"].IsLong and spread < fair_value:
            self.Liquidate()

        
        if self.Portfolio["WTI"].IsLong and self.Portfolio["BRENT"].IsShort and spread > fair_value:
            self.Liquidate()  '''      
        
    '''    
class WTI(PythonData):
    "Class to import WTI Spot Price(Dollars per Barrel) data from Dropbox"
    
    def GetSource(self, config, date, isLiveMode):
        return SubscriptionDataSource("https://www.dropbox.com/s/jpie3z6j0stp97d/wti-crude-oil-prices-10-year-daily.csv?dl=1", SubscriptionTransportMedium.RemoteFile)

    def Reader(self, config, line, date, isLiveMode):
        if not (line.strip() and line[1].isdigit()): return None
        index = WTI()
        index.Symbol = config.Symbol
        try:
            # Example File Format: (Data starts from 08/11/2008)
            # date     value
            # 8/11/08    114.44
            data = line.split(',')
            index.Time = datetime.strptime(data[0], "%Y-%m-%d")
            index.Value = Decimal(data[1])
        except:
            return None
            
        return index'''