Example #1
0
File: md.py Project: adayone/pysml
def hist_plot(id, end, delta):
    start_date = dt_tool.add(end, delta)
    df = ts.get_h_data(id, autype='qfq', start=start_date)
    df['r13'] = pd.rolling_mean(df['close'], window=13, min_periods=1, center=True)
    df['r34'] = pd.rolling_mean(df['close'], window=34, min_periods=1, center=True)
    df['r55'] = pd.rolling_mean(df['close'], window=55, min_periods=1, center=True)
    fig = df.plot(y=['close', 'r13',  'r34', 'r55', 'volume'], title=id,  secondary_y='volume', grid=True, legend=True, figsize=(18, 8))
    return fig.get_figure()
Example #2
0
def get_trainset(sd, label_start_date, label_delta=14, is_predict=False):
    # 开始构建训练数据
    # 根据给定的label时间点, 返回fx, y
    label_end_date = dt_tool.add(label_start_date, label_delta)
    
    # 将日期更换为delta
    if dt_tool.is_weekend(label_start_date):
        return None
    # 这个决定了获取多久的数据作为特征
    fea_start_date = dt_tool.add(label_start_date, -60)
    trainset = sd.query('date >= "%s" and date <= "%s"'%(fea_start_date, label_start_date))
    trainset['delta'] = trainset.date.apply(lambda x : dt_tool.delta_id(x, label_start_date))
    trainset['feature'] = trainset.type  + '_' + trainset.delta
    trainset.to_csv('data/trainset.csv', index=None)
    fea = trainset.loc[:, ['feature', 'value']]
    fea.to_csv('data/fea.csv', index=None)
    ifea = fea.set_index('feature')
    x = ifea.T
    x.to_csv('data/x.csv', index=None)
   
    if is_predict:
        return x
    
    # 确定label数据, 以七天为一个周期
    
    label_close = sd.query('date == "%s" and type == "close"'%(label_start_date)).value
    if label_close is None or len(label_close) == 0:
        return None
    label_close = label_close.values[0]
    end_close = sd.query('date >= "%s" and date <= "%s" and type == "close"'%(label_start_date, label_end_date)).value.max()
    if end_close is None:
        return None
    y = (end_close - label_close)/label_close
    # 从数据保存的角度看, 分开保存还是太麻烦了
    # 直接存进去吧, 名字叫label
    #y = end_close
    x['label'] = (y > 0.2)
    return x
Example #3
0
def hist_plot(id, end, delta):
    start_date = dt_tool.add(end, delta)
    df = ts.get_h_data(id, autype='qfq', start=start_date)
    df['r13'] = pd.rolling_mean(df['close'],
                                window=13,
                                min_periods=1,
                                center=True)
    df['r34'] = pd.rolling_mean(df['close'],
                                window=34,
                                min_periods=1,
                                center=True)
    df['r55'] = pd.rolling_mean(df['close'],
                                window=55,
                                min_periods=1,
                                center=True)
    fig = df.plot(y=['close', 'r13', 'r34', 'r55', 'volume'],
                  title=id,
                  secondary_y='volume',
                  grid=True,
                  legend=True,
                  figsize=(18, 8))
    return fig.get_figure()
Example #4
0
def get_trainset(id, sd, label_dt, delta = 7):
    label_date = dt_tool.dt2str(label_dt)
    end_date = dt_tool.add(label_date, delta)
    x = get_feature(id, sd, label_date)
    if x is None:
        return None
    if label_dt.weekday() == 5 or label_dt.weekday() == 6:
            return None
    ssd = sd.set_index('date')
    print end_date
    print label_date
    label_close = sd.query('date == "%s" and type == "close"'%(label_date)).value
    if label_close is None or len(label_close) == 0:
        return None
    label_close = label_close.values[0]
    print label_close
    print type(label_close)
    end_close = sd.query('date == "%s" and type == "close"'%(end_date)).value
    if end_close is None or len(end_close) == 0:
        return None
    end_close = end_close.values[0]
    y = (end_close - label_close)/label_close
    print y
    return x, y
Example #5
0
delta = int(sys.argv[2])
print id
base = '/Users/haoyuanhu/Write/notes'
today = dt_tool.get_today()
ticks = get_ticks(id, today, -delta)
name, now, begin, rat = trade.get_realtime(id)
fig = tick_plot(ticks)
fig.savefig('%s/image/%s_tick_%s.png' % (base, id, today), dpi=400)
fig = hist_plot(id, today, -delta)
fig.savefig('%s/image/%s_hist_%s.png' % (base, id, today), dpi=400)
sticks = ticks.sort('amount', ascending=False).head(5)
sticks.type = sticks.type.apply(lambda x: type2id(x))
sticks.to_csv('%s/data/%s_%s.csv' % (base, id, today), index=None)

# tick today
yestoday = dt_tool.add(today, -3)
#ticks_today = ts.get_today_ticks(id).sort('amount', ascending=False).head(10)
#ticks_today = ts.get_tick_data(id, today).sort('amount', ascending=False).head(5)
ticks_today = ts.get_tick_data(id, yestoday).sort('amount',
                                                  ascending=False).head(5)
ticks_today.type = ticks_today.type.apply(lambda x: type2id(x))
txt = ticks_today.reset_index().drop(['index'], 1)
tab = tabulate(txt, headers='keys', tablefmt='pipe')
tick_today = tab.encode('utf-8')

# hist tick
txt = sticks.drop(['dt', 'id'], 1)
txt = txt.reset_index().drop(['index'], 1)
tab = tabulate(txt, headers='keys', tablefmt='pipe')
tick_hist = tab.encode('utf-8')
top_tick_day = txt.ix[0].date
Example #6
0
File: md.py Project: adayone/pysml
delta = int(sys.argv[2])
print id
base = '/Users/haoyuanhu/Write/notes'
today = dt_tool.get_today()
ticks = get_ticks(id, today, -delta)
name, now, begin, rat = trade.get_realtime(id)
fig = tick_plot(ticks)
fig.savefig('%s/image/%s_tick_%s.png'%(base, id, today), dpi=400)
fig = hist_plot(id, today, -delta)
fig.savefig('%s/image/%s_hist_%s.png'%(base, id, today), dpi=400)
sticks = ticks.sort('amount', ascending=False).head(5)
sticks.type = sticks.type.apply(lambda x : type2id(x))
sticks.to_csv('%s/data/%s_%s.csv'%(base, id, today),  index=None)

# tick today
yestoday = dt_tool.add(today, -3)
#ticks_today = ts.get_today_ticks(id).sort('amount', ascending=False).head(10)
#ticks_today = ts.get_tick_data(id, today).sort('amount', ascending=False).head(5)
ticks_today = ts.get_tick_data(id, yestoday).sort('amount', ascending=False).head(5)
ticks_today.type = ticks_today.type.apply(lambda x : type2id(x))
txt = ticks_today.reset_index().drop(['index'], 1)
tab = tabulate(txt, headers='keys', tablefmt='pipe')
tick_today = tab.encode('utf-8')

# hist tick
txt = sticks.drop(['dt', 'id'], 1)
txt = txt.reset_index().drop(['index'], 1)
tab = tabulate(txt, headers='keys', tablefmt='pipe')
tick_hist = tab.encode('utf-8')
top_tick_day = txt.ix[0].date
top_tick_type = txt.ix[0].type