if condition error while placing order

Ronit
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

api_k = hidden
api_s = hidden
access_token = hidden

kws = ""
kite = ""


def get_login(api_k, api_s): # log in to zerodha API panel
global kws, kite
kite = KiteConnect(api_key=api_k)
kite.set_access_token(access_token)
kws = KiteTicker(api_k, access_token)


get_login(api_k, api_s)

trd_portfolio = {121345: {'name': '3MINDIA'}, 3038209: {'name': '63MOONS'}, 1793: {'name': 'AARTIIND'}, 2561: {'name': 'ABAN'}, 3329: {'name': 'ABB'}}

order_count = 0 #variable to keep count

def on_ticks(ws, ticks):
for single_comapny in ticks:

inst_of_single_company = single_comapny['instrument_token']
name = trd_portfolio[inst_of_single_company]['name']

# print(single_comapny, name)
high = single_comapny['ohlc']['high']
low = single_comapny['ohlc']['low']
ltp = single_comapny['last_price']

trd_portfolio[inst_of_single_company]['high'] = high
trd_portfolio[inst_of_single_company]['low'] = low

if (ltp > high) and ("bought" not in trd_portfolio[inst_of_single_company].values()) and order_count<1: #only 1 order gets executed
print("buy", name, "BREAKOUT")
trd_portfolio[inst_of_single_company]['status'] = "bought"
tp = round(ltp+0.005*ltp, 1)
sl = round(ltp-0.0025*ltp, 1)
quantity =1

orderId = kite.place_order(tradingsymbol=name, price=ltp, variety=kite.VARIETY_REGULAR, exchange=kite.EXCHANGE_NSE, transaction_type=kite.TRANSACTION_TYPE_BUY, quantity=quantity, order_type=kite.ORDER_TYPE_LIMIT, product=kite.PRODUCT_MIS)

orderId_target =kite.place_order(tradingsymbol=name, price=tp, variety=kite.VARIETY_REGULAR, exchange=kite.EXCHANGE_NSE, transaction_type=kite.TRANSACTION_TYPE_SELL, quantity=quantity, order_type=kite.ORDER_TYPE_LIMIT, product=kite.PRODUCT_MIS)

orderId_stop = kite.place_order(tradingsymbol=name, price=sl, variety=kite.VARIETY_REGULAR, exchange=kite.EXCHANGE_NSE, transaction_type=kite.TRANSACTION_TYPE_SELL, quantity=quantity, trigger_price=sl, order_type=kite.ORDER_TYPE_SL,
product=kite.PRODUCT_MIS)

trd_portfolio[inst_of_single_company]["order_ids"] = [orderId,orderId_target,orderId_stop]
order_count +=1

inst_token = [121345, 3038209, 1793, 2561, 3329]

def on_connect(ws, response):
ws.subscribe(inst_token)
ws.set_mode(ws.MODE_QUOTE, inst_token)


kws.on_ticks = on_ticks
kws.on_connect = on_connect
kws.connect()
kws.connect()

  • Ronit
    @Imran that helped! Thanks
    I also wanna thank you for your youtube tutorials, this code is from there with some changes
  • berkninan
    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.
Sign In or Register to comment.