OPEN-SOURCE SCRIPT
Momentum Gamma Straddle

Exact definition of what that script does
1) Purpose
The script is a decision aid for intraday expiry-day ATM straddle trades. It detects intraday structure breakouts and signals candidate long straddle entries for Nifty or Sensex using price structure, volume, RSI momentum, and a user-supplied combined ATM premium value (CE + PE). It draws support/resistance, shows an info box, and raises alerts.
2) Inputs the user can change
Trading time window: startHour, startMin, endHour, endMin.
Structure lookback: res_lookback (how many candles to use to compute resistance/support).
Minimum candle body as fraction of candle range: min_body_pct.
Volume multiplier threshold: vol_mult (breakout candle volume must exceed vol_mult * sma5).
RSI length and thresholds: rsi_len, rsi_bull_thresh, rsi_bear_thresh.
Combined premium source: choose Manual or Symbol. If Manual, set manual_combined. If Symbol, provide a TradingView symbol that returns CE+PE combined ATM premium.
Combined premium acceptable band: min_combined_ok and max_combined_ok.
Profit target percent and SL percent (target_pct and sl_pct).
Misc pattern heuristics: min_res_hits (min tests of resistance inside lookback), low_slope_min (used to detect rising lows).
Micro-confirmation toggle, micro timeframe, nonrepaint option, show_entry_label toggle (in the later fixed versions some of these were added, but the earlier fixed script had basic combined_symbol options and a lookahead fallback).
3) Data calculated on each bar
Safety check hasEnough: true when bar_index >= res_lookback.
resistance: the highest high over res_lookback bars.
support: the lowest low over res_lookback bars.
res_hits: count of bars within lookback whose high is within a tolerance of resistance. Tolerance is 10 percent of the range between resistance and support.
low_slope: simple slope of lows over res_lookback bars.
body_pct: the candle body as a fraction of its high-low range. strong_body true when body_pct >= min_body_pct.
bull_breakout: true if hasEnough and current close > resistance and strong_body and res_hits >= min_res_hits.
bear_breakout: true if hasEnough and current close < support and strong_body and res_hits >= min_res_hits.
vol_sma5 and vol_ok: vol_ok true when current volume > vol_mult * vol_sma5.
rsi and rsi checks: rsi_bull_ok true if rsi >= rsi_bull_thresh; rsi_bear_ok true if rsi <= rsi_bear_thresh.
combined_premium: either the manual_combined input or the value read from combined_symbol via request.security. The script attempted a fallback to manual when the symbol was not valid.
combined_ok: true if combined_premium lies between min_combined_ok and max_combined_ok.
final signals: bull_signal when in_time_window and bull_breakout and vol_ok and rsi_bull_ok and combined_ok. bear_signal similar for bearish breakout.
4) Visual output and alerts
Plots resistance and support lines on the chart.
Plots a label shape "STRADDLE BUY" below the bar for bull_signal and above the bar for bear_signal.
Creates an info label (on last bar) that shows TimeOK, VolOK and vol ratio, RSI, Combined premium and whether it is OK, ResHits and LowSlope.
Sets two alertcondition events: "Bull Straddle BUY" and "Bear Straddle BUY" with a short candidate message. The alerts fire when the corresponding signal is true.
5) Execution assumptions you must follow manually
The script does not place any orders or compute option strike-level prices or greeks. It only flags candidate entry bars.
When combined_source is Manual you must type CE+PE yourself. The indicator will only accept the manual number and treat it as the combined premium.
When combined_source is Symbol the script uses request.security to read that symbol. For historical bars the indicator may repaint depending on lookahead settings. The earlier fixed script attempted to use request.security inside a conditional which leads to runtime or compile errors. You experienced that exact error.
6) Known implementation caveats and bugs you encountered
Pine typing issue with low_slope. The earlier version set low_slope = na without explicit type. That triggers the Pine error: "Value with NA type cannot be assigned to a variable that was defined without type keyword". This required changing to float low_slope = na.
The earlier version attempted to call request.security() inside an if block or conditional. Pine prohibits request.security in conditional blocks unless allowed patterns are followed. That produced the error you saw: "Cannot use request.* call within loops or conditional structures" or similar. The correct pattern is to call request.security at top-level and decide later which value to use.
If combined_symbol is invalid or not available on your TradingView subscription, request.security can return na and the script must fall back to manual value. The earlier fixed script attempted fallback but compiled errors prevented reliable behavior.
The earlier script did not include micro-confirmation or advanced nonrepaint controls. Those were added in later versions. Because of that, the earlier script may have given signals that appear to repaint on historical bars or may have thrown errors when using combined_symbol.
7) Decision logic summary (exact)
Only operate if current chart time is inside user set time window.
Only consider trade candidates when enough history exists for res_lookback.
Identify a resistance level as the highest high in the lookback. Count how many times that resistance was tested. Ensure the breakout candle has a strong body and volume spike. Ensure RSI is aligned with breakout direction.
Require combined ATM premium to be inside a user preferred band. If combined_symbol is used the script tries to read that value and use it; otherwise it uses manual_combined input.
If all the above conditions are true on a confirmed bar, the script plots a STRADDLE BUY label and triggers an alertcondition.
8) What the script does not do
It does not calculate CE and PE prices by strike. It only consumes or accepts combined premium number.
It does not compute greeks, IV, or OI. OI and IV checks must be done manually.
It does not manage positions. No SL management or automatic exits are executed by the script.
It does not simulate fills or account for bid/ask spreads or slippage.
It cannot detect off-exchange block trades or read exchange-level auction states beyond raw volume bars.
It may repaint historical labels if the combined_symbol was read with lookahead_on or the script used request.security in a way that repainted. The corrected final version uses nonrepaint options.
9) Manual checks you must always perform even when the script signals BUY
Confirm the live combined ATM premium and the bid/ask for CE and PE.
Check ATM IV and recent IV movement for a potential IV crush risk.
Check option OI distribution and recent OI changes for strike pinning or large player exposure.
Confirm CE and PE liquidity and depth. Wide spreads make fills unrealistic.
Confirm there is no scheduled news or auction within the next few minutes.
Confirm margin and position sizing fits your risk plan.
10) Quick testing checklist you can run now
Add the script to a 5-minute chart with combined_source = Manual.
Enter manual_combined equal to the real CE+PE at the moment you test.
Set startHour and endHour so the in_time_window is true for current time.
Look for STRADDLE BUY label on confirmed bars. Inspect the info box to see why it did or did not signal.
If you set combined_source = Symbol, verify the symbol exists and that TradingView returns values for it. If you previously saw the request.security error, that was caused by placing the request inside a conditional. The correct behavior is to call request.security unconditionally at top-level like in the final fixed version.
1) Purpose
The script is a decision aid for intraday expiry-day ATM straddle trades. It detects intraday structure breakouts and signals candidate long straddle entries for Nifty or Sensex using price structure, volume, RSI momentum, and a user-supplied combined ATM premium value (CE + PE). It draws support/resistance, shows an info box, and raises alerts.
2) Inputs the user can change
Trading time window: startHour, startMin, endHour, endMin.
Structure lookback: res_lookback (how many candles to use to compute resistance/support).
Minimum candle body as fraction of candle range: min_body_pct.
Volume multiplier threshold: vol_mult (breakout candle volume must exceed vol_mult * sma5).
RSI length and thresholds: rsi_len, rsi_bull_thresh, rsi_bear_thresh.
Combined premium source: choose Manual or Symbol. If Manual, set manual_combined. If Symbol, provide a TradingView symbol that returns CE+PE combined ATM premium.
Combined premium acceptable band: min_combined_ok and max_combined_ok.
Profit target percent and SL percent (target_pct and sl_pct).
Misc pattern heuristics: min_res_hits (min tests of resistance inside lookback), low_slope_min (used to detect rising lows).
Micro-confirmation toggle, micro timeframe, nonrepaint option, show_entry_label toggle (in the later fixed versions some of these were added, but the earlier fixed script had basic combined_symbol options and a lookahead fallback).
3) Data calculated on each bar
Safety check hasEnough: true when bar_index >= res_lookback.
resistance: the highest high over res_lookback bars.
support: the lowest low over res_lookback bars.
res_hits: count of bars within lookback whose high is within a tolerance of resistance. Tolerance is 10 percent of the range between resistance and support.
low_slope: simple slope of lows over res_lookback bars.
body_pct: the candle body as a fraction of its high-low range. strong_body true when body_pct >= min_body_pct.
bull_breakout: true if hasEnough and current close > resistance and strong_body and res_hits >= min_res_hits.
bear_breakout: true if hasEnough and current close < support and strong_body and res_hits >= min_res_hits.
vol_sma5 and vol_ok: vol_ok true when current volume > vol_mult * vol_sma5.
rsi and rsi checks: rsi_bull_ok true if rsi >= rsi_bull_thresh; rsi_bear_ok true if rsi <= rsi_bear_thresh.
combined_premium: either the manual_combined input or the value read from combined_symbol via request.security. The script attempted a fallback to manual when the symbol was not valid.
combined_ok: true if combined_premium lies between min_combined_ok and max_combined_ok.
final signals: bull_signal when in_time_window and bull_breakout and vol_ok and rsi_bull_ok and combined_ok. bear_signal similar for bearish breakout.
4) Visual output and alerts
Plots resistance and support lines on the chart.
Plots a label shape "STRADDLE BUY" below the bar for bull_signal and above the bar for bear_signal.
Creates an info label (on last bar) that shows TimeOK, VolOK and vol ratio, RSI, Combined premium and whether it is OK, ResHits and LowSlope.
Sets two alertcondition events: "Bull Straddle BUY" and "Bear Straddle BUY" with a short candidate message. The alerts fire when the corresponding signal is true.
5) Execution assumptions you must follow manually
The script does not place any orders or compute option strike-level prices or greeks. It only flags candidate entry bars.
When combined_source is Manual you must type CE+PE yourself. The indicator will only accept the manual number and treat it as the combined premium.
When combined_source is Symbol the script uses request.security to read that symbol. For historical bars the indicator may repaint depending on lookahead settings. The earlier fixed script attempted to use request.security inside a conditional which leads to runtime or compile errors. You experienced that exact error.
6) Known implementation caveats and bugs you encountered
Pine typing issue with low_slope. The earlier version set low_slope = na without explicit type. That triggers the Pine error: "Value with NA type cannot be assigned to a variable that was defined without type keyword". This required changing to float low_slope = na.
The earlier version attempted to call request.security() inside an if block or conditional. Pine prohibits request.security in conditional blocks unless allowed patterns are followed. That produced the error you saw: "Cannot use request.* call within loops or conditional structures" or similar. The correct pattern is to call request.security at top-level and decide later which value to use.
If combined_symbol is invalid or not available on your TradingView subscription, request.security can return na and the script must fall back to manual value. The earlier fixed script attempted fallback but compiled errors prevented reliable behavior.
The earlier script did not include micro-confirmation or advanced nonrepaint controls. Those were added in later versions. Because of that, the earlier script may have given signals that appear to repaint on historical bars or may have thrown errors when using combined_symbol.
7) Decision logic summary (exact)
Only operate if current chart time is inside user set time window.
Only consider trade candidates when enough history exists for res_lookback.
Identify a resistance level as the highest high in the lookback. Count how many times that resistance was tested. Ensure the breakout candle has a strong body and volume spike. Ensure RSI is aligned with breakout direction.
Require combined ATM premium to be inside a user preferred band. If combined_symbol is used the script tries to read that value and use it; otherwise it uses manual_combined input.
If all the above conditions are true on a confirmed bar, the script plots a STRADDLE BUY label and triggers an alertcondition.
8) What the script does not do
It does not calculate CE and PE prices by strike. It only consumes or accepts combined premium number.
It does not compute greeks, IV, or OI. OI and IV checks must be done manually.
It does not manage positions. No SL management or automatic exits are executed by the script.
It does not simulate fills or account for bid/ask spreads or slippage.
It cannot detect off-exchange block trades or read exchange-level auction states beyond raw volume bars.
It may repaint historical labels if the combined_symbol was read with lookahead_on or the script used request.security in a way that repainted. The corrected final version uses nonrepaint options.
9) Manual checks you must always perform even when the script signals BUY
Confirm the live combined ATM premium and the bid/ask for CE and PE.
Check ATM IV and recent IV movement for a potential IV crush risk.
Check option OI distribution and recent OI changes for strike pinning or large player exposure.
Confirm CE and PE liquidity and depth. Wide spreads make fills unrealistic.
Confirm there is no scheduled news or auction within the next few minutes.
Confirm margin and position sizing fits your risk plan.
10) Quick testing checklist you can run now
Add the script to a 5-minute chart with combined_source = Manual.
Enter manual_combined equal to the real CE+PE at the moment you test.
Set startHour and endHour so the in_time_window is true for current time.
Look for STRADDLE BUY label on confirmed bars. Inspect the info box to see why it did or did not signal.
If you set combined_source = Symbol, verify the symbol exists and that TradingView returns values for it. If you previously saw the request.security error, that was caused by placing the request inside a conditional. The correct behavior is to call request.security unconditionally at top-level like in the final fixed version.
開源腳本
秉持TradingView一貫精神,這個腳本的創作者將其設為開源,以便交易者檢視並驗證其功能。向作者致敬!您可以免費使用此腳本,但請注意,重新發佈代碼需遵守我們的社群規範。
免責聲明
這些資訊和出版物並非旨在提供,也不構成TradingView提供或認可的任何形式的財務、投資、交易或其他類型的建議或推薦。請閱讀使用條款以了解更多資訊。
開源腳本
秉持TradingView一貫精神,這個腳本的創作者將其設為開源,以便交易者檢視並驗證其功能。向作者致敬!您可以免費使用此腳本,但請注意,重新發佈代碼需遵守我們的社群規範。
免責聲明
這些資訊和出版物並非旨在提供,也不構成TradingView提供或認可的任何形式的財務、投資、交易或其他類型的建議或推薦。請閱讀使用條款以了解更多資訊。