Perhaps even the simplest code is showing error when compiled by me.any idea.?

To add comments, please log in or register
saurabh suman
61
saurabh suman  

void OnTick()
{
double mymovingaveragearray1[],mymovingaveragearray2[];
int movingaveragedefinition1 = iMA {_Symbol,_Period,20,0,MODE_EMA,PRICE_CLOSE};
int movingaveragedefinition2 = iMA {_Symbol,_Period,20,0,MODE_EMA,PRICE_CLOSE};
ArraySetAsSeries(mymovingaveragearray1,true);
ArraySetAsSeries(mymovingaveragearray2,true);

CopyBuffer(movingaveragedefinition1,0,0,3,mymovingaveragearray1);
CopyBuffer(movingaveragedefinition2,0,0,3,mymovingaveragearray2);

if(
(mymovingaveragearray1[0]>mymovingaveragearray2[0])
&&
((mymovingaveragearray1[1]<mymovingaveragearray2[1])
)
{
Comment("BUY");
}
if(
(mymovingaveragearray1[0]<mymovingaveragearray2[0])
&&
((mymovingaveragearray1[1]>mymovingaveragearray2[1])
)
{
Comment("SELL");
}
}






any error in this or its the way i m compiling?

lippmaje
862
lippmaje  

Use the code format button </> to format your post. You can auto-indent your code with the Styler, Tools->Styler in MetaEditor.

The MA 1 and 2 values will never differ because they get the same handle.

saurabh suman
61
saurabh suman  

@lippmaje


void OnTick()
{
double mymovingaveragearray1[],mymovingaveragearray2[];
int movingaveragedefinition1 = iMA {_Symbol,_Period,20,0,MODE_EMA,PRICE_CLOSE};
int movingaveragedefinition2 = iMA {_Symbol,_Period,20,0,MODE_EMA,PRICE_CLOSE};
ArraySetAsSeries(mymovingaveragearray1,true);
ArraySetAsSeries(mymovingaveragearray2,true);

CopyBuffer(movingaveragedefinition1,0,0,3,mymovingaveragearray1);
CopyBuffer(movingaveragedefinition2,0,0,3,mymovingaveragearray2);

if(
(mymovingaveragearray1[0]>mymovingaveragearray2[0])
&&
((mymovingaveragearray1[1]<mymovingaveragearray2[1])
)
{
Comment("BUY");
}
if(
(mymovingaveragearray1[0]<mymovingaveragearray2[0])
&&
((mymovingaveragearray1[1]>mymovingaveragearray2[1])
)
{
Comment("SELL");
}
}
lippmaje
862
lippmaje  
Answer above, look at the MA parameters, they don't differ. I guess you want short and long MA, so get the periods right.
saurabh suman
61
saurabh suman  

Syntax error friend.

Chk this..

@lippmaje




lippmaje
862
lippmaje  
'(...)' and '{...}' are different things in this language, so you need to understand this first.
saurabh suman
61
saurabh suman  

@lippmaje


Thnx man.

My bad .

I gave extra brackets at if and confused with a simple over curly.

Thnx a lot.

lippmaje
862
lippmaje  
input int MAPeriodFast=10;
input int MAPeriodSlow=20;

double MovingAverageBuffer1[];
double MovingAverageBuffer2[];
int MovingAverageHandle1;
int MovingAverageHandle2;

int OnInit()
{
  MovingAverageHandle1=iMA(_Symbol,_Period,MAPeriodFast,0,MODE_EMA,PRICE_CLOSE);
  MovingAverageHandle2=iMA(_Symbol,_Period,MAPeriodSlow,0,MODE_EMA,PRICE_CLOSE);
  //ArraySetAsSeries(mymovingaveragearray1,true);
  //ArraySetAsSeries(mymovingaveragearray2,true); <-- not needed, 'true' is default in MQL5
  return(INIT_SUCCEEDED);
}

void OnTick()
{
  int start=0;
  int count=3;
  CopyBuffer(MovingAverageHandle1,MAIN_LINE,start,count,MovingAverageBuffer1); // <-- use speaking names as argument, it helps to understand your code better
  CopyBuffer(MovingAverageHandle2,MAIN_LINE,start,count,MovingAverageBuffer2);

  if( MovingAverageBuffer1[0]>MovingAverageBuffer2[0]
      &&
      MovingAverageBuffer1[1]<MovingAverageBuffer2[1] )
  {
    Comment("BUY");
  }
  if( MovingAverageBuffer1[0]<MovingAverageBuffer2[0]
      &&
      MovingAverageBuffer1[1]>MovingAverageBuffer2[1] )
  {
    Comment("SELL");
  }
}

Try this one. I've moved some of the intializing code to OnInit().

To add comments, please log in or register