Sir by the way my api key or password is wrong but by the way i want to ask how to delete the post
as5320029
@as5320029
Best posts made by as5320029
Latest posts made by as5320029
-
RE: Stoploss is applied to exit the position of placed order. While it is pending then to cancel it
-
Stoploss is applied to exit the position of placed order. While it is pending then to cancel it
According to this script, when sbi's buy order is placed, then when a stoploss_limit order is placed to exit its position, whose Verity is "STOPLOSS" while it is pending, I want to cancel this pending order. , when I run this script, the order with STOPLOSS Verity is not canceled, please can someone tell me what I am doing wrong, when I run the script, then """ Sell order cancelled: 2d8c042c-2896- 4341-8b55-8b3b075d76be """ is received but still order is not canceled, remains in pending
import requests
from SmartApi.smartConnect import SmartConnect
from SmartApi.smartWebSocketOrderUpdate import SmartWebSocketOrderUpdate
from SmartApi.smartWebSocketV2 import SmartWebSocketV2
import pyotp
from datetime import datetime
import time
import threadingprint("Script ko shuru karte hain")
LIVE_FEED_JSON = {}
SmartAPI connection shuru karein
api_key = '7yrR5r'
username = 'A286220'
pwd = '1009'
smartApi = SmartConnect(api_key)OTP token generate karein
try:
token = "UZTNGOU7FW3QMY5DPC6OB73OEA"
totp = pyotp.TOTP(token).now()
except Exception as e:
print("Invalid Token: Diya gaya token valid nahi hai.")
raise eSession generate karein
data = smartApi.generateSession(username, pwd, totp)
Session generation ka response handle karein
if not data['status']:
print(data)
exit()authToken = data['data']['jwtToken']
refreshToken = data['data']['refreshToken']
feedToken = smartApi.getfeedToken()
res = smartApi.getProfile(refreshToken)
smartApi.generateToken(refreshToken)
exchanges = res['data']['exchanges']WebSocket configuration
AUTH_TOKEN = authToken
API_KEY = "7yrR5r"
CLIENT_CODE = "A2220"
FEED_TOKEN = feedToken
correlation_id = "abc123"
mode = 1
token_list = [{"exchangeType": 1, "tokens": ["3045"]}, {"exchangeType": 2, "tokens": ["3045"]}]SmartWebSocketV2 instance initialize karein
sws = SmartWebSocketV2(AUTH_TOKEN, API_KEY, CLIENT_CODE, FEED_TOKEN)
WebSocket event handlers define karein
def on_data(wsapp, message):
try:
token = message.get('token')
ltp = message.get('last_traded_price') / 100
exchange_timestamp = datetime.fromtimestamp(message.get('exchange_timestamp') / 1000).isoformat()
oi = message.get('open_interest')parsed_data = { 'token': token, 'ltp': ltp, 'exchange_timestamp': exchange_timestamp, 'oi': oi } print("Parsed data:", parsed_data) LIVE_FEED_JSON[token] = parsed_data except Exception as e: print("Data parsing mein error:", e)
def on_control_message(wsapp, message):
print(f"Control Message: {message}")def on_open(wsapp):
print("WebSocket connection opened")
sws.subscribe(correlation_id, mode, token_list)def on_error(wsapp, error):
print("WebSocket Error:", error)
# Retry connection
sws.connect()def on_close(wsapp):
print("WebSocket connection closed")
# Retry connection
sws.connect()SmartWebSocketV2 instance ko event handlers assign karein
sws.on_open = on_open
sws.on_data = on_data
sws.on_error = on_error
sws.on_close = on_close
sws.on_control_message = on_control_messageWebSocket connection thread ko start karein
sws_thread = threading.Thread(target=sws.connect)
sws_thread.start()
print('WebSocket connection initiated')Order place function define karein
def place_order(symboltoken, quantity, tradingsymbol):
try:
order_params = {
"variety": "NORMAL",
"tradingsymbol": tradingsymbol,
"symboltoken": symboltoken,
"transactiontype": "BUY",
"exchange": "NSE",
"ordertype": "MARKET",
"producttype": "INTRADAY",
"duration": "DAY",
"price": "0",
"quantity": quantity
}
response = smartApi.placeOrderFullResponse(order_params)
if response and response['status']:
uniqueOrderID = response['data']['uniqueorderid']
print("Order successfully placed. Unique Order ID:", uniqueOrderID)
return uniqueOrderID
else:
print("Failed to place order:", response['message'] if response else "Response is None")
return None
except Exception as e:
print("Error placing order:", e)
return NoneSell order function define karein
def place_sell_order(symboltoken, quantity, order_id, tradingsymbol):
try:
order_params = {
"variety": "STOPLOSS",
"orderid": order_id,
"tradingsymbol": tradingsymbol,
"symboltoken": symboltoken,
"transactiontype": "SELL",
"exchange": "NSE",
"ordertype": "STOPLOSS_LIMIT",
"producttype": "INTRADAY",
"duration": "DAY",
"triggerprice": "790",
"price": "770",
"squareoff": "0",
"stoploss": "0",
"quantity": quantity
}
response = smartApi.placeOrderFullResponse(order_params)
if response and response['status']:
uniqueOrderID = response['data']['uniqueorderid']
print("Order successfully placed. Unique Order ID:", uniqueOrderID)
return uniqueOrderID
else:
print("Failed to place order:", response['message'] if response else "Response is None")
return None
except Exception as e:
print("Error placing order:", e)
return NoneCancel order function
def cancelOrder(order_id, variety):
try:
orderResponse = smartApi.cancelOrder(order_id, variety)
return orderResponse
except Exception as e:
print("Error cancelling order:", e)
return NoneOrder update event handler define karein
def on_order_update(wsapp, message):
print("Order Update Received:", message)SmartWebSocketOrderUpdate instance initialize karein
order_update_client = SmartWebSocketOrderUpdate(AUTH_TOKEN, API_KEY, CLIENT_CODE, FEED_TOKEN)
Order update event handlers define karein
def on_order_update_open(wsapp):
print("Order Update WebSocket connection opened")def on_order_update_error(wsapp, error):
print("WebSocket Order Update Error:", error)def on_order_update_close(wsapp, close_status_code, close_msg):
passSmartWebSocketOrderUpdate instance ko event handlers assign karein
order_update_client.on_open = on_order_update_open
order_update_client.on_error = on_order_update_error
order_update_client.on_close = on_order_update_close
order_update_client.on_order_update = on_order_updateOrder Update WebSocket connection thread ko start karein
order_update_thread = threading.Thread(target=order_update_client.connect)
order_update_thread.start()
print('Order Update WebSocket connection initiated')def monitor_and_place_orders():
order_1_id = None
sell_order_id = None
try:
while True:
# Condition A
if '3045' in LIVE_FEED_JSON and LIVE_FEED_JSON['3045']['ltp'] > 740 and order_1_id is None:
order_1_id = place_order('3045', '1', "SBIN-EQ")
if order_1_id:
print("Order 1 placed:", order_1_id)# Condition B if order_1_id: order_status = smartApi.individual_order_details(order_1_id)['data']['orderstatus'] if order_status == 'complete': print("Order 1 completed.") # Condition C if not sell_order_id and '3045' in LIVE_FEED_JSON and LIVE_FEED_JSON['3045']['ltp'] < 815: # Place sell order to exit position sell_order_id = place_sell_order('3045', 1, order_1_id, "SBIN-EQ") print("Sell order placed to exit position:", sell_order_id) # Condition D if sell_order_id: order_status = smartApi.individual_order_details(sell_order_id)['data']['orderstatus'] if order_status == 'trigger pending' and LIVE_FEED_JSON['3045']['ltp'] < 817: # Cancel the sell order cancelOrder(sell_order_id, "STOPLOSS") print("Sell order cancelled:", sell_order_id) time.sleep(10) finally: # WebSocket connections ko band karein sws.close_connection() order_update_client.close_connection()
monitor_thread = threading.Thread(target=monitor_and_place_orders)
monitor_thread.start() -
Error Attempting to resub scribe/reconnect (Attempt 1)...
W 240409 11:39:06 smartWebSocketV2:319] Attempting to resub
scribe/reconnect (Attempt 1)...
WebSocket Order Update Error: on_order_update_close() takes
1 positional argument but 3 were given
WebSocket connections closed
-
Order status related
Sir, I wanted your help for a long time
Below is the coding related - sell part
Problem - I have that when the order place is pending but not executed then LIVE_FEED_JSON['3045']['ltp'] < 760 (less than 760) then a short sell order appears.
I want - when the order is placed and then executed, then only sell order request is made to exit the position of the place order. (If the order is only pending but not executed, then the sell order request should not appear)
Which function will be required to find out the status of an order through order status so that we can take an action after that.
def monitor_and_place_orders():
order_1_placed = False
order_1_id = Nonewhile True: try: if not order_1_placed and '3045' in LIVE_FEED_JSON and LIVE_FEED_JSON['3045']['ltp'] > 764: trigger_price_1 = LIVE_FEED_JSON['3045']['ltp'] order_price_1 = LIVE_FEED_JSON['3045']['ltp'] order_1_id = place_order_1('3045', '1', trigger_price_1, order_price_1) if order_1_id: print('place_order_1', LIVE_FEED_JSON['3045']['ltp']) order_1_placed = True # Check for condition to place sell order if order_1_placed and '3045' in LIVE_FEED_JSON and LIVE_FEED_JSON['3045']['ltp'] < 760: sell_order_id = place_sell_order('3045', '1', order_1_id) if sell_order_id: print('Sell order placed for order ID:', order_1_id) break # Exit loop after placing sell order
-
RE: Login execute ho chuka hai ya nhi for niche di coding ki mutabik
@Mnagesh Sir, I wanted your help for a long time
Below is the coding related - sell part
Problem - I have that when the order place is pending but not executed then LIVE_FEED_JSON['3045']['ltp'] < 760 (less than 760) then a short sell order appears.
I want - when the order is placed and then executed, then only sell order request is made to exit the position of the place order. (If the order is only pending but not executed, then the sell order request should not appear)
Which function will be required to find out the status of an order through order status so that we can take an action after that.
def monitor_and_place_orders():
order_1_placed = False
order_1_id = Nonewhile True: try: if not order_1_placed and '3045' in LIVE_FEED_JSON and LIVE_FEED_JSON['3045']['ltp'] > 764: trigger_price_1 = LIVE_FEED_JSON['3045']['ltp'] order_price_1 = LIVE_FEED_JSON['3045']['ltp'] order_1_id = place_order_1('3045', '1', trigger_price_1, order_price_1) if order_1_id: print('place_order_1', LIVE_FEED_JSON['3045']['ltp']) order_1_placed = True # Check for condition to place sell order if order_1_placed and '3045' in LIVE_FEED_JSON and LIVE_FEED_JSON['3045']['ltp'] < 760: sell_order_id = place_sell_order('3045', '1', order_1_id) if sell_order_id: print('Sell order placed for order ID:', order_1_id) break # Exit loop after placing sell order
-
Order status related
Sir, I wanted your help for a long time
Below is the coding related - sell part
Problem - I have that when the order place is pending but not executed then LIVE_FEED_JSON['3045']['ltp'] < 760 (less than 760) then a short sell order appears.
I want - when the order is placed and then executed, then only sell order request is made to exit the position of the place order. (If the order is only pending but not executed, then the sell order request should not appear)
Which function will be required to find out the status of an order through order status so that we can take an action after that.
def monitor_and_place_orders():
order_1_placed = False
order_1_id = Nonewhile True: try: if not order_1_placed and '3045' in LIVE_FEED_JSON and LIVE_FEED_JSON['3045']['ltp'] > 764: trigger_price_1 = LIVE_FEED_JSON['3045']['ltp'] order_price_1 = LIVE_FEED_JSON['3045']['ltp'] order_1_id = place_order_1('3045', '1', trigger_price_1, order_price_1) if order_1_id: print('place_order_1', LIVE_FEED_JSON['3045']['ltp']) order_1_placed = True # Check for condition to place sell order if order_1_placed and '3045' in LIVE_FEED_JSON and LIVE_FEED_JSON['3045']['ltp'] < 760: sell_order_id = place_sell_order('3045', '1', order_1_id) if sell_order_id: print('Sell order placed for order ID:', order_1_id) break # Exit loop after placing sell order
-
RE: Login execute ho chuka hai ya nhi for niche di coding ki mutabik
@Mnagesh Sir, I wanted your help for a long time
Below is the coding related - sell part
Problem - I have that when the order place is pending but not executed then LIVE_FEED_JSON['3045']['ltp'] < 760 (less than 760) then a short sell order appears.
I want - when the order is placed and then executed, then only sell order request is made to exit the position of the place order. (If the order is only pending but not executed, then the sell order request should not appear)
Which function will be required to find out the status of an order through order status so that we can take an action after that.
def monitor_and_place_orders():
order_1_placed = False
order_1_id = Nonewhile True: try: if not order_1_placed and '3045' in LIVE_FEED_JSON and LIVE_FEED_JSON['3045']['ltp'] > 764: trigger_price_1 = LIVE_FEED_JSON['3045']['ltp'] order_price_1 = LIVE_FEED_JSON['3045']['ltp'] order_1_id = place_order_1('3045', '1', trigger_price_1, order_price_1) if order_1_id: print('place_order_1', LIVE_FEED_JSON['3045']['ltp']) order_1_placed = True # Check for condition to place sell order if order_1_placed and '3045' in LIVE_FEED_JSON and LIVE_FEED_JSON['3045']['ltp'] < 760: sell_order_id = place_sell_order('3045', '1', order_1_id) if sell_order_id: print('Sell order placed for order ID:', order_1_id) break # Exit loop after placing sell order
-
RE: Login execute ho chuka hai ya nhi for niche di coding ki mutabik
@as5320029 Sir, I wanted your help for a long time
Below is the coding related - sell part
Problem - I have that when the order place is pending but not executed then LIVE_FEED_JSON['3045']['ltp'] < 760 (less than 760) then a short sell order appears.
I want - when the order is placed and then executed, then only sell order request is made to exit the position of the place order. (If the order is only pending but not executed, then the sell order request should not appear)
Which function will be required to find out the status of an order through order status so that we can take an action after that.
def monitor_and_place_orders():
order_1_placed = False
order_1_id = Nonewhile True: try: if not order_1_placed and '3045' in LIVE_FEED_JSON and LIVE_FEED_JSON['3045']['ltp'] > 764: trigger_price_1 = LIVE_FEED_JSON['3045']['ltp'] order_price_1 = LIVE_FEED_JSON['3045']['ltp'] order_1_id = place_order_1('3045', '1', trigger_price_1, order_price_1) if order_1_id: print('place_order_1', LIVE_FEED_JSON['3045']['ltp']) order_1_placed = True # Check for condition to place sell order if order_1_placed and '3045' in LIVE_FEED_JSON and LIVE_FEED_JSON['3045']['ltp'] < 760: sell_order_id = place_sell_order('3045', '1', order_1_id) if sell_order_id: print('Sell order placed for order ID:', order_1_id) break # Exit loop after placing sell order
-
RE: Login execute ho chuka hai ya nhi for niche di coding ki mutabik
Sir, I wanted your help for a long time
Below is the coding related - sell part
Problem - I have that when the order place is pending but not executed then LIVE_FEED_JSON['3045']['ltp'] < 760 (less than 760) then a short sell order appears.
I want - when the order is placed and then executed, then only sell order request is made to exit the position of the place order. (If the order is only pending but not executed, then the sell order request should not appear)
Which function will be required to find out the status of an order through order status so that we can take an action after that.
def monitor_and_place_orders():
order_1_placed = False
order_1_id = Nonewhile True: try: if not order_1_placed and '3045' in LIVE_FEED_JSON and LIVE_FEED_JSON['3045']['ltp'] > 764: trigger_price_1 = LIVE_FEED_JSON['3045']['ltp'] order_price_1 = LIVE_FEED_JSON['3045']['ltp'] order_1_id = place_order_1('3045', '1', trigger_price_1, order_price_1) if order_1_id: print('place_order_1', LIVE_FEED_JSON['3045']['ltp']) order_1_placed = True # Check for condition to place sell order if order_1_placed and '3045' in LIVE_FEED_JSON and LIVE_FEED_JSON['3045']['ltp'] < 760: sell_order_id = place_sell_order('3045', '1', order_1_id) if sell_order_id: print('Sell order placed for order ID:', order_1_id) break # Exit loop after placing sell order
-
RE: Login execute ho chuka hai ya nhi for niche di coding ki mutabik
@Mnagesh Sir, I wanted your help for a long time
Below is the coding related - sell part
Problem - I have that when the order place is pending but not executed then LIVE_FEED_JSON['3045']['ltp'] < 760 (less than 760) then a short sell order appears.
I want - when the order is placed and then executed, then only sell order request is made to exit the position of the place order. (If the order is only pending but not executed, then the sell order request should not appear)
Which function will be required to find out the status of an order through order status so that we can take an action after that.
def monitor_and_place_orders():
order_1_placed = False
order_1_id = Nonewhile True: try: if not order_1_placed and '3045' in LIVE_FEED_JSON and LIVE_FEED_JSON['3045']['ltp'] > 764: trigger_price_1 = LIVE_FEED_JSON['3045']['ltp'] order_price_1 = LIVE_FEED_JSON['3045']['ltp'] order_1_id = place_order_1('3045', '1', trigger_price_1, order_price_1) if order_1_id: print('place_order_1', LIVE_FEED_JSON['3045']['ltp']) order_1_placed = True # Check for condition to place sell order if order_1_placed and '3045' in LIVE_FEED_JSON and LIVE_FEED_JSON['3045']['ltp'] < 760: sell_order_id = place_sell_order('3045', '1', order_1_id) if sell_order_id: print('Sell order placed for order ID:', order_1_id) break # Exit loop after placing sell order