Пример #1
0
def chaikin_money_flow(close_data, high_data, low_data, volume, period):
    """
    Chaikin Money Flow.

    Formula:
    CMF = SUM[(((Cn - Ln) - (Hn - Cn)) / (Hn - Ln)) * V] / SUM(Vn)
    """
    catch_errors.check_for_input_len_diff(close_data, high_data, low_data,
                                          volume)
    catch_errors.check_for_period_error(close_data, period)

    close_data = np.array(close_data)
    high_data = np.array(high_data)
    low_data = np.array(low_data)
    volume = np.array(volume)
    cmf = [
        sum((((close_data[idx + 1 - period:idx + 1] -
               low_data[idx + 1 - period:idx + 1]) -
              (high_data[idx + 1 - period:idx + 1] -
               close_data[idx + 1 - period:idx + 1])) /
             (high_data[idx + 1 - period:idx + 1] -
              low_data[idx + 1 - period:idx + 1])) *
            volume[idx + 1 - period:idx + 1]) /
        sum(volume[idx + 1 - period:idx + 1])
        for idx in range(period - 1, len(close_data))
    ]
    cmf = fill_for_noncomputable_vals(close_data, cmf)
    return cmf
Пример #2
0
def money_flow_index(close_data, high_data, low_data, volume, period):
    """
    Money Flow Index.

    Formula:
    MFI = 100 - (100 / (1 + PMF / NMF))
    """
    catch_errors.check_for_input_len_diff(
        close_data, high_data, low_data, volume
        )
    catch_errors.check_for_period_error(close_data, period)

    mf = money_flow(close_data, high_data, low_data, volume)
    tp = typical_price(close_data, high_data, low_data)

    flow = [tp[idx] > tp[idx-1] for idx in range(1, len(tp))]
    pf = [mf[idx] if flow[idx] else 0 for idx in range(0, len(flow))]
    nf = [mf[idx] if not flow[idx] else 0 for idx in range(0, len(flow))]

    pmf = [sum(pf[idx+1-period:idx+1]) for idx in range(period-1, len(pf))]
    nmf = [sum(nf[idx+1-period:idx+1]) for idx in range(period-1, len(nf))]

    # Dividing by 0 is not an issue, it turns the value into NaN which we would
    # want in that case
    with warnings.catch_warnings():
        warnings.simplefilter("ignore", category=RuntimeWarning)
        money_ratio = np.array(pmf) / np.array(nmf)

    mfi = 100 - (100 / (1 + money_ratio))

    mfi = fill_for_noncomputable_vals(close_data, mfi)

    return mfi
Пример #3
0
def avg_helper(close_data, low_data, period):
    catch_errors.check_for_input_len_diff(close_data, low_data)
    catch_errors.check_for_period_error(close_data, period)
    bp = buying_pressure(close_data, low_data)
    tr = true_range(close_data, period)
    avg = [
        sum(bp[idx + 1 - period:idx + 1]) / sum(tr[idx + 1 - period:idx + 1])
        for idx in range(period - 1, len(close_data))
    ]
    avg = fill_for_noncomputable_vals(close_data, avg)
    return avg
Пример #4
0
def conversion_base_line_helper(data, period):
    """
    The only real difference between TenkanSen and KijunSen is the period value
    """
    catch_errors.check_for_period_error(data, period)
    cblh = [(np.max(data[idx + 1 - period:idx + 1]) +
             np.min(data[idx + 1 - period:idx + 1])) / 2
            for idx in range(period - 1, len(data))]

    cblh = fill_for_noncomputable_vals(data, cblh)
    return cblh
Пример #5
0
def momentum(data, period):
    """
    Momentum.

    Formula:
    DATA[i] - DATA[i - period]
    """
    catch_errors.check_for_period_error(data, period)

    momentum = [data[idx] - data[idx+1-period] for idx in range(period-1, len(data))]
    momentum = fill_for_noncomputable_vals(data, momentum)
    return momentum
Пример #6
0
def relative_strength_index(data, period):
    """
    Relative Strength Index.

    Formula:
    RSI = 100 - (100 / 1 + (prevGain/prevLoss))
    """
    catch_errors.check_for_period_error(data, period)

    period = int(period)
    changes = [
        data_tup[1] - data_tup[0] for data_tup in zip(data[::1], data[1::1])
    ]

    filtered_gain = [val < 0 for val in changes]
    gains = [
        0 if filtered_gain[idx] is True else changes[idx]
        for idx in range(0, len(filtered_gain))
    ]

    filtered_loss = [val > 0 for val in changes]
    losses = [
        0 if filtered_loss[idx] is True else abs(changes[idx])
        for idx in range(0, len(filtered_loss))
    ]

    avg_gain = np.mean(gains[:period])
    avg_loss = np.mean(losses[:period])

    rsi = []
    if avg_loss == 0:
        rsi.append(100)
    else:
        rs = avg_gain / avg_loss
        rsi.append(100 - (100 / (1 + rs)))

    for idx in range(1, len(data) - period):
        avg_gain = ((avg_gain * (period - 1) + gains[idx + (period - 1)]) /
                    period)
        avg_loss = ((avg_loss * (period - 1) + losses[idx + (period - 1)]) /
                    period)

        if avg_loss == 0:
            rsi.append(100)
        else:
            rs = avg_gain / avg_loss
            rsi.append(100 - (100 / (1 + rs)))

    rsi = fill_for_noncomputable_vals(data, rsi)

    return rsi
Пример #7
0
def buying_pressure(close_data, low_data):
    """
    Buying Pressure.

    Formula:
    BP = current close - min()
    """
    catch_errors.check_for_input_len_diff(close_data, low_data)
    bp = [
        close_data[idx] - np.min([low_data[idx], close_data[idx - 1]])
        for idx in range(1, len(close_data))
    ]
    bp = fill_for_noncomputable_vals(close_data, bp)
    return bp
Пример #8
0
def rate_of_change(data, period):
    """
    Rate of Change.

    Formula:
    (Close - Close n periods ago) / (Close n periods ago) * 100
    """
    catch_errors.check_for_period_error(data, period)

    rocs = [((data[idx] - data[idx - (period - 1)]) / data[idx -
                                                           (period - 1)]) * 100
            for idx in range(period - 1, len(data))]
    rocs = fill_for_noncomputable_vals(data, rocs)
    return rocs
Пример #9
0
def aroon_up(data, period):
    """
    Aroon Up.

    Formula:
    AROONUP = (((PERIOD) - (PERIODS since PERIOD high)) / (PERIOD)) * 100
    """
    catch_errors.check_for_period_error(data, period)
    period = int(period)

    a_up = [((period - list(reversed(data[idx + 1 - period:idx + 1])).index(
        np.max(data[idx + 1 - period:idx + 1]))) / float(period)) * 100
            for idx in range(period - 1, len(data))]
    a_up = fill_for_noncomputable_vals(data, a_up)
    return a_up
Пример #10
0
def aroon_down(data, period):
    """
    Aroon Down.

    Formula:
    AROONDWN = (((PERIOD) - (PERIODS SINCE PERIOD LOW)) / (PERIOD)) * 100
    """
    catch_errors.check_for_period_error(data, period)
    period = int(period)

    a_down = [((period - list(reversed(data[idx + 1 - period:idx + 1])).index(
        np.min(data[idx + 1 - period:idx + 1]))) / float(period)) * 100
              for idx in range(period - 1, len(data))]
    a_down = fill_for_noncomputable_vals(data, a_down)
    return a_down
Пример #11
0
def percent_k(data, period):
    """
    %K.

    Formula:
    %k = data(t) - low(n) / (high(n) - low(n))
    """
    catch_errors.check_for_period_error(data, period)
    percent_k = [((data[idx] - np.min(data[idx + 1 - period:idx + 1])) /
                  (np.max(data[idx + 1 - period:idx + 1]) -
                   np.min(data[idx + 1 - period:idx + 1])))
                 for idx in range(period - 1, len(data))]
    percent_k = fill_for_noncomputable_vals(data, percent_k)

    return percent_k
Пример #12
0
def standard_variance(data, period):
    """
    Standard Variance.

    Formula:
    (Ct - AVGt)^2 / N
    """
    catch_errors.check_for_period_error(data, period)
    sv = [
        np.var(data[idx + 1 - period:idx + 1], ddof=1)
        for idx in range(period - 1, len(data))
    ]
    sv = fill_for_noncomputable_vals(data, sv)

    return sv
Пример #13
0
def detrended_price_oscillator(data, period):
    """
    Detrended Price Oscillator.

    Formula:
    DPO = DATA[i] - Avg(DATA[period/2 + 1])
    """
    catch_errors.check_for_period_error(data, period)
    period = int(period)
    dop = [
        data[idx] - np.mean(data[idx + 1 - (int(period / 2) + 1):idx + 1])
        for idx in range(period - 1, len(data))
    ]
    dop = fill_for_noncomputable_vals(data, dop)
    return dop
Пример #14
0
def exponential_moving_average(data, period):
    """
    Exponential Moving Average.

    Formula:
    p0 + (1 - w) * p1 + (1 - w)^2 * p2 + (1 + w)^3 * p3 +...
                /   1 + (1 - w) + (1 - w)^2 + (1 - w)^3 +...

    where: w = 2 / (N + 1)
    """
    catch_errors.check_for_period_error(data, period)
    emas = [exponential_moving_average_helper(
            data[idx - period + 1:idx + 1], period) for idx in range(period - 1, len(data))]
    emas = fill_for_noncomputable_vals(data, emas)
    return emas
Пример #15
0
def smoothed_moving_average(data, period):
    """
    Smoothed Moving Average.

    Formula:
    smma = avg(data(n)) - avg(data(n)/n) + data(t)/n
    """
    catch_errors.check_for_period_error(data, period)
    smma = [
        ((np.mean(data[idx - (period - 1):idx + 1]) -
          (np.mean(data[idx - (period - 1):idx + 1]) / period) + data[idx]) /
         period) for idx in range(0, len(data))
    ]
    smma = fill_for_noncomputable_vals(data, smma)
    return smma
Пример #16
0
def standard_deviation(data, period):
    """
    Standard Deviation.

    Formula:
    std = sqrt(avg(abs(x - avg(x))^2))
    """
    catch_errors.check_for_period_error(data, period)

    stds = [
        np.std(data[idx + 1 - period:idx + 1], ddof=1)
        for idx in range(period - 1, len(data))
    ]

    stds = fill_for_noncomputable_vals(data, stds)
    return stds
Пример #17
0
def stochrsi(data, period):
    """
    StochRSI.

    Formula:
    SRSI = ((RSIt - RSI LOW) / (RSI HIGH - LOW RSI)) * 100
    """
    rsi = relative_strength_index(data, period)[period:]
    stochrsi = [
        100 * ((rsi[idx] - np.min(rsi[idx + 1 - period:idx + 1])) /
               (np.max(rsi[idx + 1 - period:idx + 1]) -
                np.min(rsi[idx + 1 - period:idx + 1])))
        for idx in range(period - 1, len(rsi))
    ]
    stochrsi = fill_for_noncomputable_vals(data, stochrsi)
    return stochrsi
Пример #18
0
def double_smoothed_stochastic(data, period):
    """
    Double Smoothed Stochastic.

    Formula:
    dss = 100 *  EMA(Close - Lowest Low) / EMA(Highest High - Lowest Low)
    """
    catch_errors.check_for_period_error(data, period)
    lows = [data[idx] - np.min(data[idx+1-period:idx+1]) for idx in range(period-1, len(data))]
    sm_lows = ema(ema(lows, period), period)
    highs = [np.max(data[idx+1-period:idx+1]) - np.min(data[idx+1-period:idx+1]) for idx in range(period-1, len(data))]
    sm_highs = ema(ema(highs, period), period)
    dss = (sm_lows / sm_highs) * 100

    dss = fill_for_noncomputable_vals(data, dss)
    return dss
def linear_weighted_moving_average(data, period):
    """
    Linear Weighted Moving Average.

    Formula:
    LWMA = SUM(DATA[i]) * i / SUM(i)
    """
    catch_errors.check_for_period_error(data, period)

    idx_period = list(range(1, period + 1))
    lwma = [(sum([
        i * idx_period[data[idx - (period - 1):idx + 1].index(i)]
        for i in data[idx - (period - 1):idx + 1]
    ])) / sum(range(1,
                    len(data[idx + 1 - period:idx + 1]) + 1))
            for idx in range(period - 1, len(data))]
    lwma = fill_for_noncomputable_vals(data, lwma)
    return lwma
Пример #20
0
def simple_moving_average(data, period):
    """
    Simple Moving Average.

    Formula:
    SUM(data / N)
    """
    catch_errors.check_for_period_error(data, period)
    # Mean of Empty Slice RuntimeWarning doesn't affect output so it is
    # supressed
    with warnings.catch_warnings():
        warnings.simplefilter("ignore", category=RuntimeWarning)
        sma = [
            np.mean(data[idx - (period - 1):idx + 1])
            for idx in range(0, len(data))
        ]
    sma = fill_for_noncomputable_vals(data, sma)
    return sma
Пример #21
0
def upper_bollinger_band(data, period, std_mult=2.0):
    """
    Upper Bollinger Band.

    Formula:
    u_bb = SMA(t) + STD(SMA(t-n:t)) * std_mult
    """
    catch_errors.check_for_period_error(data, period)

    period = int(period)
    simple_ma = sma(data, period)[period - 1:]

    upper_bb = []
    for idx in range(len(data) - period + 1):
        std_dev = np.std(data[idx:idx + period])
        upper_bb.append(simple_ma[idx] + std_dev * std_mult)
    upper_bb = fill_for_noncomputable_vals(data, upper_bb)

    return np.array(upper_bb)
def volume_adjusted_moving_average(close_data, volume, period):
    """
    Volume Adjusted Moving Average.

    Formula:
    VAMA = SUM(CLOSE * VolumeRatio) / period
    """
    catch_errors.check_for_input_len_diff(close_data, volume)
    catch_errors.check_for_period_error(close_data, period)

    avg_vol = np.mean(volume)
    vol_incr = avg_vol * 0.67
    vol_ratio = [val / vol_incr for val in volume]
    close_vol = np.array(close_data) * vol_ratio
    vama = [
        sum(close_vol[idx + 1 - period:idx + 1]) / period
        for idx in range(period - 1, len(close_data))
    ]
    vama = fill_for_noncomputable_vals(close_data, vama)
    return vama
Пример #23
0
def weighted_moving_average(data, period):
    """
    Weighted Moving Average.

    Formula:
    (P1 + 2 P2 + 3 P3 + ... + n Pn) / K
    where K = (1+2+...+n) = n(n+1)/2 and Pn is the most recent price
    """
    catch_errors.check_for_period_error(data, period)
    k = (period * (period + 1)) / 2.0

    wmas = []
    for idx in range(0, len(data) - period + 1):
        product = [
            data[idx + period_idx] * (period_idx + 1)
            for period_idx in range(0, period)
        ]
        wma = sum(product) / k
        wmas.append(wma)
    wmas = fill_for_noncomputable_vals(data, wmas)

    return wmas
Пример #24
0
def true_range(close_data, period):
    """
    True Range.

    Formula:
    TRt = MAX(abs(Ht - Lt), abs(Ht - Ct-1), abs(Lt - Ct-1))
    """
    catch_errors.check_for_period_error(close_data, period)

    tr = [
        np.max([
            np.max(close_data[idx + 1 - period:idx + 1]) -
            np.min(close_data[idx + 1 - period:idx + 1]),
            abs(
                np.max(close_data[idx + 1 - period:idx + 1]) -
                close_data[idx - 1]),
            abs(
                np.min(close_data[idx + 1 - period:idx + 1]) -
                close_data[idx - 1])
        ]) for idx in range(period - 1, len(close_data))
    ]
    tr = fill_for_noncomputable_vals(close_data, tr)
    return tr