thinking mistake

 

Good day,


double highestP = High[iHighest(NULL,0,MODE_HIGH,48,1)];
int    x = iHighest(NULL,0,MODE_HIGH,48,1);
double xrespMAh = iMA(NULL,0,20,0,0,2,x);

bool highcheck;

if (highestP > xrespMAh) highcheck = True;
else highcheck = False;


I have thinking mistake: I wanted to make code to check if within last 48 bars has been there a high price that is above its corresponding moving average. Because I beliefed that within this period time highest price automatically meaned also above moving average of that bar. But some time highest price was not above moving average and some time has been high price above moving average but it is not highest of the most total within 48 bars. So I have been thinked about a way to remember every high price within last 48 bars and make condition to get only that bars who make above moving average. So all this look like this:


double sma[4];
sma[0] = iMA(NULL,0,20,0,0,0,1);
sma[1] = iMA(NULL,0,20,0,0,0,2);
sma[2] = iMA(NULL,0,20,0,0,0,3);
sma[3] = iMA(NULL,0,20,0,0,0,4);
double high[4];
high[0] = High[1];
high[1] = High[2];
high[2] = High[3];
high[3] = High[4];
string name[4];
name[0] = "1";
name[1] = "2";
name[2] = "3";
name[3] = "4";

for(int g = ArraySize(high)-1; g >= 0; g--)
{
   if (high[g] < sma[g])
   {
      ObjectCreate(name[g],OBJ_HLINE, 0, Time[0], high[g]);
   }
}


You saw that I have reducing code above to only 4 variables because 48 time variable is too big. My questions to you are two: First number: Is this good code? Second number: There is more intelligent alternative for this so not so many variables to write? I just thinked to you to forget question number first. Code I know is bad. But is better way exist to code this problem?

 

Why not . . .

int g=1;

while(g <= 48)
   {
   
   if(High[g] < iMA(NULL,0,20,0,0,0,g))
      ObjectCreate(StringConcatenate("SMA_Line_", g),OBJ_HLINE, 0, Time[0], High[g]);
   g++;  
   }

. . . or similar ?

 
RaptorUK:

Why not . . .

. . . or similar ?


Of course this it is! Simply solution of include the increment to condition for array to loop! You are clever many time! Thank you!
Reason: