problem including mqh file

 

I am having a problem with including a mqh file.

the mqh file itself has been included and has no error. The errors come when i try to call upon classes from the mqh file. This code is from the mt4 book by Andrew R. Young, At page 148 the end of a simple expert advisor revised for those who have used the book to learn mql4. 

#include <DevlinTrade>  // this works fine. I have no errors relating the the include file code itself. 
CTrade Trade;
CCount Count;

void OnInit()
 {
  // Set magic number
  DevlinTrade.SetMagicNumber(MagicNumber);  // none of this works.
  DevlinTrade.SetSlippage(Slippage);                 // There are many more lines of code after this that also do NOT work and all errors are from lines of code called from the mqh file 
  return(INIT_SUCCEEDED);
 }
Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator
Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator
  • www.mql5.com
MetaQuotes Programming Language 5 (MQL5), included in MetaTrader 5 Client Terminal, has many new possibilities and higher performance, compared to MQL4. This article will help you to get acquainted with this new programming language. The simple examples of how to write an Expert Advisor and Custom Indicator are presented in this article. We will also consider some details of MQL5 language, that are necessary to understand these examples.
 
oDEVLINo1o:

I am having a problem with including a mqh file.

the mqh file itself has been included and has no error. The errors come when i try to call upon classes from the mqh file. This code is from the mt4 book by Andrew R. Young, At page 148 the end of a simple expert advisor revised for those who have used the book to learn mql4. 

you say you have a problem including the file but your comment in the code says the include works fine.... does it include when compiled or not?

In your code you reference object  DevlinTrade  but where have you created it?

Without more code or proper details of the errors it's a guessing game..  

 
oDEVLINo1o:

I am having a problem with including a mqh file.

the mqh file itself has been included and has no error. The errors come when i try to call upon classes from the mqh file. This code is from the mt4 book by Andrew R. Young, At page 148 the end of a simple expert advisor revised for those who have used the book to learn mql4. 

Have you tried adding the .mqh extension in the file name i.e. 

#include <DevlinTrade.mqh>

as opposed to

#include <DevlinTrade>

Also, have you included the relevant global variables to the MQH file as well? I have an external MQH document which contains all the global variables which every source file (EA's, and other MQH files) points to. It may not be getting to the global variables to it can't do the function you're asking it to.

 
TheHonestPrussian #:

Have you tried adding the .mqh extension in the file name i.e. 

as opposed to

Also, have you included the relevant global variables to the MQH file as well? I have an external MQH document which contains all the global variables which every source file (EA's, and other MQH files) points to. It may not be getting to the global variables to it can't do the function you're asking it to.

Sorry the .mqh is in the include file just forgot add it to the comment.

yes the global variables have been assigned.

I also have an error stating that DevlinTrade is an undeclared identifier when trying to call from the mqh file.

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

//|                              Devlin_Simple_EA_With_Functions.mq4 |

//|                                                     Craig Devlin |

//|                                                                  |

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

#property copyright "Craig Devlin"

#property link      ""

#property version   "1.00"

#property strict



// include and objects


#include <DevlinTrade.mqh>

CTrade Trade;

CCount Count;


// Input variables


input int MagicNumber = 102;

input int Slippage = 10;


input double LotSize = 0.1;

input int StopLoss = 0;

input int TakeProfit = 0;


input int MaPeriod = 5;

input ENUM_MA_METHOD MaMethod = MODE_EMA;

input ENUM_APPLIED_PRICE MaPrice = PRICE_CLOSE;


// Global variables


int gBuyTicket, gSellTicket;


// OnInit() event handler


void OnInit()

   {

      // Set magic number

      DevlinTrade.SetMagicNumber(MagicNumber);

      DevlinTrade.SetSlippage(Slippage);

      

      return(INIT_SUCCEEDED);

   }


// OnTick() event handler


void OnTick()

   {

      // Moving average and close price from last bar

      double ma =  iMA(_Symbol, _Period, MaPeriod, 0, MaMethod, MaPrice, 1);

      double close = Close[1];

      

      // Buy order condition

      if(close > ma && Count.Buy() == 0 && gBuyTicket == 0)

         {

            // Close sell orders

            DevlinTrade.CloseAllSellOrders();

            

            // Open buy order

            gBuyTicket = DevlinTrade.OpenBuyOrder(_Symbol, LotSize);

            gSellTicket = 0;

            

            // Add stoploss and take profit

            ModifyStopByPoints(gBuyTicket, StopLoss, TakeProfit);

         }

      

      // Sell order condition

      if(close < ma && Count.Sell() == 0 && gSellTicket == 0)

         {

            // Close buy orders

            DevlinTrade.CloseAllBuyOrders();

            

            // Open sell order

            gSellTicket = DevlinTrade.OpenSellOrder(_Symbol, LotSize);

            gBuyTicket = 0;

            

            // Add stoploss and take profit to the order

            ModifyStopByPoints(gSellTicket, StopLoss, TakeProfit);

         }   

   }

 
Paul Anscombe #:

you say you have a problem including the file but your comment in the code says the include works fine.... does it include when compiled or not?

In your code you reference object  DevlinTrade  but where have you created it?

Without more code or proper details of the errors it's a guessing game..  

//+------------------------------------------------------------------+
//|                              Devlin_Simple_EA_With_Functions.mq4 |
//|                                                     Craig Devlin |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Craig Devlin"
#property link      ""
#property version   "1.00"
#property strict


// include and objects

#include <DevlinTrade.mqh>
CTrade Trade;
CCount Count;

// Input variables

input int MagicNumber = 102;
input int Slippage = 10;

input double LotSize = 0.1;
input int StopLoss = 0;
input int TakeProfit = 0;

input int MaPeriod = 5;
input ENUM_MA_METHOD MaMethod = MODE_EMA;
input ENUM_APPLIED_PRICE MaPrice = PRICE_CLOSE;

// Global variables

int gBuyTicket, gSellTicket;

// OnInit() event handler

void OnInit()
   {
      // Set magic number
      DevlinTrade.SetMagicNumber(MagicNumber);
      DevlinTrade.SetSlippage(Slippage);
      
      return(INIT_SUCCEEDED);
   }

// OnTick() event handler

void OnTick()
   {
      // Moving average and close price from last bar
      double ma =  iMA(_Symbol, _Period, MaPeriod, 0, MaMethod, MaPrice, 1);
      double close = Close[1];
      
      // Buy order condition
      if(close > ma && Count.Buy() == 0 && gBuyTicket == 0)
         {
            // Close sell orders
            DevlinTrade.CloseAllSellOrders();
            
            // Open buy order
            gBuyTicket = DevlinTrade.OpenBuyOrder(_Symbol, LotSize);
            gSellTicket = 0;
            
            // Add stoploss and take profit
            ModifyStopByPoints(gBuyTicket, StopLoss, TakeProfit);
         }
      
      // Sell order condition
      if(close < ma && Count.Sell() == 0 && gSellTicket == 0)
         {
            // Close buy orders
            DevlinTrade.CloseAllBuyOrders();
            
            // Open sell order
            gSellTicket = DevlinTrade.OpenSellOrder(_Symbol, LotSize);
            gBuyTicket = 0;
            
            // Add stoploss and take profit to the order
            ModifyStopByPoints(gSellTicket, StopLoss, TakeProfit);
         }   

   }

'DevlinTrade' - undeclared identifier Devlin_Simple_EA_With_Functions.mq4 40 7
'SetMagicNumber' - struct or class type expected Devlin_Simple_EA_With_Functions.mq4 40 19
'SetSlippage' - struct or class type expected Devlin_Simple_EA_With_Functions.mq4 41 19
'return' - 'void' function returns a value Devlin_Simple_EA_With_Functions.mq4 43 7
'DevlinTrade' - undeclared identifier Devlin_Simple_EA_With_Functions.mq4 58 13
'CloseAllSellOrders' - struct or class type expected Devlin_Simple_EA_With_Functions.mq4 58 25
'OpenBuyOrder' - struct or class type expected Devlin_Simple_EA_With_Functions.mq4 61 38
'DevlinTrade' - undeclared identifier Devlin_Simple_EA_With_Functions.mq4 72 13
'CloseAllBuyOrders' - struct or class type expected Devlin_Simple_EA_With_Functions.mq4 72 25
'OpenSellOrder' - struct or class type expected Devlin_Simple_EA_With_Functions.mq4 75 39

 

I have attached the .mqh file if it helps.

Files:
DevlinTrade.mqh  34 kb
 
oDEVLINo1o #:

I have attached the .mqh file if it helps.

use the code button </>  to post code it is unreadable otherwise....

your problem is still the same as I said before, you are not declaring the class instance DevlinTrade

You are declaring CTrade Trade  then using DevlinTrade instead...

 
Paul Anscombe #:

use the code button </>  to post code it is unreadable otherwise....

your problem is still the same as I said before, you are not declaring the class instance Devlintrade

Paul I am learning from the book and the code is wrong I know this because not only have I checked but It also have the source code provided on the website and when compiling it has more errors than mine as I have fixed a few things but my knowledge is very limited would you be able to provide an example of how to fix my problem ? it would be much appreciated.     
 
Paul Anscombe #:

use the code button </>  to post code it is unreadable otherwise....

your problem is still the same as I said before, you are not declaring the class instance DevlinTrade

You are declaring CTrade Trade  then using DevlinTrade instead...

Paul you are an absolute legend down to 1 error I will tackle this one on my own. much appreciated mate thanks.
 
oDEVLINo1o #:
Paul you are an absolute legend down to 1 error I will tackle this one on my own. much appreciated mate thanks.

here you go

it is good practice to have the include file name and the class name to be obviously linked for ease of debugging especially if the include only contains a single class

#include <DevlinTrade.mqh>

CTrade DevlinTrade;

int OnInit()
{
   int MagicNumber = 12345;
   int Slippage = 1; 
   DevlinTrade.SetMagicNumber(MagicNumber);  // none of this works.
   DevlinTrade.SetSlippage(Slippage);  
   return(INIT_SUCCEEDED);
}
Reason: