Why buy position always fail while testing

 

I am using MT5, demo account.

I have a EA that will do PositionOpen for buy and sell and I found that all my buy position failed and get requote message. Here is my code for buy and sell position:

    #include <Trade\Trade.mqh>
    CTrade g_Trader;
    ResetLastError();
    if (!g_Trader.PositionOpen(Symbol(), ORDER_TYPE_SELL, g_Lots, last, sl, tp))
    {
        string str;
        MqlTradeRequest request;
        MqlTradeResult result;
        g_Trader.Result(result);
        g_Trader.Request(request);
        g_Logger.PrintError(StringFormat("%s:%d Sell PositionOpen error %s error_code %d", __FUNCTION__, __LINE__, g_Trader.FormatRequestResult(str, request, result), GetLastError()));
    }
    else
    {
        g_Logger.PrintInfo(StringFormat("%s:%d Success Sell Request", __FUNCTION__, __LINE__));
    }
    ResetLastError();
    if (!g_Trader.PositionOpen(Symbol(), ORDER_TYPE_BUY, g_Lots, last, sl, tp))
    {
        string str;
        MqlTradeRequest request;
        MqlTradeResult result;
        g_Trader.Result(result);
        g_Trader.Request(request);
        g_Logger.PrintError(StringFormat("%s:%d Buy PositionOpen error %s error_code %d", __FUNCTION__, __LINE__, g_Trader.FormatRequestResult(str, request, result), GetLastError()));
    }
    else
    {
        g_Logger.PrintInfo(StringFormat("%s:%d Success Buy Request", __FUNCTION__, __LINE__));
    }

Here is the log about the error:

20181018_000000 INFO OnInit:93 IN
20181018_000000 INFO OnInit:94 Point: 0.000010 Digits: 5
20181018_005836 INFO GetLastHigh:126 last high is: 1.617790
20181018_005836 INFO MakeSellRequest:225 lots: 0.010000 price: 1.617410 sl: 1.618410 tp: 1.617220 bid: 1.617410 ask: 1.617960
20181018_005836 INFO MakeSellRequest:244 Success Sell Request
20181018_032017 INFO GetLastHigh:126 last high is: 1.617240
20181018_032017 INFO MakeSellRequest:225 lots: 0.010000 price: 1.616910 sl: 1.617910 tp: 1.616750 bid: 1.616910 ask: 1.617110
20181018_032017 INFO MakeSellRequest:244 Success Sell Request
20181018_042638 INFO GetLastHigh:126 last high is: 1.615340
20181018_042638 INFO MakeSellRequest:225 lots: 0.010000 price: 1.614800 sl: 1.615800 tp: 1.614530 bid: 1.614800 ask: 1.615020
20181018_042639 INFO MakeSellRequest:244 Success Sell Request
20181018_050041 INFO GetLastLow:140 last low is: 1.614510

20181018_050041 INFO MakeBuyRequest:181 lots: 0.010000 price: 1.614860 sl: 1.613860 tp: 1.615360 bid: 1.614860 ask: 1.615060
20181018_050041 ERROR MakeBuyRequest:196 Buy PositionOpen error requote (1.61486/1.61506) error_code 4756
20181018_065912 INFO GetLastLow:140 last low is: 1.611910
20181018_065912 INFO MakeBuyRequest:181 lots: 0.010000 price: 1.612140 sl: 1.611140 tp: 1.612640 bid: 1.612140 ask: 1.612360
20181018_065912 ERROR MakeBuyRequest:196 Buy PositionOpen error requote (1.61214/1.61236) error_code 4756

From the log you can see that sell position is okay, anyone knows the reason?

More logs from MT5 and my EA's log:

MK      0       11:27:01.122    scalpingBasic_EA (EURAUD,M2)    2018.10.18 06:59:12   MakeBuyRequest:181 lots: 0.010000 price: 1.612140 sl: 1.611140 tp: 1.612640 bid: 1.612140 ask: 1.612360
QP      0       11:27:01.122    Trade   2018.10.18 06:59:12   requote 1.61214 / 1.61236 / 1.61214 (instant buy 0.01 EURAUD at 1.61214 sl: 1.61114 tp: 1.61264)
CL      0       11:27:01.122    Trades  2018.10.18 06:59:12   requote 1.61214 / 1.61236 (instant buy 0.01 EURAUD at 1.61214 sl: 1.61114 tp: 1.61264)
JQ      0       11:27:01.122    scalpingBasic_EA (EURAUD,M2)    2018.10.18 06:59:12   CTrade::OrderSend: instant buy 0.01 EURAUD at 1.61214 sl: 1.61114 tp: 1.61264 [requote (1.61214/1.61236)]
ND      0       11:27:01.122    scalpingBasic_EA (EURAUD,M2)    2018.10.18 06:59:12   MakeBuyRequest:196 Buy PositionOpen error requote (1.61214/1.61236) error_code 4756
 
fxea729:

I am using MT5, demo account.

I have a EA that will do PositionOpen for buy and sell and I found that all my buy position failed and get requote message. Here is my code for buy and sell position:

Here is the log about the error:

From the log you can see that sell position is okay, anyone knows the reason?

What is the value of your "last" variable. It's incorrect, a BUY position is opened at ask price, not at bid :

20181018_050041 INFO MakeBuyRequest:181 lots: 0.010000 price: 1.614860 sl: 1.613860 tp: 1.615360 bid: 1.614860 ask: 1.615060
 

Thank you so much, it seems fixed the problem. I used the last price as the price for buy order and sell order like this:

double GetLastPrice()
{
    MqlRates      current_rates[1];

    ResetLastError();
    if(CopyRates(Symbol(), Period(), 0, 1, current_rates) != 1)
    {
        g_Logger.PrintInfo(StringFormat("%s:%d CopyRates copy error, Code = %d", __FUNCTION__, __LINE__, GetLastError()));
        return 0;
    }

    return current_rates[0].close;
}

And now I do this and the problem is fixed. Thank you so much,

    double price = SymbolInfoDouble(Symbol(), SYMBOL_ASK);

    if (!g_Trader.PositionOpen(Symbol(), ORDER_TYPE_BUY, g_Lots, price, sl, tp))
    {
        string str;
        MqlTradeRequest request;
        MqlTradeResult result;
        g_Trader.Result(result);
        g_Trader.Request(request);
        g_Logger.PrintError(StringFormat("%s:%d Buy PositionOpen error %s error_code %d", __FUNCTION__, __LINE__, g_Trader.FormatRequestResult(str, request, result), GetLastError()));
    }
    else
    {
        g_Logger.PrintInfo(StringFormat("%s:%d Success Buy Request", __FUNCTION__, __LINE__));
    }
 
fxea729:

Thank you so much, it seems fixed the problem. I used the last price as the price for buy order and sell order like this:

And now I do this and the problem is fixed. Thank you so much,

You will need to do the same for the SELL which open at Bid. The last price is of no use on Forex and can be different of the bid price on other markets.
Reason: