OPEN-SOURCE SCRIPT
Updated

Polynomial Multiplication using CRT

127
Multiplies two polynomials under different moduli
Uses modular arithmetic to store intermediate results
Applies the Chinese Remainder Theorem (CRT) to reconstruct the final polynomial product
Displays the computed value on the TradingView chart
Release Notes
//version=6
indicator("Alpha Trend + Polynomial Multiplication using CRT", overlay=true)

// === Alpha Trend Indicator ===
atrLength = input(14, title="ATR Length")
atrMultiplier = input(3.0, title="ATR Multiplier")

// Compute Alpha Trend
atrValue = ta.atr(atrLength)
upperBand = ta.sma(close, atrLength) + (atrMultiplier * atrValue)
lowerBand = ta.sma(close, atrLength) - (atrMultiplier * atrValue)

// Alpha Trend Direction
trend = close > ta.sma(close, atrLength) ? 1 : -1
trendColor = trend == 1 ? color.green : color.red

// ✅ FIXED: Use `color.new()` for transparency
plot(trend == 1 ? high : na, title="Bullish Background", color=color.new(color.green, 85), style=plot.style_area)
plot(trend == -1 ? low : na, title="Bearish Background", color=color.new(color.red, 85), style=plot.style_area)

// Plot Alpha Trend Bands
plot(upperBand, title="Upper Band", color=color.blue)
plot(lowerBand, title="Lower Band", color=color.orange)

// === Polynomial Multiplication using CRT ===
// Function to perform modular multiplication of two polynomials
f_modPolyMult(polyA, polyB, mod) =>
lenA = array.size(polyA)
lenB = array.size(polyB)
result = array.new_float(lenA + lenB - 1, 0)

for i = 0 to lenA - 1
for j = 0 to lenB - 1
prod = array.get(polyA, i) * array.get(polyB, j) % mod
array.set(result, i + j, (array.get(result, i + j) + prod) % mod)

result

// Function to reconstruct using the Chinese Remainder Theorem (CRT)
f_chineseRemainder(remainders, mods) =>
prod = 1.0
for i = 0 to array.size(mods) - 1
prod := prod * array.get(mods, i)

result = 0.0
for i = 0 to array.size(mods) - 1
ni = array.get(mods, i)
ai = array.get(remainders, i)
pi = prod / ni
inv = math.pow(pi, ni - 2) % ni
result := (result + ai * pi * inv) % prod

result

// Example polynomials A(x) = 1 + 2x + 3x^2, B(x) = 4 + 5x
polyA = array.from(1.0, 2.0, 3.0)
polyB = array.from(4.0, 5.0)

// Moduli for CRT (must be pairwise coprime)
mods = array.from(7.0, 11.0, 13.0)

// Initialize remainders array with correct size
remainders = array.new_float(array.size(mods), 0.0)

// Compute polynomial products under different moduli
for i = 0 to array.size(mods) - 1
modResult = f_modPolyMult(polyA, polyB, array.get(mods, i))
array.set(remainders, i, array.get(modResult, 0)) // Store only the first coefficient

// Reconstruct the result using CRT
finalResult = f_chineseRemainder(remainders, mods)

// ✅ FIXED: `label.new()` syntax
label.new(bar_index, high, "Alpha Trend + Polynomial Multiplication using CRT", color=color.white, textcolor=color.black, size=size.large)
label.new(bar_index, low, "Final Reconstructed Result: " + str.tostring(finalResult), color=color.white, textcolor=color.black, size=size.large)
.

Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.