EA entry and close at specific time

 

Hello guys, i've just started making this experts advisor. The entry is running well, but i cant sorted out about the closing code. Please help :)


#include <Trade\Trade.mqh>

CTrade trade;

void OnTick()
  {
   double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
   
   double Bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
   
   datetime time=TimeLocal();
   
   string hoursAndMinutes=TimeToString(time,TIME_MINUTES);
   
   if ((PositionsTotal()==0)&&(StringSubstr(hoursAndMinutes,0,5)=="10:10"))
   
   {
    trade.Buy(0.01,NULL,Ask,(Ask-150 * _Point),(Ask+80 * _Point),NULL);
    //trade.Sell(0.01,NULL,Bid,(Bid+150 * _Point),(Bid-80 * _Point),NULL);
   }
   
   if ((PositionsTotal()==1)&&(StringSubstr(hoursAndMinutes,0,5)=="10:11"))
   
   {
   
    CloseAllPositions();
    
   }
      
   Comment (hoursAndMinutes);
   
  }
  
void CLoseAllPositions()

  {
  
   for(int i=PositionsTotal()-1; i>=0; i--)
   
   {
   
    int ticket=PositionGetTicket(i);
    
    trade.PositionClose(ticket);
    
   }
   
  }
 
  1.    datetime time=TimeLocal();
       
       string hoursAndMinutes=TimeToString(time,TIME_MINUTES);
       
       if ((PositionsTotal()==0)&&(StringSubstr(hoursAndMinutes,0,5)=="10:10"))
    What if there are no ticks during a specific candle period? There can be minutes between ticks during the Asian session.

  2.     trade.Buy(0.01,NULL,Ask,(Ask-150 * _Point),(Ask+80 * _Point),NULL);

    You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit and open at the Ask.

    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close to a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

    3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)
      Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY (OANDA) shows average spread = 26 points, but average maximum spread = 134 (your broker will be similar).

 
William Roeder #:
  1. What if there are no ticks during a specific candle period? There can be minutes between ticks during the Asian session.

  2. You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit and open at the Ask.

    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close to a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

    3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)
      Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY (OANDA) shows average spread = 26 points, but average maximum spread = 134 (your broker will be similar).

My SL and TP works fine Mr Roeder. In this case, i need help to close the trade if within 2 minutes it has not meet SL or TP condition. My code still not working. Can you help me?

CloseAllPositions(); — still have errors in it :(