Hello forum, I have put a similar post to this on the forum previously, but still struggling to understand "Lookback" function and whether it is actually what I want to use anyway.
If I want to have as part of a condition that the close price must have been higher than a moving average at least once during the last 5 bars
You need to clarify what you mean, sorry it isn't 100% clear.
Do you mean,
1. close for any of the last 5 bars > MA for any of the last 5 bars . . . or
2. close for any of the last 5 bars > MA for the same bar . . . or
3. close for any of the last 5 bars > MA for a specified bar
You will most likely be using iHighest with type MODE_CLOSE and/or a for/while loop that counts 5 cycles.
explaining with a picture might be helpful when you ask such a question

You need to clarify what you mean, sorry it isn't 100% clear.
Do you mean,
1. close for any of the last 5 bars > MA for any of the last 5 bars . . . or
2. close for any of the last 5 bars > MA for the same bar . . . or
3. close for any of the last 5 bars > MA for a specified bar
You will most likely be using iHighest with type MODE_CLOSE and/or a for/while loop that counts 5 cycles.
Sorry I wasn't clear, I should have done as deVries suggested and put in a simple diagram.
I have attached a picture of what I am talking about.
In this example, say I was looking to enter short at bar 0, but only if the close of any of the previous 5 bars was > than the moving average.
In this example the nearest previous close of an individual bar above the moving average was 6 bars previous, so my entry condition would not be satisfied.
So what I would like to be able to specify in my code is:
that the close of any of the previous 5 bars must have been above the specified moving average
and this is what I am not sure how to define.
If you could help me, it would be greatly appreciated
regards
Dave
So what I would like to be able to specify in my code is:
that the close of any of the previous 5 bars must have been above the specified moving average
Something like this would probably do it. I can't see a way of doing it on one line since the MA will change on each bar.
Regards, Paul.
bool EntryOK() { for ( int i = 1; i <= 5; i++ ) { if ( Close[i] > iMA(NULL,0,5,0,MODE_SMA,PRICE_CLOSE,i) ) return(true); } return(false); }

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello forum, I have put a similar post to this on the forum previously, but still struggling to understand "Lookback" function and whether it is actually what I want to use anyway.
If I want to have as part of a condition that the close price must have been higher than a moving average at least once during the last 5 bars
Do I simply specify it as
iClose>iMA(Symbol(),0,5,0,MODE_SMA,PRICE_CLOSE,i)
If the Lookback value was specified as 5
OR
Do I need to some how use all shift values, like...
iClose>( iMA..shift1) && iMA(...shift 2) &&...iMA(..shift5))
Any help would be appreciated with this
thanks in advance
Dave