hi, I am trying to use ORB strategy, having this error for the variable "order_count" while running the code
error msg: error msg: if (ltp > high) and ("bought" not in trd_portfolio[inst_of_single_company].values()) and order_countbuiltins.UnboundLocalError: local variable 'order_count' referenced before assignment Connection error: 1006 - connection was closed uncleanly (peer dropped the TCP connection without previous WebSocket closing handshake) Connection closed: 1006 - connection was closed uncleanly (peer dropped the TCP connection without previous WebSocket closing handshake)
code: from kiteconnect import KiteConnect from kiteconnect import KiteTicker import pandas as pd import datetime import pdb
The Unboundlocalerror: local variable referenced before assignment is raised when you try to use a variable before it has been assigned in the local context. Python doesn't have variable declarations , so it has to figure out the scope of variables itself. It does so by a simple rule: If there is an assignment to a variable inside a function, that variable is considered local . To solve this problem, you can explicitly say it's a global by putting global declaration in your function. The global statement does not have to be at the beginning of the function definition, but that is where it is usually placed. Wherever it is placed, the global declaration makes a variable to global variable everywhere in the function.
see this link : https://stackoverflow.com/questions/10851906/python-3-unboundlocalerror-local-variable-referenced-before-assignment
I also wanna thank you for your youtube tutorials, this code is from there with some changes