The Bull & Bear Power
Creating and the Bull & Bear Power Index in Python
There are many methods to measure who is in control, bulls or bears, and among those methods is Elder Ray’s bull bear power Indicator where it uses simple formulas to approximate this implied technical strength. In this article, we will code the indicator and then judge it from an objective point of view.
Creating the Bull & Bear Power Index Step-by-Step
The Elder Ray Index measures the amount of buying and selling pressure and is composed of two histograms where one is called the Bull Power and the other the Bear Power. The lines are calculated following these formulas:
The EMA variable refers to the exponential moving average which is a type of a moving average that places more weight on recent values. The exponential moving average can be calculated using this below function:
def adder(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 deleter(data, index, times): for i in range(1, times + 1): data = np.delete(data, index, axis = 1) return data
def jump(data, jump): data = data[jump:, ] return data
def ma(data, lookback, close, where): data = adder(data, 1) for i in range(len(data)): try: data[i, where] = (data[i - lookback + 1:i + 1, close].mean()) except IndexError: pass data = jump(data, lookback) return data
def ema(data, alpha, lookback, what, where): alpha = alpha / (lookback + 1.0) beta = 1 - alpha data = ma(data, lookback, what, where) data[lookback + 1, where] = (data[lookback + 1, what] * alpha) + (data[lookback, where] * beta) 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 above shows the GBPUSD hourly data with the 13-period bull bear indicator in the second panel.
def bull_bear_power(data, lookback, high, low, close, where): # Adding the required columns data = adder(data, 3) # Calculating the exponential moving average data = ema(data, 2, lookback, close, where) # Calculating the Bull Power data[:, where + 1] = data[:, high] - data[:, where] # Calculating the Bear Power data[:, where + 2] = data[:, low] - data[:, where] # Deleting initial empty rows data = jump(data, lookback) # Deleting EMA Column data = deleter(data, where, 1) return data
Knowledge must be accessible to everyone. This is why, from now on, a purchase of either one of my new books “Contrarian Trading Strategies in Python” or “Trend Following Strategies in Python” comes with free PDF copies of my first three books (Therefore, purchasing one of the new books gets you 4 books in total). The two new books listed above feature a lot of advanced indicators and strategies with a GitHub page. You can use the below link to purchase one of the two books (Please specify which one and make sure to include your e-mail in the note).
Pay Kaabar using PayPal.Me
Go to paypal.me/sofienkaabar and type in the amount. Since it’s PayPal, it’s easy and secure. Don’t have a PayPal…www.paypal.com
The indicator has an interesting way of analysis. Basically, whenever the bear power is increasing while the bull power is making higher highs, we know that we ought to be bullish. Similarly, whenever the bull power is making lower highs while the bear power is making lower lows, we know that we ought to be bearish.
The indicator is vulnerable to whipsaws however and the back-testing results are not impressive. Therefore, I would rank this indicator in the basic section of technical indicators.
If you want to see how to create all sorts of algorithms yourself, feel free to check out Lumiwealth. From algorithmic trading to blockchain and machine learning, they have hands-on detailed courses that I highly recommend.
Learn Algorithmic Trading with Python Lumiwealth
Learn how to create your own trading algorithms for stocks, options, crypto and more from the experts at Lumiwealth. Click to learn more
To sum up, what I am trying to do is to simply contribute to the world of objective technical analysis which is promoting more transparent techniques and strategies that need to be back-tested before being implemented. This way, technical analysis will get rid of the bad reputation of being subjective and scientifically unfounded.
I recommend you always follow the the below steps whenever you come across a trading technique or strategy:
Have a critical mindset and get rid of any emotions.
Back-test it using real life simulation and conditions.
If you find potential, try optimizing it and running a forward test.
Always include transaction costs and any slippage simulation in your tests.
Always include risk management and position sizing in your tests.
Finally, even after making sure of the above, stay careful and monitor the strategy because market dynamics may shift and make the strategy unprofitable.
Building tech is slow and expensive. Bubble is the most powerful no-code platform for creating digital products. Build better and faster.