def main():
configuration = upstox_client.Configuration()
configuration.access_token = access_token
streamer = upstox_client.MarketDataStreamer(
upstox_client.ApiClient(configuration))
def print_line():
print('-------------------------------------------------------------------')
def updateorder_leg1():
url = "https://api.upstox.com/v2/order/modify"
headers = {
'Accept': 'application/json',
'Content-Type' : 'application/json',
'Api-Version': '2.0',
'Authorization': f'Bearer {access_token}'
}
data={
"quantity": 15,
"validity": "DAY",
"price": f'{price_1}',
"order_id": f'{leg1_sell}',
"order_type": f'{order_type1}',
"disclosed_quantity": 0,
"trigger_price": f'{trigger_price_1}'
}
mod_response1 = requests.put(url, headers=headers, json=data)
print(mod_response1.text)
def updateorder_leg2():
url = "https://api.upstox.com/v2/order/modify"
headers = {
'Accept': 'application/json',
'Content-Type' : 'application/json',
'Api-Version': '2.0',
'Authorization': f'Bearer {access_token}'
}
data={
"quantity": 15,
"validity": "DAY",
"price": f'{price_2}',
"order_id": f'{leg2_sell}',
"order_type": f'{order_type2}',
"disclosed_quantity": 0,
"trigger_price": f'{trigger_price_2}'
}
mod_response2 = requests.put(url, headers=headers, json=data)
print(mod_response2.text)
def updateorder_leg3():
url = "https://api.upstox.com/v2/order/modify"
headers = {
'Accept': 'application/json',
'Content-Type' : 'application/json',
'Api-Version': '2.0',
'Authorization': f'Bearer {access_token}'
}
data={
"quantity": 15,
"validity": "DAY",
"price": f'{price_3}',
"order_id": f'{leg3_sell}',
"order_type": f'{order_type3}',
"disclosed_quantity": 0,
"trigger_price": f'{trigger_price_3}'
}
mod_response3 = requests.put(url, headers=headers, json=data)
print(mod_response3.text)
def updateorder_leg4():
url = "https://api.upstox.com/v2/order/modify"
headers = {
'Accept': 'application/json',
'Content-Type' : 'application/json',
'Api-Version': '2.0',
'Authorization': f'Bearer {access_token}'
}
data={
"quantity": 15,
"validity": "DAY",
"price": f'{price_4}',
"order_id": f'{leg4_sell}',
"order_type": f'{order_type4}',
"disclosed_quantity": 0,
"trigger_price": f'{trigger_price_4}'
}
mod_response4 = requests.put(url, headers=headers, json=data)
print(mod_response4.text)
def on_open():
streamer.subscribe(
[instrument_key], "ltpc")
def on_message(message):
print_line()
option_ltp = message['feeds'][instrument_key]['ltpc']['ltp']
print('Last Traded Price : ',message['feeds'][instrument_key]['ltpc']['ltp'])
print_line()
if option_ltp <= current_stoploss :
print("\n YOU ARE OUT OF TRADE WITH PLANNED STOPLOSS : KEEP YOUR CAPITAL SAFE : DO NOT OVER TRADE")
lots_remaining = 0
sys.exit()
if lots_remaining == 4 and option_ltp >= entryprice + 11:
price_1 = 0
trigger_price_1 = 0
order_type1 = "MARKET" # Order type : MARKET, LIMIT, SL, SL-M
orderupdate_trl1 = updateorder_leg1()
price_2 = (entryprice + 1)
trigger_price_2 = (price_2 + 0.5)
order_type2 = "SL" # Order type : MARKET, LIMIT, SL, SL-M
orderupdate_trl2 = updateorder_leg2()
price_3 = (entryprice + 1)
trigger_price_3 = (price_3 + 0.5)
order_type3 = "SL" # Order type : MARKET, LIMIT, SL, SL-M
orderupdate_trl3 = updateorder_leg3()
price_4 = (entryprice + 1)
trigger_price_4 = (price_4 + 0.5)
order_type4 = "SL" # Order type : MARKET, LIMIT, SL, SL-M
orderupdate_trl4 = updateorder_leg4()
current_stoploss = buy_price + 1
lots_remaining -= 1
print(f"Price rose above 11 points, sold 1st lot. New stoploss set to {current_stoploss}. Remaining lots: {lots_remaining}")
if lots_remaining == 3 and price >= buy_price + 21:
price_2 = 0
trigger_price_2 = 0
order_type2 = "MARKET" # Order type : MARKET, LIMIT, SL, SL-M
orderupdate_trl2 = updateorder_leg2()
lots_remaining -= 1
print(f"Price rose above 21 points, sold 2nd lot. Remaining lots: {lots_remaining}")
if lots_remaining == 2 and price >= buy_price + 31:
price_3 = 0
trigger_price_3 = 0
order_type3 = "MARKET" # Order type : MARKET, LIMIT, SL, SL-M
orderupdate_trl3 = updateorder_leg3()
print(f"Price rose above 31 points, sold 3rd lot. Remaining lots: {lots_remaining}")
lots_remaining -= 1
if lots_remaining == 1 and price >= buy_price + 50:
new_stoploss = price - 25
if new_stoploss > current_stoploss:
current_stoploss = new_stoploss
print(f"Price rose above 50 points, trailing stoploss of 4th lot updated to {current_stoploss}")
if lots_remaining == 1 and price >= buy_price + 75:
new_stoploss = price - 25
if new_stoploss > current_stoploss:
current_stoploss = new_stoploss
print(f"Price rose above 75 points, trailing stoploss updated to {current_stoploss}")
if lots_remaining == 1 and price >= buy_price + 100:
new_stoploss = price - 25
if new_stoploss > current_stoploss:
current_stoploss = new_stoploss
print(f"Price rose above 100 points, trailing stoploss updated to {current_stoploss}")
if lots_remaining == 1 and price >= buy_price + 125:
new_stoploss = price - 25
if new_stoploss > current_stoploss:
current_stoploss = new_stoploss
print(f"Price rose above 125 points, trailing stoploss updated to {current_stoploss}")
streamer.on("open", on_open)
streamer.on("message", on_message)
streamer.connect()
if name == “main”:
main()