TradingView默认指标与PineScript自定义指标结果不一致:15分钟图表复用1小时CCI参数结果不符
Hey there, I’ve run into this exact issue a few times when building cross-period indicators in Pine Script, so let’s break down why your custom 1h CCI on a 15m chart doesn’t match TradingView’s default tool, and how to fix it.
Common Reasons for the Mismatch
- Bar Merging & Data Alignment
TradingView’s built-in cross-period indicators use strict no-lookahead logic by default. If your Pine Script usesrequest.security()without explicitly disabling lookahead or gap filling, you might be pulling future data (relative to the current 15m bar) that the default indicator doesn’t use. This is a super common gotcha. - Calculation Precision & Formula Exactness
The default CCI might handle decimal rounding or mean deviation calculations slightly differently than your manual code. For example, if you’re using a different moving average type (like EMA instead of the standard SMA for CCI) or miscalculating the typical price, results will diverge. - Session & Filtering Differences
If your chart uses a custom trading session (e.g., only regular market hours), the default indicator automatically adapts to this filter. If your code doesn’t include session parameters in the data request, it’ll pull all 1h bars—including off-session ones—leading to mismatched values.
Step-by-Step Fixes
1. Fix the Cross-Period Data Request
Make sure your request.security() call matches the default indicator’s no-lookahead, no-gap logic. Here’s the corrected code structure:
//@version=5 indicator("Custom 1h CCI (Aligned to Default)", overlay=false) cci_length = input.int(20, title="CCI Length") // Match default length // Request 1h data with strict alignment to default behavior [h_high, h_low, h_close] = request.security( syminfo.tickerid, "60", [high, low, close], barmerge.gaps_off, barmerge.lookahead_off ) // Calculate typical price and CCI exactly as per the standard formula h_typical = (h_high + h_low + h_close) / 3 h_cci = ta.cci(h_typical, cci_length) plot(h_cci, color=color.new(color.blue, 0), title="1h CCI")
2. Verify the CCI Formula
Double-check that you’re using the exact standard CCI formula:
CCI = (Typical Price - SMA(Typical Price, Length)) / (0.015 * Mean Deviation)
Pine Script’s ta.cci() function already follows this, so avoid reinventing the wheel—use the built-in function instead of calculating manually to match the default indicator’s logic.
3. Align Session Settings
If your chart uses a custom session, add the session parameter to your request.security() call to filter data the same way the default indicator does:
[h_high, h_low, h_close] = request.security( syminfo.tickerid, "60", [high, low, close], barmerge.gaps_off, barmerge.lookahead_off, session=session.regular // Match your chart's session setting )
4. Debug with Side-by-Side Comparison
Add the default 1h CCI to your 15m chart, then plot both indicators side by side. Zoom into specific bars where the values differ—this will help you spot if the issue is with data alignment, session filtering, or calculation logic.
内容的提问来源于stack exchange,提问作者mohammad yousefi

