掘金社区

将tick对象中的盘口数据转换为dataframe二维表格式Pinned highlighted

摸鱼校尉王大壮 发表在策略研究 2021-11-10 22:35:59

策略研究
239
1
0

tick数据是一个list[dict]格式,直接打印没法获取有效信息,既不便于观察也不便于分析数据。
提供一种结构化tick数据的函数

def init(context):
    subscribe(symbols=first_symbol, frequency='tick', count=2)


def on_tick(context, tick):
    quotes = {
        'bid_p': [
            tick['quotes'][0]['bid_p'],
            tick['quotes'][1]['bid_p'],
            tick['quotes'][2]['bid_p'],
            tick['quotes'][3]['bid_p'],
            tick['quotes'][4]['bid_p'],
        ],
        'bid_v': [
            tick['quotes'][0]['bid_v'],
            tick['quotes'][1]['bid_v'],
            tick['quotes'][2]['bid_v'],
            tick['quotes'][3]['bid_v'],
            tick['quotes'][4]['bid_v'],
        ],
        'ask_p': [
            tick['quotes'][4]['ask_p'],
            tick['quotes'][3]['ask_p'],
            tick['quotes'][2]['ask_p'],
            tick['quotes'][1]['ask_p'],
            tick['quotes'][0]['ask_p'],
        ],
        'ask_v': [
            tick['quotes'][4]['ask_v'],
            tick['quotes'][3]['ask_v'],
            tick['quotes'][2]['ask_v'],
            tick['quotes'][1]['ask_v'],
            tick['quotes'][0]['ask_v'],
        ],
    }
    df = pd.DataFrame(quotes)
    print(df)

设置回测的起止时间

        backtest_start_time='2021-11-01 09:30:00',
        backtest_end_time='2021-11-01 09:30:10',

打印出如下数据,从左往右分别是 买盘价1-5档;买盘量1-5档;卖盘价1-5档;卖盘量1-5档;

     bid_p  bid_v    ask_p  ask_v
0  138.004    850  138.899     10
1  138.000    930  138.880    290
2  137.999    640  138.860    320
3  137.888    100  138.850    500
4  137.880    200  138.835   1800
     bid_p  bid_v    ask_p  ask_v
0  138.069     10  138.834    840
1  138.007    120  138.832    390
2  138.006    140  138.821     50
3  138.005    400  138.795    140
4  138.002    210  138.444     10
     bid_p  bid_v    ask_p  ask_v
0  138.840     80  138.999    530
1  138.073    160  138.998     20
2  138.072     80  138.990    280
3  138.071     10  138.980   3060
4  138.069    190  138.970    110

在这里我把卖盘倒着写的,因为卖盘的数据是从高到低排列,不符合我们平时的习惯

context.data也能获取同样的数据

def on_tick(context, tick):
    data = context.data(symbol=first_symbol, frequency='tick', count=1)
    quotes2 = {
        'bid_p': [
            data[0]['quotes'][0]['bid_p'],
            data[0]['quotes'][1]['bid_p'],
            data[0]['quotes'][2]['bid_p'],
            data[0]['quotes'][3]['bid_p'],
            data[0]['quotes'][4]['bid_p'],
        ],
        'bid_v': [
            data[0]['quotes'][0]['bid_v'],
            data[0]['quotes'][1]['bid_v'],
            data[0]['quotes'][2]['bid_v'],
            data[0]['quotes'][3]['bid_v'],
            data[0]['quotes'][4]['bid_v'],
        ],
        'ask_p': [
            data[0]['quotes'][4]['ask_p'],
            data[0]['quotes'][3]['ask_p'],
            data[0]['quotes'][2]['ask_p'],
            data[0]['quotes'][1]['ask_p'],
            data[0]['quotes'][0]['ask_p'],
        ],
        'ask_v': [
            data[0]['quotes'][4]['ask_v'],
            data[0]['quotes'][3]['ask_v'],
            data[0]['quotes'][2]['ask_v'],
            data[0]['quotes'][1]['ask_v'],
            data[0]['quotes'][0]['ask_v'],
        ],
    }
    df2 = pd.DataFrame(quotes2)
    print(df2)

data原来是一个列表格式吗,还不清楚里边还有什么。有待研究。
社区的搜索功能太差劲了,以前记得看到过有相关的内容找了半天都没找到,还得自己造轮子。
如果我真的是第一个可以给个赞~

—————追加—————
说出来你可能不信
context.data是一个列表嵌套字典嵌套列表嵌套字典
好家伙,搁这套娃呢

评论: 1

Looks like your connection to 掘金量化社区 - 量化交易者的交流社区 was lost, please wait while we try to reconnect.