GTT ORDER PLACEMENT ISSUE

theankitdabas
hi @sujith / @rakeshr please assist a bit in this, action is to place gtt sell order automatally agaisnt buy order punched manually.

def on_order_update(self, ws, data):
# TODO: Here we need to add code to place the order
print(f"[+] NEW ORDER UPDATE: {data}")
Thread(target=KiteConnect.place_gtt, args=(data,)).start()

# Extract necessary information from the order update
order_id = data['order_id']
status = data['status']
symbol = data['tradingsymbol']
price = data['price']
quantity = data['quantity']
order_type = data['order_type']
exchange = data['exchange']
product = data['product']

# Check if the order status is complete
if status == 'COMPLETE':
# Calculate stop-loss and target prices based on the percentage
stop_loss_percentage = -0.03
target_percentage = 0.02

stop_loss_price = price * (1 + stop_loss_percentage)
target_price = price * (1 + target_percentage)

# Assuming you have access to the required parameters for the GTT order
quantity = data['quantity']

# Create a fresh sell GTT order against the order ID
try:
order_oco = [{
"exchange":exchange,
"tradingsymbol": symbol,
"transaction_type": kite.TRANSACTION_TYPE_SELL,
"quantity": quantity,
"order_type": "LIMIT",
"product": "NRML",
"price": 99
},{
"exchange": exchange,
"tradingsymbol": symbol,
"transaction_type": kite.TRANSACTION_TYPE_SELL,
"quantity": quantity,
"order_type": "LIMIT",
"product": "NRML",
"price": 121
}]
# Using a thread to place the GTT order asynchronously
Thread(target=self.kite.place_gtt, args=(order_oco,)).start()

except Exception as e:
print(f"Error preparing GTT order: {e}")


def place_gtt_oco_thread(self, kite, order_oco):
try:
gtt_oco = KiteConnect.place_gtt(trigger_type=kite.GTT_TYPE_OCO, tradingsymbol="symbol", exchange="NSE", trigger_values=[470,], last_price="price", orders=order_oco)
logging.info("GTT OCO trigger_id : {}".format(gtt_oco['trigger_id']))
except Exception as e:
logging.info("Error placing gtt oco order: {}".format(e))

print("GTT OCO trigger_id: {}".format(gtt_oco['trigger_id']))
except Exception as e:
print("Error placing GTT OCO order: {}".format(e))

def on_close(self, ws, code, reason):
print(f"[!] Connection closed with code {code} - {reason}")
# Add retry logic here if needed
self.is_running = False # Stop the main loop
Sign In or Register to comment.