Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
pandas,python:收集、使用和保存实时数据_Python_Pandas - Fatal编程技术网

pandas,python:收集、使用和保存实时数据

pandas,python:收集、使用和保存实时数据,python,pandas,Python,Pandas,在下面的Python(2.7.9)代码中,我通过twsapi和IBpy回调接收实时股票数据。作为感兴趣的数据,买入、卖出、最后价格和最后交易规模进入熊猫(0.16.1)数据框。此外,我还在数据框“bidVol”中添加了一列,最后几行代码将交易或交易量的运行总和放在出价上。目前,我在“bidVol”栏中获得了某一特定股票的投标时交换的总股份。我想用78列替换单个“bidVol”列,一个交易日五分钟间隔内产生的每一个总计一列。 我将如何: 1) 在9:30和4:00 pm之间的5分钟间隔内创建具有时

在下面的Python(2.7.9)代码中,我通过twsapi和IBpy回调接收实时股票数据。作为感兴趣的数据,买入、卖出、最后价格和最后交易规模进入熊猫(0.16.1)数据框。此外,我还在数据框“bidVol”中添加了一列,最后几行代码将交易或交易量的运行总和放在出价上。目前,我在“bidVol”栏中获得了某一特定股票的投标时交换的总股份。我想用78列替换单个“bidVol”列,一个交易日五分钟间隔内产生的每一个总计一列。 我将如何:

1) 在9:30和4:00 pm之间的5分钟间隔内创建具有时钟时间标题的其他列,以便数据帧的标题如下所示:

“符号”、“证券”、“兑换”、“货币”、“9:30”、“9:35”、“3:55”

2) 然后将在给定时间间隔内以投标价格完成的交易量之和放入相应的列中

 from __future__ import print_function
 import pandas as pd
 import numpy
 from ib.opt import ibConnection, message
 from ib.ext.Contract import Contract
 from ib.ext.TickType import TickType as tt
 from time import sleep

 # establish universe of stocks to watch...

 contracts = pd.DataFrame([
        ['FGEN', 'STK', 'SMART', 'USD'],
        ['AAPL', 'STK', 'SMART', 'USD'],
        ['GILD', 'STK', 'SMART', 'USD'],
        ['INTC', 'STK', 'SMART', 'USD'],
        ['MSFT', 'STK', 'SMART', 'USD']

 ])

 # create column names for DataFrame

 contracts.columns = ['symbol', 'security', 'exchange', 'currency']

 # add specific column names to match name returned by tickType.getField()
 contracts['bidPrice'] = 0
 contracts['askPrice'] = 0
 contracts['lastPrice'] = 0
 contracts['lastSize'] = 0
 contracts['bidVol'] = 0
 def error_handler(msg):
        print(msg)

 def my_callback_handler(msg):
        if msg.field in [tt.BID, tt.ASK, tt.LAST]:
               # now store response in the data frame
                 contracts.loc[msg.tickerId, tt.getField(msg.field)] =   msg.price
        elif msg.field in [tt.LAST_SIZE]:
                 contracts.loc[msg.tickerId, tt.getField(msg.field)] = msg.size
                 if msg.field == tt.LAST_SIZE:
                         if contracts.loc[msg.tickerId, 'bidPrice'] ==    contracts.loc[msg.tickerId, 'lastPrice']:
                        contracts.loc[msg.tickerId, 'bidVol'] += contracts.loc[msg.tickerId, 'lastSize']

                             print(contracts.values)

在代码顶部附近插入以下代码:

from datetime import *

def fivemin():
    """Generate time string in 5 minute intervals"""
    dt0 = datetime.now()
    dt1 = dt0.replace(minute=5*(int)(dt0.minute/5),second=0,microsecond=0)
    return dt1.time().strftime('%H:%M')
错误处理程序的定义上方添加以下内容:

ds = datetime.now().replace(hour=9,minute=30,second=0,microsecond=0)
for i in range(79):
    d_i = ds+timedelta(minutes=5*i)
    contracts[d_i.strftime('%H:%M') = 0
contracts.loc[msg.tickerId, fivemin()] += contracts.loc[msg.tickerId, 'lastSize']
然后将您的
合同…bidVol
行替换为以下内容:

ds = datetime.now().replace(hour=9,minute=30,second=0,microsecond=0)
for i in range(79):
    d_i = ds+timedelta(minutes=5*i)
    contracts[d_i.strftime('%H:%M') = 0
contracts.loc[msg.tickerId, fivemin()] += contracts.loc[msg.tickerId, 'lastSize']