首页 > 解决方案 > 通过 webscoket 从 python 打印中的代理 api 更新后显示为零的所有数据值包含 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 的更新值并打印,但它无法更新和打印相同的值。请帮助任何人。这是印度股票市场的 alice blue api,有助于算法交易。一切正常,除了更新的数据

标签: pythonwebsocketquantitative-financealgorithmic-trading

解决方案


尝试直接在您的订阅后面进行睡眠。它看起来像一个异步调用,并且您的回调函数可能会在您打印结果后触发。

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

编辑:

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

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 库解决此问题,但找不到如此快速完成这项工作的解决方案。

您只需将消息写入队列。我希望这有帮助。


推荐阅读