通过webscoket从broker api更新后,所有数据值显示为零,在python中打印包含0,0,0,0,0,0。

问题描述 投票:0回答:1
 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)

Please someone help I am new to python and algo trading.I am expecting the updated value from websocket and print in but it's unable to update and print same.Please help anyone.This is alice blue api for indian stock market which helps in algo trading.Everything working fine except updated data. 一切工作正常,除了更新的数据

python websocket quantitative-finance algorithmic-trading
1个回答
1
投票

尽量让睡眠直接在你的订阅后面。它看起来像一个异步调用,你的回调函数可能会在你打印结果后开火。

你确定你的睡眠是在正确的行?

编辑。

你也许应该简单地使用queue. 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可以让你等到一些东西被写入Queue。我曾试图用asyncio库来解决这个问题,但无法找到一个解决方案来让这个工作如此之快。

你可以简单地把你的消息写进Queue里。我希望这能帮助你。

© www.soinside.com 2019 - 2024. All rights reserved.