- 2010.02.25
- MetaQuotes Software Corp.
- www.mql5.com
'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()工作
- www.cplusplus.com
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
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); }
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
'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