//@version=6 indicator("RSI TMA", overlay=true) // Arrows on the main chart (overlay=true) // Input parameters rsiLength = input.int(2, title="RSI Length") rsiPrice = input.source(close, title="RSI Price") halfLength = input.int(2, title="Half Length") devPeriod = input.int(100, title="Deviation Period") deviations = input.float(0.7, title="Deviations") noDellArr = input.bool(false, title="No Delete Arrows") arrOtstup = input.int(0, title="Arrow Offset") arrUpColor = input.color(color.lime, title="Arrow Up Color") arrDnColor = input.color(color.red, title="Arrow Down Color") alertsMessage = input.bool(false, title="Alerts Message") alertsSound = input.bool(false, title="Alerts Sound") alertsEmail = input.bool(false, title="Alerts Email") alertsMobile = input.bool(false, title="Alerts Mobile") signalBar = input.int(0, title="Signal Bar") ...
//@version=6
indicator("RSI TMA", overlay=true) // Arrows on the main chart (overlay=true)
// Input parameters
rsiLength = input.int(2, title="RSI Length")
rsiPrice = input.source(close, title="RSI Price")
halfLength = input.int(2, title="Half Length")
devPeriod = input.int(100, title="Deviation Period")
deviations = input.float(0.7, title="Deviations")
noDellArr = input.bool(false, title="No Delete Arrows")
arrOtstup = input.int(0, title="Arrow Offset")
arrUpColor = input.color(color.lime, title="Arrow Up Color")
arrDnColor = input.color(color.red, title="Arrow Down Color")
alertsMessage = input.bool(false, title="Alerts Message")
alertsSound = input.bool(false, title="Alerts Sound")
alertsEmail = input.bool(false, title="Alerts Email")
alertsMobile = input.bool(false, title="Alerts Mobile")
signalBar = input.int(0, title="Signal Bar")
history = input.int(3000, title="History")
// Define variables
var float[] rs = array.new_float()
var float[] chMid = array.new_float()
var float[] chUp = array.new_float()
var float[] chDn = array.new_float()
// Calculate RSI
rsiValue = ta.rsi(rsiPrice, rsiLength)
array.push(rs, rsiValue)
// Limit history
if array.size(rs) > history
array.shift(rs)
// Manual Standard Deviation Calculation
calculate_stdev(arr, length) =>
if array.size(arr) >= length
slicedArr = array.new_float(length) // Temporary array to avoid out-of-bounds
for i = 0 to length - 1
slicedIndex = array.size(arr) - length + i
array.set(slicedArr, i, array.get(arr, slicedIndex))
mean = array.sum(slicedArr) / length
variance = 0.0
for i = 0 to array.size(slicedArr) - 1
value = array.get(slicedArr, i)
variance := variance + math.pow(value - mean, 2)
math.sqrt(variance / length)
else
na
// Calculate TMA bands
if array.size(rs) >= devPeriod
dev = calculate_stdev(rs, devPeriod)
if not na(dev) // Ensure deviation is valid
sum = 0.0
sumw = 0.0
for i = 0 to halfLength
if array.size(rs) > i // Prevent out-of-bounds access
weight = halfLength + 1 - i
sum += weight * array.get(rs, -i - 1)
sumw += weight
chMidValue = sum / sumw
array.push(chMid, chMidValue)
chUpValue = chMidValue + dev * deviations
chDnValue = chMidValue - dev * deviations
array.push(chUp, chUpValue)
array.push(chDn, chDnValue)
// Detect signals
if array.size(chMid) > 1 and array.size(chUp) > 1 and array.size(chDn) > 1
rsCurr = array.get(rs, -1)
rsPrev = array.get(rs, -2)
chDnCurr = array.get(chDn, -1)
chDnPrev = array.get(chDn, -2)
chUpCurr = array.get(chUp, -1)
chUpPrev = array.get(chUp, -2)
// Up Signal
if rsCurr < chDnCurr and rsPrev > chDnPrev
label.new(bar_index, low - arrOtstup * syminfo.mintick, text="", style=label.style_arrowup, color=arrUpColor, size=size.small, yloc=yloc.belowbar)
if alertsMessage
alert("RSI TMA - Signal for BUY")
// Down Signal
if rsCurr > chUpCurr and rsPrev < chUpPrev
label.new(bar_index, high + arrOtstup * syminfo.mintick, text="", style=label.style_arrowdown, color=arrDnColor, size=size.small, yloc=yloc.abovebar)
if alertsMessage
alert("RSI TMA - Signal for SELL")
// Plot RSI and TMA bands in the indicator window
plot(rsiValue, color=color.blue, title="RSI", linewidth=2) // RSI plotted in the indicator window
plot(array.size(chMid) > 0 ? array.get(chMid, -1) : na, color=color.gray, title="ChMid", linewidth=2) // Middle TMA Band
plot(array.size(chUp) > 0 ? array.get(chUp, -1) : na, color=color.orange, title="ChUp", linewidth=2) // Upper TMA Band
plot(array.size(chDn) > 0 ? array.get(chDn, -1) : na, color=color.green, title="ChDn", linewidth=2) // Lower TMA Band
Comments
Post a Comment