Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
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.

https://www.tradingview.com/wiki/Awesome_Oscillator_(AO)

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  

Here is the code in an algorithm...

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:].mean()  
    mavg_slow = median_prices[-slow:].mean()

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