Trying to change the Momemtum indicator into an EA - Unable to fix a basic bug of variable initialisation

 

Hello,

I am a beginner, trying to convert the momemtum indicator from MT4 platform to an EA to do further testing and also to learn how works the process of building EA on MT4 ;

The code I have is the following and it returns 2 time the error 'iMomemtum' initialisation expected.

I have tried several times to make the error disappear by initializing the variable and it does not work although it seems to be an very basic erro (sorry!).

I have based my first trials on the sample MACD given as a standard with the platform.

So code I have is :


extern double TakeProfit = 30;
extern double Lots = 0.1;
extern double StopLoss = 30;
extern double MomPeriod=14;
extern double MomemtumCurrent=iMomemtum;
extern double MomemtumPrevious=iMomemtum;

//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+

int start()
{
double MomemtumCurrent, MomemtumPrevious, iMomemtum;
double SignalCurrent, SignalPrevious;
int cnt, ticket, total;
// initial data checks
// it is important to make sure that the expert works with a normal
// chart and the user did not make any mistakes setting external
// variables (Lots, StopLoss, TakeProfit,
// TrailingStop) in our case, we check TakeProfit
// on a chart of less than 100 bars
if(Bars<100)
{
Print("bars less than 100");
return(0);
}
if(TakeProfit<10)
{
Print("TakeProfit less than 10");
return(0); // check TakeProfit
}


// to simplify the coding and speed up access
// data are put into internal variables

MomemtumCurrent=iMomemtum(NULL,14, PRICE_CLOSE,MODE_MAIN,0);
MomemtumPrevious=iMomemtum(NULL,14,PRICE_CLOSE,MODE_MAIN,1);
SignalCurrent=iMomemtum(NULL,0,14,PRICE_CLOSE,MODE_SIGNAL,0);
SignalPrevious=iMomemtum(NULL,0,14,PRICE_CLOSE,MODE_SIGNAL,1);


total=OrdersTotal();
if(total<0.1)
{
// no opened orders identified
if(AccountFreeMargin()<(5*Lots))
{
Print("We have no money. Free Margin = ", AccountFreeMargin());
return(0);
}
// check for long position (BUY) possibility
if(MomemtumCurrent>100 && MomemtumCurrent>MomemtumPrevious
{
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point," motfund_momemtum_custom",16384,0,Green);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
}
else Print("Error opening BUY order : ",GetLastError());
return(0);
}
// check for short position (SELL) possibility
if(MomemtumCurrent<100 && MomemtumCurrent<MomemtumPrevious
{
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point," motfund_momemtum_custom",16384,0,Red);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
}
else Print("Error opening SELL order : ",GetLastError());
return(0);
}
return(0);
}

// check for stops necessity
if(StopLoss>0)
{
if(Bid-OrderOpenPrice()>Point*StopLoss)
{
if(OrderStopLoss()<Bid-Point*StopLoss)
{
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*StopLoss, OrderTakeProfit(), 0,Green);
return(0);
}
}
}


//+------------------------------------------------------------------+


Thanks a lot for your help. :)

Matttt

 

Also tried to solve the issue with using the icustom indicator, but then I have issue with the buffer set rules and the init function....


:(


 
 
robofx.org:

Use iMomentum()

https://docs.mql4.com/indicators/iMomentum


Hello robofx;

Thanks for your answer. I'll try to make it work this way. Hope I'll be skilled enough, lol.

cheers.

 
These lines might be the problem?
extern double MomemtumCurrent=iMomemtum;
extern double MomemtumPrevious=iMomemtum;
 
fxcourt:
These lines might be the problem?

Yes, could be. This was one way I have tried to clear errors. But with your first precision, I'll try to rework it properly in the next hours . Thanks again. If it works with those changes, I'll post the updated code.
 
matttt:

Yes, could be. This was one way I have tried to clear errors. But with your first precision, I'll try to rework it properly in the next hours . Thanks again. If it works with those changes, I'll post the updated code.

Ok, let's be honest, cannot make it work and have very little chance to succeed with no prog skills.

I will continue to do it manually without EA, knowing indeed that it was only step 1 of the indicators' compilation I wanted to do.

Too bad, need to gain prog skills before hoping to make it work.

If any change, I will let you know. thx

 
matttt:

Ok, let's be honest, cannot make it work and have very little chance to succeed with no prog skills.

I will continue to do it manually without EA, knowing indeed that it was only step 1 of the indicators' compilation I wanted to do.

Too bad, need to gain prog skills before hoping to make it work.

If any change, I will let you know. thx


OK, try this... I haven't run it, but it will now compile. The reason it didn't compile was the extern doubles you used for momentum, there were a few brackets missing and you spelt imomentum wrong... Also, your use of imomentum was incorrect. there is no signal / main mode as you had included. It renders the 2 vars momentum and signal equal. so this won't do much as it stands. (Momentum oscillates around 100. maybe this is what you were meaning by signal). if it's a different indicator to the standard momentum, then you probably need to refer to it with iCustom...

You were so close... don't give up so easily :)

anyway, hope this helps...

V

extern double TakeProfit = 30;
extern double Lots = 0.1;
extern double StopLoss = 30;
extern double MomPeriod=14;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
  

//----
   return(0);
  }
//============================================================================
//============================================================================
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }

//====

//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+

int start()
   {
   double MomemtumCurrent, MomemtumPrevious, iMomemtum;
   double SignalCurrent, SignalPrevious;
   int cnt, ticket, total;
// initial data checks
// it is important to make sure that the expert works with a normal
// chart and the user did not make any mistakes setting external
// variables (Lots, StopLoss, TakeProfit,
// TrailingStop) in our case, we check TakeProfit
// on a chart of less than 100 bars
   if(Bars<100)
      {
      Print("bars less than 100");
      return(0);
      }
   if(TakeProfit<10)
      {
      Print("TakeProfit less than 10");
      return(0); // check TakeProfit
      }


// to simplify the coding and speed up access
// data are put into internal variables

   MomemtumCurrent=iMomentum(NULL,0, 14,PRICE_CLOSE,0);
   MomemtumPrevious=iMomentum(NULL,0,14,PRICE_CLOSE,1);
   SignalCurrent=iMomentum(NULL,0,14,PRICE_CLOSE,0);
   SignalPrevious=iMomentum(NULL,0,14,PRICE_CLOSE,1);


   total=OrdersTotal();
   if(total<0.1)
      {
      // no opened orders identified
      if(AccountFreeMargin()<(5*Lots))
         {
         Print("We have no money. Free Margin = ", AccountFreeMargin());
         return(0);
         }
      // check for long position (BUY) possibility
      if(MomemtumCurrent>100 && MomemtumCurrent>MomemtumPrevious)
         {
         ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point," motfund_momemtum_custom",16384,0,Green);
         if(ticket>0)
            {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
            }
            else Print("Error opening BUY order : ",GetLastError());
            return(0);
         }
      // check for short position (SELL) possibility
      if(MomemtumCurrent<100 && MomemtumCurrent<MomemtumPrevious)
         {
         ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point," motfund_momemtum_custom",16384,0,Red);
         if(ticket>0)
            {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
            }
         else Print("Error opening SELL order : ",GetLastError());
         return(0);
         }
      return(0);
      }

      // check for stops necessity
      if(StopLoss>0)
         {
         if(Bid-OrderOpenPrice()>Point*StopLoss)
            {
            if(OrderStopLoss()<Bid-Point*StopLoss)
               {
               OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*StopLoss, OrderTakeProfit(), 0,Green);
               }
             }  
          }
               
  return(0);
}


//+------------------------------------------------------------------+


 
Viffer:

OK, try this... I haven't run it, but it will now compile. The reason it didn't compile was the extern doubles you used for momentum, there were a few brackets missing and you spelt imomentum wrong... Also, your use of imomentum was incorrect. there is no signal / main mode as you had included. It renders the 2 vars momentum and signal equal. so this won't do much as it stands. (Momentum oscillates around 100. maybe this is what you were meaning by signal). if it's a different indicator to the standard momentum, then you probably need to refer to it with iCustom...

You were so close... don't give up so easily :)

anyway, hope this helps...

V


Waou, that's cool to help like that.

Such help is a good reason for me to push further. it might take time, but if I manage to reach the point I am aiming (compiling the indicators I use manually for trading in a working EA), I will post the progress here.


thx Viffer, and thx all. cheers,

 

It compiles, I confirm. Now I'll try to add more to this beginning and will keep you updated.

 

Hello,

Update :

> Have fixed the stop loss problem

> Added a maximum number of lots with

> Added some security on those points


Will post shortly the code with stop loss and maximum lots indeed.

But now I am trying to store the previous order trade with its type (buy or short), so that it only take position once after the signal, then stay out of market until the opposite signal for trade is formed.

I guess i need to store somewhere the data in a txt file and then call it as a condition to trade, but seems not so simple.

Would some work with the "history" file make it ? (gethistory?)

Reason: