Hi team, @Ketan_Gupta
Following is the code written by using ChatGPT
I’m unable to place limit orders. Can anyone help me out by correcting the code
import time
from datetime import datetime
import requests
import json
URL_PLACE = ‘https://api-hft.upstox.com/v2/order/place’
URL_MODIFY = ‘https://api-hft.upstox.com/v2/order/modify’
HEADERS = {
‘Content-Type’: ‘application/json’,
‘Accept’: ‘application/json’,
‘Authorization’: ‘Bearer {access token}’,
}
INSTRUMENT_TOKENS = {
“WAAREE ENERGIES”: “NSE_EQ|INE377N01017”,
“OLA”: “NSE_EQ|INE0LXG01040”,
“SWIGGY”: “NSE_EQ|INE00H001014”,
}
def round_price(price):
return round(price * 20) / 20 # Round to nearest 0.05
def place_order(quantity, price, instrument_token, order_type, transaction_type):
data = {
‘quantity’: quantity,
‘product’: ‘I’,
‘validity’: ‘DAY’,
‘price’: round_price(price),
‘tag’: ‘string’,
‘instrument_token’: instrument_token,
‘order_type’: order_type,
‘transaction_type’: transaction_type,
‘disclosed_quantity’: 0,
‘trigger_price’: None,
‘is_amo’: False,
}
response = requests.post(URL_PLACE, headers=HEADERS, json=data)
return response.json()
def modify_order(order_id, quantity, price, order_type):
data = json.dumps({
“quantity”: quantity,
“validity”: “DAY”,
“price”: round_price(price),
“order_id”: order_id,
“order_type”: order_type,
“disclosed_quantity”: 0,
“trigger_price”: 0
})
response = requests.put(URL_MODIFY, headers=HEADERS, data=data)
return response.json()
def calculate_order_prices(market_opening_price):
buy_limit_price = round_price(market_opening_price * 1.0075)
sell_limit_price = round_price(market_opening_price * 0.9925)
buy_stop_loss = round_price(buy_limit_price * 0.995)
sell_stop_loss = round_price(sell_limit_price * 1.005)
buy_target = round_price(buy_limit_price * 1.01)
sell_target = round_price(sell_limit_price * 0.99)
return buy_limit_price, buy_stop_loss, buy_target, sell_limit_price, sell_stop_loss, sell_target
def main():
stock_data = {}
stocks = [“OLA”, “SWIGGY”, “WAAREE ENERGIES”]
for stock in stocks:
market_opening_price = float(input(f"Enter market opening price for {stock}: "))
quantity = int(input(f"Enter quantity to trade for {stock}: "))
buy_limit, buy_stop, buy_target, sell_limit, sell_stop, sell_target = calculate_order_prices(market_opening_price)
stock_data[stock] = {
"market_opening_price": market_opening_price,
"buy_limit": buy_limit,
"buy_stop": buy_stop,
"buy_target": buy_target,
"sell_limit": sell_limit,
"sell_stop": sell_stop,
"sell_target": sell_target,
"quantity": quantity,
"position": None,
"buy_order_id": None,
"sell_order_id": None
}
for stock, data in stock_data.items():
instrument_token = INSTRUMENT_TOKENS[stock]
print(f"Placing Buy Limit Order for {stock} at {data['buy_limit']} with Stop Loss {data['buy_stop']} and Target {data['buy_target']}")
buy_response = place_order(data['quantity'], data['buy_limit'], instrument_token, 'LIMIT', 'BUY')
data['buy_order_id'] = buy_response.get("order_id")
print(f"Placing Sell Limit Order for {stock} at {data['sell_limit']} with Stop Loss {data['sell_stop']} and Target {data['sell_target']}")
sell_response = place_order(data['quantity'], data['sell_limit'], instrument_token, 'LIMIT', 'SELL')
data['sell_order_id'] = sell_response.get("order_id")
while True:
for stock, data in stock_data.items():
current_price = float(input(f"Enter current market price for {stock}: "))
instrument_token = INSTRUMENT_TOKENS[stock]
if data["position"] is None:
if current_price >= data['buy_limit']:
print(f"Buy order executed for {stock} at {data['buy_limit']}")
data["position"] = "long"
modify_order(data['sell_order_id'], data['quantity'], data['market_opening_price'], 'LIMIT')
elif current_price <= data['sell_limit']:
print(f"Sell order executed for {stock} at {data['sell_limit']}")
data["position"] = "short"
modify_order(data['buy_order_id'], data['quantity'], data['market_opening_price'], 'LIMIT')
time.sleep(1)
if name == “main”:
main()