//@version=5 indicator("MAA", overlay = false) // Input parameters periodM1 =…
- User
//@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")
hline(MADiffMean + MADiffStdDev, "Mean + StdDev", color=color.green, linestyle=hline.style_dotted, editable = true) hline(MADiffMean - MADiffStdDev, "Mean - StdDev", color=color.green, linestyle=hline.style_dotted) hline(MADiffMean + 2 * MADiffStdDev, "Mean + 2StdDev", color=color.blue, linestyle=hline.style_dashed) hline(MADiffMean - 2 * MADiffStdDev, "Mean - 2StdDev", color = color.blue, linestyle=hline.style_dashed)
// 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) как исправить этот код
- Assistant
Your code is mostly correct, but there are a few issues that can be improved or fixed to ensure it works correctly. Below are the suggestions and fixes:
- Make sure to use proper syntax and indentation, especially in the
ifstatement andswitchcase. - Make sure you handle edge cases.
- Correct some typos and logical issues.
Here's a revised version of your Pine Script code:
//@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 period = switch tf "1" => periodM1 "5" => periodM5 "15" => periodM15 "30" => periodM30 "60" => periodH1 "240" => periodH4 "D" => periodD1 "W" => periodW1 "M" => periodMN1 => 14 periodForStats = switch tf "1" => 10 "5" => 10 "15" => 588 "30" => 294 "60" => 147 "240" => 42 "D" => 1610 "W" => 230 "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") hline(MADiffMean + MADiffStdDev, "Mean + StdDev", color=color.green, linestyle=hline.style_dotted, editable=true) hline(MADiffMean - MADiffStdDev, "Mean - StdDev", color=color.green, linestyle=hline.style_dotted, editable=true) hline(MADiffMean + 2 * MADiffStdDev, "Mean + 2StdDev", color=color.blue, linestyle=hline.style_dashed, editable=true) hline(MADiffMean - 2 * MADiffStdDev, "Mean - 2StdDev", color=color.blue, linestyle=hline.style_dashed, editable=true) // Showing comment on the chart var label infoLabel = na info = "Mean: " + str.tostring(MADiffMean, format.mintick) + ", StdDev: " + str.tostring(MADiffStdDev, format.mintick) + ", 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)Here's what I have improved:
- Proper indentation and consistency in
switchcases. - Made horizontal lines (
hline) editable. - Correctly formatted string values of
MADiffMeanandMADiffStdDevto useformat.mintickfor better readability. - Ensured the
ifstatement forbarstate.islastconfirmedhistoryis properly indented to ensure the label updates correctly.
This should resolve any issues in the provided Pine Script code and improve readability and functionality.
- Make sure to use proper syntax and indentation, especially in the
- User
мне нужно что бы вместо plot использовалась функция которая выведет линию последнего значения в этих строках 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)
- Assistant
To draw lines indicating the last values of the calculation, you can use
line.newandline.set_xy1functions instead ofplot. Here's how you can modify your script to achieve this://@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 period = switch tf "1" => periodM1 "5" => periodM5 "15" => periodM15 "30" => periodM30 "60" => periodH1 "240" => periodH4 "D" => periodD1 "W" => periodW1 "M" => periodMN1 => 14 periodForStats = switch tf "1" => 10 "5" => 10 "15" => 588 "30" => 294 "60" => 147 "240" => 42 "D" => 1610 "W" => 230 "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) // Define line variables var line meanLine1 = na var line meanLine2 = na var line meanLine3 = na var line meanLine4 = na // Update lines if (bar_index > 1) meanValue1 = MADiffMean + MADiffStdDev meanValue2 = MADiffMean - MADiffStdDev meanValue3 = MADiffMean + 2 * MADiffStdDev meanValue4 = MADiffMean - 2 * MADiffStdDev // Create lines if they don't exist if na(meanLine1) meanLine1 := line.new(x1 = bar_index - 1, y1 = meanValue1, x2 = bar_index, y2 = meanValue1, color=color.green, style=line.style_dotted) meanLine2 := line.new(x1 = bar_index - 1, y1 = meanValue2, x2 = bar_index, y2 = meanValue2, color=color.green, style=line.style_dotted) meanLine3 := line.new(x1 = bar_index - 1, y1 = meanValue3, x2 = bar_index, y2 = meanValue3, color=color.blue, style=line.style_dashed) meanLine4 := line.new(x1 = bar_index - 1, y1 = meanValue4, x2 = bar_index, y2 = meanValue4, color=color.blue, style=line.style_dashed) else // Update the existing lines line.set_xy1(meanLine1, x1 = bar_index - 1, y1 = meanValue1) line.set_xy2(meanLine1, x2 = bar_index, y2 = meanValue1) line.set_xy1(meanLine2, x1 = bar_index - 1, y1 = meanValue2) line.set_xy2(meanLine2, x2 = bar_index, y2 = meanValue2) line.set_xy1(meanLine3, x1 = bar_index - 1, y1 = meanValue3) line.set_xy2(meanLine3, x2 = bar_index, y2 = meanValue3) line.set_xy1(meanLine4, x1 = bar_index - 1, y1 = meanValue4) line.set_xy2(meanLine4, x2 = bar_index, y2 = meanValue4) // Plotting the MADiff and the bands plot(MADiff, color=color.red, title="MADiff") // Showing comment on the chart var label infoLabel = na info = "Mean: " + str.tostring(MADiffMean, format.mintick) + ", StdDev: " + str.tostring(MADiffStdDev, format.mintick) + ", 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)Here's what I changed:
- I removed the
plotfunctions for the mean and standard deviation lines and replaced them withline.newandline.set_xy1andline.set_xy2to create and update the lines at the last bar. - The lines are created on the first bar and updated on each subsequent bar to the current values of
MADiffMean + MADiffStdDev,MADiffMean - MADiffStdDev,MADiffMean + 2 * MADiffStdDev, andMADiffMean - 2 * MADiffStdDev. - Style and color are defined directly in the
line.newandline.set_xy2functions.
These changes ensure the lines represent the final calculated values correctly and update dynamically with each new bar.
- I removed the