PineScript v2转v5策略结果不一致,如何匹配原v2逻辑?
Fixing Heikin Ashi Log Transform Discrepancy Between PineScript v2 and v5
The root cause of inconsistent results is that your v5 strategy omitted the log transformation and exponential reversion steps present in the v2 version. Below is the corrected implementation to align both versions' logic:
Critical Adjustments:
- Apply
math.log()to the Heikin Ashi close price when the log transform parameter is enabled - Use
math.exp()to revert transformed values back to their original scale for plotting
Corrected PineScript v5 Code:
//@version=5 strategy("Heikin Ashi Log Transform Strategy", overlay=true, loginput=true) // Match v2's logtransform input setting log_transform = input.bool(true, title="Log Transform") // Fetch Heikin Ashi close price ha_ticker = heikinashi(syminfo.tickerid) ha_close = request.security(ha_ticker, timeframe.period, close) // Apply log transformation (missing in original v5 code) if log_transform ha_close := math.log(ha_close) // --- Insert your existing strategy logic here --- // Example: Calculate indicator using transformed HA close sma_length = input.int(20, title="SMA Length") sma_val = ta.sma(ha_close, sma_length) // --- Plot with exponential reversion when needed --- // Revert to original scale for accurate plotting plot_ha_close = log_transform ? math.exp(ha_close) : ha_close plot_sma = log_transform ? math.exp(sma_val) : sma_val plot(plot_ha_close, color=color.blue, title="HA Close") plot(plot_sma, color=color.red, title="SMA on HA Close")
Key Checks:
- Ensure the
log_transforminput uses the exact boolean value from your v2 strategy - All calculations that relied on the transformed HA close in v2 must use the modified
ha_closevariable in v5 - Plotting requires the exponential revert step to match v2's visual output and strategy performance metrics
内容的提问来源于stack exchange,提问作者Long token
相关产品推荐
相关产品推荐

