Need help creating "Awesome Oscillator"

I'm trying to create an alg that trades a specific stock based on the equation below. The problem I'm running into is doing the math of the high and low price over X Period.

AO = SMA(High+Low)/2, 5 Periods) - SMA(High+Low/2, 34 Periods)

• When AO crosses above the Zero Line, short term momentum is now rising faster than the long term momentum. This can present a bullish buying opportunity.
• When AO crosses below the Zero Line, short term momentum is now falling faster then the long term momentum. This can present a bearish selling opportunity.

Can anyone offer some help?

5 responses

Are you looking for the frequency to be daily or minutely. That is, should the prices be over 5 and 34 days OR 5 and 34 minutes?

Also, if daily, should the current days prices be included in the averages (this may make it more complicated if so).

I should have specified, sorry. I'd like the frequency to be in minutes. Thanks for your help!

Maybe something like this? Also see the attached notebook.


# Add 1 to the LONG_SMA to get the current and previous bar values
my_data = data.history(context.security, ['high', 'low'], LONG_SMA+1, '1m')
high_low_mean_short = (my_data.high.rolling(SHORT_SMA).mean() -
my_data.low.rolling(SHORT_SMA).mean())/2
high_low_mean_long = (my_data.high.rolling(LONG_SMA).mean() -
my_data.low.rolling(LONG_SMA).mean())/2
ao = high_low_mean_short - high_low_mean_long

2
Notebook previews are currently unavailable.

Here is the code in an algorithm...

9
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
 Returns 1 Month 3 Month 6 Month 12 Month
 Alpha 1 Month 3 Month 6 Month 12 Month
 Beta 1 Month 3 Month 6 Month 12 Month
 Sharpe 1 Month 3 Month 6 Month 12 Month
 Sortino 1 Month 3 Month 6 Month 12 Month
 Volatility 1 Month 3 Month 6 Month 12 Month
 Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 599739136bef0b5108746b93
There was a runtime error.

Here is another version of Bill Williams Awesome Oscillator utilizing talib.MEDPRICE function

import talib
# -------------------
stock = symbol('SPY')
fast, slow = 5, 34
# -------------------
def initialize(context):
pass

def handle_data(context, data):

H = data.history(stock, 'high', slow , '1m').bfill()
L = data.history(stock, 'low', slow , '1m').bfill()

median_prices = talib.MEDPRICE(H, L)
mavg_fast = median_prices[-(fast + 1):].mean()
mavg_slow = median_prices[-(slow + 1):].mean()

AO = mavg_fast - mavg_slow # Awesome_Oscillator
print AO
record(Awesome_Oscillator = AO)