Hello,
Please help to write a code in Python in which I want to exit from positions if the target or stop loss is achieved. I am a beginner in Python but have got training in it.
Hello,
Please help to write a code in Python in which I want to exit from positions if the target or stop loss is achieved. I am a beginner in Python but have got training in it.
def placeOrderFyers(inst , t_type, qty, order_type, price, variety):
exch = inst[:3]
symb = inst[4:]
if(order_type=="MARKET"):
type1 = 2
price = 0
elif(order_type=="LIMIT"):
type1 = 1
if(t_type=="BUY"):
side1=1
elif(t_type=="SELL"):
side1=-1
data = {
"symbol":inst,
"qty":qty,
"type":type1,
"side":side1,
"productType":"INTRADAY",
"limitPrice":0,
"stopPrice":0,
"validity":"DAY",
"disclosedQty":0,
"offlineOrder":"False",
"stopLoss":0,
"takeProfit":0
}
try:
orderid = fyers.place_order(data)
return orderid
except Exception as e:
print("Failed : {} ".format(e))
#INPUT####
client_id = ""
access_token = ""
fyers = fyersModel.FyersModel(token=access_token,is_async=False,client_id=client_id,log_path="/")
target_price = 1300
stoploss_price = 1200
symbol = "NSE:INFY-EQ"
dir = "BUY"
intrade = 1
qty = 5
###########
while intrade == 1:
ltp = (fyers.quotes(symbol))['d'][0]['v']['lp']
if ((dir == "BUY") and (ltp >= target_price) or (ltp = stoploss_price)):
oidexit = placeOrderFyers( symbol, "BUY", qty, "MARKET", 0, "regular")
print("Order Exited: ", oidexit)
intrade = 0
else:
time.sleep(1)