Mastering Moving Average Zone Strategy in Python
Written on
Understanding the Moving Average Zone Strategy
Contrarian trading can be effectively executed using moving averages, particularly when the market approaches these indicators. This method provides a means to identify dynamic support and resistance levels. Grasping the mechanics of the market is crucial for selecting the appropriate moving average.
The Importance of Moving Averages
Moving averages are essential for confirming trends and riding the momentum. Renowned as one of the most fundamental technical indicators, their simplicity and proven effectiveness make them invaluable for analysis. Traders utilize them to pinpoint support and resistance levels, establish stop-loss points, and identify targets, thereby enhancing their trading toolkit.
For example, consider the hourly values of the EUR/USD paired with its 55-period Simple Moving Average. The simple moving average, as the name implies, is merely the average of a set of observations and is widely used in statistics and various fields of life. It can be mathematically expressed as:
# Simple Moving Average Calculation in Python
def add_column(data, times):
for i in range(1, times + 1):
new = np.zeros((len(data), 1), dtype=float)
data = np.append(data, new, axis=1)
return data
def delete_column(data, index, times):
for i in range(1, times + 1):
data = np.delete(data, index, axis=1)return data
def delete_row(data, number):
data = data[number:, ]
return data
def ma(data, lookback, close, position):
data = add_column(data, 1)
for i in range(len(data)):
try:
data[i, position] = (data[i - lookback + 1:i + 1, close].mean())except IndexError:
passdata = delete_row(data, lookback)
return data
Next, we will delve into coding the K's Envelopes and constructing the signal function to comprehend the triggers they generate.
Powerful Entry Strategy Using One Moving Average - YouTube
The video explains a powerful entry strategy utilizing a single moving average, highlighting its effectiveness in trading.
The K's Envelopes Trading Method
I must admit that K's Envelopes is one of the simplest indicators I have encountered. It comprises two simple moving averages: one applied to the highs and the other to the lows. This setup forms support and resistance zones, helping traders identify optimal entry points. The K's Envelopes strategy underpins many of my trading signals.
After optimization, the envelopes are constructed using two 800-period moving averages:
- One for the highs.
- One for the lows.
Together, these create a dynamic support and resistance zone, depending on the price's position relative to the envelopes.
For instance, here’s how to implement K's Envelopes in Python:
def k_envelopes(data, high, low, lookback, where):
data = add_column(data, 2)
data = ma(data, lookback, high, where)
data = ma(data, lookback, low, where + 1)
return data
The data variable corresponds to the OHLC array, while the lookback variable should be set to 800. The high variable indicates the column for high prices, the low variable for low prices, and the where variable specifies where to place the newly calculated moving average.
Analyzing the Trading Strategy
The initial step in evaluating this strategy is to establish the trading rules. We need to define when the system will execute a buy or sell order. The conditions can be set as follows:
- Go long (buy) when the market enters the envelopes from above.
- Go short (sell) when the market enters the envelopes from below.
The accompanying chart for the EUR/GBP illustrates the signals generated from this strategy.
The Ultimate Step-by-Step Moving Average Trading Guide (Full Training) - YouTube
This video offers a comprehensive step-by-step guide to moving average trading, providing insights into effective strategies.
The chart above showcases the signals produced by the system. It's essential to consider the frequency of signals when developing a trading algorithm. The signal function used to trigger these actions is outlined below:
def signal(data, close, lower_k_envelope, upper_k_envelope, buy, sell):
for i in range(len(data)):
if (data[i, close] > data[i, lower_k_envelope] and
data[i, close] < data[i, upper_k_envelope] and
data[i - 1, close] > data[i - 1, upper_k_envelope] and
data[i - 3, close] > data[i - 3, upper_k_envelope]):
data[i, buy] = 1
if (data[i, close] > data[i, lower_k_envelope] and
data[i, close] < data[i, upper_k_envelope] and
data[i - 1, close] < data[i - 1, lower_k_envelope] and
data[i - 3, close] < data[i - 3, lower_k_envelope]):
data[i, sell] = -1
return data
For those interested in learning how to create various algorithms, I highly recommend visiting Lumiwealth. They offer detailed hands-on courses covering algorithmic trading, blockchain, and machine learning, which are incredibly beneficial.