Hi guys,
I made an indicator with Centered Mobile Averages and to translate the data I have to know how many bars are on the graph, and I use:
return barindex
just know the exact number (it changes on different graphs)
Is there anyway to solve this?
Thanks
>> Please update your country flag in your profile. Thank you 🙂 <<
BARINDEX will always return the bars quantity of the opened chart. So sorry if I don’t understand but what do you want to do exactly please? Because if you use BARINDEX as your period for your moving average, it will instantly calculate the good mean for all the data displayed on your chart.
Usually i read the number of total bars and then write it on my indicator as a parameter (see image).
If I use barindex as a variable inside the code it doesn’t work because it has a different number for every bar, but I want just the last one.
REM traslazione
if (barindex <= numbars - indietro) and (barindex > indietro) then //numbars = total number of bars seen from barindex
if indietro = 0 then
ind = close
elsif indietro = 1 then
ind = -DPO[1](close)+close
elsif indietro = 2 then
ind = 2*(-DPO[2](close)+close) - (-DPO[1](close)+close)
else
ind = (2*indietro-2)*(-DPO[2*indietro-2](close)+close) - (2*indietro-4)*(-DPO[2*indietro-4](close)+close)-close[indietro-3]
endif
//ind1 = round(ind)
ind1=ind
else
if barindex<= indietro then
ind1 = undefined
else
ind1 =ind1[1]
endif
endif
return ind1 as "traslazione"
To know if you are on the last bar of the chart, you can use the code snippet below:
LastBarOnChart=currenttime=opentime and date=today
return LastBarOnChart
But because the history is only read once, in your case, you will not be able to use it as a period for your indicator automatically. Another way would be to make loop in the past starting only on the current bar and with the last barindex as the period.. But still, ProBuilder doesn’t allow to plot curves in the past..