# me again, need help to create a moving average indicator (closed)

631

I'm having a little problem in learning to create custom indicators, EA's seem more easy, and i'm new to mql4  and programming, could anyone

help me to create this simple moving average indicator. I dont know what i'm missing, can anyone help me?

//+------------------------------------------------------------------+
//|                                                MovingAverage.mq4 |
//|                        Copyright 2017, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property version   "1.00"
#property strict
#property indicator_chart_window
#property indicator_color1 Red
#property indicator_buffers 2

double MovingAverage [];

int init()
{
SetIndexBuffer(0,MovingAverage);
SetIndexStyle (0,DRAW_LINE,STYLE_SOLID,2);

return(INIT_SUCCEEDED);
}

int start ()

{

int i=1000;
double MovAverage= iMA (NULL,0,5,0,MODE_SMA,PRICE_CLOSE,0);

while (i>=0)

{

MovingAverage [i]= MovAverage;
i= i-1;

}

return (0);
}

885
Yerlan Imangeldinov
#include <Indicators\Trend.mqh>
int MovinBarsQuant =20;
int Shift=0;
CiMA moving;

int OnInit()
{

moving.Create(_Symbol,_Period,MovinBarsQuant,Shift,MODE_SMA,PRICE_CLOSE);
moving.Refresh();

}
//---
return(INIT_SUCCEEDED);
}
moving.Refresh();
631

ToYerlan Imangeldinov : I can't understand your code, could you provide me a C version with a normal loop? thanks

I need help and just can't find 1 example that fits. I just need a code that will copy iMA values and create a moving average, i dont know

how to set the loop and array correctly, just that, please.

15934

 mrluck1: help me to create this simple moving average indicator. I dont know what i'm missing, can anyone help me? double MovAverage= iMA (NULL,0,5,0,MODE_SMA,PRICE_CLOSE,0); while (i>=0){   MovingAverage [i]= MovAverage;   i= i-1;} Why are you assigning the same value to every buffer element? See How to do your lookbacks correctly.
631

To whroeder: Yes i noted that it created just a straight line, but how can i assign the current iMA value to every buffer element to create this moving average? I dont know how to do it and i'm new in mql4

and mql4 book doesnt teach you this. Thanks

15934

 mrluck1: but how can i assign the current iMA value to every buffer element to create this moving average? That is what you are doing, thus a straight line.Assign different values. What is the last argument to iMA?
631

whroeder1:
 That is what you are doing, thus a straight line.Assign different values. What is the last argument to iMA?

could you just provide me the code and i will understand imediatelly, i have a problem with arrays, and also in mql4 book they dont teach you this, just assigning to High, Low, thats not the case.

so i need to see the code, to create a moving averege copying iMA values, for learning purposes. thanks

Moderator
5563

mrluck1:

could you just provide me the code and i will understand imediatelly, i have a problem with arrays, and also in mql4 book they dont teach you this, just assigning to High, Low, thats not the case.

so i need to see the code, to create a moving averege copying iMA values, for learning purposes. thanks

You have to use i for the bar shift otherwise you assign the same value to each position in the array, as i think you noticed.

int i=1000;

while (i>=0)
{
MovingAverage [i]= iMA (NULL,0,5,0,MODE_SMA,PRICE_CLOSE,i);
i= i-1;
}
631

To Marco Vd Heijden: Thanks a lot man, you basically teached me how to create EAs and custom indicators, if it wasn't for you this forum wouldn't be the same,  i'll be forever in debt

Best regards