HOW TO PLACE STOP LOSS ORDER AFTER COMPLETION OF BUY ORDER

rahulchoure4541
def PlaceBuyOrderMarketNSE(tradingsymbol, qty):
orderid = 1
try:
orderid = kite.place_order(variety=kite.VARIETY_REGULAR,
exchange=kite.EXCHANGE_NSE,
product=kite.PRODUCT_CNC,
order_type=kite.ORDER_TYPE_MARKET,
tradingsymbol= tradingsymbol,
transaction_type=kite.TRANSACTION_TYPE_BUY,
quantity = qty
)
print("Order placed successfully. Order ID: {}".format(orderid))
except Exception as e:
print("Order placement failed: {}".format(e))
return orderid

def Export15minuteHistoricalDataByDate(from_date, to_date, symbol):
if from_date > to_date:
return
token = db.get_instrument_token(symbol)
if token == -1:
print('Invalid symbol provided')
return 'None'
# provide interval as per candle size given above
interval = '15minute'
records = kite.historical_data(token, from_date=from_date, to_date=to_date, interval=interval)
df = pd.DataFrame(records)
if len(df) == 0:
print('No data returned')
return
df.drop('volume', inplace=True, axis=1)
df.set_index('date',inplace=True)
return df

while True:
today = datetime.datetime.today().strftime('%Y-%m-%d')
sdate = 'today'
edate = 'today'
df = Export15minuteHistoricalDataByDate(today, today, 'NTPC')
df.to_csv('symbol.csv')
data = pd.read_csv("symbol.csv", parse_dates=["date"], index_col="date")
# Fetch 15 minute Data of the day
df15min = data # assuming its the same data
high = df15min.high.iloc[0]
close = df15min.close.iloc[-2]
if high < close:
orderid = PlaceBuyOrderMarketNSE('NTPC', 100)
print("Placed order with ID: {}".format(orderid))
break # Exit the loop after placing the order
time.sleep(1) # sleep for 1 seconds before continuing





Can anyone help me to write python code to place stop loss order at 1% of buy order price after successful placement of buy order
Tagged:
  • rajnivp
    You can receive order updates over websocket and then once you get order filling confirmation you can place SL order.
  • tahseen
    There are two different use cases
    1. When you get filled completely
    2. When you get filled partially

    In both the cases you should start placing SL orders as per the fill and update as more fills happen
Sign In or Register to comment.