I have been deep dive teaching myself python - and would appreciate some help i am struggling to problem solve.
I get: <TypeError: byte indices must be integers or slices, not str >for the following line of code:
WHookID = int(data['Alert_ID'])
The WHookID is data from a JSON Webhook. example below:
"Alert_ID": "7960057",
"Order_Descr": "Long Bull Engulf",
"Order_Action": "BUY",
"Entry_Stop_Price": "18500",
"Take_Profit": "18550",
"Stop_Loss": "18400",
"Contract_Qty": "1",
"Ticker": "NQ",
"Local_Symbol": "NQM4",
"Contract_Type": "FUT",
"Exchange": "CME",
"Contract_Exp_Date": "20240621",
"Currency": "USD",
"Instrument Price": "18366"
}
As a bit of a noob to Python; i have researched and tried so many ideas - all of which havent helped. I have ran out of ideas how to problem solve and would be extremely grateful for some guidance from those here.
The full code for the class is below:
def webhook():
data = request.data
if data:
r.publish('tradingview', data)
data_dict = request.json
WHookID = int(data['Alert_ID'])
WHook_IdDB = (json.loads(WHookID))
for value in WHook_IdDB[WHookID]:
print(type(WHook_IdDB))
print(WHook_IdDB)
db = get_db()
cursor = db.cursor()
cursor.execute("SELECT ALERT_ID FROM WebHook_SigDB WHERE rowid = (SELECT MAX(rowid) FROM WebHook_SigDB)")
#cur.execute("SELECT ALERT_ID FROM WebHook_SigDB ORDER BY rowid desc LIMIT 1")
result = cursor.fetchone()
print(f"Result = {result[0]}")
if result == WHook_IdDB:
print("Webhook Id:" + result + "is NOT unqiue - no DB entry")
else:
print("Webhook Id is unique:" + result + "Enter into DB")
cursor.execute("""
INSERT OR REPLACE INTO WebHook_SigDB (Alert_ID, Ticker, Order_Action, Contract_Qty, Entry_Stop_Price, Take_Profit, StopLoss)
VALUES (?, ?, ?, ?, ?, ?, ?)
""", (data_dict['Alert_ID'],
data_dict['Ticker'],
data_dict['Order_Action'],
data_dict['Contract_Qty'],
data_dict['Entry_Stop_Price'],
data_dict['Take_Profit'],
data_dict['Stop_Loss']))
db.commit()
print(data_dict)
return data
return {
"code": "success"
}