I am trying to save tick data supplied by websocket in sql server with following code in python
def on_data(wsapp, message):
print (““)
print (““)
print(message)
#################################
import pandas as pd
myDict=json.loads(message)
df=pd.DataFrame([myDict])
row_count=len(df)
x = df.iat[row_count-1,3]
print(x)
scrip_code = (x["scripCode"])
bid= (x ["bidInfo"][0]) # GET PRICE, QTY , ORDERS FROM BID INFO
offer=(x["offerInfo"][0])
bid_price = (bid["price"])
bid_qty = (bid["qty"])
offer_price = (offer["price"])
offer_qty = (offer["qty"])
if offer_qty > bid_qty :
ltp = bid_price
trade_qty = bid_qty
if bid_qty > offer_qty :
ltp = offer_price
trade_qty = offer_qty
print("*****")
print(ltp)
print(trade_qty)
print("*****")
print(bid_price)
print(bid_qty)
print("*****")
print(offer_price)
print(offer_qty)
print("*****")
# Adding data to Data Table
now = datetime.now() # Get the current date and time
hrs= now.strftime("%H")
min= now.strftime("%M")
dt=now.strftime("%d-%m-%Y")
mydataset= ( scrip_code,ltp, trade_qty, bid_qty, offer_qty, dt, hrs, min )
print("############################")
cursor = cnxn.cursor()
cursor.execute('INSERT INTO LIVE_DATA (SCRIP_CODE, LTP, TRADE_QTY, BID_QTY, OFFER_QTY, DT, HRS, MIN) VALUES(?,?,?,?,?,?,?,?)', mydataset)
cnxn.commit()
cnxn.close()
print("############################")
but it does not work. Please guide
how to save live tick data supplied by websocket to sql server database
New contributor
Milind Sonawane is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.