예제 #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)
예제 #2
0
def atr(hlc, window=14):
    ''' ATR '''
    high, low, close = utils.safe_hlc(hlc)
    close_tm1 = close.shift(1)
    true_high = (high>=close_tm1)*high + (high<close_tm1)*close_tm1
    true_low = (low<=close_tm1)*low + (low>close_tm1)*close_tm1
    true_range = true_high - true_low
    atr_ = ma.ema(true_range, window=window)

    return pd.DataFrame(dict(tr=true_range, atr=atr_, 
                             trueHigh=true_high,
                             trueLow=true_low), 
                        index=hlc.index)
예제 #3
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)
예제 #4
0
def mfi(hlc, volume, window=14):
    '''MFI'''
    high, low, close = utils.safe_hlc(hlc)
    volume = utils.safe_series(volume) / 1000
    price = (high+low+close) * 1.0 / 3
    mf = price * volume
    pmf = (mf > mf.shift(1)).astype(int) * mf
    nmf = (mf < mf.shift(1)).astype(int) * mf
    mr = pd.rolling_sum(pmf, window) / pd.rolling_sum(nmf, window)
    
    rval = 100 - (100/(1 + mr))
    utils.safe_name(rval, name='MFI')
    rval.index = hlc.index
    
    return rval    
예제 #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_
예제 #6
0
def adx(hlc, window=14, atr_=None):
    ''' ADX '''
    if atr_ is None: 
        atr_ = atr(hlc, window)
    high, low, _= utils.safe_hlc(hlc)

    up_move = high-high.shift(1)
    down_move = low.shift(1)-low
    
    pos_dm = ((up_move>down_move)*(up_move>0)*(up_move)).astype(np.float)
    neg_dm = ((down_move>up_move)*(down_move>0)*(down_move)).astype(np.float)
    
    tr_sum = fast.wilder_sum(atr_.tr, window)
    
    DIp = 100 * fast.wilder_sum(pos_dm, window) / tr_sum
    DIn = 100 * fast.wilder_sum(neg_dm, window) / tr_sum

    dx = 100 * np.abs((DIp-DIn)/(DIp+DIn))
    adx = ma.ema(dx, window=window)

    return pd.DataFrame(dict(DIp=DIp, DIn=DIn, DX=dx, ADX=adx), index=hlc.index)
예제 #7
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)