// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at…
- User
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//@version=5 indicator("MAA", overlay = false)
// Input parameters periodM1 = input.int(10, title = "M1 period") periodM5 = input.int(10, title = "M5 period") periodM15 = input.int(84, title = "M15 period") periodM30 = input.int(42, title = "M30 period") periodH1 = input.int(21, title = "H1 period") periodH4 = input.int(6, title = "H4 period") periodD1 = input.int(252, title = "D1 period") periodW1 = input.int(55, title = "W1 period") periodMN1 = input.int(12, title = "MN1 period")
tf = timeframe.period
var int period = switch tf == "1" => periodM1 tf == "5" => periodM5 tf == "15" => periodM15 tf == "30" => periodM30 tf == "60" => periodH1 tf == "240" => periodH4 tf == "D" => periodD1 tf == "W" => periodW1 tf == "M" => periodMN1 => 14
var int periodForStats = switch tf == "1" => 10 tf == "5" => 10 tf == "15" => 588 tf == "30" => 294 tf == "60" => 147 tf == "240" => 42 tf == "D" => 1610 tf == "W" => 230 tf == "M" => 124 => 14
// Calculate moving average MAa = ta.sma(close, period)
// Calculate MADiff MADiff = -((MAa - close) / close) * 100
// Calculate mean and standard deviation for MADiff MADiffMean = ta.sma(MADiff, periodForStats) MADiffStdDev = ta.stdev(MADiff, periodForStats)
// Plotting the MADiff and the bands plot(MADiff, color=color.red, title="MADiff") plot(MADiffMean + MADiffStdDev, color=color.green, title="Mean + StdDev", style=plot.style_cross) plot(MADiffMean - MADiffStdDev, color=color.green, title="Mean - StdDev", style=plot.style_cross) plot(MADiffMean + 2 * MADiffStdDev, color=color.blue, title="Mean + 2StdDev", style=plot.style_stepline) plot(MADiffMean - 2 * MADiffStdDev, color=color.blue, title="Mean - 2StdDev", style=plot.style_stepline)
// Showing comment on the chart var string info = "Mean: " + str.tostring(MADiffMean) + ", StdDev: " + str.tostring(MADiffStdDev) + ", MA Period: " + str.tostring(period) + ", Stats Period: " + str.tostring(periodForStats) label.new(x=bar_index, y=high, text=info, style=label.style_label_down)
// Ensures that the label is always placed on the latest bar if (barstate.islastconfirmedhistory) label.delete(na(label.new(x=bar_index, y=high, text="")))
// Add a simple label to prevent the retained status bar label.new(x=bar_index, y=high, text=info) ошибка Cannot call 'label.delete' with argument 'id'='call 'na' (series bool)'. An argument of 'series bool' type was used but a 'series label' is expected. в строчке label.delete(na(label.new(x=bar_index, y=high, text="")))
- Assistant
The problem you're encountering is due to improper handling of the
labelidentifiers when trying to delete them. Specifically, you're passing a boolean series tolabel.delete()instead of a label identifier. The correct way to manage the labels is to keep track of the label instances properly.Here is an adjusted version of your Pine Script code to correctly manage the label lifecycle:
//@version=5 indicator("MAA", overlay = false) // Input parameters periodM1 = input.int(10, title = "M1 period") periodM5 = input.int(10, title = "M5 period") periodM15 = input.int(84, title = "M15 period") periodM30 = input.int(42, title = "M30 period") periodH1 = input.int(21, title = "H1 period") periodH4 = input.int(6, title = "H4 period") periodD1 = input.int(252, title = "D1 period") periodW1 = input.int(55, title = "W1 period") periodMN1 = input.int(12, title = "MN1 period") tf = timeframe.period var int period = switch tf == "1" => periodM1 tf == "5" => periodM5 tf == "15" => periodM15 tf == "30" => periodM30 tf == "60" => periodH1 tf == "240" => periodH4 tf == "D" => periodD1 tf == "W" => periodW1 tf == "M" => periodMN1 => 14 var int periodForStats = switch tf == "1" => 10 tf == "5" => 10 tf == "15" => 588 tf == "30" => 294 tf == "60" => 147 tf == "240" => 42 tf == "D" => 1610 tf == "W" => 230 tf == "M" => 124 => 14 // Calculate moving average MAa = ta.sma(close, period) // Calculate MADiff MADiff = -((MAa - close) / close) * 100 // Calculate mean and standard deviation for MADiff MADiffMean = ta.sma(MADiff, periodForStats) MADiffStdDev = ta.stdev(MADiff, periodForStats) // Plotting the MADiff and the bands plot(MADiff, color=color.red, title="MADiff") plot(MADiffMean + MADiffStdDev, color=color.green, title="Mean + StdDev", style=plot.style_cross) plot(MADiffMean - MADiffStdDev, color=color.green, title="Mean - StdDev", style=plot.style_cross) plot(MADiffMean + 2 * MADiffStdDev, color=color.blue, title="Mean + 2StdDev", style=plot.style_stepline) plot(MADiffMean - 2 * MADiffStdDev, color=color.blue, title="Mean - 2StdDev", style=plot.style_stepline) // Showing comment on the chart var label infoLabel = na info = "Mean: " + str.tostring(MADiffMean) + ", StdDev: " + str.tostring(MADiffStdDev) + ", MA Period: " + str.tostring(period) + ", Stats Period: " + str.tostring(periodForStats) // Update label only on the latest bar if (barstate.islastconfirmedhistory) if (na(infoLabel) infoLabel := label.new(x=bar_index, y=high, text=info, style=label.style_label_down) else label.set_xy(infoLabel, x=bar_index, y=high) label.set_text(infoLabel, text=info)Changes made:
- Removed the faulty
label.deleteline since it's causing an issue. - Added a
varstatement to initializeinfoLabel. - Used
label.set_xy()andlabel.set_text()to update the label on the latest bar instead of deleting and creating a new label.
This way, the script ensures that the label stays updated with the latest information and is always displayed on the most recent bar without errors.
- Removed the faulty
- User
используй только скобочки ""
- Assistant
Конечно! Вот исправленная версия вашего скрипта, в которой я использую только кавычки
""для строковых значений://@version=5 indicator("MAA", overlay = false) // Input parameters periodM1 = input.int(10, title = "M1 period") periodM5 = input.int(10, title = "M5 period") periodM15 = input.int(84, title = "M15 period") periodM30 = input.int(42, title = "M30 period") periodH1 = input.int(21, title = "H1 period") periodH4 = input.int(6, title = "H4 period") periodD1 = input.int(252, title = "D1 period") periodW1 = input.int(55, title = "W1 period") periodMN1 = input.int(12, title = "MN1 period") tf = timeframe.period var int period = switch tf == "1" => periodM1 tf == "5" => periodM5 tf == "15" => periodM15 tf == "30" => periodM30 tf == "60" => periodH1 tf == "240" => periodH4 tf == "D" => periodD1 tf == "W" => periodW1 tf == "M" => periodMN1 => 14 var int periodForStats = switch tf == "1" => 10 tf == "5" => 10 tf == "15" => 588 tf == "30" => 294 tf == "60" => 147 tf == "240" => 42 tf == "D" => 1610 tf == "W" => 230 tf == "M" => 124 => 14 // Calculate moving average MAa = ta.sma(close, period) // Calculate MADiff MADiff = -((MAa - close) / close) * 100 // Calculate mean and standard deviation for MADiff MADiffMean = ta.sma(MADiff, periodForStats) MADiffStdDev = ta.stdev(MADiff, periodForStats) // Plotting the MADiff and the bands plot(MADiff, color=color.red, title="MADiff") plot(MADiffMean + MADiffStdDev, color=color.green, title="Mean + StdDev", style=plot.style_cross) plot(MADiffMean - MADiffStdDev, color=color.green, title="Mean - StdDev", style=plot.style_cross) plot(MADiffMean + 2 * MADiffStdDev, color=color.blue, title="Mean + 2StdDev", style=plot.style_stepline) plot(MADiffMean - 2 * MADiffStdDev, color=color.blue, title="Mean - 2StdDev", style=plot.style_stepline) // Showing comment on the chart var label infoLabel = na info = "Mean: " + str.tostring(MADiffMean) + ", StdDev: " + str.tostring(MADiffStdDev) + ", MA Period: " + str.tostring(period) + ", Stats Period: " + str.tostring(periodForStats) // Update label only on the latest bar if (barstate.islastconfirmedhistory) if (na(infoLabel)) infoLabel := label.new(x=bar_index, y=high, text=info, style=label.style_label_down) else label.set_xy(infoLabel, x=bar_index, y=high) label.set_text(infoLabel, text=info)Изменения:
- Константные строковые значения заключены в двойные кавычки
"". - Убедился, что всё остальное верно и соответствует требованиям.
- Константные строковые значения заключены в двойные кавычки
- User
замени “ на "
- Assistant
Конечно, я уже заменил все кавычки на двойные кавычки
". Вот ваш исправленный и обновленный код://@version=5 indicator("MAA", overlay = false) // Input parameters periodM1 = input.int(10, title = "M1 period") periodM5 = input.int(10, title = "M5 period") periodM15 = input.int(84, title = "M15 period") periodM30 = input.int(42, title = "M30 period") periodH1 = input.int(21, title = "H1 period") periodH4 = input.int(6, title = "H4 period") periodD1 = input.int(252, title = "D1 period") periodW1 = input.int(55, title = "W1 period") periodMN1 = input.int(12, title = "MN1 period") tf = timeframe.period var int period = switch tf == "1" => periodM1 tf == "5" => periodM5 tf == "15" => periodM15 tf == "30" => periodM30 tf == "60" => periodH1 tf == "240" => periodH4 tf == "D" => periodD1 tf == "W" => periodW1 tf == "M" => periodMN1 => 14 var int periodForStats = switch tf == "1" => 10 tf == "5" => 10 tf == "15" => 588 tf == "30" => 294 tf == "60" => 147 tf == "240" => 42 tf == "D" => 1610 tf == "W" => 230 tf == "M" => 124 => 14 // Calculate moving average MAa = ta.sma(close, period) // Calculate MADiff MADiff = -((MAa - close) / close) * 100 // Calculate mean and standard deviation for MADiff MADiffMean = ta.sma(MADiff, periodForStats) MADiffStdDev = ta.stdev(MADiff, periodForStats) // Plotting the MADiff and the bands plot(MADiff, color=color.red, title="MADiff") plot(MADiffMean + MADiffStdDev, color=color.green, title="Mean + StdDev", style=plot.style_cross) plot(MADiffMean - MADiffStdDev, color=color.green, title="Mean - StdDev", style=plot.style_cross) plot(MADiffMean + 2 * MADiffStdDev, color=color.blue, title="Mean + 2StdDev", style=plot.style_stepline) plot(MADiffMean - 2 * MADiffStdDev, color=color.blue, title="Mean - 2StdDev", style=plot.style_stepline) // Showing comment on the chart var label infoLabel = na info = "Mean: " + str.tostring(MADiffMean) + ", StdDev: " + str.tostring(MADiffStdDev) + ", MA Period: " + str.tostring(period) + ", Stats Period: " + str.tostring(periodForStats) // Update label only on the latest bar if (barstate.islastconfirmedhistory) if (na(infoLabel)) infoLabel := label.new(x=bar_index, y=high, text=info, style=label.style_label_down) else label.set_xy(infoLabel, x=bar_index, y=high) label.set_text(infoLabel, text=info)Теперь все кавычки в коде заменены на двойные кавычки
". - User
график индикатора исчезает если график цены не касаеться края
- Assistant
Проблема исчезновения графика индикатора может быть связана с использованием динамического определения позиционирования метки (
label). В вашем случае меткаinfoLabelзакреплена на уровнеhighтекущей свечи, что может привести к ситуации, когда она окажется вне видимой области графика.Для решения этой проблемы, вместо того чтобы использовать значение
highсвечи, мы можем использовать фиксированную высоту (y) для размещения метки непосредственно на графике индикатора. Ваш графический элемент будет оставаться в видимой области, например, мы можем разместить его на уровне последнего значения индикатораMADiff.Также, для размещения метки в удобном месте, можно использовать значения
naдля координатыy, чтобы поместить метку вверхнее или внизнее со следующими пропорциями.Вот исправленный код:
//@version=5 indicator("MAA", overlay = false) // Input parameters periodM1 = input.int(10, title = "M1 period") periodM5 = input.int(10, title = "M5 period") periodM15 = input.int(84, title = "M15 period") periodM30 = input.int(42, title = "M30 period") periodH1 = input.int(21, title = "H1 period") periodH4 = input.int(6, title = "H4 period") periodD1 = input.int(252, title = "D1 period") periodW1 = input.int(55, title = "W1 period") periodMN1 = input.int(12, title = "MN1 period") tf = timeframe.period var int period = switch tf == "1" => periodM1 tf == "5" => periodM5 tf == "15" => periodM15 tf == "30" => periodM30 tf == "60" => periodH1 tf == "240" => periodH4 tf == "D" => periodD1 tf == "W" => periodW1 tf == "M" => periodMN1 => 14 var int periodForStats = switch tf == "1" => 10 tf == "5" => 10 tf == "15" => 588 tf == "30" => 294 tf == "60" => 147 tf == "240" => 42 tf == "D" => 1610 tf == "W" => 230 tf == "M" => 124 => 14 // Calculate moving average MAa = ta.sma(close, period) // Calculate MADiff MADiff = -((MAa - close) / close) * 100 // Calculate mean and standard deviation for MADiff MADiffMean = ta.sma(MADiff, periodForStats) MADiffStdDev = ta.stdev(MADiff, periodForStats) // Plotting the MADiff and the bands plot(MADiff, color=color.red, title="MADiff") plot(MADiffMean + MADiffStdDev, color=color.green, title="Mean + StdDev", style=plot.style_cross) plot(MADiffMean - MADiffStdDev, color=color.green, title="Mean - StdDev", style=plot.style_cross) plot(MADiffMean + 2 * MADiffStdDev, color=color.blue, title="Mean + 2StdDev", style=plot.style_stepline) plot(MADiffMean - 2 * MADiffStdDev, color=color.blue, title="Mean - 2StdDev", style=plot.style_stepline) // Showing comment on the chart var label infoLabel = na info = "Mean: " + str.tostring(MADiffMean) + ", StdDev: " + str.tostring(MADiffStdDev) + ", MA Period: " + str.tostring(period) + ", Stats Period: " + str.tostring(periodForStats) // Calculate the y position of the label label_y_position = na(MADiff) ? na : MADiff // Update label only on the latest bar if (barstate.islastconfirmedhistory) if (na(infoLabel)) infoLabel := label.new(x=bar_index, y=label_y_position, text=info, style=label.style_label_down) else label.set_xy(infoLabel, x=bar_index, y=label_y_position) label.set_text(infoLabel, text=info)В этой версии метка обновляется и перемещается с координатой
yна уровне значенияMADiff, что гарантирует ее расположение в видимой области графика индикатора.