Help need with Pinescript for Tradingview.

A PINESCRIPT WHICH WILL CALCULATE THE AVERAGE OF LAST 10 TRADING DAYS IN WHICH PRICE MOVED FROM OPEN TO HIGH AND OPEN TO LOW. THAT MEANS TWO LINE WHICH ARE THE AVERAGE MOVEMENT OF PRICE FROM OPEN TO HIGH AND LOW FOR THE LAST 10 TRADING DAYS
I managed to write the code. but the lines are not visible. can somebody help me with his
//@version=5
indicator(‘Custom Lines’, overlay=true)

// Calculate the average of the last 10 trading days’ open-to-high range
var float highRangeSum = 0.0
var float lowRangeSum = 0.0
var int count = 0
[do_1, dh, dl] = request.security(syminfo.ticker, ‘D’, [open, high, low], lookahead=barmerge.lookahead_on)

for i = 1 to 10 by 1
highRangeSum += dh[i] - do_1[i]
lowRangeSum += do_1[i] - dl[i]
count += 1
count

averageHighRange = highRangeSum / count
averageLowRange = lowRangeSum / count
// Get opening price
openPrice = request.security(syminfo.tickerid, ‘D’, open, lookahead=barmerge.lookahead_on)

// Calculate the levels for today’s lines
todayHighLevel = openPrice + averageHighRange
todayLowLevel = openPrice - averageLowRange

// Plot the lines
plot(todayHighLevel, color=color.purple, title=‘Today's High Level’, linewidth=2)
plot(todayLowLevel, color=color.black, title=‘Today's Low Level’, linewidth=2)

1 Like

I see some bad/unnecessary code there. If you wait a little, maybe I can help.

I don’t have a computer right now so I’m just gonna tell you what to do.

If you want a simple moving average, you need to put the ta.sma() built-in function inside the plot() function, as an argument to input data for ta.sma(), you should use “open - low” without quotes for Open-Low and “high - low” for High-Low MA, that’s it. All the other arguments like the color of the line, or the length of the SMA, that you can do yourself. Let me know if it works.

NOTE: I’m doubtful here whether directly supplying “open - low” and “high - open” will work with ta.sma(), so please let me know if it works, if it doesn’t, I’ll give you the workaround.

2 Likes

Thanks for the reply bro.
Let me explain.
Lets take the example of airtel.
For the Last 10 days data is taken.
Sum of (day high-dayopen)/10 is calculated.
Similarly sum of (day open-day low) is calculated.
will assume average open to high is 10 and to low is 5.
assuming Airtel todays open is at 850 then i want 2 horizontal lines drawn at 860 and 845.
Im not that good at coding. The code above i generated using chatgpt.
when u get time can u code it…
It will be very helpful

Oh yeah, of course I’ll do it for you.

Thanks a lot :slightly_smiling_face:

Can you try the following code and see if it works?

// @version = 5

indicator(“High Open Low 10-day SMA”, overlay = true)

plot(ta.sma(high - open, 10), title = “High - Open 10-day SMA”, color = color.red, linewidth = 2, style = plot.style_line, trackprice = true)
plot(ta.sma(open - low, 10), title = “Open - Low 10-day SMA”, color = color.green, linewidth = 2, style = plot.style_line, trackprice = true)

Sorry no lines are shown…
Sorry if im wrong… but the average price according to this code is not getted added to todays open price right?

It will calculate the SMA of High - Open for today too and plot that. Since the ta.sma() didn’t work, we’ll need to code everything manually.

I didn’t really read your last text thoroughly but I get that you want the average of High - Open (& Open - Low) for last 10 days to be added to today’s open and make a linear representation of that data, right?

// @version = 5

indicator(“Your custom indicator”, overlay = true)

high_line_value = 0
low_line_value = 0

for i = 1 to 10
// You need to put 4 spaces in the beginning of the following line for block indentation
high_line_value := ((high_line_value * (i - 1)) + (high[i] - open[i])) / i
// The same as the above for the following line
low_line_value := ((low_line_value * (i - 1)) + (open[i] - low[i])) / i

plot(high_line_value + open, title = “The higher line”, color = color.red, linewidth = 2, style = plot.style_line, trackprice = true)
plot(open - low_line_value, title = “The lower line”, color = color.green, linewidth = 2, style = plot.style_line, trackprice = true)

Code has no errors.
But no visible lines on the chart

  1. Did you indent the for loop body? I have specified that in the code, see that line about 4 spaces.
  2. Did you add the code to the chart?
    Also, share a screenshot of the page so I can see it.

I’ll get back to you in a moment.

1 Like

Take out the keyword “var” from the variable declarations. Then it’ll work.

the code is working and i got the results.
But bro i think u didnt understand what im asking for…
the code is giving zig zag lines.
for example airtel on moday moved 10 points from open to high and 14 points from open to high on tuesday.
so the average is 12 points.
so today if airtel is opening at 800 i want a horizontal line at 812. similarly for low also…
i hope u understand.

I did the same thing, but for 10 days as you asked. The zig zags you’re saying is the correct output for what you want. Canyou make manual calculations and see if they match or not?

I have added the average High - Open of last 10 days to today’s open, and subtracted the average Open - Low of last 10 days from today’s open, that’s what you wanted, right?

Wait, you want a horizontal line for today only and not for previous days? Do you want a linear representation of the numbers for infinite data back, or only for today from last 10 days?

Sorry man, I’ve been distracted a little today, horizontal lines will be fairly easy. Give me a moment.


Let me explain with this excel sheet.
look at adaniport. for the last 10 days it moved an average of 17.6 from open to high and 9.1 from open to low.
so today when it opened at 858.55 im saying it could go upto 876.2 on the high side or 849.4 on the lower side.
so basically its just 2 horizontal lines.