Example #1
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)
Example #2
0
def new_price_in_n_days(stock, period, params):
    """
    filter out price if it's new high/low in specified days
    """
    # parse params
    days = int(params[0])
    if days <= 0:
        messagebox.showerror('error', 'days can\'t be <= 0')
        return -1
    high = is_high(params[1])

    # -1 for it's included
    res = fetch.fetch_price(stock, period, days - 1)
    if res[0] == False: # empty files for a long term
        return False
    else:
        df = res[1]

    # be compatible between twse/otc
    if fetch.stock_table.loc[stock]['TWSE/OTC'] == '上櫃':
        price_label = '收盤'
    else:
        price_label = '收盤價'
    
    if high:
        res = pandas.stats.moments.rolling_max(df[price_label], days)
    else:
        res = pandas.stats.moments.rolling_min(df[price_label], days)
    res = (res == df[price_label])

    if True in list(res):
        return True
    else:   
        return False
Example #3
0
def fetch_price_test(stock, period, addition, column, expect):
    """
    test module for fetch_data('stock price')
    """
    res = fetch.fetch_price(stock, period, addition)
    if res[0] == expect[0] == False:
        print('pass')
    elif res[0] == expect[0] == True and list(res[1][column]) == expect[1]:
        print('pass')
    else:
        print('fail')
        print(list(res[1][column]))
        print(expect[1])