Please Help. Basic EA will not close orders. I have spent 3 days, do not understand why iy will not close

 

I have coded this Basic EA, which works fine, except fot the second half, which will not close orders, or operate the trailing stop.

I have spent many hours, no Joy. I have exhauste all my knowledge, and am at a total loss.

It all works Ok, but will not operate anything below the " for(cnt=0;cnt<total;cnt++)" line hallf way down.

Any help would be most appreciated

The advisor is as follows

//+------------------------------------------------------------------+
//| "HELP ME PLEASE" Custom |
//| Copyright © 2009, The Veeb. |
//| USD/JPY 15m |
//+------------------------------------------------------------------+


extern double magicnumber =2477;
extern double Lots = 0.1;
extern double TP =150;
extern double SL =400;
extern double TrailingStop =150;
extern double buffer =0.002;
extern double fast_ema =25;
extern double slow_ema=125;
extern double MACD_signal =25;
extern double HL =0;
extern double HH =24;



int start()

{
int cnt, ticket,total;
double h;
h = TimeHour(TimeCurrent());

if (OrdersTotal() < 1)

{
if(AccountFreeMargin()<(1000*Lots))

{ Print("We have no money. Free Margin = ", AccountFreeMargin());
return(0); }

// check for long position (BUY) possibility


if (((iMACD(NULL,0,fast_ema,slow_ema,MACD_signal,2,MODE_SIGNAL,0))+ buffer)
> (iMACD(NULL,0,fast_ema,slow_ema,MACD_signal,2,MODE_MAIN,0)) )

if ((iMACD(NULL,0,fast_ema,slow_ema,MACD_signal,2,MODE_SIGNAL,5))
< (iMACD(NULL,0,fast_ema,slow_ema,MACD_signal,2,MODE_MAIN,5)) )


if (h>=HL && h<=HH)

{ ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,4,0,0,"MULTIPLE",magicnumber,0,CLR_NONE);

if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))

Print("BUY order opened : ",OrderOpenPrice());
{

if ( OrderStopLoss() <1)

OrderModify(OrderTicket(),Ask,OrderOpenPrice()-Point*SL,OrderOpenPrice()+Point*TP,0,CLR_NONE);

}

}
else Print("Error opening BUY order : ",GetLastError());

return(0);
}


// check for short position (SELL) possibility


if (((iMACD(NULL,0,fast_ema,slow_ema,MACD_signal,3,MODE_SIGNAL,0))- buffer)
< (iMACD(NULL,0,fast_ema,slow_ema,MACD_signal,3,MODE_MAIN,0)) )

if ((iMACD(NULL,0,fast_ema,slow_ema,MACD_signal,3,MODE_SIGNAL,5))
> (iMACD(NULL,0,fast_ema,slow_ema,MACD_signal,3,MODE_MAIN,5)) )

if (h>=HL && h<=HH)


{
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,0,"MULTIPLE",magicnumber,0,CLR_NONE);

if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))

Print("SELL order opened : ",OrderOpenPrice());

{
if ( OrderStopLoss()<1)

OrderModify(OrderTicket(),Bid,OrderOpenPrice()+Point*SL,OrderOpenPrice()-Point*TP,0,CLR_NONE);

}

}
else Print("Error opening SELL order : ",GetLastError());

return(0);
}

return(0);
}


// My indictor does not read anything below this line. Please Please HELP





for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_SELL && // check for opened position
OrderSymbol()==Symbol()) // check for symbol
{
if(OrderType()==OP_BUY) // long position is opened
{
// should it be closed?


if (Ask>1) //Just a test to see it it will close. It doesn't


{
OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position
return(0); // exit
}


// check for trailing stop
if(TrailingStop>0)
{
if(Bid-OrderOpenPrice()>Point*TrailingStop)
{
if(OrderStopLoss()<Bid-Point*TrailingStop)
{
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,CLR_NONE);
return(0);
}
}
}
}
else // go to short position
{
// should it be closed?

if (Bid >1) //Again, just a test. It will not close

{
OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close position
return(0); // exit
}
// check for trailing stop

if(TrailingStop>0)
{
if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
{
if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
{
OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,CLR_NONE);
return(0);
}
}
}
}
}
}

return(0);
}

// the end.

 
theveeb:

I have coded this Basic EA, which works fine, except fot the second half, which will not close orders, or operate the trailing stop.

I have spent many hours, no Joy. I have exhauste all my knowledge, and am at a total loss.

It all works Ok, but will not operate anything below the " for(cnt=0;cnt<total;cnt++)" line hallf way down.

Any help would be most appreciated

The advisor is as follows

//+------------------------------------------------------------------+
//| "HELP ME PLEASE" Custom |
//| Copyright © 2009, The Veeb. |
//| USD/JPY 15m |
//+------------------------------------------------------------------+


extern double magicnumber =2477;
extern double Lots = 0.1;
extern double TP =150;
extern double SL =400;
extern double TrailingStop =150;
extern double buffer =0.002;
extern double fast_ema =25;
extern double slow_ema=125;
extern double MACD_signal =25;
extern double HL =0;
extern double HH =24;



int start()

{
int cnt, ticket,total;
double h;
h = TimeHour(TimeCurrent());

if (OrdersTotal() < 1)

{
if(AccountFreeMargin()<(1000*Lots))

{ Print("We have no money. Free Margin = ", AccountFreeMargin());
return(0); }

// check for long position (BUY) possibility


if (((iMACD(NULL,0,fast_ema,slow_ema,MACD_signal,2,MODE_SIGNAL,0))+ buffer)
> (iMACD(NULL,0,fast_ema,slow_ema,MACD_signal,2,MODE_MAIN,0)) )

if ((iMACD(NULL,0,fast_ema,slow_ema,MACD_signal,2,MODE_SIGNAL,5))
< (iMACD(NULL,0,fast_ema,slow_ema,MACD_signal,2,MODE_MAIN,5)) )


if (h>=HL && h<=HH)

{ ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,4,0,0,"MULTIPLE",magicnumber,0,CLR_NONE);

if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))

Print("BUY order opened : ",OrderOpenPrice());
{

if ( OrderStopLoss() <1)

OrderModify(OrderTicket(),Ask,OrderOpenPrice()-Point*SL,OrderOpenPrice()+Point*TP,0,CLR_NONE);

}

}
else Print("Error opening BUY order : ",GetLastError());

return(0);
}


// check for short position (SELL) possibility


if (((iMACD(NULL,0,fast_ema,slow_ema,MACD_signal,3,MODE_SIGNAL,0))- buffer)
< (iMACD(NULL,0,fast_ema,slow_ema,MACD_signal,3,MODE_MAIN,0)) )

if ((iMACD(NULL,0,fast_ema,slow_ema,MACD_signal,3,MODE_SIGNAL,5))
> (iMACD(NULL,0,fast_ema,slow_ema,MACD_signal,3,MODE_MAIN,5)) )

if (h>=HL && h<=HH)


{
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,0,"MULTIPLE",magicnumber,0,CLR_NONE);

if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))

Print("SELL order opened : ",OrderOpenPrice());

{
if ( OrderStopLoss()<1)

OrderModify(OrderTicket(),Bid,OrderOpenPrice()+Point*SL,OrderOpenPrice()-Point*TP,0,CLR_NONE);

}

}
else Print("Error opening SELL order : ",GetLastError());

return(0);
}

return(0);
}


// My indictor does not read anything below this line. Please Please HELP





for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_SELL && // check for opened position
OrderSymbol()==Symbol()) // check for symbol
{
if(OrderType()==OP_BUY) // long position is opened
{
// should it be closed?


if (Ask>1) //Just a test to see it it will close. It doesn't


{
OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position
return(0); // exit
}


// check for trailing stop
if(TrailingStop>0)
{
if(Bid-OrderOpenPrice()>Point*TrailingStop)
{
if(OrderStopLoss()<Bid-Point*TrailingStop)
{
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,CLR_NONE);
return(0);
}
}
}
}
else // go to short position
{
// should it be closed?

if (Bid >1) //Again, just a test. It will not close

{
OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close position
return(0); // exit
}
// check for trailing stop

if(TrailingStop>0)
{
if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
{
if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
{
OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,CLR_NONE);
return(0);
}
}
}
}
}
}

return(0);
}

// the end.

Couple of suggestions:

1. Split your code into separate functions called from start(). That will make it easier for you and others to maintain.

2. Loops (such as your loop to close orders) should start at the top and increment downwards, so you should recode yours to reverse it.

3. Pop some Print() statements into key locations in your code to a) check if certain lines are executed and b) check the content of certain variables


CB

 
cloudbreaker wrote >>

Couple of suggestions:

1. Split your code into separate functions called from start(). That will make it easier for you and others to maintain.

2. Loops (such as your loop to close orders) should start at the top and increment downwards, so you should recode yours to reverse it.

3. Pop some Print() statements into key locations in your code to a) check if certain lines are executed and b) check the content of certain variables

CB

I greatly appreciate your help, but your explanation is a bit over my head. I am still not sure where the error lies. It is a very simple advisor, based heavily on the sample MACD advisor in the MQ4 template. The template works, but mine doesn't. Thanks heaps, but I am still lost.
 
theveeb:
I greatly appreciate your help, but your explanation is a bit over my head. I am still not sure where the error lies. It is a very simple advisor, based heavily on the sample MACD advisor in the MQ4 template. The template works, but mine doesn't. Thanks heaps, but I am still lost.

Which bit went over your head?


CB

 

//Instead of


for(cnt=0;cnt<total;cnt++)
{

...

}


// try


for(int i=0;i<OrdersTotal();i++) //<--- THE PROBLEM IS HERE, instead of total, put OrdersTotal()
{

if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;

if(OrderSymbol()!=Symbol()) continue;

if(OrderType()==OP_BUY)

{

/// perform close, or trailing stop

}

if(OrderType()==OP_SELL)

{

/// perform close, or trailing stop

}

}

 
abstract_mind:

//Instead of


for(cnt=0;cnt<total;cnt++)
{

...

}


// try


for(int i=0;i<OrdersTotal();i++) //<--- THE PROBLEM IS HERE, instead of total, put OrdersTotal()
{

if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;

if(OrderSymbol()!=Symbol()) continue;

if(OrderType()==OP_BUY)

{

/// perform close, or trailing stop

}

if(OrderType()==OP_SELL)

{

/// perform close, or trailing stop

}

}



Abstract_mind - don't forget we should always be looping downwards to ensure we catch all orders by their index number - otherwise the logic will break. For example when we delete the order in position 0, then the order in position 1 becomes position 0. However, since we've now incremented our counter, we are now looking at position 1, missing the order that's just dropped into position 0. Oops.


CB

 

Yes, you are rigth. Therefore, correct code is:

for(int i=OrdersTotal()-1;i>=0;i--)
{

...

}

thanks CB.

 
cloudbreaker wrote >>

Abstract_mind - don't forget we should always be looping downwards to ensure we catch all orders by their index number - otherwise the logic will break. For example when we delete the order in position 0, then the order in position 1 becomes position 0. However, since we've now incremented our counter, we are now looking at position 1, missing the order that's just dropped into position 0. Oops.

CB

You guys are amazing. Thanks heaps. Couldn't see the forrest for the trees. I meant to specify at the start

total= OrdersTotal(). This line however got omitted, and was the cause. Something so simple. Thanks heaps. Ledgends !!

Reason: