Keshav Choudhary
Keshav Choudhary

Reputation: 39

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)

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

Upvotes: 1

Views: 1033

Answers (1)

Ofi91
Ofi91

Reputation: 96

Try to make the sleep directly behind your subscribe. It looks like an asynchonous call and your callback-function probably fires after you print your results.

Are you sure that your sleep is in the right line?

Edit:

You should maybe simply use the queue.Queue class. I hope this solves your problem. You can use the Queue simply like an normal list. As an example:

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")

You will receive the output:

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

The queue.get takes care that you wait until something is written into the Queue. I have tried to solve this with the asyncio library but could not find a solution to get this work so fast.

You would simply write your messages into the Queue. I hope this helps.

Upvotes: 3

Related Questions