微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将实时流数据重新采样到15m时出错

如何解决将实时流数据重新采样到15m时出错

在将实时流数据重新采样到15M时间范围时,我得到了多次错误提示,如果我删除了我们,则删除了重新采样代码,程序运行正常。

import time,os,datetime,math                                                                                                                
import xlwings as xw
from time import sleep
import logging
from alice_blue import *
import pandas as pd
import datetime
import pdb  
from pandas.io.json import json_normalize
############################################################################

logging.basicConfig(level=logging.DEBUG)

access_token = open('access_token.txt','r').read().strip()

alice = AliceBlue(username='username',password='password',access_token=access_token,master_contracts_to_download=['NFO'])
#############################################################################
token = 0                                                                                                                                       
ltp = 0
open_price=0
high_price=0
low_price=0
close_price=0
t_stamp=0
row_no =2

socket_opened = False
def event_handler_quote_update(message):
    # print(f"quote update {message}")
    global row_no
    global token
    global ltp
    global open_price
    global high_price
    global close_price
    global low_price
    global t_stamp
    
    token = message['token']
    ltp =   message['ltp']
    open_price = message['open']
    high_price = message['high']
    low_price = message['low']
    close_price = message['close']
    t_stamp = message['exchange_time_stamp']
    
####################################################################################
    wb = xw.Book('TickData.xlsx')        
    sht = wb.sheets['Sheet1']
    sht.range('A' + str(row_no)).value = token                                                      
    sht.range('B' + str(row_no)).value = datetime.datetime.fromtimestamp(int(t_stamp)).strftime('%Y-%m-%d %H:%M:%s')  
    sht.range('C' + str(row_no)).value = ltp                                                        
    sht.range('D' + str(row_no)).value = open_price                                                 
    sht.range('E' + str(row_no)).value = high_price                                                 
    sht.range('F' + str(row_no)).value = low_price                                                  
    sht.range('G' + str(row_no)).value = close_price                                                

    row_no = row_no + 1 
    # print(token,t_stamp,ltp,open_price,high_price,low_price,close_price)
    
####################################################################################    
def open_callback():
    global socket_opened
    socket_opened = True

alice.start_websocket(subscribe_callback=event_handler_quote_update,socket_open_callback=open_callback,run_in_background=True)
while(socket_opened==False):
    pass
    
df=pd.read_excel('TickData.xlsx',names=['token','t_stamp','ltp','open_price','high_price','low_price','close_price'],index_col=1,parse_dates=True)     
df=pd.DataFrame(df)
data=df['ltp'].resample('1min').ohlc()                                                                                                                  
print(data)
data.to_excel('a.xlsx')


alice.subscribe(alice.get_instrument_by_symbol('NFO','BANKNIFTY SEP FUT'),LiveFeedType.MARKET_DATA)

sleep(0.200)

# print(token,close_price)
sleep(4000)
####################################################################################

我已要求在每收到15M的已整理和基本实时数据上创建OHLC重采样。

我们还可以在一个Excel中做同样的事情,并且/或者不将数据保存到任何csv / xls中。

感谢您的进步,并为我的编码能力感到抱歉。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。