how to call ctrade??

 

'CTrade' - import is not defined USDCHF_break_x_bar_Low.mq5 66 32

 my code:

 

 #include <Trade\Trade.mqh>

int check_Exit()

{

  position_bar_name = define_Pos_bar_name();

  if (position_bar_name >= 2)  CTrade::PositionClose(symbol,deviation);

  

  return(888);

}  

how to solve?

thanks 


 
You should know how to insert code properly, I think.
MQL5.community - User Memo
  • 2010.02.25
  • MetaQuotes Software Corp.
  • www.mql5.com
You have just registered and most likely you have questions such as, "How do I insert a picture to my a message?" "How do I format my MQL5 source code?" "Where are my personal messages kept?" You may have many other questions. In this article, we have prepared some hands-on tips that will help you get accustomed in MQL5.community and take full advantage of its available features.
 

'CTrade' - import is not defined

   


#include <Trade\Trade.mqh>

int check_Exit()

{

  position_bar_name = define_Pos_bar_name();

  if (position_bar_name >= 2)  CTrade::PositionClose(symbol,deviation);

  

  return(888);

}  

 

how to solve?

thanks 

 
kelly:

'CTrade' - import is not defined

   


 

how to solve?

thanks 


Hi Kelly,

You need to create an object of the CTrade class before you can use it, see an example below

#include <Trade\Trade.mqh>
// create an object of the class
CTrade trader;
/*
  You can now use this object to access all the functions of the CTrade class
  Example:
  ========
  trader.PositionOpen(......);
  trader.PositionClose(......);
*/ 

int check_Exit()

{

  position_bar_name = define_Pos_bar_name();

  if (position_bar_name >= 2)  trader.PositionClose(symbol,deviation);

  

  return(888);

}  

 

 
class CTrade
  {
protected:
   MqlTradeRequest   m_request;         // request data
   MqlTradeResult    m_result;          // result data
   ulong             m_magic;           // expert magic number
   ulong             m_deviation;       // deviation default
public:
                     CTrade();
   //--- methods of access to protected data

   bool              PositionClose(const string symbol,ulong deviation=ULONG_MAX);
}
//-----------------------------------------------------------------------------
bool CTrade::PositionClose(const string symbol,ulong deviation)
  {
   double price;
//--- checking buy or sell
   if(PositionSelect(symbol))
     {
      if((ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
        {
         //--- prepare query for close BUY position
         m_request.type =ORDER_TYPE_SELL;
         m_request.price=SymbolInfoDouble(symbol,SYMBOL_BID);
        }
      else
        {
         //--- prepare query for close SELL position
         m_request.type =ORDER_TYPE_BUY;
         m_request.price=SymbolInfoDouble(symbol,SYMBOL_ASK);
        }
     }
//--- setting request
   m_request.action      =TRADE_ACTION_DEAL;
   m_request.symbol      =symbol;
   m_request.volume      =PositionGetDouble(POSITION_VOLUME);
   m_request.sl          =0.0;
   m_request.tp          =0.0;
   m_request.deviation   =(deviation==ULONG_MAX) ? m_deviation : deviation;
   m_request.type_filling=ORDER_FILLING_AON;
//---
   return(OrderSend(m_request,m_result));
  }

CTrade trader;
/*
  You can now use this object to access all the functions of the CTrade class
  Example:
  ========
  trader.PositionOpen(......);
  trader.PositionClose(......);
*/ 

int check_Exit()

{
  position_bar_name = define_Pos_bar_name();
  if (position_bar_name >= 2)  trader.PositionClose(symbol,deviation);
  return(888);
}  

there is the full code, without using #include

thanks 

 I need to create an object of the CTrade class by using :"CTrade trader; "

 use this objectTrader) to access all the functions in the CTrade class, i.e.  PositionOpen(......), PositionClose(......)

class = CTrade

 trader = Object

function() = PositionOpen(......), PositionClose(......)

object =  Properties (屬性) + Function() 行為

properties (屬性)  = student_nub.mark1, student_nub.mark2, student_nub.mark3 = variable / data structure

 

http://www.cplusplus.com/doc/tutorial/classes/ 

:: is to define a member of a class from outside the class definition itself. 

member can be type or function()工作 

Classes (I) - C++ Tutorials
  • www.cplusplus.com
Classes are an expanded concept of data structures: like data structures, they can contain data members, but they can also contain functions as members. An object is an instantiation of a class. In terms of variables, a class would be the type, and an object would be the variable. Classes are defined using either keyword or keyword , with...
 

i have encount a compile error :

'trader' - declaration without type 

at line : trader.PositionOpen(......);     

class CTrade
  {
protected:
   MqlTradeRequest   m_request;         // request data
   MqlTradeResult    m_result;          // result data
   ulong             m_magic;           // expert magic number
   ulong             m_deviation;       // deviation default
public:
                     CTrade();
   //--- methods of access to protected data

   bool              PositionClose(const string symbol,ulong deviation=ULONG_MAX);
}
//-----------------------------------------------------------------------------
bool CTrade::PositionClose(const string symbol,ulong deviation)
  {
   double price;
//--- checking buy or sell
   if(PositionSelect(symbol))
     {
      if((ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
        {
         //--- prepare query for close BUY position
         m_request.type =ORDER_TYPE_SELL;
         m_request.price=SymbolInfoDouble(symbol,SYMBOL_BID);
        }
      else
        {
         //--- prepare query for close SELL position
         m_request.type =ORDER_TYPE_BUY;
         m_request.price=SymbolInfoDouble(symbol,SYMBOL_ASK);
        }
     }
//--- setting request
   m_request.action      =TRADE_ACTION_DEAL;
   m_request.symbol      =symbol;
   m_request.volume      =PositionGetDouble(POSITION_VOLUME);
   m_request.sl          =0.0;
   m_request.tp          =0.0;
   m_request.deviation   =(deviation==ULONG_MAX) ? m_deviation : deviation;
   m_request.type_filling=ORDER_FILLING_AON;
//---
   return(OrderSend(m_request,m_result));
  }

  /*You can now use this object to access all the functions of the CTrade class
  Example : */
  
CTrade trader;
trader.PositionOpen(......);   // copile error here : 'trader' - declaration without type
trader.PositionClose(......);  // copile error here : 'trader' - declaration without type

int check_Exit()

{
  position_bar_name = define_Pos_bar_name();
  if (position_bar_name >= 2)  trader.PositionClose(symbol,deviation);
  return(888);
}  
 

how to solve?

trader.PositionOpen(......);   

 by the way, what the .... should be ?

Thankyou very much 

 
kelly:

i have encount a compile error :

'trader' - declaration without type 

at line : trader.PositionOpen(......);     

how to solve?

 by the way, what the .... should be ?

Thankyou very much 

oh , i know what i misunderstood now

 

trader.PositionOpen(......);   // copile error here : 'trader' - declaration without type
trader.PositionClose(......);  // copile error here : 'trader' - declaration without type

 i discover that above 2 line is useless , just delete it

 

so the entire code should be

class CTrade
  {
protected:
   MqlTradeRequest   m_request;         // request data
   MqlTradeResult    m_result;          // result data
   ulong             m_magic;           // expert magic number
   ulong             m_deviation;       // deviation default
public:
                     CTrade();

   bool              PositionClose(const string symbol,ulong deviation=ULONG_MAX);
}
//-----------------------------------------------------------------------------
bool CTrade::PositionClose(const string symbol,ulong deviation)
  {
   double price;
//--- checking buy or sell
   if(PositionSelect(symbol))
     {
      if((ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
        {
         //--- prepare query for close BUY position
         m_request.type =ORDER_TYPE_SELL;
         m_request.price=SymbolInfoDouble(symbol,SYMBOL_BID);
        }
      else
        {
         //--- prepare query for close SELL position
         m_request.type =ORDER_TYPE_BUY;
         m_request.price=SymbolInfoDouble(symbol,SYMBOL_ASK);
        }
     }
//--- setting request
   m_request.action      =TRADE_ACTION_DEAL;
   m_request.symbol      =symbol;
   m_request.volume      =PositionGetDouble(POSITION_VOLUME);
   m_request.sl          =0.0;
   m_request.tp          =0.0;
   m_request.deviation   =(deviation==ULONG_MAX) ? m_deviation : deviation;
   m_request.type_filling=ORDER_FILLING_AON;
//---
   return(OrderSend(m_request,m_result));
  }

  
CTrade trader;   // You can now use this object(trader) to access all the functions() defined inside the CTrade class


int check_Exit()

{
  position_bar_name = define_Pos_bar_name();
  if (position_bar_name >= 2)  trader.PositionClose(symbol,deviation);
  return(888);
}  

 

Reason: