Order is getting executed multiple times as Order number is not generating

jenish_shakti
Hello,
Below is my code to check conditions in excel and placing order through Python.
  • The problem is happening that this python scripts check the conditions on every 2 seconds.
  • And place order when condition become "TRUE" in "AA" column of excel.
  • While placing order it collects order number and paste in "AF" Column. Which is formulated to make "AA" Column "FALSE".
  • i.e. until order number is pasted in excel column "AF", "AA" column's cell is remaining true.
  • I have faced issue in volatile market that Kite is taking very long time to collect order number. And because of it, this program is placing 4-5 orders of same row.
  • Even "print("Order BankNifty PE")" is also not printing. But still placing order.
  • This incident is happening majorly almost on every Wednesday/Thursday after 1:30-2 PM random time.
My questions here:
  • Why order numbers are not generating?
  • If order numbers are generating then why it is not getting retrieved by API?
  • Possible modification in the code? I can wait for 3-5-10 seconds if you recommend.


import schedule
import xlwings as xw
import kiteSettings
from kiteconnect import KiteConnect
import logging
import time
from datetime import datetime


kite = KiteConnect(kiteSettings.api_key)
kite.set_access_token(kiteSettings.access_token)
wbtest = xw.Book("xxxx.xlsm")

def order_place1(symbol, bs, qty, price1):
# Place an order
if bs == "BUY":
transaction_type1 = kite.TRANSACTION_TYPE_BUY,
elif bs == "SELL":
transaction_type1 = kite.TRANSACTION_TYPE_SELL,
try:
order_id = kite.place_order(
variety=kite.VARIETY_REGULAR,
exchange=kite.EXCHANGE_NFO,
tradingsymbol=symbol,
transaction_type=transaction_type1,
quantity=qty,
price=price1,
product=kite.PRODUCT_NRML,
order_type=kite.ORDER_TYPE_LIMIT
)

logging.info("Order placed. ID is: {}".format(order_id))
except Exception as e:
logging.info("Order placement failed: {}".format(e.message))

return order_id


def sch_chk():

try:
i = 51

while i < 72:
# wbtest.sheets("NIFTY50").range("AA"+str(i)).select()
if wbtest.sheets("BANKNIFTY").range("AA" + str(i)).value == wbtest.sheets("BANKNIFTY").range("B18").value:
BS1 = wbtest.sheets("BANKNIFTY").range("AA" + str(i)).offset(0, 1).value
sym1 = wbtest.sheets("BANKNIFTY").range("AA" + str(i)).offset(0, 11).value
prc1 = wbtest.sheets("BANKNIFTY").range("AA" + str(i)).offset(0, 4).value
qty1 = 300

a4 = order_place1(sym1, BS1, int(qty1), prc1)
wbtest.sheets("BANKNIFTY").range("AA" + str(i)).offset(0, 5).value = a4
print("Order BankNifty PE")
i = i + 1

print(datetime.now())

except:
pass


if __name__ == '__main__':
while True:
try:
sch_chk()
time.sleep(2)
except:
pass


Sign In or Register to comment.