# how to close after 5 bars

192

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

633

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

}
}```
192

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

192

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

}
}

192

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 ?
633

Hi

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

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
• www.mql5.com
192

Snelle Moda:

Hi

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

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   ?
633

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.

OrderSelect - MQL4 Documentation
• docs.mql4.com
OrderSelect - MQL4 Documentation
192

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
8406

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);