OPEN-SOURCE SCRIPT
Fix Webhook Latency & DH-905 Errors (PineScript to Python Bridge

If you are routing TradingView alerts directly to an Indian broker API (like Dhan or Zerodha) and experiencing execution delays or DH-905 lot size rejections, the flaw is in your routing architecture.
The Problem: Dynamic vs. Static Data
PineScript generates dynamic data. A moving average crossover fires a signal based on a dynamic {{close}} price. However, broker APIs require static Exchange Tokens and rigid SEM_LOT_SIZE multipliers (e.g., Nifty = 65).
If you try to pass quantity: 1 directly to the Dhan API, or if your hardcoded exchange token moves out of the ATM range overnight, the exchange rejects the payload. Furthermore, standard third-party webhook routing adds 1 to 2 seconds of latency, destroying your entry price on fast breakouts.
The Middleware Solution
The PineScript above is Step 1. It formats a clean JSON payload containing the dynamic variables.
Step 2 is interception. You must not send this JSON to the broker. You must send it to a localized Python Flask server hosted on a headless Ubuntu VPS.
On startup, your Python server downloads the NSE Scrip Master directly into a Pandas DataFrame stored in RAM. When this PineScript webhook hits your VPS, the Pandas logic instantly snaps the dynamic {{close}} price to the nearest ATM strike, extracts the exact static Exchange Token, and routes the order to the API.
By removing the third-party webhook bottleneck and relying on RAM-cached token lookups, execution time drops to sub-50ms.
The Open-Source Architecture
I engineered this complete routing logic to eliminate my own Nifty options slippage. I have open-sourced the underlying Python Flask and Pandas routing logic so you can build out the middleware yourself.
Check the GitHub repository for the full Python architecture: github.com/codetradesalgo-cmyk/dhan-api-tradingview-webhook-python
The Problem: Dynamic vs. Static Data
PineScript generates dynamic data. A moving average crossover fires a signal based on a dynamic {{close}} price. However, broker APIs require static Exchange Tokens and rigid SEM_LOT_SIZE multipliers (e.g., Nifty = 65).
If you try to pass quantity: 1 directly to the Dhan API, or if your hardcoded exchange token moves out of the ATM range overnight, the exchange rejects the payload. Furthermore, standard third-party webhook routing adds 1 to 2 seconds of latency, destroying your entry price on fast breakouts.
The Middleware Solution
The PineScript above is Step 1. It formats a clean JSON payload containing the dynamic variables.
Step 2 is interception. You must not send this JSON to the broker. You must send it to a localized Python Flask server hosted on a headless Ubuntu VPS.
On startup, your Python server downloads the NSE Scrip Master directly into a Pandas DataFrame stored in RAM. When this PineScript webhook hits your VPS, the Pandas logic instantly snaps the dynamic {{close}} price to the nearest ATM strike, extracts the exact static Exchange Token, and routes the order to the API.
By removing the third-party webhook bottleneck and relying on RAM-cached token lookups, execution time drops to sub-50ms.
The Open-Source Architecture
I engineered this complete routing logic to eliminate my own Nifty options slippage. I have open-sourced the underlying Python Flask and Pandas routing logic so you can build out the middleware yourself.
Check the GitHub repository for the full Python architecture: github.com/codetradesalgo-cmyk/dhan-api-tradingview-webhook-python
סקריפט קוד פתוח
ברוח האמיתית של TradingView, יוצר הסקריפט הזה הפך אותו לקוד פתוח, כך שסוחרים יוכלו לעיין בו ולאמת את פעולתו. כל הכבוד למחבר! אמנם ניתן להשתמש בו בחינם, אך זכור כי פרסום חוזר של הקוד כפוף ל־כללי הבית שלנו.
כתב ויתור
המידע והפרסומים אינם מיועדים להיות, ואינם מהווים, ייעוץ או המלצה פיננסית, השקעתית, מסחרית או מכל סוג אחר המסופקת או מאושרת על ידי TradingView. קרא עוד ב־תנאי השימוש.
סקריפט קוד פתוח
ברוח האמיתית של TradingView, יוצר הסקריפט הזה הפך אותו לקוד פתוח, כך שסוחרים יוכלו לעיין בו ולאמת את פעולתו. כל הכבוד למחבר! אמנם ניתן להשתמש בו בחינם, אך זכור כי פרסום חוזר של הקוד כפוף ל־כללי הבית שלנו.
כתב ויתור
המידע והפרסומים אינם מיועדים להיות, ואינם מהווים, ייעוץ או המלצה פיננסית, השקעתית, מסחרית או מכל סוג אחר המסופקת או מאושרת על ידי TradingView. קרא עוד ב־תנאי השימוש.