How I built my algo system on top of DhanHQ APIs?

Here is the algorithmic flow chart I utilize for my trading, which incorporates the use of DHAN APIs:

  1. I gathered data from the past 10 days using the Dhan Historical Data API (Link) and stored it in a file. I used Excel to retrieve the data with Pandas, but other methods are also viable.
  2. I saved the Open Interest (OI) data, obtained from the NSE website, in a file. Since DhanAPI v1.2.3 cannot fetch OI data, I acquired it using requests and then processed it for use with Pandas.
  3. I downloaded the Security ID sheet from the DhanAPI site.
  4. I stored the intraday historical data obtained via the Dhan API in a file.
  5. To determine market bias, I simply compare the data from the previous day with the current market price.
  6. I derived support and resistance levels based on historical data, OI data, and intraday data using a custom formula. As I mainly trade on indices, this formula is applied to Nifty 50 and Bank Nifty. The logic is based on the amount of OI at respective strikes, previous resistances and supports, and the current market price. These three data points provide a rough estimate of the current day’s resistance and support levels.
  7. Logical Container 2 includes conditions such as:
    i. Current Market Price
    ii. OI Activity (L, S, LU, SC)
    iii. Derived intraday support and resistance, etc.
  8. Logical Container 3 allows for the selection of strikes either automatically by the algorithm in autopilot mode, or manually. This uses simple if-else logic: if autopilot is set to 1, then it operates independently; otherwise, manual intervention is required.
  9. Additionally, once conditions are met, alerts are sent via Telegram (integration with Telegram APIs is straightforward).
  10. Based on the identified strikes, the corresponding security IDs are fetched from the sheet mentioned in step 3, and the trade is executed.
  11. Trade execution can follow the strategy of your choice, such as Strangle, Straddle, Butterfly, Opening Range Breakout, etc. You need to develop the logic for this in Logical Container 2, and then monitor the conditions in Logical Container 3. I personally favor the Straddle strategy.
  12. In my algorithm, there are defined stop losses (SL) and targets. The positions are marked for exit once either is achieved.

Finally, all executed trades for the day are stored in a file for journaling purposes.

As of now , its working Ok-ish. However, i am constantly working to improve it.

Best part in this algo is DhanAPI which are robust, easy to use and easy to applied.
It is extremely useful with good documentation. Whole logic is based on it except OI Data and i believe that is covered in newer version of DhanAPI.

6 Likes

This is a great post, @Sammy!

It will be quite helpful for a lot of beginning traders, providing guidance on how they can start trading.

2 Likes

Great post! Found very helpful for me. :slight_smile:

2 Likes

Thank you for your appreciation.

1 Like

How I create websockets dhan please
Help me

Hi Tapas, we have created a detailed playlist for this on our YouTube channel. Check here - https://www.youtube.com/playlist?list=PLnuHyqUCoJsOy0NjnACJRFz98ByhYVkCu

Hi all,

We posted about the system @Sammy has built on top of our APIs across our channels:

Instagram: DhanHQ on Instagram: "Here’s how our community member built his own trading system on top of our APIs. Catch more such conversations happening only on http://MadeForTrade.in community. A community for like minded Traders & Investors who share their strategies, solve each other’s doubts and more. #dhan #madefortrade"

Twitter: https://x.com/DhanHQ/status/1771119993167761898?s=20

LinkedIn: Dhan on LinkedIn: API DhanHQ

3 Likes

Thank you very much @RahulDeshpande

hi @Rahul

is there any example code that shows how to place limit and stop orders? and also how to place stop loss for those respective orders once executed as a follow up?

can the stop loss order (prices) be placed along simultaenously with the single limit or stop order that is in a single order?

Once the order execute, It contains information like strike ID , Price etc.
Now for the very same strike ID , it place another order which is set to be Stop Loss.
As far as concern with the code snippets i refer to DHAN API documentation which is very neat and precise for setting up various orders.

you said that you wrote a code to analyze the data and implemented your logic and if all the conditions are mate it sends alert message to telegram does it executes trade or you do it manually and the Documentation only provides quantity based codes i want to write a code which executes orders on specific amount which ill enter in my code manually

Telegram alerts serve solely for informational and notification purposes. Conditional checks are conducted against specific instrument levels automatically, based on logical calculations derived from the initial logical container. When the criteria are met, trades are executed automatically. As for the code and logic, they are proprietary.

1 Like

If you wish to place an order for a specific amount, manual intervention is required. The first two logical containers will furnish you with the necessary information and generate alerts. Subsequently, you must configure the code to manually input the respective strike ID, depending on the amount intended for the trade, and execute it accordingly.

1 Like

hi @Sammy

refering from another thread on your comments: can the order not involve both stoploss and takeprofit in a single order execution with limit/stop price?

For Option trading, No, you can not place both orders

Ok, just so that I understand it correctly, could you please affirm that place orders at limit or stop prices using the bracket order as described in Bracket Order at Market Price - #3 by Hardik will not work?

Hi @mnikhil ,
I never tried to place BO for option using API.
Will check it and share you the details and still strongly believe you can not place BO for Options.