I am trying to open a position when there is no existing position for the symbol. If there is an existing position then i'll close that position before opening a new position.
I came up with the below code snippet but it's putting 100's of trades for every few seconds when my chart itself was running on 30 mins frequency.
Would need your help to properly open and close a position.
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
Hi All
I am trying to open a position when there is no existing position for the symbol. If there is an existing position then i'll close that position before opening a new position.
I came up with the below code snippet but it's putting 100's of trades for every few seconds when my chart itself was running on 30 mins frequency.
Would need your help to properly open and close a position.
if (PositionSelect(_Symbol))
{
pType = (ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);
posType = EnumToString(pType);
long pos_id = PositionGetInteger(POSITION_IDENTIFIER);
if (posType == "POSITION_TYPE_BUY")
{
if ((K_cBar > 80 && D_cBar > 80))
{
trade.PositionClose(pos_id);
/*for(int i = PositionsTotal() - 1; i >= 0; i--)
{
ticket = PositionGetTicket (i);
trade.PositionClose(ticket);
}
*/
}
}
else if (posType == "POSITION_TYPE_SELL")
{
if ((K_cBar < 20 && D_cBar < 20))
{
trade.PositionClose(pos_id);
/*for(int i = PositionsTotal() - 1; i >= 0; i--)
{
ticket = PositionGetTicket (i);
trade.PositionClose(ticket);
}*/
}
}
}
else if (!PositionSelect(_Symbol))
{
if (K_cBar > D_cBar && K_pBar < D_pBar && ADX_Val > 30)
{
//trade.Buy(posEnterLot,_Symbol,closePrice,0.0,0.0,"");
trade.PositionOpen(_Symbol,ORDER_TYPE_BUY,posEnterLot,closePrice,0.0,0.0,"");
//Comment(barTime);
}
else if (K_cBar < D_cBar && K_pBar > D_pBar && ADX_Val > 30)
{
//trade.Sell(posEnterLot,_Symbol,closePrice,0.0,0.0,"");
trade.PositionOpen(_Symbol,ORDER_TYPE_SELL,posEnterLot,closePrice,0.0,0.0,"");
//Comment(barTime);
}
}
}