NEED HELP:1 Buy and 1 Sell in a cycle

 

Dear MQ4 Coder


Kindly need your help

Trying many time with difference approaching, develop my EA with condition: 1 buy and sell in a cycle of trend, but so far getting failed.


My EA Logic:

1. First time activate the EA, EA will OP BUY and SELL based on signal.

2. After order closed by hit TP or SL, and Signal still goes on EA will not take any position until the opposite signal appears.


Herewith my code, but still not working:


int last_trade=HistoryTotal();
if(last_trade>0)
 {
 if(OrderSelect(last_trade-1,SELECT_BY_POS,MODE_HISTORY)==true)
 { 
 if((OrderType()==OP_SELL) && (SIGNAL=="SELL"))  return(0); 
 if((OrderType()==OP_BUY) && (SIGNAL=="BUY"))  return(0);  
 if((OrderType()==OP_BUY) && (SIGNAL=="SELL")) OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Ask+StopLoss*Point,TakeProfitSell,"Smart Sell",MagicNumber,0,Red);
 if((OrderType()==OP_SELL) && (SIGNAL=="BUY")) OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Bid-StopLoss*Point,Ask+TakeProfit*Point,"Smart Buy",MagicNumber,0,Green); 
 }
 else if(OrderSelect(last_trade-1,SELECT_BY_POS,MODE_HISTORY)==false)
 {
 if((OrderType()==OP_BUY) && (SIGNAL=="BUY"))OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Bid-StopLoss*Point,Ask+TakeProfit*Point,"Smart Buy",MagicNumber,0,Green); 
 if((OrderType()==OP_SELL) && (SIGNAL=="SELL"))OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Ask+StopLoss*Point,TakeProfitSell,"Smart Sell",MagicNumber,0,Red); 
 }
 else { Comment("ERROR BUY and SELL"); }
 }
 }


Million Thanks and sorry for my poor English

Alex | Indonesia

 
alexcorvis:

Dear MQ4 Coder


Kindly need your help

Trying many time with difference approaching, develop my EA with condition: 1 buy and sell in a cycle of trend, but so far getting failed.


My EA Logic:

1. First time activate the EA, EA will OP BUY and SELL based on signal.

2. After order closed by hit TP or SL, and Signal still goes on EA will not take any position until the opposite signal appears.


Herewith my code, but still not working:



Million Thanks and sorry for my poor English

Alex | Indonesia




Maybe you should change the code a little bit like this:

int last_trade = OrdersHistoryTotal() - 1;

Everything else remains the same...

 
if(OrderSelect(last_trade-1,SELECT_BY_POS,MODE_HISTORY)==true) { 
  if((OrderType()==OP_SELL) && (SIGNAL=="SELL"))  return(0); 
  //...
}
// At this point you can use just an else
else {// if(OrderSelect(last_trade-1,SELECT_BY_POS,MODE_HISTORY)==false)
 /* if orderSelect failed then OrderType is not valid.
  * if((OrderType()==OP_BUY) ... */
 
WHRoeder:


Thanks WHRoeder for your prompt response,
let me try
Reason: