Overall Statistics Total Trades118Average Win0.57%Average Loss-0.16%Compounding Annual Return-7.668%Drawdown70.400%Expectancy0.342Net Profit-29.217%Sharpe Ratio-0.106Loss Rate71%Win Rate29%Profit-Loss Ratio3.59Alpha0.598Beta-32.245Annual Standard Deviation0.303Annual Variance0.092Information Ratio-0.171Tracking Error0.303Treynor Ratio0.001Total 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

def Initialize(self):
self.SetStartDate(2014, 1, 1)
self.SetEndDate(2018, 5, 1)
self.SetCash(1000000)
# import the custom data
# create the moving average indicator of the pread = WTI price - BRENT price
hist = self.History(["WTI", "BRENT"], 400, Resolution.Daily)["value"].unstack(level=0).dropna()
hist_20days = hist[-20:]
# linear regression to decide the fair value
hist_one_year = hist[-252:]
X = hist_one_year["WTI"][:, np.newaxis]
y = hist_one_year["BRENT"]
self.regr = linear_model.LinearRegression()
self.regr.fit(X, y)

def OnData(self, data):
if not (data.ContainsKey("WTI") and data.ContainsKey("BRENT")): return

fair_value =self.Securities["WTI"].Price - Decimal(self.regr.predict(self.Securities["WTI"].Price)[0])

self.SetHoldings("WTI", -0.5)
self.SetHoldings("BRENT", 0.5)

self.SetHoldings("WTI", 0.5)
self.SetHoldings("BRENT", -0.5)

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

class BRENT(PythonData):
"Class to import BRENT Spot Price(Dollars per Barrel) data from Dropbox"

def GetSource(self, config, date, isLiveMode):
return SubscriptionDataSource("https://www.dropbox.com/s/w380c4n7xjmdqxl/brent-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 = BRENT()
index.Symbol = config.Symbol
try:
# Example File Format: (Data starts from 08/11/2008)
# date     value
# 8/11/08    110.54
data = line.split(',')
index.Time = datetime.strptime(data[0], "%Y-%m-%d")
index.Value = Decimal(data[1])
except:
return None

return index```