Esempio n. 1
0
def str_to_dates_test(period, exp):
    """ test module for str_to_dates() """
    res = fetch.str_to_dates(period)
    if res == exp:
        print('pass')
    else:
        print('fail')
Esempio n. 2
0
def new_price_in_n_days_multi(stock, period, params):
    """
    filter out price if it's new high/low in specified multiple windows
    """
    # parse params
    windows = params[0]
    windows.sort() # e.g. [20, 60, 120, 240]

    # -1 for it's included
    res = fetch.fetch_price('index', period, windows[-1] - 1) # just get data once, so get it big
    if res[0] == False: # empty files for half of a year
        return False
    else:
        idx_df = res[1]

    # -1 for it's included
    res = fetch.fetch_price(stock, period, windows[-1] - 1) # just get data once, so get it big
    if res[0] == False: # empty files for half of a year
        return False
    else:
        df = res[1]

    # be compatible between twse/otc
    if fetch.stock_table.loc[stock]['TWSE/OTC'] == '上櫃':
        price_label = '收盤'
    else:
        price_label = '收盤價'

    df = pandas.DataFrame(index=idx_df.index, data=df[price_label])
    
    period = fetch.str_to_dates(period)
    if period[0] != [False]:
        start_dt, end_dt = period
    else:
        return False

    res_list = []
    for win in windows:
        for high_low in ['high', 'low']:
            if high_low == 'high':
                res = pandas.stats.moments.rolling_max(df[price_label], win, min_periods=1)
            else:
                res = pandas.stats.moments.rolling_min(df[price_label], win, min_periods=1)
            df[str(win) + high_low] = res == df[price_label]

    df = df.drop(price_label, 1)
    df = df[str(start_dt) <= res.index]
    df = df[df.index <= str(end_dt)]

    return df.astype(int)