CTrade: Set stop loss e take profit

To add comments, please log in or register
Fabio Fredduzzi
51
Fabio Fredduzzi  

Here you are my Main code:

//+------------------------------------------------------------------+
//|                                            CloseAllPositions.mq5 |
//|                        Copyright 2016, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"

#include <Fabio/My_Trade_Class.mqh>    // Include la mia classe di trading
My_Trade_Class My_Trade;

//+------------------------------------------------------------------+
//|              OnInit                                              |
//+------------------------------------------------------------------+
void OnInit()
  {

// Compra
   My_Trade.Compra();
   ExpertRemove();

  }
//+------------------------------------------------------------------+
void OnTick()
  {

  }// chiusura VOID OnTick
//+------------------------------------------------------------------+

And the code copied from this article: https://www.mql5.com/en/articles/481

//+------------------------------------------------------------------+
//|                                              OOP_CLibArray_1.mqh |
//|                        Copyright 2011, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2011, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"


#include <Trade\Trade.mqh>
CTrade trade;

//+==================================================================+
//=                                                                  =
//=                  Classe Trade                                    =
//=                                                                  =
//+==================================================================+

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
class My_Trade_Class
  {
   //property
private:
   


public:
   //+------------------------------------------------------------------+
   //|    Compra (Buy in Italian :-) )                                                  |
   //+------------------------------------------------------------------+
   void              Compra()
     {
      //--- set MagicNumber for your orders identification
      int MagicNumber=123456;
      trade.SetExpertMagicNumber(MagicNumber);
      //--- set available slippage in points when buying/selling
      int deviation=10;
      trade.SetDeviationInPoints(deviation);
      //--- order filling mode, the mode allowed by the server should be used
      trade.SetTypeFilling(ORDER_FILLING_FOK);
      //--- logging mode: it would be better not to declare this method at all, the class will set the best mode on its own
      trade.LogLevel(1);
      //--- what function is to be used for trading: true - OrderSendAsync(), false - OrderSend()
      trade.SetAsyncMode(false);
      double volume=0.1;         // specify a trade operation volume
      string symbol="GBPUSD";
      int    digits=(int)SymbolInfoInteger(symbol,SYMBOL_DIGITS); // number of decimal places
      double point=SymbolInfoDouble(symbol,SYMBOL_POINT);         // point
      double bid=SymbolInfoDouble(symbol,SYMBOL_BID);             // current price for closing LONG
      double SL=bid-1000*point;                                   // unnormalized SL value
      SL=NormalizeDouble(SL,digits);                              // normalizing Stop Loss
      double TP=bid+1000*point;                                   // unnormalized TP value
      TP=NormalizeDouble(TP,digits);                              // normalizing Take Profit
      //--- receive the current open price for LONG positions
      double open_price=SymbolInfoDouble(symbol,SYMBOL_ASK);
      string comment=StringFormat("Buy %s %G lots at %s, SL=%s TP=%s",
                                  symbol,volume,
                                  DoubleToString(open_price,digits),
                                  DoubleToString(SL,digits),
                                  DoubleToString(TP,digits));


      //double Ask=SymbolInfoDouble(_Symbol, SYMBOL_ASK);

      if(!trade.Buy(0.01,"GBPUSD"))
        {
         //--- failure message
         Print("Buy() method failed. Return code=",trade.ResultRetcode(),
               ". Code description: ",trade.ResultRetcodeDescription());
        }
      else
        {
         Print("Buy() method executed successfully. Return code=",trade.ResultRetcode(),
               " (",trade.ResultRetcodeDescription(),")");
        }
     }


  };
//+------------------------------------------------------------------+

It works, but whithout TP and SL. Where I can put there?

Also this piece of code works (of course whithout SL and TP), but I will not able to insert the PRICE of SL and TP

  double Ask=NormalizeDouble(SymbolInfoDouble(simbolo_i,SYMBOL_ASK),_Digits);
     trade.Buy(volume_i,simbolo_i,Ask,0,0,NULL);

I know the TP and SL must be a PRICE and not a number, but I was unable to do it

Also I read manual (https://www.mql5.com/en/docs/standardlibrary/tradeclasses/ctrade/ctradebuy) but nothing. Yes, I think to be silly :-(

Previously I used MQL_Easy, but after MT5 update it isn't works.

Vladimir Karputov
Moderator
161381
Vladimir Karputov  

Reference: Buy

Opens a long position with specified parameters.

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
   )

You need to add these three parameters: price,  sl and tp

Vladimir Karputov
Moderator
161381
Vladimir Karputov  
I would do this: Example - 
Fabio Fredduzzi
51
Fabio Fredduzzi  
Vladimir Karputov:
I would do this: Example - 

Thanks Vladimir, It works and I have a starting point

To add comments, please log in or register