How to implement trading logic?

import requests

url = ‘https://api-hft.upstox.com/v2/order/place
headers = {
‘Content-Type’: ‘application/json’,
‘Accept’: ‘application/json’,
‘Authorization’: ‘Bearer {your_access_token}’,
}

data = {
‘quantity’: 1,
‘product’: ‘I’,
‘validity’: ‘DAY’,
‘price’: 0,
‘tag’: ‘string’,
‘instrument_token’: ‘NSE_EQ|INE528G01035’,
‘order_type’: ‘MARKET’,
‘transaction_type’: ‘BUY’,
‘disclosed_quantity’: 0,
‘trigger_price’: 0,
‘is_amo’: False,
}

try:
# Send the POST request
response = requests.post(url, json=data, headers=headers)

# Print the response status code and body
print('Response Code:', response.status_code)
print('Response Body:', response.json())

except Exception as e:
# Handle exceptions
print(‘Error:’, str(e))

The above program is the python code for placing intraday order. How do i implement trading logic within this to automate the trades (eg: buy a stock when a condition is met) ?

Any advice will be helpful🙏

Hello,

Could you please share the specific conditions or strategy you would like to implement for automating your trades? This will help in providing tailored guidance for integrating your trading logic into the program.

Looking forward to your response!

Thanks & Regards

@Sanjay_Jain I just wanted to learn how to implement some conditions for trial purposes. Lets say, on a 5 min timeframe, buy when RSI is >60. Any guidance?

Hello,

To implement a simple condition for trial purposes (like buying when RSI > 60 on a 5-minute timeframe), here’s a step-by-step guide:

1. Fetch Historical Data

To calculate RSI, you’ll need historical price data (typically close prices). Fetch 5-minute candlestick data (OHLC) using UpStox Historical or Intraday Candle API.

2. Use a Library for RSI Calculation

There are libraries that make calculating RSI easy:

  • TA-Lib
  • Pandas-Ta

Both libraries will let you calculate the 14-period RSI.

3. Check the Condition

Once you have the RSI values:

  • Check if RSI > 60 to signal a buy (indicating the stock is in an uptrend).

4. Automate the Process

Run this check periodically (every 5 minutes) to fetch new data, recalculate RSI, and check if the condition is met. If RSI > 60, place a buy order via your trading API.

5. Backtest Before Going Live

It’s essential to backtest your strategy on historical data to ensure that it performs well before you go live with real money.

This is a simple way to automate the condition. Once you’re comfortable with this, you can start enhancing it with additional conditions like GTT Trigger, Trailing SL etc.

Thanks & Regards

2 Likes

Thank you, @Sanjay_Jain, for your valuable responses. We appreciate your efforts in making our community more interactive and for supporting other users!

1 Like

@Sanjay_Jain Thank you for your reply. My query is, where to implement the logic (in code)? Should it be implemented in the code sample ive given above? Or a seperate code? Please shed some light

You need to create separate code for your trading logic. As mentioned by @Sanjay_Jain you need to create script that will fetch historical data and based on your entry condition it will place but order