The Pendulum Indicator for Range Trading – a Python study

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

Developing & Back-testing a New Indicator Using Range and Volatility.

Image for post
www.pxfuel.com

The Pendulum Indicator

Image for post
EURUSD in the first panel with the 13-period Pendulum Indicator in the second panel. (Image by Author)
def pendulum_indicator(Data, lookback, lookback_ma, what, where):

# Price Range
for i in range(len(Data)):

Data[i, where] = Data[i, what] – Data[i – lookback, what]

# Volatility
Data = volatility(Data, lookback, what, where + 1)

# Pendulum Ratio
Data[:, where + 2] = Data[:, where] / Data[:, where + 1]

# Pendulum Indicator
Data = ma(Data, lookback_ma, where + 2, where + 3)

# Removing Excess Columns
Data = deleter(Data, where, 3)

return Data

# 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

# Moving average function
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

# Volatility (Standard Deviation) function
def volatility(Data, lookback, what, where):

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

except IndexError:
pass

return Data

Image for post
USDCHF in the first panel with the 13-period Pendulum Indicator in the second panel. (Image by Author)

Back-testing the Strategy

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
Signal chart on the AUDCAD following the Pendulum strategy. (Image by Author)
Image for post
Example of an equity curve on the AUDCAD. (Image by Author)
Image for post
Example of an equity curve on the EURUSD. (Image by Author)
Image for post
Signal chart on the GBPUSD following the Pendulum 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