//+------------------------------------------------------------------+
перенеси этот код на pine Script
Pine Script is TradingView's scripting language, which differs from MQL used for MetaTrader. The following is an equivalent Pine Script version of your MQL indicator:
pinescript
//@version=5
indicator("ATR-Based Price Levels", overlay=true)
// Input for ATR period
atrPeriod = input.int(10, title="ATR Period")
// Fetch monthly time frame high, low, and open
[monthlyHigh, monthlyLow, monthlyOpen] = request.security(syminfo.tickerid, "M", [high, low, open])
// ATR Calculation
H_L = array.new_float()
openATR = array.new_float()
m = 0
for i = 0 to atrPeriod - 1
if not na(request.security(syminfo.tickerid, "M", high[i]))
array.push(H_L, high[i] - low[i])
array.push(openATR, open[i])
m := m + 1
atrSum = 0.0
for i = 0 to m - 2
if i + 1 < array.size(H_L)
atrSum := atrSum + (array.get(H_L, i) + array.get(H_L, i+1)) / 2 / array.get(openATR, i+1) * 100
ATRavg = atrSum / (m - 1)
ATRavg := na(ATRavg) ? 0 : ATRavg // Handle potential NA values
if m > 0
priceHigh = array.get(openATR, 0) * (1 + ATRavg / 100)
priceLow = array.get(openATR, 0) * (1 - ATRavg / 100)
priceHigh_0.5 = array.get(openATR, 0) * (1 + ATRavg / 200)
priceLow_0.5 = array.get(openATR, 0) * (1 - ATRavg / 200)
// Extracting monthly time range, assuming 31 days for simplicity
start = request.security(syminfo.tickerid, "M", time)
end = start + 31 * 24 * 60*60 * 1000
// Plot lines
line.new(x1=start, y1=priceHigh, x2=end, y2=priceHigh, color=color.red, width=2, style=line.style_dashed)
line.new(x1=start, y1=priceLow, x2=end, y2=priceLow, color=color.red, width=2, style=line.style_dashed)
line.new(x1=start, y1=priceHigh_0.5, x2=end, y2=priceHigh_0.5, color=color.blue, width=2, style=line.style_dashed)
line.new(x1=start, y1=priceLow_0.5, x2=end, y2=priceLow_0.5, color=color.blue, width=2, style=line.style_dashed)
// Delete lines from previous calculations
if bar_index > 1
line.delete(id=0)
line.delete(id=1)
line.delete(id=2)
line.delete(id=3)
Explanation and Conversion Details:
Used request.security(syminfo.tickerid, "M", [high, low, open]) to fetch the monthly time-frame data.
Calculated the ATR for the given period by iterating over monthly bars.
Calculated the average ATR and used it to determine the price levels.
Drew lines on the chart using line.new, setting red and blue colors with a width of 2.
Handled potential na values to avoid errors.
Cleaned up old lines when recalculating, considering Pine Script limitations on object management compared to MQL.
Note: Pine Script treats time in UNIX milliseconds, whereas the provided MQL code used UNIX seconds. The code example uses simplistic 31-day month logic for the timeframe (end). Please adjust accordingly to fit your needs or complex calendar month operations.
To address the error related to the incorrect argument type for line.delete, we need to store the created lines in variables and ensure the proper handling and deletion of these lines. Pine Script doesn’t support object manipulation in the same way as MQL, but we can manage the lines by keeping references to them in variables. Here’s an updated version of the script with appropriate handling of line references:
pinescript
//@version=5
indicator("ATR-Based Price Levels", overlay=true)
// Input for ATR period
atrPeriod = input.int(10, title="ATR Period")
// Fetch monthly time frame high, low, and open
[monthlyHigh, monthlyLow, monthlyOpen] = request.security(syminfo.tickerid, "M", [high, low, open])
// ATR Calculation
H_L = array.new_float()
openATR = array.new_float()
m = 0
for i = 0 to atrPeriod - 1
if not na(request.security(syminfo.tickerid, "M", high[i]))
array.push(H_L, high[i] - low[i])
array.push(openATR, open[i])
m := m + 1
atrSum = 0.0
for i = 0 to m - 2
if i + 1 < array.size(H_L)
atrSum := atrSum + (array.get(H_L, i) + array.get(H_L, i+1)) / 2 / array.get(openATR, i+1) * 100
ATRavg = atrSum / (m - 1)
ATRavg := na(ATRavg) ? 0 : ATRavg // Handle potential NA values
var line priceHighLine = na
var line priceLowLine = na
var line priceHighHalfLine = na
var line priceLowHalfLine = na
if m > 0
priceHigh = array.get(openATR, 0) * (1 + ATRavg / 100)
priceLow = array.get(openATR, 0) * (1 - ATRavg / 100)
priceHigh_0.5 = array.get(openATR, 0) * (1 + ATRavg / 200)
priceLow_0.5 = array.get(openATR, 0) * (1 - ATRavg / 200)
// Extracting monthly time range, assuming 31 days for simplicity
start = request.security(syminfo.tickerid, "M", time)
end = start + 31 * 24 * 60*60 * 1000
// Delete previous lines if they exist
if not na(priceHighLine)
line.delete(priceHighLine)
if not na(priceLowLine)
line.delete(priceLowLine)
if not na(priceHighHalfLine)
line.delete(priceHighHalfLine)
if not na(priceLowHalfLine)
line.delete(priceLowHalfLine)
// Plot new lines
priceHighLine := line.new(x1=start, y1=priceHigh, x2=end, y2=priceHigh, color=color.red, width=2, style=line.style_dashed)
priceLowLine := line.new(x1=start, y1=priceLow, x2=end, y2=priceLow, color=color.red, width=2, style=line.style_dashed)
priceHighHalfLine := line.new(x1=start, y1=priceHigh_0.5, x2=end, y2=priceHigh_0.5, color=color.blue, width=2, style=line.style_dashed)
priceLowHalfLine := line.new(x1=start, y1=priceLow_0.5, x2=end, y2=priceLow_0.5, color=color.blue, width=2, style=line.style_dashed)
Explanation of Adjustments:
Variable Initialization using var: Created persistent variables priceHighLine, priceLowLine, priceHighHalfLine, and priceLowHalfLine to store references to the created lines.
Conditional Deletion: Before creating new lines, we check if these variables are not na, and if they are not, the existing lines are deleted using line.delete.
Assignment of New Lines: Create new lines and assign them to the corresponding variables so they can be managed and deleted in the next iteration.
This ensures that the script properly handles and deletes old lines before creating new ones, avoiding potential issues with the wrong argument types and ensuring that there are no lingering old lines cluttering the chart.
// Extracting monthly time range, assuming 31 days for simplicity
start = request.security(syminfo.tickerid, "M", time)
end = start + 31 * 24 * 60*60 * 1000
Error on bar 0: Objects positioned using xloc.bar_index cannot be drawn further than 500 bars into the future.
at #main():39
The error you're encountering occurs because line.new cannot draw lines more than 500 bars into the future when using xloc.bar_index. To work around this limitation, we'll use xloc.bar_time instead, which allows the use of absolute timestamps to position the lines. This will be particularly helpful given our use of monthly timeframes.
Here's the updated Pine Script code:
pinescript
//@version=5
indicator("ATR-Based Price Levels", overlay=true)
// Input for ATR period
atrPeriod = input.int(10, title="ATR Period")
// Fetch monthly time frame high, low, and open
[monthlyHigh, monthlyLow, monthlyOpen] = request.security(syminfo.tickerid, "M", [high, low, open])
// ATR Calculation
H_L = array.new_float()
openATR = array.new_float()
m = 0
for i = 0 to atrPeriod - 1
array.push(H_L, high[i] - low[i])
array.push(openATR, open[i])
m := m + 1
atrSum = 0.0
for i = 0 to m - 2
if i + 1 < array.size(H_L)
atrSum := atrSum + (array.get(H_L, i) + array.get(H_L, i+1)) / 2 / array.get(openATR, i+1) * 100
ATRavg = atrSum / (m - 1)
ATRavg := na(ATRavg) ? 0 : ATRavg // Handle potential NA values
priceHigh = array.get(openATR, 0) * (1 + ATRavg / 100)
priceLow = array.get(openATR, 0) * (1 - ATRavg / 100)
priceHigh_5 = array.get(openATR, 0) * (1 + ATRavg / 200)
priceLow_5 = array.get(openATR, 0) * (1 - ATRavg / 200)
// Extracting monthly time range, assuming 31 days for simplicity
start = request.security(syminfo.tickerid, "M", time)
end = start + 31 * 24 * 60 * 60 * 1000
// Plot lines using specific time positions with xloc.bar_time
var line priceHighLine = na
var line priceLowLine = na
var line priceHighHalfLine = na
var line priceLowHalfLine = na
if not na(priceHighLine)
line.delete(priceHighLine)
if not na(priceLowLine)
line.delete(priceLowLine)
if not na(priceHighHalfLine)
line.delete(priceHighHalfLine)
if not na(priceLowHalfLine)
line.delete(priceLowHalfLine)
start_time = timestamp(year(start/1000), month(start/1000), dayofmonth(start/1000))
end_time = timestamp(year(end/1000), month(end/1000), dayofmonth(end/1000))
priceHighLine := line.new(x1=start_time, y1=priceHigh, x2=end_time, y2=priceHigh, color=color.red, width=2, style=line.style_dashed, xloc=xloc.bar_time)
priceLowLine := line.new(x1=start_time, y1=priceLow, x2=end_time, y2=priceLow, color=color.red, width=2, style=line.style_dashed, xloc=xloc.bar_time)
priceHighHalfLine := line.new(x1=start_time, y1=priceHigh_5, x2=end_time, y2=priceHigh_5, color=color.blue, width=2, style=line.style_dashed, xloc=xloc.bar_time)
priceLowHalfLine := line.new(x1=start_time, y1=priceLow_5, x2=end_time, y2=priceLow_5, color=color.blue, width=2, style=line.style_dashed, xloc=xloc.bar_time)
Key Adjustments:
Use of xloc.bar_time: The lines are now positioned with xloc.bar_time, which is suitable for absolute time positioning and avoids the issue of exceeding the 500-bar limit into the future.
Calculating Timestamps:timestamp is used to convert the date components back into UNIX timestamps for the start and end times.
Handling Previous Lines: Ensures previous lines are deleted before creating new ones, avoiding clutter and errors.
These changes should eliminate the limitations and errors, providing a more robust and functioning indicator.
for i = 0 to atrPeriod - 1
array.push(H_L, high[i] - low[i])
array.push(openATR, open[i])
m := m + 1
atrSum = 0.0
for i = 0 to m - 2
if i + 1 < array.size(H_L)
atrSum := atrSum + (array.get(H_L, i) + array.get(H_L, i+1)) / 2 / array.get(openATR, i+1) * 100
// Extracting monthly time range, assuming 31 days for simplicity
start = request.security(syminfo.tickerid, "M", time)
end = start + 31 * 24 * 60 * 60 * 1000
// Plot lines using specific time positions with xloc.bar_time
var line priceHighLine = na
var line priceLowLine = na
var line priceHighHalfLine = na
var line priceLowHalfLine = na
if not na(priceHighLine)
line.delete(priceHighLine)
if not na(priceLowLine)
line.delete(priceLowLine)
if not na(priceHighHalfLine)
line.delete(priceHighHalfLine)
if not na(priceLowHalfLine)
line.delete(priceLowHalfLine)
этот код работает но я не вижу линий с чем это связано
The reason you might not be seeing the lines could be due to the way the script fetches and calculates the ATR values or the range of the dates used for the line.new function might not be correct. Let's debug and simplify your script to ensure all necessary calculations are made correctly and the lines are plotted effectively.
Firstly, we should ensure that the ATR calculations and the time ranges are correct. Here is the updated version of your script with added debug plots to help identify the issue:
pinescript
//@version=5
indicator("ATR-Based Price Levels", overlay=true)
// Input for ATR period
atrPeriod = input.int(10, title="ATR Period")
// Fetch monthly time frame high, low, and open
[monthlyHigh, monthlyLow, monthlyOpen] = request.security(syminfo.tickerid, "M", [high, low, open])
// ATR Calculation
H_L = array.new_float()
openATR = array.new_float()
for i = 0 to atrPeriod - 1
h = high[i]
l = low[i]
o = open[i]
if (not na(h) and not na(l) and not na(o))
array.push(H_L, h - l)
array.push(openATR, o)
atrSum = 0.0
for i = 0 to array.size(H_L) - 2
atrSum := atrSum + (array.get(H_L, i) + array.get(H_L, i+1)) / 2 / array.get(openATR, i+1) * 100
ATRavg = atrSum / (array.size(H_L) - 1)
ATRavg := na(ATRavg) ? 0 : ATRavg // Handle potential NA values
priceHigh = array.get(openATR, 0) * (1 + ATRavg / 100)
priceLow = array.get(openATR, 0) * (1 - ATRavg / 100)
priceHigh_5 = array.get(openATR, 0) * (1 + ATRavg / 200)
priceLow_5 = array.get(openATR, 0) * (1 - ATRavg / 200)
// Extracting monthly time range, assuming 31 days for simplicity
start = request.security(syminfo.tickerid, "M", time)
end = start + 31 * 24 * 60 * 60 * 1000
// Convert to milliseconds since the Pine Script timestamp function works with milliseconds
start_time = timestamp(year(start / 1000), month(start / 1000), dayofmonth(start / 1000))
end_time = timestamp(year(end / 1000), month(end / 1000), dayofmonth(end / 1000))
// Ensure to adjust the timeframe of lines rendering to a reasonable range of current time and future time for visibility in the script
var line priceHighLine = na
var line priceLowLine = na
var line priceHighHalfLine = na
var line priceLowHalfLine = na
if not na(priceHighLine)
line.delete(priceHighLine)
if not na(priceLowLine)
line.delete(priceLowLine)
if not na(priceHighHalfLine)
line.delete(priceHighHalfLine)
if not na(priceLowHalfLine)
line.delete(priceLowHalfLine)
// Plot debug information to verify calculations
plot(priceHigh, color=color.red, title="Price High")
plot(priceLow, color=color.red, title="Price Low")
plot(priceHigh_5, color=color.blue, title="Price High 0.5")
plot(priceLow_5, color=color.blue, title="Price Low 0.5")
// Plot lines using specific time positions with xloc.bar_time
priceHighLine := line.new(x1=start_time, y1=priceHigh, x2=end_time, y2=priceHigh, color=color.red, width=2, style=line.style_dashed, xloc=xloc.bar_time)
priceLowLine := line.new(x1=start_time, y1=priceLow, x2=end_time, y2=priceLow, color=color.red, width=2, style=line.style_dashed, xloc=xloc.bar_time)
priceHighHalfLine := line.new(x1=start_time, y1=priceHigh_5, x2=end_time, y2=priceHigh_5, color=color.blue, width=2, style=line.style_dashed, xloc=xloc.bar_time)
priceLowHalfLine := line.new(x1=start_time, y1=priceLow_5, x2=end_time, y2=priceLow_5, color=color.blue, width=2, style=line.style_dashed, xloc=xloc.bar_time)
Key Debug Steps:
Plot Intermediate Values: Added debug plots for priceHigh, priceLow, priceHigh_5, and priceLow_5. This will help confirm that calculations are correct and the ATR levels are as expected.
Condition Checks: Ensure that only non-NA values are pushed into the arrays H_L and openATR.
Timestamps: Use the correct method to calculate the start and end timestamps for the lines.
Lines Rendering:
Make sure the timeframe of the chart is appropriate and that the script's settings match the trading view. The simulated monthly timeframe might not show up correctly unless you zoom out or check higher timeframes. This updated script should give you more insights into where the issue might be. Debugging intermediate values on the plot should guide you in ensuring correct setup and rendering the expected lines.