Hello Dhan Team & Community Members,
I have developed a Pine Script strategy for Nifty 50 with built-in daily stop-loss and kill switch logic. However, since I do not have a programming or coding background, I am unable to configure the Dhan Python API setup on my own.
I need an automated setup using Dhan’s free Python API that can do the following:
Continuously monitor my live MTM (Profit/Loss).
If my total daily loss Limit, immediately square off all open positions at market price.
Automatically activate the trading Kill Switch / deactivate the F&O segment for the day to prevent further trades.
Could any developer or community member please share a ready-to-use Python script and guide me on how to run it step-by-step (or via AnyDesk / remote support)?
Thank you for your help!
You can deploy it on Dhan Cloud, which offers a convenient way to run your strategy without setting up the environment on your local machine.
We request you to refer to the Dhan Cloud documentation for details on configuration, deployment, and applicable charges:
Alternatively, if you wish to configure and run the strategy on your local machine using the Dhan Python API, the setup and implementation would need to be managed at your end.
The Pine side is only half of it, and the part you’re stuck on is the bridge. Pine runs on TradingView, so the usual path is a TradingView alert firing a webhook to a small server you control, and that server places the order through the Dhan API. Your daily stop-loss and kill switch logic can live in that server too, so it checks your running loss and stops sending orders once the limit is hit, rather than trusting the alert alone. Two things to get right: the server needs a fixed address registered with Dhan, since API orders have to come from a static IP, and webhooks need a TradingView plan that supports them, so check yours. Also handle duplicate alerts and alerts outside market hours. Run it on paper with live alerts before real orders.
I set up this kind of thing for people and run my own on Dhan. If you paste your Pine alert message format here, I can tell you what the server would need to read from it.
First thing I’d check is the Dhan API’s kill switch endpoint and the positions/funds calls. Polling the positions endpoint for realised plus unrealised P&L, then firing exit orders and the kill switch call once the limit breaks, is very doable in a small script. Doing it safely with retries and a hard stop is the real work. I take on small builds like this at a fixed price, with the source code delivered. Is your Pine Script alerting through webhooks today, or running only on TradingView?