Open an order following an order after the current bar is finished

 

Anyone have idea on how to do the following?

An order was opened within this bar. And after the current bar is finished, a new order will be opened.

I tried play with the time, but it doesnt help. Are there anything wrong with my code?

And any alternatives I can take to do this?


*Without putting if(TimeCurrent()>=OrderOpenTime()+3600), it just open a sceond trade immediately after the first trade.

void SecondTrade ()
 {

if(OrderSelect(OrdersTotal()-1,SELECT_BY_POS))
if(TimeCurrent()>=OrderOpenTime()+3600)
if(OrderType()==OP_BUY)  
  {

  int buyorder1=OrderSend (NULL,0,LotSize,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,NULL,MagicNumber,0,Green);
  
 
  
  }


if(OrderSelect(OrdersTotal()-1,SELECT_BY_POS))
if(TimeCurrent()>=OrderOpenTime()+3600)
if(OrderType()==OP_SELL) 
  {

  int sellorder1=OrderSend (NULL,1,LotSize,Bid,3,Bid+StopLoss*Point,Bid-TakeProfit*Point,NULL,MagicNumber,0,Red);
 
 
 
  }
 }
 
  1. if(OrderSelect(OrdersTotal()-1,SELECT_BY_POS))
    Using OrdersTotal directly and/or no Magic number filtering on your OrderSelect loop means your code is incompatible with every EA (including itself on other charts and manual trading.) Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 forum
  2. What happens if there are no open orders?
  3. if(TimeCurrent()>=OrderOpenTime()+3600)
    
    Don't hard code numbers. Fails if you ever change timeframes. PeriodSeconds
  4. If you don't want to open until a new bar starts just check if Time[0] > OrderOpenTime()
  5. int buyorder1=OrderSend (NULL,0,LotSize,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,NULL,MagicNumber,0,Green);
    
    Check your return codes What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
    • You can use NULL in place of _Symbol only in those calls that the documentation specially says you can. iHigh does, MarketInfo does not. OrderSend does not.
    • Don't use NULL (except for pointers where you explicitly check for it.) Use _Symbol and _Period, that is minimalist as possible and more efficient.
    • Zero is the same as PERIOD_CURRENT which means _Period.
    • No need for a function call with iHigh(NULL,0,s) just use the predefined arrays, i.e. High[]
  6. You are not adjusting SL, TP, and slippage; for 4/5 digit brokers and for JPY pairs.
    double   pip          = StringFind(_Symbol,"JPY") < 00.010.0001;
    int      pipsToPoints = int(pip / _Point);
    int      pipDigits    = (int)MathLog10(pipsToPoints);
    int      slippage     = 3 * pipsToPoints;
Reason: