如何结合syminfo.tickerid实现JMA指标?(无内置JMA函数场景)
syminfo.tickerid in Pine Script Got it, let's walk through how to adapt your custom JMA code to work with syminfo.tickerid exactly like your SMA implementation. Here's a clear, reusable solution:
Core Idea
Your existing SMA uses the security() function to fetch a 60-minute SMA for the current ticker (syminfo.tickerid). For JMA, we'll do the same—wrap your manual JMA calculation inside security(). To keep code clean, we'll first turn your JMA logic into a reusable custom function.
Step 1: Create a Reusable JMA Function
First, encapsulate your JMA calculation into a function so it's easy to call inside security():
// Custom JMA function - takes source, length, phase, power and returns the JMA value jma(src, length, phase, power) => phaseRatio = phase < -100 ? 0.5 : phase > 100 ? 2.5 : phase / 100 + 1.5 beta = 0.45 * (length - 1) / (0.45 * (length - 1) + 2) alpha = pow(beta, power) // Use var to initialize variables once (persists across bars) var float e0 = 0.0 var float e1 = 0.0 var float e2 = 0.0 var float jmaVal = 0.0 // JMA calculation logic e0 := (1 - alpha) * src + alpha * nz(e0[1]) e1 := (src - e0) * (1 - beta) + beta * nz(e1[1]) e2 := (e0 + phaseRatio * e1 - nz(jmaVal[1])) * pow(1 - alpha, 2) + pow(alpha, 2) * nz(e2[1]) jmaVal := e2 + nz(jmaVal[1]) jmaVal
The var keyword ensures internal variables don't reset on each bar, which is critical for accurate JMA calculations, especially when used with security().
Step 2: Full Combined Code
Here's the complete script that mirrors your SMA setup, using syminfo.tickerid to fetch the JMA from your desired timeframe:
study(title="JMA with Security & syminfo.tickerid", overlay=true) // JMA Inputs (matches your original JMA code) length1 = input(title="Length JMA 1", type=input.integer, defval=21) phase1 = input(title="Phase JMA 1", type=input.integer, defval=50) power1 = input(title="Power JMA 1", type=input.integer, defval=1) src1 = input(title="Source JMA 1", type=input.source, defval=ohlc4) highlightMovements1 = input(title="Highlight Movements JMA 1?", type=input.bool, defval=true) // Timeframe input (adjust to match your SMA's "60" or use other values like "D", "H4") jmaTimeframe = input(title="JMA Timeframe", type=input.string, defval="60") // Custom JMA function (from Step 1) jma(src, length, phase, power) => phaseRatio = phase < -100 ? 0.5 : phase > 100 ? 2.5 : phase / 100 + 1.5 beta = 0.45 * (length - 1) / (0.45 * (length - 1) + 2) alpha = pow(beta, power) var float e0 = 0.0 var float e1 = 0.0 var float e2 = 0.0 var float jmaVal = 0.0 e0 := (1 - alpha) * src + alpha * nz(e0[1]) e1 := (src - e0) * (1 - beta) + beta * nz(e1[1]) e2 := (e0 + phaseRatio * e1 - nz(jmaVal[1])) * pow(1 - alpha, 2) + pow(alpha, 2) * nz(e2[1]) jmaVal := e2 + nz(jmaVal[1]) jmaVal // Fetch JMA for current ticker (syminfo.tickerid) and specified timeframe out_jma1 = security(syminfo.tickerid, jmaTimeframe, jma(src1, length1, phase1, power1)) // Set plot color (matches your original highlight logic) jmaColor1 = highlightMovements1 ? out_jma1 > out_jma1[1] ? color.green : color.red : #6d1e7f // Plot the JMA plot(out_jma1, title="JMA 1", linewidth=2, color=jmaColor1, transp=0)
Key Details
security(syminfo.tickerid, jmaTimeframe, jma(...)): This line does exactly what your SMA code does—it pulls the JMA calculation from the specified timeframe for the current ticker.- Flexible Timeframe: The
jmaTimeframeinput lets you switch between timeframes easily (e.g.,"D"for daily,"H1"for 1-hour). If you want to hardcode it like your SMA, replace the input with"60"directly. - Maintained Logic: All your original JMA features (color highlighting, input parameters) are preserved, just adapted to work with cross-timeframe data.
内容的提问来源于stack exchange,提问作者Marko Markotić

