How to useTrade.mqh library in mql5?

 

#include <Trade\Trade.mqh>

CTrade trade;


I am new to mql. I have seen the code above in *** but there are parts I do not understand. 

I understand the first line. We are importing the Trade header file from the Trade.mqh library.

What does the second line mean? Are we assigning the CTrade class to a variable named 'trade' ? And

where does the CTrade value come from. Is it from the first line? If so how does the CTrade gets its value from

the first line?

When we include a library in mql how do we get to access its functions and other values?

 
Yule Msee:

#include <Trade\Trade.mqh>

CTrade trade;


I am new to mql. I have seen the code above in this tutorial but there are parts I do not understand. 

I understand the first line. We are importing the Trade header file from the Trade.mqh library.

What does the second line mean? Are we assigning the CTrade class to a variable named 'trade' ? And

where does the CTrade value come from. Is it from the first line? If so how does the CTrade gets its value from

the first line?

When we include a library in mql how do we get to access its functions and other values?

try this   https://www.mql5.com/en/articles/481

Trade Operations in MQL5 - It's Easy
Trade Operations in MQL5 - It's Easy
  • www.mql5.com
Almost all traders come to market to make money but some traders also enjoy the process itself. However, it is not only manual trading that can provide you with an exciting experience. Automated trading systems development can also be quite absorbing. Creating a trading robot can be as interesting as reading a good mystery novel. When...
 

Example:

 //+------------------------------------------------------------------+ 
 //|                                                     Open Buy.mq5 | 
 //|                              Copyright © 2018, Vladimir Karputov | 
 //+------------------------------------------------------------------+ 
 #property  copyright "Copyright © 2018, Vladimir Karputov" 
 #property  version    "1.000" 
 //--- 
 #include  <Trade\Trade.mqh>
CTrade         m_trade;                       // object of CTrade class 
 //+------------------------------------------------------------------+ 
 //| Script program start function                                    | 
 //+------------------------------------------------------------------+ 
 void OnStart ()
  {
 //--- 
   m_trade.Buy( 1.0 ); // open Buy position, volume 1.0 lot 
  }
 //+------------------------------------------------------------------+ 


We connect a trading class:

 #include  <Trade\Trade.mqh>


We declare the object ( m_trade ) of the trade class CTrade

CTrade         m_trade;                       // object of CTrade class 

after that, through the m_trade object , we get access to the public methods of the CTrade trading class (in the example below, we turn to the 1.0 Open BUY Position method)

   m_trade.Buy( 1.0 ); // open Buy position, volume 1.0 lot 
 
double CalcularAssertividade()
{
   int totalDeals = HistóricoDealsTotal();
   int ofertas vencedoras = 0 ;
   
   para( int i = 0 ; i < totalDeals; i++)
   {
      se(HistóricoDealSelect(i))
      {
         se(HistoryDealSymbol() == _Symbol && HistoryDealMagic() == MagicNumber)
         {
            se(HistóricoProfit() > 0 )
            {
               ofertas vencedoras++;
            }
         }
      }
   }

   se(totalDeals == 0 ) retornar 0 , 0 ;

   retornar (duplo(ofertas vencedoras) / totalOfertas) * 100 , 0 ;
}

Good afternoon everyone, I'm having trouble making this section work perfectly and help.

 

I Can some1 please assist me with the corrections...


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

//|                    CoDaC ZigZag EA                              |
//|      Integrates with CoDaC ZigZag Arrows Duplex MT5 Indicator  |
//+------------------------------------------------------------------+
#include <Trade/Trade.mqh>
CTrade trade;

// Indicator handles
int zigzagHandle;

// Buffers
double slowZigzagBuffer[];
double fastZigzagBuffer[];

// Indicator Parameters
input string IndicatorName = "CoDaC ZigZag Arrows Duplex MT5";
input int IndicatorSlowBuffer = 0;
input int IndicatorFastBuffer = 1;

//+------------------------------------------------------------------+
//| Expert initialization function                                  |
//+------------------------------------------------------------------+
int OnInit()
{
    Print("Initializing EA...");
    
    zigzagHandle = iCustom(Symbol(), PERIOD_CURRENT, IndicatorName);
    
    if (zigzagHandle == INVALID_HANDLE)
    {
        Print("Error loading indicator: ", IndicatorName);
        return INIT_FAILED;
    }
    else
    {
        Print("Indicator successfully loaded: ", IndicatorName);
    }
    
    return INIT_SUCCEEDED;
}

//+------------------------------------------------------------------+
//| Expert deinitialization function                                |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
    Print("Deinitializing EA...");
    IndicatorRelease(zigzagHandle);
}

//+------------------------------------------------------------------+
//| Function to close all trades                                     |
//+------------------------------------------------------------------+
void closeAllTrades()
{
    for (int i = PositionsTotal() - 1; i >= 0; i--)
    {
       #include <Trade.mqh>

CTrade trade;

int OnInit()
{
    Print("Initializing EA...");
    return INIT_SUCCEEDED;
}

void OnTick()
{
    for(int i = PositionsTotal() - 1; i >= 0; i--)
    {
        if(PositionSelect(i))  // Corrected from PositionSelectByIndex
        {
            long ticket = PositionGetInteger(POSITION_TICKET);  // Corrected from PositionGetTicket(index)
            Print("Position Ticket: ", ticket);
        }
    }
}
 
Thabo Modiba #:
void closeAllTrades() {     for (int i = PositionsTotal() - 1; i >= 0; i--)     {        #include <Trade.mqh> CTrade trade; int OnInit() {     Print("Initializing EA...");     return INIT_SUCCEEDED; }
void closeAllTrades()
{
    for (int i = PositionsTotal() - 1; i >= 0; i--)
    {
       #include <Trade.mqh>

CTrade trade;

int OnInit()
{
    Print("Initializing EA...");
    return INIT_SUCCEEDED;
}

can't you see the problems here...?

I use PositionSelectByTicket to directly modify the open positions

https://www.mql5.com/en/docs/trading/positionselectbyticket

if(PositionSelectByTicket(  PositionGetTicket(i) )){

//

}

PositionSelect takes a symbol name, not an index

https://www.mql5.com/en/docs/trading/positionselect


Maybe you should study the reference and read articles on building EAs

Documentation on MQL5: Trade Functions / PositionSelectByTicket
Documentation on MQL5: Trade Functions / PositionSelectByTicket
  • www.mql5.com
Selects an open position to work with based on the ticket number specified in the position. If successful, returns true. Returns false if the...