Need help with EA involving momentum indicator

 

First I would like to say thanks to anyone who helps me with this EA. Im a newb at programming and have been working really hard to try and get an EA that will trade based on the standard momentum indicator. My idea is to only have one position open at a time. If no position is open then the EA will buy only if momentum is < 100.01 and then crosses above this point. For a short I want the EA to sell only if momentum is > 99.99 and then drops down to or below this level. Stop loss and take profit would set as soon as a position is taken.

Here is what I have so far.

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

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

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
extern int Expert_ID = 1234;

int start()
{

double MOM_1 = iMA(NULL,0,14,0,MODE_SMMA,PRICE_CLOSE,0); *Edit changed to- double MOM_1 = iMomentum(NULL, 0,14, PRICE_CLOSE, 0);


int _GetLastError = 0, _OrdersTotal = OrdersTotal();
//---- search in all open positions
for ( int z = _OrdersTotal - 1; z>= 0; z -- )
{
//---- if an error occurs when searching for a position, go to the next one
if ( !OrderSelect( z, SELECT_BY_POS ) )
{
_GetLastError = GetLastError();
Print( "OrderSelect( ", z, ", SELECT_BY_POS ) - Error #", _GetLastError );
continue;
}

//---- if a position is closed not for the current symbol, skip it
if ( OrderSymbol() != Symbol() ) continue;

//---- if the MagicNumber is not equal to the Expert_ID, skip this position
if ( OrderMagicNumber() != Expert_ID ) continue;

//---- if a BUY position is opened,
if ( OrderType() == OP_BUY )
{
//---- if the Momentum has met the 99. 99 line top-down,
if ( NormalizeDouble( MOM_1, Digits + 1 ) < 99. 99 &&
NormalizeDouble( MOM_1, Digits + 1 )>= 99.99 )
{
//---- close the position
if ( !OrderClose( OrderTicket(), OrderLots(), Bid, 5, Green ) )
{
_GetLastError = GetLastError();
Alert( "Error OrderClose # ", _GetLastError );
return(-1);
}
}
//---- if the alert has not changed, exit: it is too early to open a new position
else
{ return(0); }
}
//---- if a SELL position is opened,
if ( OrderType() == OP_SELL )
{
//---- if the Momentum has met the zero line bottom-up,
if ( NormalizeDouble( MOM_1, Digits + 1 )> 100. 01 &&
NormalizeDouble( MOM_1, Digits + 1 ) <= 100.01 )
{
//---- close the position
if ( !OrderClose( OrderTicket(), OrderLots(), Ask, 5, Red ) )
{
_GetLastError = GetLastError();
Alert( "Error OrderClose No ", _GetLastError );
return(-1);
}
}
//---- if the alert has not changed, exit: it is too early to open a new position
else return(0);
}
}

//+------------------------------------------------------------------+
//| if execution reached this point, there is no open position |
//| check whether it is still possible to open a position |
//+------------------------------------------------------------------+

//---- if the Momentum has met the zero line bottom-up,
if ( NormalizeDouble( MOM_1, Digits + 1 )> 100.01 &&
NormalizeDouble( MOM_1, Digits + 1 ) <= 100.01 )
{
//---- open a BUY position
if ( OrderSend( Symbol(), OP_BUY, 0.1, Ask, 5, Ask-5. 0*Point, Ask+5. 0*Point, "MOM_test",
Expert_ID, 0, Green ) < 0 )
{
_GetLastError = GetLastError();
Alert( "Error OrderSend # ", _GetLastError );
return(-1);
}
return(0);
}
//---- if the Momentum has met the zero line top-down,
if ( NormalizeDouble( MOM_1, Digits + 1 ) < 99.99 &&
NormalizeDouble( MOM_1, Digits + 1 )>= 99.99 )
{
//---- open a SELL position
if ( OrderSend( Symbol(), OP_SELL, 0. 1, Bid, 5, Bid+5. 0*Point, Bid-5. 0*Point, "MOM_test",
Expert_ID, 0, Red ) < 0 )
{
_GetLastError = GetLastError();
Alert( "Error OrderSend # ", _GetLastError );
return(-1);
}
return(0);
}

return(0);
}


//----
return(0);

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

Does it work?

 
phy:

Does it work?

It doesnt seem to work, there is a problem with the code. If anyone knows where I went wrong and can point it out, I would be grateful.
 

First error:

double MOM_1 = iMA(NULL,0,14,0,MODE_SMMA,PRICE_CLOSE,0);

iMA is "moving Average" indicator, not "Momentum"

Second Error:

if ( NormalizeDouble( MOM_1, Digits + 1 ) < 99.99 && NormalizeDouble( MOM_1, Digits + 1 ) >= 99.99 )

MOM_1 cannot be less than 99.99 AND greter than or equal to 99.99 at the same time, so the statement

always returns FALSE

 
phy:

First error:

double MOM_1 = iMA(NULL,0,14,0,MODE_SMMA,PRICE_CLOSE,0);

iMA is "moving Average" indicator, not "Momentum"

Second Error:

if ( NormalizeDouble( MOM_1, Digits + 1 ) < 99.99 && NormalizeDouble( MOM_1, Digits + 1 ) >= 99.99 )

MOM_1 cannot be less than 99.99 AND greter than or equal to 99.99 at the same time, so the statement

always returns FALSE


I changed the iMA mistake to (Funny how that was staring me in the face and I missed it :)

double MOM_1 = iMomentum(NULL,0,14,PRICE_CLOSE,0);

I was trying to find a way to tell the EA to short if the value for momentum was larger than 99.99 and then dropped down to or below that level. Any suggestions on how to do this? Is there a way to tell the EA to compare values every tick so that it only shorts when the value was above 99.99 and then drops down to/below it on the next tick when that scenario occurs?

 

"Is there a way to tell the EA to compare values every tick so that it only shorts when the value was above 99.99 and then drops down to/below it on the next tick when that scenario occurs?
"

Compare momentum values between two bars to determine if a cross has occurred

double momentumCurrentBar;

double momentumPriorBar;

start {

momentumCurrentBar = iMomentum(NULL,0,14,PRICE_CLOSE,0);
momentumPriorBar = iMomentum(NULL, 0, 14,PRICE_CLOSE, 1);
...

f(momentumPriorBar > 99.99 && momentumCurrentBar <= 99.99){

... do stuff

}

 
phy:

"Is there a way to tell the EA to compare values every tick so that it only shorts when the value was above 99.99 and then drops down to/below it on the next tick when that scenario occurs?
"

Compare momentum values between two bars to determine if a cross has occurred

double momentumCurrentBar;

double momentumPriorBar;

start {

momentumCurrentBar = iMomentum(NULL,0,14,PRICE_CLOSE,0);
momentumPriorBar = iMomentum(NULL, 0, 14,PRICE_CLOSE, 1);
...

f(momentumPriorBar > 99.99 && momentumCurrentBar <= 99.99){

... do stuff

}

Thanks a lot !!
Reason: