python 中的代理 api 通过 websocket 更新后所有数据值显示为零 打印包含 0,0,0,0,0

all data values showing zero after updating via webscoket from broker api in python print contains 0,0,0,0,0

 import time,os,datetime,math
 from time import sleep
 import logging
 from alice_blue import *
 import pandas as pd

 username = "AAAAAA"
 password = "AAAAAAA"
 api_secret = "AAAAAAA"
 twoFA = "AAAAAA"

access_token=
AliceBlue.login_and_get_access_token(username=username,password=password,twoFA=twoFA,api_se 
cret=api_secret)
alice = 
AliceBlue(username=username,password=password,access_token=access_token,master_contracts_to_download= 
['NSE'])

print("Master contract loaded succesfully")
print("\n")

print("Getting Profile Details ")
print("\n")
profile = alice.get_profile()
profile_data=(profile['data'])
exchange = profile_data['exchanges']
print("Name: ",profile_data['name']," Client Id: ",profile_data['login_id']," Pan card: ",profile_data["pan_number"],"Exchange : ",exchange[1])
print("\n")
balance = alice.get_balance()
balance_data = balance['data']
cash_position = balance_data['cash_positions']
print("Balance available is : ",cash_position[0]['utilized']['var_margin'])

print("Wait for breakout Time\n")

socket_opened = False
token = 0
open_price=0
high_price=0
low_price=0
close_price=0

def event_handler_quote_update(message):
    global token
    global open_price
    global high_price
    global close_price
    global low_price
    token = message['token']
    open_price = message['open']
    high_price = message['high']
    low_price = message['low']
    close_price = message['close']
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
alice.subscribe(alice.get_instrument_by_symbol("NSE","ONGC"), LiveFeedType.FULL_SNAPQUOTE)
print(token,open_price,high_price,low_price,close_price)
sleep(10)

请有人帮助我是 python 和算法交易的新手。 我期待来自 websocket 的更新值并打印,但它无法更新和打印 same.Please help anyone.This is alice blue api for indian stock market 这有助于算法交易。除更新数据外一切正常

尽量让睡眠紧跟在您的订阅之后。它看起来像是一个异步调用,您的回调函数可能会在您打印结果后触发。

你确定你的睡眠是正确的吗?

编辑:

您也许应该简单地使用 queue.Queue class。我希望这能解决你的问题。您可以像普通列表一样简单地使用队列。例如:

from concurrent.futures import ThreadPoolExecutor
from queue import Queue
from time import sleep

def write_in_Queue(some_message, seconds):
    print("Wait {} seconds".format(seconds))
    sleep(seconds)
    queue.put(some_message)
    print('message put into Queue')

if __name__ == "__main__":
    queue = Queue()
    threadpool = ThreadPoolExecutor()
    threadpool.submit(write_in_Queue, "Hallo", 2)
    threadpool.submit(write_in_Queue, "End", 4)

    print("Waiting for first message")
    message_1 = queue.get()
    print("Wait for second message")
    message_2 = queue.get()
    print(message_2)
    print("Finish this thread")

您将收到输出:

Wait 2 seconds
Wait 4 seconds
 Waiting for first message
message put into Queue
Wait for second message
message put into Queue
End
Finish this thread

queue.get 负责等待,直到将内容写入队列。我试图用 asyncio 库解决这个问题,但找不到一个解决方案来如此快速地完成这项工作。

您只需将消息写入队列即可。希望对您有所帮助。