Example #1
0
def calc_trailing_stop_along_price(df):
    stop_loss_bd = pd.concat([
        gfc.rolling_extend(max, df['High']),
        gfc.rolling_extend(min, df['Low'])
    ],
                             axis=1)
    stop_loss_bd.columns = ['High', 'Low']
    stop_loss_bd['High'] = stop_loss_bd['High'] - df['eor'] - 0.01
    stop_loss_bd['Low'] = stop_loss_bd['Low'] + df['eor'] + 0.01
    stop_loss_bd.ix[stop_loss_bd['High'] < df['lb'] - 0.01,
                    'High'] = df['lb'] - 0.01
    stop_loss_bd.ix[stop_loss_bd['Low'] > df['ub'] + 0.01,
                    'Low'] = df['ub'] + 0.01
    stop_loss_bd.rename(columns={'High': 1, 'Low': -1}, inplace=True)
    return stop_loss_bd
Example #2
0
def calc_sortino(price, mar_th, win=None, forward=None):
    """Summary
    Calculate Sortino Ratio in vectorized way

    Args:
        price (TYPE): Description
        mar_th (float): threshold of minimum accepted return
        win (int, optional): Description
        forward (bool, optional): Description

    Returns:
        numpy.array: Description

    Raises:
        ValueError: Description
    """
    def __sortino(x):
        return gperf.sortino(x, mar_th)

    rtns = np.divide(price[1:], price[:-1]) - 1
    if mar_th == 'avg':
        mar_th = np.mean(rtns)
    if bool(win) != bool(forward):
        if win:
            res = gfc.rolling_apply(__sortino, rtns, win)
            res[np.isinf(res)] = np.nan
            res = gfc.pandas_fill(res, method='ffill')
        elif forward:
            res = gfc.rolling_extend(__sortino, rtns)
    elif not any([None, None]):
        res = gperf.sortino(rtns, mar_th)
    else:
        raise ValueError('Can only assign value to either win or forward.')

    return res
Example #3
0
def calc_cmo(price, win='forward'):
    """Summary
    Calculcate Chande Momentum Oscillator in vectorized way

    Args:
        price (TYPE): Description
        win (None, int): Description

    Returns:
        numpy.array:

    Raises:
        ValueError: Description
    """
    def cmo(ud):
        SoU = sum(ud > 0)
        SoD = sum(ud < 0)
        if SoU + SoD == 0:
            return 0
        return 100 * float(SoU - SoD) / float(SoU + SoD)

    pdiff = np.diff(price)

    if isinstance(win, int):
        res = gfc.rolling_apply(cmo, pdiff, win)
    elif win.lower() == 'forward':
        res = gfc.rolling_extend(cmo, pdiff)
    else:
        res = cmo(pdiff)

    return res
Example #4
0
def drawdown(arr):
    """Drawdown

    input must be position integer or float

    Arguments:
        arr {[type]} -- [description]

    Returns:
        [type] -- [description]
    """
    rolmax = gfc.rolling_extend(np.nanmax, arr, forward=True)
    return abs(1 - arr / rolmax)