Download MetaTrader 5

How to use OrderSend() function to open a order in EA,please give a example,thank you.

To add comments, please log in or register
Do you need OpenCL? MQL5 can provide it!
Xianba Xia
2469
Xianba Xia 2013.10.27 12:13 

Hi,all.

Now,I have a big problem,what is a big big prolem.

the function of OrderSend() does't useful? I don't know how to use, If someone know ,can write a example,

Thank you. 

Anton Nel
36972
Anton Nel 2013.10.27 12:20  
MT4 or MT5?
Alain Verleyen
Moderator
30721
Alain Verleyen 2013.10.27 13:49  
scuxia:

Hi,all.

Now,I have a big problem,what is a big big prolem.

the function of OrderSend() does't useful? I don't know how to use, If someone know ,can write a example,

Thank you. 

Have you tried the documentation ?
amin_mohammadi
88
amin_mohammadi 2013.11.26 20:31  

Hello,

I use the code below to make order but I get error number 10016 (Invalid SL):


bool OrderManagement::OpenPosition(string pSymbol, ENUM_ORDER_TYPE pType, double pVolume, double pStop = 200, double pProfit = 0, string pComment = NULL)
{
ZeroMemory(request);
        request.action = TRADE_ACTION_DEAL;
        request.symbol = pSymbol;
        request.type = pType;
        request.sl = pStop;
        request.tp = pProfit;
        request.comment = pComment;
        
        // Calculate lot size
        double positionLots = 0;
        long positionType = WRONG_VALUE;
        
        if(PositionSelect(pSymbol) == true)
        {
                positionLots = PositionGetDouble(POSITION_VOLUME);
                positionType = PositionGetInteger(POSITION_TYPE);
        }
                
        if((pType == ORDER_TYPE_BUY && positionType == POSITION_TYPE_SELL) || (pType == ORDER_TYPE_SELL && positionType == POSITION_TYPE_BUY)) 
        {
                request.volume = pVolume + positionLots;
        }
        else request.volume = pVolume;
        
        // Order loop
        int retryCount = 0;
        int checkCode = 0;
        
        do 
        {
                if(pType == ORDER_TYPE_BUY) request.price = SymbolInfoDouble(pSymbol,SYMBOL_ASK);
                else if(pType == ORDER_TYPE_SELL) request.price = SymbolInfoDouble(pSymbol,SYMBOL_BID);
                
                OrderSend(request,result);
                
                checkCode = CheckReturnCode(result.retcode);
                
                if(checkCode == CHECK_RETCODE_OK) break;
                else if(checkCode == CHECK_RETCODE_ERROR)
                {
                        string errDesc = TradeServerReturnCodeDescription(result.retcode);
                        Alert("Open market order: Error ",result.retcode," - ",errDesc);
                        break;
                }
                else
                {
                        Print("Server error detected, retrying...");
                        Sleep(RETRY_DELAY);
                        retryCount++;
                }
        }
        while(retryCount < MAX_RETRIES);
        
        if(retryCount >= MAX_RETRIES)
        {
                string errDesc = TradeServerReturnCodeDescription(result.retcode);
                Alert("Max retries exceeded: Error ",result.retcode," - ",errDesc);
        }
        
        string orderType = CheckOrderType(pType);
        
        string errDesc = TradeServerReturnCodeDescription(result.retcode);
        Print("Open ",orderType," order #",result.deal,": ",result.retcode," - ",errDesc,", Volume: ",result.volume,", Price: ",result.price,", Bid: ",result.bid,", Ask: ",result.ask);
        
        if(checkCode == CHECK_RETCODE_OK) 
        {
                Comment(orderType," position opened at ",result.price," on ",pSymbol);
                return(true);
        }
        else return(false);
}
Alain Verleyen
Moderator
30721
Alain Verleyen 2013.11.26 21:45  
SL in MqlTradeRequest is a price. Why do you expect 200.0 is a valid price ? Are you trading gold, only ?
amin_mohammadi
88
amin_mohammadi 2013.11.27 06:24  
angevoyageur:
SL in MqlTradeRequest is a price. Why do you expect 200.0 is a valid price ? Are you trading gold, only ?

I know, I wanted to leave SL in 0 but after this error I thought it shouldn't be 0, so I changed it to an unreachable value.

anyhow, when it was 0 the same error was thrown.

Alain Verleyen
Moderator
30721
Alain Verleyen 2013.11.27 08:07  
amin_mohammadi:

I know, I wanted to leave SL in 0 but after this error I thought it shouldn't be 0, so I changed it to an unreachable value.

anyhow, when it was 0 the same error was thrown.

Error 10016 isn't invalid SL but invalid stops :

10016

TRADE_RETCODE_INVALID_STOPS

Invalid stops in the request

So you have to check your TP also. What are the values of SL/TP when you get this error ?

amin_mohammadi
88
amin_mohammadi 2013.11.27 08:12  
angevoyageur:

Error 10016 isn't invalid SL but invalid stops :

10016

TRADE_RETCODE_INVALID_STOPS

Invalid stops in the request

So you have to check your TP also. What are the values of SL/TP when you get this error ?

TP:1.7777

SL: 0.0

Price: 1.7774

BUY - GBPAUD.m M1

Simon Gniadkowski
Moderator
17998
Simon Gniadkowski 2013.11.27 10:25  
amin_mohammadi:

TP:1.7777

SL: 0.0

Price: 1.7774

BUY - GBPAUD.m M1

What is your SYMBOL_TRADE_STOPS_LEVEL  ?
enbo lu
Moderator
1943
enbo lu 2013.11.27 10:28  

you trade GBPAUD  ,but set the stoploss as 200? or you mean 200pips? 

Maxim Khrolenko
7097
Maxim Khrolenko 2013.11.27 11:22  

Article Trade Operations in MQL5 - It's Easy, read it carefully and several times. After I red this I have no problems with trade operations in MQL5.

12
To add comments, please log in or register