Home Article BUY SELL ORDER PLACE WITH LTP DATA BUY SELL ORDER PLACE WITH LTP DATA Software Technique Web Technique All Computer Language and Computer Language Practice Computer Educa March 28, 2025 0 Comments Share: Facebook Twitter Google+ Pinterest Whatsapp TREDING NIFTY SYMBALTOKEN-26000 , BANKNIFTY - SYMBALTOKEN-26009 import time import os from SmartApi import SmartConnect import pyotp from logzero import logger # Setup API connection api_key = 'dfhgfggh' username = 'dshgh838488' password = '7467576' smartApi = SmartConnect(api_key) # Instrument details exchange = "NFO" tradingsymbol = "NIFTY03APR2523650CE" symboltoken = "54753" # Define Single Entry Price entry_price = 60 # Yaha sirf ek entry price define karein sleep_time = 0.1 # 100ms interval for checking LTP # Function to generate OTP def generate_otp(token, correlation_id): """Generates an OTP using the provided token.""" try: otp = pyotp.TOTP(token).now() logger.info(f"[{correlation_id}] Generated OTP: {otp}") return otp except Exception as e: logger.error(f"[{correlation_id}] Invalid Token: {e}") raise e # Function to fetch LTP Data def fetch_ltp(correlation_id): """Fetches LTP data for the given symbol.""" try: ltp_data = smartApi.ltpData(exchange, tradingsymbol, symboltoken) if ltp_data: ltp_price = ltp_data['data']['ltp'] logger.info(f"[{correlation_id}] Current LTP: {ltp_price}") return ltp_price return None except Exception as e: logger.error(f"[{correlation_id}] Error fetching LTP data: {e}") return None # Function to prepare order parameters def prepare_order_params(entry_price, quantity, correlation_id): """Prepares order parameters for buy and sell orders.""" buy_order_params = { "variety": "NORMAL", "tradingsymbol": tradingsymbol, "symboltoken": symboltoken, "transactiontype": "BUY", "instrumenttype": "OPTIDX", "optiontype": "call", "exchange": exchange, "ordertype": "LIMIT", "producttype": "INTRADAY", "duration": "DAY", "price": str(entry_price), "quantity": str(quantity) } sell_order_params = { "variety": "STOPLOSS", "tradingsymbol": tradingsymbol, "symboltoken": symboltoken, "transactiontype": "SELL", "instrumenttype": "OPTIDX", "optiontype": "call", "exchange": exchange, "ordertype": "STOPLOSS_LIMIT", "producttype": "INTRADAY", "duration": "DAY", "price": str(entry_price), "triggerprice": str(entry_price - 0.1), "quantity": str(quantity) } logger.info(f"[{correlation_id}] Buy Order Params: {buy_order_params}") logger.info(f"[{correlation_id}] Sell Order Params: {sell_order_params}") return buy_order_params, sell_order_params # Function to place an order def place_order(order_params, correlation_id): """Places an order and returns the order ID.""" try: order_id = smartApi.placeOrder(order_params) logger.info(f"[{correlation_id}] Order placed successfully: {order_id}") return order_id except Exception as e: logger.error(f"[{correlation_id}] Order placement failed: {e}") return None # Main function to execute buy/sell orders with single entry price def main(): correlation_id = "abcde" try: # Generate OTP token = os.getenv("OTP_SECRET_TOKEN", "8JDHHSKKKDURYRUWOJHDKFNCDJHFHGDS") otp = generate_otp(token, correlation_id) # Login to API session_data = smartApi.generateSession(username, password, otp) if session_data.get('status', False): logger.info(f"[{correlation_id}] Logged in successfully.") order_placed = False # Track if order is placed while not order_placed: # Fetch LTP ltp_price = fetch_ltp(correlation_id) if ltp_price is None: continue # Agar LTP nahi aayi to dobara try kare # Check for Entry Price if ltp_price == entry_price: logger.info(f"[{correlation_id}] LTP {ltp_price} reached entry price {entry_price}. Placing orders...") # Define quantity num_lots = 1 lot_size = 75 quantity = num_lots * lot_size # Prepare order parameters buy_order_params, sell_order_params = prepare_order_params(entry_price, quantity, correlation_id) # Place BUY Order buy_order_id = place_order(buy_order_params, correlation_id) if buy_order_id: logger.info(f"[{correlation_id}] Buy Order ID: {buy_order_id}") # Place SELL Order (Stoploss Order) sell_order_id = place_order(sell_order_params, correlation_id) if sell_order_id: logger.info(f"[{correlation_id}] Sell Order ID: {sell_order_id}") # Mark this order as placed order_placed = True time.sleep(sleep_time) # Check LTP every 100ms else: logger.error(f"[{correlation_id}] Session generation failed: {session_data}") except Exception as e: logger.exception(f"[{correlation_id}] Error in execution: {e}") # Run script if __name__ == "__main__": main() Copy Copied Text: You Might Also Like Post a Comment
No comments