tazz90: trying to mirror moving average indicator by following code , but its not working. kindly can anyone spot the problem
- Because what you wrote is complete babel. I can't even begin to describe it, it's that bad. Toss what you have and look at the moving_averages.mq4 in the experts\indicators folder
Play videoPlease edit your post.
For large amounts of code, attach it.value[Bars-i]=0;
Since you didn't set an empty value of zero, you must either leave the values unmodified (they are already EMPTY_VALUE) or set them to EMPTY_VALUE. Setting them to zero will draw them.sum=0; for(int j=1;j < timePeriod ; j++,pos--) { //index=index+1; sum=sum +Close[pos]; } value[pos+timePeriod]=sum/timePeriod;
This sums one less than timePeriod. should be j=0;- The first time that loop starts with pos = Bars-1. and would set value[Bars-1] = sum/tp. The next time. pos is already at Bars-1-timePeriod. so you skip all bars between value[Bars-2] and pos.
WHRoeder:
- Because what you wrote is complete babel.
hahahha now i have changed my code ... it works fine when IndicatorCounted() =0, but after that calculated values are not like MA indicator values . can you help
thanks//+------------------------------------------------------------------+ //| sampleIndicator.mq4 | //| Copyright 2013, MetaQuotes Software Corp. | //| http://www.metaquotes.net | //+------------------------------------------------------------------+ #property copyright "Copyright 2013, MetaQuotes Software Corp." #property link "http://www.metaquotes.net" #property indicator_chart_window #property indicator_buffers 1 #property indicator_color1 Blue double value[]; double sum=0; // int index=1; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int init() { //---- indicators SetIndexStyle(0,DRAW_LINE); SetIndexBuffer(0,value); //---- return(0); } //+------------------------------------------------------------------+ //| Custom indicator deinitialization function | //+------------------------------------------------------------------+ int deinit() { //---- //---- return(0); } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ int start() { int unChangedBars = IndicatorCounted(); int pos; int index; int timePeriod = 13; double avg; if (unChangedBars ==0) { index=1; for(int i=1;i < timePeriod ; i++) { value[Bars-i]=0; index++; sum=sum+Close[Bars-i]; } pos=Bars-index; while(pos>=0) { sum=sum+Close[pos]; value[pos]=sum/timePeriod; avg=sum/timePeriod; pos--; sum=sum-Close[pos+timePeriod]; Print(" avg= " + avg +" pos= "+pos); } } else { pos=Bars-unChangedBars-1; sum=0; if(pos<timePeriod) pos=timePeriod; for(int j=1;j < timePeriod ; j++) { sum=sum+Close[pos-j]; } while(pos>=0) { sum=sum+Close[pos]; value[pos]=sum/timePeriod; avg=sum/timePeriod; pos--; sum=sum-Close[pos+timePeriod]; Print(" avg= " + avg +" pos= "+pos +" sum="+sum); } } //---- return(0); } //+------------------------------------------------------------------+
Files:
sampleindicator.mq4
3 kb

You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
hi
i am new to automated trading domain and mql and trying to mirror moving average indicator by following code , but its not working. kindly can anyone spot the problem so that i could fix it
thanks
//+------------------------------------------------------------------+
//| sampleIndicator.mq4 |
//| Copyright 2013, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net"
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Blue
double value[];
int timePeriod =10;
double sum=0;
int index=1;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,value);
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int unChangedBars = IndicatorCounted();
int pos=Bars-unChangedBars-1;
if (unChangedBars ==0)
{
for(int i=1;i < timePeriod ; i++)
{
value[Bars-i]=0;
index=index+1;
sum=sum +Close[Bars-i];
}
for (;;)
{
if ((index+1)>Bars)
break;
else
{
sum=sum+Close[Bars-index];
int x=Bars-index;
double avg =sum/timePeriod;
Print("bars-index=" + x +" sum= " + avg + " close= " + Close[index-timePeriod]) ;
value[Bars-index]=sum/timePeriod;
sum= sum-Close[index-timePeriod];
index=index+1;
}
}
}
else
{
sum=0;
for(int j=1;j < timePeriod ; j++,pos--)
{
//index=index+1;
sum=sum +Close[pos];
}
value[pos+timePeriod]=sum/timePeriod;
}
//----
return(0);
}
//+------------------------------------------------------------------+