English

## Highest valueof moving average

 2 Levas 2016.01.22 19:45   Hello!! This is my first topic. I am new in this forum and just started to program some expert advisors :)I am trying to get a code for getting the highest value of a moving averaage within a period of time,for example 10 bars, but I am not able to get it.I have tried:```int P2t=iHighest(iMA(NULL,0,2,0,MODE_SMA,PRICE_CLOSE,i),0,MODE_HIGH,10,1); int P2=High[P2t];``` But it is not working and I don´t manage to solve it.My idea is for example buying if: `Close[0]
13130
WHRoeder 2016.01.22 20:56 #

Levas:    int P2t=iHighest(iMA(NULL,0,2,0,MODE_SMA,PRICE_CLOSE,i),0,MODE_HIGH,10,1);

1.  Play video Please edit your post. For large amounts of code, attach it.

2. iMA returns a price. What is the first parameter of iHighest? Of course it won't work.
3. You would have to read multiple bars of iMA and find the highest value.
2
Levas 2016.01.23 00:14 #

WHRoeder:

1.  Play video Please edit your post. For large amounts of code, attach it.

2. iMA returns a price. What is the first parameter of iHighest? Of course it won't work.
3. You would have to read multiple bars of iMA and find the highest value.

Thanks a lot. I have updated the post. Much better now :)

I have also tried:

```double MA2H = 0;
double findMA2H()
{
for (int i=1; i<10; i++)
if (iMA(NULL,0,2,0,MODE_SMA,PRICE_CLOSE,i)>MA2H)
{
MA2H = iMA(NULL,0,2,0,MODE_SMA,PRICE_CLOSE,i);
return MA2H;
}
};

```

And then the buying condition if:

`Close[0]<MA2H`

But I don´t manage it..... :(

 Moderator3526 GumRai 2016.01.23 03:01 #   Please make it clear whether your code is part of the main code or a function as we don't know where your return takes you.```for (int i=1; i<10; i++) if (iMA(NULL,0,2,0,MODE_SMA,PRICE_CLOSE,i)>MA2H) { MA2H = iMA(NULL,0,2,0,MODE_SMA,PRICE_CLOSE,i); return MA2H; }``` Your loop exits after the first pass.MA2H=0, so the iMA for index 1 must be bigger and that causes the return.The return should be after the loop.If it is not part of a function, then you don't want to return at all.