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 } }
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
ty so much i will have a play and see how i go
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
}
}
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
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 } }
- www.mql5.com
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
Use this example to check the number of bars and the opening time of your position:
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
Hi
Use the Bars() function to count the number of BARS between the time you opened the position and the current time. For example:
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--;
}
}
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; |
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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