Using Exponential Smoothing in Algorithmic Trading

mediumThis post was originally published by Sofien Kaabar at Medium [AI]

Creating an Exponential Smoothing Indicator to Trade the Market in Python.

Image for post
www.pxfuel.com

The Concept of Moving Averages

Image for post
EURUSD with its 200-period Simple moving average. (Image by Author)
Image for post
def ma(Data, lookback, what, where):

for i in range(len(Data)):
try:
Data[i, where] = (Data[i – lookback + 1:i + 1, what].mean())

except IndexError:
pass
return Data

Image for post
EURUSD with its 200-period Exponential moving average. (Image by Author)
def ema(Data, alpha, lookback, what, where):

# alpha is the smoothing factor
# window is the lookback period
# what is the column that needs to have its average calculated
# where is where to put the exponential moving average

alpha = alpha / (lookback + 1.0)
beta = 1 – alpha

# First value is a simple SMA
Data = ma(Data, lookback, what, where)

# Calculating first EMA
Data[lookback + 1, where] = (Data[lookback + 1, what] * alpha) + (Data[lookback, where] * beta)

    # Calculating the rest of EMA
    for i in range(lookback + 2, len(Data)):
            try:
                Data[i, where] = (Data[i, what] * alpha) + (Data[i - 1, where] * beta)

except IndexError:
pass
return Data

The Stochastic Smoothing Oscillator

Image for post
def stochastic(Data, lookback, what, high, low, where):

for i in range(len(Data)):

try:
Data[i, where] = (Data[i, what] – min(Data[i – lookback + 1:i + 1, low])) / (max(Data[i – lookback + 1:i + 1, high]) – min(Data[i – lookback + 1:i + 1, low]))

except ValueError:
pass

Data[:, where] = Data[:, where] * 100 return Data

# The Data variable refers to the OHLC array
# The lookback variable refers to the period (5, 14, 21, etc.)
# The what variable refers to the closing price
# The high variable refers to the high price
# The low variable refers to the low price
# The where variable refers to where to put the Oscillator
Image for post
EURUSD hourly data in the first panel with the 14-period Stochastic Oscillator. (Image by Author)
Image for post
EURUSD hourly data in the first panel with the 14-period Stochastic Smoothing Oscillator. (Image by Author)
def stochastic_smoothing_oscillator(Data, high, low, close, lookback, where):

Data = ema(Data, 2, lookback, high, where)
Data = ema(Data, 2, lookback, low, where + 1)
Data = ema(Data, 2, lookback, close, where + 2)

for i in range(len(Data)):

try:
Data[i, where + 3] = (Data[i, where + 2] – min(Data[i – lookback + 1:i + 1, where + 1])) / (max(Data[i – lookback + 1:i + 1, where]) – min(Data[i – lookback + 1:i + 1, where + 1]))

except ValueError:
pass

Data[:, where + 3] = Data[:, where + 3] * 100
Data = deleter(Data, where, 3)

Data = jump(Data, lookback)

return Data

Image for post
USDCHF hourly data in the first panel with the 14-period Stochastic Smoothing Oscillator. (Image by Author)
# To add a number of columns
def adder(Data, times):

for i in range(1, times + 1):

z = np.zeros((len(Data), 1), dtype = float)
Data = np.append(Data, z, axis = 1) return Data

# To delete a number of columns starting from an index
def deleter(Data, index, times):

for i in range(1, times + 1):

Data = np.delete(Data, index, axis = 1) return Data

# To delete a number of rows
def jump(Data, jump):

Data = Data[jump:, ]

return Data

Image for post
Comparison between the Stochastic Oscillator and the Stochastic Smoothing Oscillator. (Image by Author)

Strategy Back-test

Image for post
Signal chart on the EURUSD. (Image by Author)
def signal(Data, what, buy, sell):

for i in range(len(Data)):

if Data[i, what] < lower_barrier and Data[i – 1, what] > lower_barrier and Data[i – 2, what] > lower_barrier :
Data[i, buy] = 1

if Data[i, what] > upper_barrier and Data[i – 1, what] < upper_barrier and Data[i – 2, what] < upper_barrier :
Data[i, sell] = -1

Image for post
Performance Summary Table. (Image by Author)
Image for post
Equity curves following the Stochastic Smoothing Oscillator strategy. (Image by Author)

A Word on Risk Management

Image for post
EURUSD hourly data with the 50-period Exponential Average True Range. (Image by Author).

Conclusion

Spread the word

This post was originally published by Sofien Kaabar at Medium [AI]

Related posts