Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Market Bias Indicator

Just wanted to share this indicator I've been working on in case anyone sees any value. The basic idea is to determine trend while reducing the false positives in moving averages. So instead of caring about whether a price is above or below a MA, it looks at the relationship between the mean of the price for n periods vs the mean of the MA for the same period.

def get_bias(ma,pc,context):  
    ma = ma[-context.bias_lookback:]# array of moving average values  
    pc = pc[-context.bias_lookback:]# array of price close values  
    ma_mean = np.mean(ma)# mean of moving averages  
    pc_mean = np.mean(pc)# mean of price values  
    if ma_mean > pc_mean:# determines down bias and gets strength  
        strength = ma_mean - pc_mean  
        return 2,strength #short     

    if pc_mean > ma_mean:# determines up bias and gets strength  
        strength = pc_mean - ma_mean  
        return 1,strength #long  

By returning the strength of the bias (up or down), we can place long and short trades for the given bias and adjust the shares according to the strength. So if the long bias is low, then we only risk a few shares and vice versa for a strong bias.

5 responses

things to consider

Interesting, thanks garyha. I suspect this may have more of an intraday application - unfortunately that's hard to test out in Quantopian

@garyha... It stop working....

def handle_data(context, data):
----> prices_close = history(context.lookback, context.charttype, 'close_price')
prices_close = list(prices_close.values.flatten())
-----> prices = history(context.lookback, context.charttype, 'price')[context.stock]
prices = history(context.lookback, context.charttype, 'price')[context.stock]

Vlademir,

Here is slightly modified Gary version for Q2

thanks Vladimir.... having alot of difficulty in using this new api... and codes... which are not working anymore... its not backward compatible.... with codes that have work in the the pass....