### Introduction

The WTI-Brent spread is the difference between the prices of two types of crude oil: West Texas Intermediate (WTI) on the long side and Brent Crude (Brent) on the short side. For years, the price difference between the two has only been a few dollars on average. As both oils are very similar, their spread shows signs of strong predictability and usually oscillates around some average value. Therefore, it is possible to use deviations from the fair spread value to bet on convergence back to fair value. Here we present a trading strategy based on the price deviations of the spread.

### Method

We use the WTI and Brent crude oil price their CFDs traded in Oanda exchange.

```
def Initialize(self):
# import the spot price data
self.wti = self.AddCfd("WTICOUSD", Resolution.Daily, Market.Oanda).Symbol
self.b = self.AddCfd("BCOUSD", Resolution.Daily, Market.Oanda).Symbol
```

The spread is defined as the difference between WTI price and Brent price. Next, we need to calculate the moving average of the spread series using the the SimpleMovingAverage indicator. As the indicator uses the price difference instead of the price series, we need to manually initialize the indicator with the history request.

```
self.SpreadSMA = SimpleMovingAverage(20)
hist = self.History([self.wti, self.b], 252, Resolution.Daily).unstack(level=0)['close'].ffill().dropna()
wti_hist = hist[self.wti.ID.ToString()]
b_hist = hist[self.b.ID.ToString()]
spread = wti_hist - b_hist
for index, value in spread.iloc[-20:].items():
self.SpreadSMA.Update(index, value)
```

To get the fair value of the spread, we perform the linear regression between WTI and Brent price over the last one year history price.

Then the fair value of the spread is

```
# linear regression to decide the fair value
self.regr = linear_model.LinearRegression()
self.regr.fit(wti_hist.values.reshape(-1, 1), b_hist.values.reshape(-1, 1))
```

The fair value is calculated every day. If the current spread value is above SMA 20, then we enter a short position in the spread on close (betting that the spread will decrease to fair value represented by SMA 20). The trade is closed at the close of the trading day when the spread crosses below fair value. If the current spread value is below SMA 20 then we enter a long position betting that the spread will increase and the trade is closed at the close of the trading day when the spread crosses above fair value.

```
def OnData(self, data):
if not (data.ContainsKey(self.wti) and data.ContainsKey(self.b)):
return
spread = data[self.wti].Price - data[self.b].Price
self.Plot("Spread Plot", "Spread", spread)
self.SpreadSMA.Update(self.Time, spread)
fair_value = data[self.wti].Price - self.regr.predict(np.array([data[self.wti].Price]).reshape(1, -1))[0]
if spread > self.SpreadSMA.Current.Value and not (self.Portfolio[self.wti].IsShort and self.Portfolio[self.b].IsLong):
self.Log("spread > self.SpreadSMA.Current.Value")
self.SetHoldings(self.wti, -0.5)
self.SetHoldings(self.b, 0.5)
self.Plot("Spread Plot", "Long Spread Trade", spread)
elif spread < self.SpreadSMA.Current.Value and not (self.Portfolio[self.wti].IsLong and self.Portfolio[self.b].IsShort):
self.Log("spread < self.SpreadSMA.Current.Value")
self.SetHoldings(self.wti, 0.5)
self.SetHoldings(self.b, -0.5)
self.Plot("Spread Plot", "Short Spread Trade", spread)
if self.Portfolio[self.wti].IsShort and self.Portfolio[self.b].IsLong and spread < fair_value:
self.Liquidate()
if self.Portfolio[self.wti].IsLong and self.Portfolio[self.b].IsShort and spread > fair_value:
self.Liquidate()
```

We also set up a scheduled linear regression model re-training every month start.

```
def Initialize(self):
# Recalibrate model every month
self.Train(self.DateRules.MonthStart(), self.TimeRules.At(0,0), self.MyTrainingMethod)
def MyTrainingMethod(self):
hist = self.History([self.wti, self.b], 252, Resolution.Daily).unstack(level=0)['close'].ffill().dropna()
wti_hist = hist[self.wti.ID.ToString()]
b_hist = hist[self.b.ID.ToString()]
# linear regression to decide the fair value
self.regr.fit(wti_hist.values.reshape(-1, 1), b_hist.values.reshape(-1, 1))
```

To demonstrate the trend of the spread series, we add the spread plot and mark the spread long/short point on the spread curve.

```
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)
```