simple question about OrderSend

 

Hello,

I have a very simple question. I want to buy EURUSD and close by Takeprofit or Stoploss.

My code open the position but never close. Could you help me a little?

 


 MqlTick last_tick;

 SymbolInfoTick("EURUSD",last_tick);

   

 Ask=last_tick.ask;

 Bid=last_tick.bid;

         

         MqlTradeRequest request;

         MqlTradeResult result;

         ZeroMemory(request);

         ZeroMemory(result);   

 

         request.symbol="EURUSD";

         request.action=TRADE_ACTION_DEAL;

         request.type = ORDER_TYPE_BUY;         

         request.volume=0.1;                          // volume in 0.1 lots

         request.price=NormalizeDouble(Ask,_Digits);                                

         request.sl=NormalizeDouble(Ask-SL,_Digits);

         request.tp=NormalizeDouble(Ask+TP,_Digits);   

         request.deviation=10;                       // Deviation from current price

         request.type_filling=ORDER_FILLING_AON;                              


         ResetLastError();

         OrderSend(request,result); 


 

 

What is missing or wrong?

 Many thanks!

 

Check for errors by adding a line of code after OrderSend:

Print("retcode=",result.retcode," error=", GetLastError()); 
 
iwanttoberich:

Hello,

I have a very simple question. I want to buy EURUSD and close by Takeprofit or Stoploss.

My code open the position but never close. Could you help me a little?

 

 MqlTick last_tick;

 SymbolInfoTick("EURUSD",last_tick);

   

 Ask=last_tick.ask;

 Bid=last_tick.bid;

         

         MqlTradeRequest request;

         MqlTradeResult result;

         ZeroMemory(request);

         ZeroMemory(result);   

 

         request.symbol="EURUSD";

         request.action=TRADE_ACTION_DEAL;

         request.type = ORDER_TYPE_BUY;         

         request.volume=0.1;                          // volume in 0.1 lots

         request.price=NormalizeDouble(Ask,_Digits);                                

         request.sl=NormalizeDouble(Ask-SL,_Digits);

         request.tp=NormalizeDouble(Ask+TP,_Digits);   

         request.deviation=10;                       // Deviation from current price

         request.type_filling=ORDER_FILLING_AON;                              

         ResetLastError();

         OrderSend(request,result); 

 

 

What is missing or wrong?

 Many thanks!

 

 

Hi, iwanttoberich

 

Put a few Alert function calls in your code. These allow you to watch closely what your EA  is *really* doing.

 

[The messages go into the log, and are also visible in the MetaTester tool.]

 

It may be that the TP/SL values are not being applied correctly to the 'request' variable (scaling? Limits?), or perhaps another error is creeping in?

 

Avoitenko's idea to use the Print function is also good advice - particularly while developing. Once your code is sound, you can remove/dummy them out.

 

Something is not working how you are expecting it work - so look in detail, the answer is always there.

 

Good luck!

 

UKC. 

 

Hi everybody,

The resultcode is 10009 (which corresponds to "Request is completed") 

I will add some more info:

I developed the simplest EA using MQL5:

- Buy EURUSD with SL and TP

Run the backtest and see this Orders section:

 

As you can see the test runs until the end of the test but i don't see the SL and TP.
SL and TP columns are blank/empty (Why? this columns are filled when SL and TP are valued before sending the trade)

 

No ideas?. I think is a very simple code. It buys EURUSD with a SL and TP.

In MQL4 is very easy, but a have no luck in MQL5. 

 
Your problem long been known - Market Execution!
First step - open position without SL and TP
Second step - setup SL and TP
Documentation on MQL5: Standard Constants, Enumerations and Structures / Environment State / Symbol Properties
  • www.mql5.com
Standard Constants, Enumerations and Structures / Environment State / Symbol Properties - Documentation on MQL5
 

hi i wrote this code but it,s not working,i want to send an sell order 

is anyone to help me

#property copyright "Copyright 2022, MetaQuotes Ltd."

#property link      "https://www.mql5.com"

#property version   "1.00"

#define EXPERT_MAGIC 123456

//+------------------------------------------------------------------+

//| Script program start function                                    |

//+------------------------------------------------------------------+

void OnStart()

  {long ask;

  long Bid;

  long sl;

  long tp;

  MqlTick last_tick;


 SymbolInfoTick("EURUSD",last_tick);

ask=last_tick.ask;


 Bid=last_tick.bid;

  

   MqlTradeRequest request;

   MqlTradeResult  result;

   ZeroMemory(request);


         ZeroMemory(result);   

//--- parameters of request

  request.symbol   ="EURUSD";     // symbol

   request.action   =TRADE_ACTION_SLTP;                     // type of trade operation                    

   request.type     =ORDER_TYPE_BUY;                        // order type

   request.volume   =0.1;                                   // volume of 0.1 lot

     request.price    =NormalizeDouble(ask,_Digits);

    request.sl=NormalizeDouble(ask-sl,_Digits);

    request.tp=NormalizeDouble(ask+tp,_Digits);                              // price for tp

   request.deviation=10;                                     // allowed deviation from the price

  request.type_filling=ORDER_FILLING_IOC;                          // MagicNumber of the order

//--- send the request

 ResetLastError();

   OrderSend(request,result) ;

    

  }

   

Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • 2022.09.20
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
Reason: