Download MetaTrader 5

how to close after 5 bars

To add comments, please log in or register
Ronald Kirby
192
Ronald Kirby  

hi guys   I am trying to figure out how to close a trade after five  1 minute bars

i dont seem to be very good at it   , just hoping someone can point me in the right direction


thanks

Ron

Snelle Moda
633
Snelle Moda  

Hi

Use the Bars() function to count the number of BARS between the time you opened the position and the current time. For example:


// Check if you have an opened position             
   if(PositionSelect(Symbol()) == true) 
         {

// Count the number of BARS between the time you opened the position and the current time.
         int nPosition_OpeningTime_Difference = Bars(Symbol(), Period(), PositionGetInteger(POSITION_TIME), TimeCurrent()) - 1;

// Check if the number of BARS is greater or equal than 5.
         if(nPosition_OpeningTime_Difference >= 5)
               {
               
// your code to close the position
               
               }
         }
Ronald Kirby
192
Ronald Kirby  
Snelle Moda:

Hi

Use the Bars() function to count the number of BARS between the time you opened the position and the current time. For example:


ty   so much   i will have a play and see how i go

Ronald Kirby
192
Ronald Kirby  
Ronald Kirby:

ty   so much   i will have a play and see how i go

Snelle Moda:

Hi

Use the Bars() function to count the number of BARS between the time you opened the position and the current time. For example:

void CheckForClose()
  {
 
 
              
           
        
    
     
  
//--- go trading only for first tiks of new bar
 //  if(Volume[0]>1) return;
//--- get Moving Average
 
//---

   for(int i=0;i<OrdersTotal();i++)


int PositionGetInteger = i;

double  POSITION_TIME= OrderOpenTime();


Print ("PositionGetInteger",PositionGetInteger,"POSITION_TIME",POSITION_TIME);


     {
    // Check if you have an opened position            

 //  if(PositionSelect(Symbol()) == true)


         {

// Count the number of BARS between the time you opened the position and the current time.

         int nPosition_OpeningTime_Difference = Bars(Symbol(), Period(), PositionGetInteger(POSITION_TIME), TimeCurrent()) - 1;



// Check if the number of BARS is greater or equal than 5.


   //      if(nPosition_OpeningTime_Difference >= 5)
               {
              
// your code to close the position
              
               }
         }

Ronald Kirby
192
Ronald Kirby  
Ronald Kirby:
as you can see i have dropped your suggested code into my program   the error is asking for  PositionGetInteger      Function not defined   do you have any help for that ?
Snelle Moda
633
Snelle Moda  

Hi

You use the PositionGetInteger() in the wrong way, read: 

https://www.mql5.com/en/docs/trading/positionselect

https://www.mql5.com/en/docs/trading/positiongetinteger

https://www.mql5.com/en/docs/constants/tradingconstants/positionproperties#enum_position_property_integer



Use this example to check the number of bars and the opening time of your position:

// Check if you have an opened position.                if(PositionSelect(Symbol()) == true) // You must call this function before you can use the PositionGetInteger() function!          { // The number of BARS between the time you opened the position and the current time, you need to check for the difference in server or winter time. This can make a difference.          int nPosition_OpeningTime_Difference = Bars(Symbol(), Period(), PositionGetInteger(POSITION_TIME), TimeCurrent()) - 1;          Print(nPosition_OpeningTime_Difference, " Bars have elapsed since the position was opened! Position opening time: ", TimeToString(PositionGetInteger(POSITION_TIME)), " Current Time: ", TimeCurrent()); // Check if the number of BARS is greater or equal than 5.          if(nPosition_OpeningTime_Difference >= 5)                {                Print("The current position will be closed! ", nPosition_OpeningTime_Difference, " Bars have elapsed since the position was opened!"); // your code to close the position                               }          }

Documentation on MQL5: Trade Functions / PositionGetInteger
Documentation on MQL5: Trade Functions / PositionGetInteger
  • www.mql5.com
Trade Functions / PositionGetInteger - Reference on algorithmic/automated trading language for MetaTrader 5
Ronald Kirby
192
Ronald Kirby  
Snelle Moda:

Hi

You use the PositionGetInteger() in the wrong way, read: 

https://www.mql5.com/en/docs/trading/positionselect

https://www.mql5.com/en/docs/trading/positiongetinteger

https://www.mql5.com/en/docs/constants/tradingconstants/positionproperties#enum_position_property_integer



Use this example to check the number of bars and the opening time of your position:

hi again  Mate thankyou for helping    but i am not improving   , i am wondering now if mt MT4 is not right for making these function calls   ?
Snelle Moda
633
Snelle Moda  

You are right MT4 can't use the positionselect() function because position management is different compare to MT5.

MT4 uses the OrderSelect() function because you can have multiple different positions for 1 symbol while in MT5, you can have only 1 position for each symbol. 

This method above is only useful for MT5.


You first need to learn to select an open position and then you can use the rest of the code.

Read this for MT4.

https://docs.mql4.com/trading/orderselect

https://docs.mql4.com/trading/ordersymbol

https://docs.mql4.com/trading/ordermagicnumber

https://docs.mql4.com/trading/ordertype

OrderSelect - MQL4 Documentation
  • docs.mql4.com
OrderSelect - MQL4 Documentation
Ronald Kirby
192
Ronald Kirby  
Snelle Moda:

Hi

Use the Bars() function to count the number of BARS between the time you opened the position and the current time. For example:


thankyou
Mohamad Zulhairi Baba
8406
Mohamad Zulhairi Baba  
Use open order time.
int MinutesToClose = 5; // 5 minute or 5 bar on M1 chart
datetime OpenTime = 0;

for(int order = 0; order <= OrdersTotal() - 1; order++)
{
   bool select = OrderSelect(order,SELECT_BY_POS);
   if(OrderType() == OP_SELL && select) OpenTime = OrderOpenTime();
}
  
bool CloseTime = TimeCurrent() > OpenTime + (MinutesToClose*60);

if(CloseTime)
{  
  for(int order = 0; order <= OrdersTotal() - 1; order++)
  {
     bool select = OrderSelect(order,SELECT_BY_POS);
     if(OrderType() == OP_SELL && select);
    
     bool Closed = OrderClose(gBuyTicket,OrderLots(),Bid,Slippage,clrRed);
     if(Closed) order--;  
   }
}
whroeder1
16572
whroeder1  
This fails if there are any missing bars, such as over the weekend, or if there were no ticks during a bar "Free-of-Holes" Charts - MQL4 Articles)
bool CloseTime = TimeCurrent() > OpenTime + (MinutesToClose*60);
The OP question was not "after 5 minutes," it was after "5 bars."
bool isCloseTime = iBarShift(NULL,0, OpenTime) ) >= BarsToClose;
To add comments, please log in or register