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. اقرأ المزيد في شروط الاستخدام.