Pine Script - 在一个布局中左右缩放
Pine Script - Both left and right scales in a single layout
我想一起编写 RSI ADX 和 ATR。
RSI 和 ADX 是带状振荡器 (0-100),而 ATR 不是。如果 ATR 明显高于 100,则整个地块都会被挤压。我想在相同的代码中添加 ATR,但只想将左轴用于 ATR。
我在情节代码中添加了 scale=scale.left
,但这似乎不起作用,我收到以下错误:-
Add to Chart operation failed, reason:
-line 79: Unknown argument 'scale' of type 'const integer';
-line 79: Cannot call 'plot' with arguments (type_unknown, title=literal string, color=literal color, transp=literal integer, scale=const integer); available overloads: plot(series[float], const string, series[color], input integer, input integer, input bool, input integer, input float, series[integer], input bool, series[float], const bool, input integer, const integer, string) => plot; plot(fun_arg__<arg_series_type>, const string, fun_arg__<arg_color_type>, input integer, input integer, input bool, input integer, input float, series[integer], input bool, series[float], const bool, input integer, const integer, string) => plot
我写了下面的代码:-
// RSI //
len = input(14, minval=1, title="RSI Length")
rsisrc = input(close, "RSI Source", type = input.source)
up = rma(max(change(rsisrc), 0), len)
down = rma(-min(change(rsisrc), 0), len)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
plot(rsi, "RSI", color=#ffa726)
band1 = hline(60, "RSI Upper Band", color=#C0C0C0)
band2 = hline(50, "RSI Middle Band", color=#C0C0C0)
band0 = hline(40, "RSI Lower Band", color=#C0C0C0)
fill(band1, band0, color=#ffa726, transp=92, title="RSI Background")
// ADX //
adxlen = input(8, title="ADX Smoothing")
dilen = input(13, title="DI Length")
dirmov(len) =>
up = change(high)
down = -change(low)
plusDM = na(up) ? na : (up > down and up > 0 ? up : 0)
minusDM = na(down) ? na : (down > up and down > 0 ? down : 0)
truerange = rma(tr, len)
plus = fixnan(100 * rma(plusDM, len) / truerange)
minus = fixnan(100 * rma(minusDM, len) / truerange)
[plus, minus]
adx(dilen, adxlen) =>
[plus, minus] = dirmov(dilen)
sum = plus + minus
adx = 100 * rma(abs(plus - minus) / (sum == 0 ? 1 : sum), adxlen)
sig = adx(dilen, adxlen)
plot(sig, color=#e57373, title="ADX")
band3 = hline(25, "ADX Line", color=#C0C0C0)
// ATR //
length = input(title="Length", defval=14, minval=1)
smoothing = input(title="Smoothing", defval="RMA", options=["RMA", "SMA", "EMA", "WMA"])
ma_function(source, length) =>
if smoothing == "RMA"
rma(source, length)
else
if smoothing == "SMA"
sma(source, length)
else
if smoothing == "EMA"
ema(source, length)
else
wma(source, length)
plot(ma_function(tr(true), length), title = "ATR", color=#991515, transp=0, scale=scale.left)
请帮助我并告诉我该怎么做。
一个脚本不能使用两种不同的比例。
正如@PineCoders-LucF 所说,“一个脚本不能使用两种不同的尺度”,但是...
但是您可以尝试根据您的需要自定义原始数学公式,这样它就可以限制在 0-100 范围内,同时在一定程度上保留绘制线和原始公式的信息。这将需要您进行一些研究,了解如何在数学上使这些值适应新的比例(在本例中为 0 到 100%)。例如,RSI 值以这种方式被操纵,即转换为 0-100 值。对于 ATR,这意味着您必须对它将获得的 0-100 值给出自己的解释。
我花时间为你做了:
// ATR for 0-100 range
//@version=4
enableATR = input(true, title="Enable ATR")
length = input(title="ATR Length", defval=14, minval=1)
smoothing = input(title="ATR Smoothing", defval="RMA", options=["RMA", "SMA", "EMA", "WMA"])
ma_function(source, length) =>
if smoothing == "RMA"
up = rma(max(change(source), 0), length)
down = rma(-min(change(source), 0), length)
rma = (down == 0 ? 100 : (up == 0 ? 0 : 100) - (100 / (1 + up / down)))
else
if smoothing == "SMA"
up = sma(max(change(source), 0), length)
down = sma(-min(change(source), 0), length)
rma = (down == 0 ? 100 : (up == 0 ? 0 : 100) - (100 / (1 + up / down)))
else
if smoothing == "EMA"
up = ema(max(change(source), 0), length)
down = ema(-min(change(source), 0), length)
rma = (down == 0 ? 100 : (up == 0 ? 0 : 100) - (100 / (1 + up / down)))
else
up = wma(max(change(source), 0), length)
down = wma(-min(change(source), 0), length)
rma = (down == 0 ? 100 : (up == 0 ? 0 : 100) - (100 / (1 + up / down)))
atrValue = ma_function(tr(true), length)
stretchedAtrValue = atrValue < 50 ? atrValue - atrValue * 0.35 : atrValue + atrValue * 0.35
atrSmoothed = sma(stretchedAtrValue, length)
plot(enableATR and atrSmoothed ? atrSmoothed : na, title = "ATR", color=color.orange, transp=0)
这个脚本在我的电视上完美运行。
请记住,此转换需要代表您进一步研究此值与原始 ATR 的值的可比性。请考虑这样做并将您的结论添加到此 post。
两个 ATR 之间有相似之处。
这 3 行是我能想到的最好的改进。两行新的更好的输入:
lowerATR = input(title="ATR Lower Extention", defval=0.35)
upperATR = input(title="ATR Upper Extention", defval=0.35)
而这个替换:
stretchedAtrValue = atrValue < 50 ? atrValue - atrValue * lowerATR : atrValue + atrValue * upperATR
有一种方法可以将 ATR 曲线围绕其移动的线的中心从 50 移动到比方说 10,这将使它更类似于原始 ATR 曲线,但我找不到它(再次).
在指标设置中添加乘数。很不方便的是,对于每只股票,您都必须 select 您自己的乘数,但到目前为止我还没有找到其他选择。
// ATR //
length = input(title="Length", defval=14, minval=1)
smoothing = input(title="Smoothing", defval="RMA", options=["RMA", "SMA", "EMA", "WMA"])
multiplier = input.int(title="Multiplier for ATR", minval = 1, defval = 1)
ma_function(source, length) =>
if smoothing == "RMA"
rma(source, length)
else
if smoothing == "SMA"
sma(source, length)
else
if smoothing == "EMA"
ema(source, length)
else
wma(source, length)
plot(ma_function(source*multiplier, length), title = "ATR", color=#991515, transp=0)
我想一起编写 RSI ADX 和 ATR。
RSI 和 ADX 是带状振荡器 (0-100),而 ATR 不是。如果 ATR 明显高于 100,则整个地块都会被挤压。我想在相同的代码中添加 ATR,但只想将左轴用于 ATR。
我在情节代码中添加了 scale=scale.left
,但这似乎不起作用,我收到以下错误:-
Add to Chart operation failed, reason:
-line 79: Unknown argument 'scale' of type 'const integer';
-line 79: Cannot call 'plot' with arguments (type_unknown, title=literal string, color=literal color, transp=literal integer, scale=const integer); available overloads: plot(series[float], const string, series[color], input integer, input integer, input bool, input integer, input float, series[integer], input bool, series[float], const bool, input integer, const integer, string) => plot; plot(fun_arg__<arg_series_type>, const string, fun_arg__<arg_color_type>, input integer, input integer, input bool, input integer, input float, series[integer], input bool, series[float], const bool, input integer, const integer, string) => plot
我写了下面的代码:-
// RSI //
len = input(14, minval=1, title="RSI Length")
rsisrc = input(close, "RSI Source", type = input.source)
up = rma(max(change(rsisrc), 0), len)
down = rma(-min(change(rsisrc), 0), len)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
plot(rsi, "RSI", color=#ffa726)
band1 = hline(60, "RSI Upper Band", color=#C0C0C0)
band2 = hline(50, "RSI Middle Band", color=#C0C0C0)
band0 = hline(40, "RSI Lower Band", color=#C0C0C0)
fill(band1, band0, color=#ffa726, transp=92, title="RSI Background")
// ADX //
adxlen = input(8, title="ADX Smoothing")
dilen = input(13, title="DI Length")
dirmov(len) =>
up = change(high)
down = -change(low)
plusDM = na(up) ? na : (up > down and up > 0 ? up : 0)
minusDM = na(down) ? na : (down > up and down > 0 ? down : 0)
truerange = rma(tr, len)
plus = fixnan(100 * rma(plusDM, len) / truerange)
minus = fixnan(100 * rma(minusDM, len) / truerange)
[plus, minus]
adx(dilen, adxlen) =>
[plus, minus] = dirmov(dilen)
sum = plus + minus
adx = 100 * rma(abs(plus - minus) / (sum == 0 ? 1 : sum), adxlen)
sig = adx(dilen, adxlen)
plot(sig, color=#e57373, title="ADX")
band3 = hline(25, "ADX Line", color=#C0C0C0)
// ATR //
length = input(title="Length", defval=14, minval=1)
smoothing = input(title="Smoothing", defval="RMA", options=["RMA", "SMA", "EMA", "WMA"])
ma_function(source, length) =>
if smoothing == "RMA"
rma(source, length)
else
if smoothing == "SMA"
sma(source, length)
else
if smoothing == "EMA"
ema(source, length)
else
wma(source, length)
plot(ma_function(tr(true), length), title = "ATR", color=#991515, transp=0, scale=scale.left)
请帮助我并告诉我该怎么做。
一个脚本不能使用两种不同的比例。
正如@PineCoders-LucF 所说,“一个脚本不能使用两种不同的尺度”,但是...
但是您可以尝试根据您的需要自定义原始数学公式,这样它就可以限制在 0-100 范围内,同时在一定程度上保留绘制线和原始公式的信息。这将需要您进行一些研究,了解如何在数学上使这些值适应新的比例(在本例中为 0 到 100%)。例如,RSI 值以这种方式被操纵,即转换为 0-100 值。对于 ATR,这意味着您必须对它将获得的 0-100 值给出自己的解释。
我花时间为你做了:
// ATR for 0-100 range
//@version=4
enableATR = input(true, title="Enable ATR")
length = input(title="ATR Length", defval=14, minval=1)
smoothing = input(title="ATR Smoothing", defval="RMA", options=["RMA", "SMA", "EMA", "WMA"])
ma_function(source, length) =>
if smoothing == "RMA"
up = rma(max(change(source), 0), length)
down = rma(-min(change(source), 0), length)
rma = (down == 0 ? 100 : (up == 0 ? 0 : 100) - (100 / (1 + up / down)))
else
if smoothing == "SMA"
up = sma(max(change(source), 0), length)
down = sma(-min(change(source), 0), length)
rma = (down == 0 ? 100 : (up == 0 ? 0 : 100) - (100 / (1 + up / down)))
else
if smoothing == "EMA"
up = ema(max(change(source), 0), length)
down = ema(-min(change(source), 0), length)
rma = (down == 0 ? 100 : (up == 0 ? 0 : 100) - (100 / (1 + up / down)))
else
up = wma(max(change(source), 0), length)
down = wma(-min(change(source), 0), length)
rma = (down == 0 ? 100 : (up == 0 ? 0 : 100) - (100 / (1 + up / down)))
atrValue = ma_function(tr(true), length)
stretchedAtrValue = atrValue < 50 ? atrValue - atrValue * 0.35 : atrValue + atrValue * 0.35
atrSmoothed = sma(stretchedAtrValue, length)
plot(enableATR and atrSmoothed ? atrSmoothed : na, title = "ATR", color=color.orange, transp=0)
这个脚本在我的电视上完美运行。
请记住,此转换需要代表您进一步研究此值与原始 ATR 的值的可比性。请考虑这样做并将您的结论添加到此 post。
两个 ATR 之间有相似之处。
这 3 行是我能想到的最好的改进。两行新的更好的输入:
lowerATR = input(title="ATR Lower Extention", defval=0.35)
upperATR = input(title="ATR Upper Extention", defval=0.35)
而这个替换:
stretchedAtrValue = atrValue < 50 ? atrValue - atrValue * lowerATR : atrValue + atrValue * upperATR
有一种方法可以将 ATR 曲线围绕其移动的线的中心从 50 移动到比方说 10,这将使它更类似于原始 ATR 曲线,但我找不到它(再次).
在指标设置中添加乘数。很不方便的是,对于每只股票,您都必须 select 您自己的乘数,但到目前为止我还没有找到其他选择。
// ATR //
length = input(title="Length", defval=14, minval=1)
smoothing = input(title="Smoothing", defval="RMA", options=["RMA", "SMA", "EMA", "WMA"])
multiplier = input.int(title="Multiplier for ATR", minval = 1, defval = 1)
ma_function(source, length) =>
if smoothing == "RMA"
rma(source, length)
else
if smoothing == "SMA"
sma(source, length)
else
if smoothing == "EMA"
ema(source, length)
else
wma(source, length)
plot(ma_function(source*multiplier, length), title = "ATR", color=#991515, transp=0)