예제 #1
0
def pd_ema(arg, ratio=0.1):
    ''' EMA, implemented with `pandas.stats.moments.ewma` '''
    span = 2.0 / (1-ratio) - 1
    arg = utils.safe_series(arg)
    rval = ewma(arg, span=span)
    utils.safe_name(rval, name='pdEMA')
    return rval
예제 #2
0
def wilder_sum(arg, window=14):
    ''' An internal function of R TTR package '''
    arg = utils.safe_series(arg)
    rval = fast.wilder_sum(arg, window)
    rval.name = arg.name
    utils.safe_name(rval, name='wilderSum')
    rval.index = arg.index
    
    return rval
예제 #3
0
def ema(arg, window=10, ratio=None, wilder=False):
    ''' EMA, implemented in Cython '''
    if ratio is None:
        if wilder:
            ratio = 1.0 / window
        else:
            ratio = 2.0 / (window+1)
    rval = copy.copy(np.asarray(arg))
    rval = pd.Series(fast.ema(rval, ratio))
    utils.safe_name(rval, name='EMA')
    return rval
예제 #4
0
def py_ema(arg, window=10, ratio=None, wilder=False):
    ''' EMA, implemented in pure Python '''
    if ratio is None:
        if wilder:
            ratio = 1.0 / window
        else:
            ratio = 1.0 / (2*window+1)
    rval = pd.Series(copy.copy(arg))
    for i in xrange(1, len(rval)):
        rval[i] = rval[i-1] + ratio * (arg[i] - rval[i-1])
    utils.safe_name(rval, name='pyEMA')
    return rval
예제 #5
0
def obv(price, volume):
    '''OBV'''
    price = utils.safe_series(price)
    volume = utils.safe_series(volume)
    obv = (roc(price) > 0).astype(int) * volume - (roc(price) <= 0).astype(int) * volume
    obv[0:1] = volume[0:1]
    obv = obv.cumsum()
    
    rval = obv
    utils.safe_name(rval, name='OBV')
    rval.index = price.index
    
    return rval
예제 #6
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    
예제 #7
0
def roc(arg, window=1, type_='continuous'):
    arg = utils.safe_series(arg)
    arg = arg.astype(float)
    if type_ == 'continuous':
        rval = np.log(arg) - np.log(arg.shift(window))
    elif type_ == 'discrete':
        rval = arg / arg.shift(window) - 1
    else:
        raise NotImplementedError()
    
    rval.name = arg.name
    utils.safe_name(rval, name='ROC')
    rval.index = arg.index
    
    return rval
예제 #8
0
def dpo(arg, window, ma_type='sma', shift=None, percent=False):
    if shift is None:
        shift = window / 2 + 1
        
    arg = utils.safe_series(arg)
    ma_fn = eval('ma.%s'%ma_type)
    arg_mean = ma_fn(arg, window=window)
    arg_mean = arg_mean.shift(-shift)
    
    if percent:
        rval = 100 * (arg/arg_mean - 1)
    else:
        rval = arg - arg_mean
    
    rval.name = arg.name
    utils.safe_name(rval, name='DPO')
    rval.index = arg.index
    
    return rval
예제 #9
0
def rsi(arg, window=14, ma_type='ema'):
    ''' Relative strength index '''

    arg = utils.safe_series(arg)
    ma_fn = eval('ma.%s'%ma_type)

    last = arg.shift(1).fillna(0)
    diff = arg - last

    up = diff * (diff > 0)
    down = diff * (diff < 0) * -1

    rs = ma_fn(up, window=window) / ma_fn(down, window=window)
    rval = 100 - 100 / (1+rs)

    rval.name = arg.name
    utils.safe_name(rval, name='RSI')
    rval.index = arg.index

    return rval
예제 #10
0
def sma(arg, window=10):
    ''' Simple Mean Average '''
    arg = utils.safe_series(arg)
    rval = pd.rolling_mean(arg, window)
    utils.safe_name(rval, name='SMA')
    return rval