Indicator Question - page 4

 
SDC:

Or maybe something like this ?

you really ought to use IndicatorCounted() though, because if you do it like this your indicator is redrawing all those objects at every new tick instead of drawing all them just one time and adding new ones as new bars are formed

This and your other post about external bools gives me some other ideas too, thanks

My focus needs to be more broad but because I'm NooB I tend to focus on one part of code and neglecting others like the externals.
I'm only now getting into the arrays and had a huge struggle understanding the buffers, but starting to get past that now recently.

Thanks this will give me a bunch of new ideas now too.
 
SDC:


You have to consider what it is you are doing, when you create an indicator you are applying your code to each and every bar on the chart, this means you need to get the macd indicator value as it was for each of those historical bars.

in your original code you did:

the last parameter 1 is the value of the macd as it was at Bar 1 of the chart, the bar previous to the current bar which is indexed as bar 0

Obviously you do not want to use that single value of the macd in your conditional operator over the whole historical chart .

You need to index the macd to the same bar index as each bar so for instance at bar 500 you need this

that last parameter must change to match the bar number your indicator is applying its algorithms to.

this is why you need it inside the loop, so you can use the loop cycle iterator for the last parameter therfore you get your macd value as it was for each bar of the historical chart.

i hope that helps to clear things up a little.

Right I did have that worked out.

However, when adding the codes such as if(val1 > 0 && faster > slower) this would not work when declaring faster and slower outside the loop.

AHHHHH I see so because the shift is (i) then I need it inside the loop OOOOPPPS LOL
 
Agent86:
Why for(int i = Bars-1 ? <----------------------- Bars are indexed just like arrays, the first bar=0, instead of 1. So the last Bar is Bars-1;

And why is this better then for(int i = Bars ?? <----- see above.

Please advise thanks


Also:

I'm assuming this is true for all, and not just indicators / custom indicators ?<--- what me and WHRoeder: discussed about are pretty general matters. Nothing specific. We were just talking with each other about variables declaration and their assignments, etc, w.r.t. loops.

 
diostar:

Also:


for(int i=Bars; i>=0; i--) is the code I used

for(int i=Bars-1; i>=0: i--) is suggested

I thought that Bars = number of bars in the current chart ? at least according to the dictionary.

So --i I figured was counting down from the Highest bar number such as 1002 or something and counting --i down to actually -1
Because while i>=0 so it should count down to actually -1 before it become false. If it were 0 or greater it would be true and loop one last time until it reaches -1

At least thats the way I thought that it worked ? Am I wrong ?

If i=0 and ++i and counted up then I thought it would count to the end to the last bar.

But in this case Bars-1 your saying would not really be all the close Bars but actually includes the Bar[0] which is not closed yet or something ?

I'm not really sure I understand the difference between Bars-1 and Bars. Does it have something to do with the current bar 0 which is not counted when simply referring to Bars ??

Please advise Thanks


 

yes it is because you have a bar 0 so Bars gives you the count of bars in the chart including bar 0

so consider a tiny chart, there is only 2 bars in the chart, bar[0] and bar[1]

Bars gives you the count so Bars == 2

if you want to apply your indicator to begin at the last bar in the chart

You could try i=Bars.

As we know, Bars = 2 but there is no bar indexed as bar[2] the last bar is Bar[1]

so you need to do i=Bars-1

 
Agent86:
for(int i=Bars; i>=0; i--) is the code I used<--- say, for eg. Close[i], this counts from close[Bars] down to close[0]. When i=Bars, Does close[i] exist?

for(int i=Bars-1; i>=0: i--) is suggested<--- say, for eg. Close[i], this counts from close[Bars-1] down to close[0]. When i=Bars-1, Does close[i] exist?

I thought that Bars = number of bars in the current chart ? at least according to the dictionary. <---ok

So --i I figured was counting down from the Highest bar number such as 1002 or something and counting --i down to actually -1 Does close[1002], close[-1] exist?
Because while i>=0 so it should count down to actually -1 before it become false. If it were 0 or greater it would be true and loop one last time until it reaches -1 <---- at close[-1], it returns 0. Not false

At least thats the way I thought that it worked ? Am I wrong ? <--- see below ref.

If i=0 and ++i and counted up then I thought it would count to the end to the last bar. <--- it will Obediently, do that.

But in this case Bars-1 your saying would not really be all the close Bars but actually includes the Bar[0] which is not closed yet or something ? <---in this case, the close price will return current Bid tick price

I'm not really sure I understand the difference between Bars-1 and Bars. Does it have something to do with the current bar 0 which is not counted when simply referring to Bars ?? <--- see below ref

Please advise Thanks welcome





double Close[]
Series array that contains close prices for each bar of the current chart.

Series array elements are indexed in the reverse order, i.e., from the last one to the first one. The current bar which is the last in the array is indexed as 0. The oldest bar, the first in the chart, is indexed as Bars-1.
 
I see, for some reason I was thinking that the buffer handled this topic a little differently because technically I didn't even declare the number of elements in the array so I though that the buffer worked this out already.

Anyhow I do understand this and it makes sense, thanks

 
Agent86:
I see, for some reason I was thinking that the buffer handled this topic a little differently because technically I didn't even declare the number of elements in the array so I though that the buffer worked this out already.

Anyhow I do understand this and it makes sense, thanks



What??? I didn't realise you were actually referring all this to indicator buffers. Now that I do, read these references taken from: https://www.mql5.com/en/articles/1500

(1) indicator buffers & user arrays - they are both same and DIFFERENT. & The reason why indicator buffers are not sized by user, has got nothing to do with it being worked out already. see below ref:

double ExtMapBuffer1[];

This is a usual array. But the dimensionality is not indicated and initialization is not performed. This array will later be set up as a data buffer.

(2) indicator buffers at output & at input (the data buffer above) may look the same, but different.

SetIndexBuffer(0,ExtMapBuffer1);

This function "binds" an array to a buffer number. I.e. it shows that the buffer with the indicated number will use the indicated array for storing data. So, changing the elements of this array you will change the value of the buffer. Actually an array is a data buffer. The first argument is the name of the array that should be bound.


You see, it all works. Now let us see what the code does:

for(int i=0;i<Bars;i++)

We use the cycle for to go through all elements of the data buffer. As a certain bar corresponds to each element of the buffer, we use the cycle, starting from the zero bar (the last available) and end with the first available, which is in succession one less than the variable Bars (because we count bars from zero).

{
   ExtMapBuffer1[i]=MathRand()%1001;
}

At each iteration a counter is increased by one, and we move from the last available bar to the first one at the same time assigning to each buffer element (which corresponds to a certain bar) a random number from 0 to 1000. If it is difficult for you to understand, how a certain buffer element corresponds to a certain bar, try to change the cycle in the following way and see the result in the terminal:

for(int i=0;i<Bars;i++)
{
   ExtMapBuffer1[i]=i;
}



 
diostar:

double Close[]
Series array that contains close prices for each bar of the current chart.

Series array elements are indexed in the reverse order, i.e., from the last one to the first one. The current bar which is the last in the array is indexed as 0. The oldest bar, the first in the chart, is indexed as Bars-1.

Ok, I think I see this now, thanks
 
Thanks everyone

As I learn more about indicators it seems that the design I'm using would not be very useful for use in an EA but only perhaps good for visual and/or manual use.

Since the counter counts down 1-- it would seem that using this method would not be good to refer to any array objects for use in trading unless perhaps I only refer to a particular shift[i] in which case I would have to figure out how to specifically refer to only those array elements that I would want to use for a trading signal located at only those particular Bars.

Using it as it stands would likely cause trading to occur on the whole chart just like the indicator

So now I guess I will be learning just how to refer to various array elements in any indicator in order to refer to those values/times/bars etc.

Thanks everyone for the help, and this will give me a lot to think about on how I might create iCustom indicators and make them useful in an EA
Reason: