1

我在将实时流数据重新采样到 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, t_stamp, open_price, high_price, low_price, close_price)
sleep(4000)
####################################################################################

我已要求对收到的每 15M 编译和基本实时数据创建一个 OHLC 重采样。

此外,我们可以在一个 excel 和/或不将数据保存到任何 csv/xls 的情况下做同样的事情。

感谢您的提前和对不起我的编码技巧。

4

0 回答 0