Stochastic + DMI + ADX

I have uploaded a custom chart in Pine v6 on Trading Chart, it is verified but freezes and have to remove and load again. It also gives Runtime error: exceeding 2000ms. What can I do to rectify? It works flawless on Trading View

Hello,

Can you share your pine script with us on riken.mehta@dhan.co. we will look into it

I have sent you 2 scripts by email. Thanks

Thank you. We have recieved it. Will go through it and update you

Any update?

I have used it in Fyers, it s working beutifully

Sir ,

You can try running script again, it will work properly. Let us know if you face any issue

The reason this happens on broker-embedded charts while running smoothly on TradingView is execution architecture: Native TradingView compiles and executes Pine Script on their high-performance cloud clusters, whereas broker-hosted TV libraries run script evaluation directly in your client browser thread alongside live WebSocket market data tickers. To protect browser UI responsiveness, the engine enforces a strict 2000ms script execution timeout.

To fix the 2000ms runtime crash in Pine v6:

  1. Eliminate iterative loops on historical bars: If your custom script uses for loops to calculate rolling DMI True Range or Stochastic highs/lows across historical bars, switch entirely to built-in vectorized functions (ta.stoch(), ta.dmi(), ta.rma()). Built-in C++/Wasm bindings execute orders of magnitude faster than interpreted loops.
  2. Avoid dynamic array resizing: If you are using array.push() or array.unshift() inside bar evaluation loops, pre-allocate fixed-size buffers with array.new_float(size, 0.0) or leverage series history operators ([1], [2]) instead of arrays.
  3. Guard heavy calculations by visible bar window: If you have custom iterative logic that only needs to render on recent price action, guard it with:
if bar_index > last_bar_index - 1000
    // run custom calculation only on recent bars

This prevents the engine from redundantly executing heavy loop iterations over 10,000 historical bars during the initial chart load.

Thanks for the explanation. I checked, and my script has no for/while loops and no arrays. It only uses the built-ins ta.stoch, ta.sma and ta.dmi, so those fixes don’t apply.

I tested it alone on a Nifty 5-minute chart. With request.security in the script I get “tick exceeded 2000ms” or the table freezes. The same script with request.security removed runs fine and updates live, and a bare Stoch + DMI/ADX version also runs fine. Removing the table or the alerts doesn’t help while request.security stays.

Can you check how request.security is handled? The docs say scripts using it are recomputed from the first bar on every tick. I need a higher-timeframe Stochastic/DMI (weekly on a daily chart) within the 2 s budget. Is there a recommended approach, or can the budget be adjusted?

It works perfectly on Fyers.