Esempio n. 1
0
def stoch(hlc, n_fastK=14, n_fastD=3, n_slowD=3, ma_type='sma', bounded=True, smooth=1):
    ''' Stochastic Oscillator '''
    high, low, close = utils.safe_hlc(hlc)
    
    if bounded:
        hmax = pd.rolling_max(high, n_fastK) 
        lmin = pd.rolling_min(low, n_fastK)
    else:
        raise NotImplementedError()
    
    num = close - lmin
    den = hmax - lmin
    
    mafunc = ma.get_ma(ma_type)
    num_ma = mafunc(num, smooth)
    den_ma = mafunc(den, smooth)
    
    fastK = num_ma / den_ma
    fastK[np.isnan(fastK)] = 0.5
    fastD = mafunc(fastK, n_fastD)
    slowD = mafunc(fastD, n_slowD)
    
    return pd.DataFrame(dict(fastK=fastK, 
                             fastD=fastD, 
                             slowD=slowD), 
                        index=hlc.index)
Esempio n. 2
0
def emv(hl, volume, window=9, ma_type='sma', vol_divisor=1000):
    '''EMV'''
    high, low = utils.safe_hl(hl)
    volume = utils.safe_series(volume)
    mid = .5 * (high + low)
    volume /= vol_divisor
    rval = (mid - mid.shift(1)) / (volume / (high - low))
    rval_ma = ma.get_ma(ma_type)(rval, window)
    return pd.DataFrame(dict(emv=rval, maEMV=rval_ma), index=hl.index)
Esempio n. 3
0
def trix(price, window=20, n_sig=9, ma_type='ema', percent=True):
    ''' Triple Smoothed Exponential Oscillator '''
    warnings.warn('The parameter of n_sig is not used in TTR. So currently it is not used as well here for unittest purpose.')
    price = utils.safe_series(price)
    mafunc = ma.get_ma(ma_type)
    mavg0 = mafunc(price, window)
    mavg1 = mafunc(mavg0, window)
    mavg2 = mafunc(mavg1, window)
    if percent:
        trix_ = 100 * roc(mavg2, window=1, type_='discrete')
    else:
        trix_ = mavg2 - mavg2.shift(1)
    signal = mafunc(trix_, window)
    return pd.DataFrame(dict(TRIX=trix_, signal=signal), index=price.index)
Esempio n. 4
0
def bbands(hlc, window=20, ma_type='sma', sd=2, **kwargs):
    ''' Bolling Bands '''
    high, low, close = utils.safe_hlc(hlc)
    price = (high + low + close) / 3
    ma_fn = ma.get_ma(ma_type)
    mean = ma_fn(price, window, **kwargs)
    sdev = utils.biased_rolling_std(price, window=window)
    
    up = mean + sd * sdev
    down = mean - sd * sdev
    pctB = (price - down) / (up - down)
    
    return pd.DataFrame(dict(dn=down, mavg=mean, up=up, pctB=pctB),
                        index=hlc.index)
Esempio n. 5
0
def cci(hlc, window=20, ma_type='sma', c=0.015, **kwargs):
    ''' Commodity Channel Index (CCI) 
    
    :returns: pandas.Series
    '''
    
    high, low, close = utils.safe_hlc(hlc)
    ma_fn = ma.get_ma(ma_type)
    
    # true price
    tp = (high + low + close) / 3
    tp_mean = ma_fn(tp, window)
    tp_md = utils.rolling_mean_dev(tp, window)
    cci_ = (tp - tp_mean) / c / tp_md
    cci_.name = 'CCI'
    
    return cci_
Esempio n. 6
0
def smi(hlc, window=13, n_fast=2, n_slow=25, n_sig=9, ma_type='sma', bounded=True):
    ''' Stochastic Momentum Index '''
    high, low, close = utils.safe_hlc(hlc)
    
    if bounded:
        hmax = pd.rolling_max(high, window) 
        lmin = pd.rolling_min(low, window)
    else:
        raise NotImplementedError()
    
    hl_diff = hmax - lmin
    c_diff = close - (hmax+lmin) / 2
    
    mafunc = ma.get_ma(ma_type)
    
    num0 = mafunc(c_diff, n_slow)
    den0 = mafunc(hl_diff, n_slow)
    num1 = mafunc(num0, n_fast)
    den1 = mafunc(den0, n_fast)
    
    smi_ = 100 * (num1 / den1 * 2)
    signal = mafunc(smi_, n_sig)
    
    return pd.DataFrame(dict(SMI=smi_, signal=signal), index=hlc.index)