MQL5 Standard Library Ctrade Buy

 

Hello everyone!

I am using Buy method:

https://www.mql5.com/en/docs/standardlibrary/tradeclasses/ctrade/ctradebuy


It's necessary to inform those variables:

bool  Buy(
   double        volume,          // position volume
   const string  symbol=NULL,     // symbol
   double        price=0.0,       // execution price
   double        sl=0.0,          // stop loss price
   double        tp=0.0,          // take profit price
   const string  comment=""       // comment
   )


So, I am using this code:

//+------------------------------------------------------------------+
//|                                                        Lucas.mq5 |
//|                                                    Dadalt Morero |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Dadalt Morero"
#property link      "https://www.mql5.com"
#property version   "1.00"

#include <Trade\Trade.mqh>
#include <Trade\SymbolInfo.mqh>
string tempo="0";
CTrade trade;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   if (tempo=="0")
      {
      trade.Buy(1,"WDOM21",5436,5420,5500,"BUY TEST");
      tempo="1";
      }
  }
//+------------------------------------------------------------------+


When I execute this code the buy operation occurs but not the StopLoss operation.



What should I do?

Documentation on MQL5: Standard Library / Trade Classes / CTrade / Buy
Documentation on MQL5: Standard Library / Trade Classes / CTrade / Buy
  • www.mql5.com
Buy(double,const string,double,double,double,const string) - CTrade - Trade Classes - Standard Library - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
Files:
Capture.PNG  29 kb
 

If Stop Loss did not work - this is not a problem of the Standard Library - this is a problem of your broker.

You should check the terminal log (there may be a description of the error).

Reason: