I cant find a way to use this function on different EAs because it uses global variable

 

Hi Experts, I have trade news filters that I think can be better coded for multiple EAs usage.
Currently copy paste for each EAs I am aware it is a bad practice.

Tried doing a library for it but this function required global variable so that does not work

Below is one of the function. In this case NFP is the global variable.
Is there experts experienced something similar maybe can point out my silly mistake

bool NFPDate()
  {
   MqlDateTime TimeNow;
   TimeToStruct(TimeCurrent(),TimeNow);

   bool check = false;
   if(NFP==false) return check;
   if((TimeNow.day == 04 && TimeNow.mon == 03 && TimeNow.year == 2024)  ||
      (TimeNow.day == 29 && TimeNow.mon == 01 && TimeNow.year == 2024)  ||
      (TimeNow.day == 01 && TimeNow.mon == 01 && TimeNow.year == 2024)  ||
      (TimeNow.day == 04 && TimeNow.mon == 12 && TimeNow.year == 2023)  ||
      (TimeNow.day == 30 && TimeNow.mon == 10 && TimeNow.year == 2023)  ||
      (TimeNow.day == 02 && TimeNow.mon == 10 && TimeNow.year == 2023)  ||
      (TimeNow.day == 28 && TimeNow.mon == 08 && TimeNow.year == 2023)  ||
      (TimeNow.day == 31 && TimeNow.mon == 07 && TimeNow.year == 2023)  ||
      (TimeNow.day == 03 && TimeNow.mon == 07 && TimeNow.year == 2023)  ||
      (TimeNow.day == 29 && TimeNow.mon == 05 && TimeNow.year == 2023)  ||
      (TimeNow.day == 01 && TimeNow.mon == 05 && TimeNow.year == 2023)  ||
      (TimeNow.day == 03 && TimeNow.mon == 04 && TimeNow.year == 2023)  ||
      (TimeNow.day == 06 && TimeNow.mon == 03 && TimeNow.year == 2023)  ||
      (TimeNow.day == 30 && TimeNow.mon == 01 && TimeNow.year == 2023)  ||
      (TimeNow.day == 02 && TimeNow.mon == 01 && TimeNow.year == 2023)  ||
      (TimeNow.day == 28 && TimeNow.mon == 11 && TimeNow.year == 2022)  ||
      (TimeNow.day == 31 && TimeNow.mon == 10 && TimeNow.year == 2022)  ||
      (TimeNow.day == 03 && TimeNow.mon == 10 && TimeNow.year == 2022)  ||
      (TimeNow.day == 29 && TimeNow.mon == 08 && TimeNow.year == 2022)  ||
      (TimeNow.day == 01 && TimeNow.mon == 08 && TimeNow.year == 2022)  ||
      (TimeNow.day == 06 && TimeNow.mon == 07 && TimeNow.year == 2022)  ||
      (TimeNow.day == 30 && TimeNow.mon == 05 && TimeNow.year == 2022)  ||
      (TimeNow.day == 02 && TimeNow.mon == 05 && TimeNow.year == 2022)  ||
      (TimeNow.day == 28 && TimeNow.mon == 03 && TimeNow.year == 2022)  ||
      (TimeNow.day == 02 && TimeNow.mon == 03 && TimeNow.year == 2022)  ||
      (TimeNow.day == 31 && TimeNow.mon == 01 && TimeNow.year == 2022)  ||
      (TimeNow.day == 03 && TimeNow.mon == 01 && TimeNow.year == 2022)  ||
      (TimeNow.day == 29 && TimeNow.mon == 11 && TimeNow.year == 2021)  ||
      (TimeNow.day == 01 && TimeNow.mon == 11 && TimeNow.year == 2021)  ||
      (TimeNow.day == 04 && TimeNow.mon == 10 && TimeNow.year == 2021)  ||
      (TimeNow.day == 30 && TimeNow.mon == 08 && TimeNow.year == 2021)  ||
      (TimeNow.day == 02 && TimeNow.mon == 08 && TimeNow.year == 2021)  ||
      (TimeNow.day == 28 && TimeNow.mon == 06 && TimeNow.year == 2021)  ||
      (TimeNow.day == 31 && TimeNow.mon == 05 && TimeNow.year == 2021)  ||
      (TimeNow.day == 03 && TimeNow.mon == 05 && TimeNow.year == 2021)  ||
      (TimeNow.day == 29 && TimeNow.mon == 03 && TimeNow.year == 2021)  ||
      (TimeNow.day == 01 && TimeNow.mon == 03 && TimeNow.year == 2021)  ||
      (TimeNow.day == 01 && TimeNow.mon == 02 && TimeNow.year == 2021)  ||
      (TimeNow.day == 04 && TimeNow.mon == 01 && TimeNow.year == 2021)  ||
      (TimeNow.day == 30 && TimeNow.mon == 11 && TimeNow.year == 2020)  ||
      (TimeNow.day == 02 && TimeNow.mon == 11 && TimeNow.year == 2020)  ||
      (TimeNow.day == 28 && TimeNow.mon == 09 && TimeNow.year == 2020)  ||
      (TimeNow.day == 31 && TimeNow.mon == 08 && TimeNow.year == 2020)  ||
      (TimeNow.day == 03 && TimeNow.mon == 08 && TimeNow.year == 2020)  ||
      (TimeNow.day == 29 && TimeNow.mon == 06 && TimeNow.year == 2020)  ||
      (TimeNow.day == 01 && TimeNow.mon == 06 && TimeNow.year == 2020)  ||
      (TimeNow.day == 04 && TimeNow.mon == 05 && TimeNow.year == 2020)  ||
      (TimeNow.day == 30 && TimeNow.mon == 03 && TimeNow.year == 2020)  ||
      (TimeNow.day == 02 && TimeNow.mon == 03 && TimeNow.year == 2020)  ||
      (TimeNow.day == 03 && TimeNow.mon == 02 && TimeNow.year == 2020)  ||
      (TimeNow.day == 06 && TimeNow.mon == 01 && TimeNow.year == 2020)  ||
      (TimeNow.day == 6 && TimeNow.mon == 12 && TimeNow.year == 2019)  ||
      (TimeNow.day == 1 && TimeNow.mon == 11 && TimeNow.year == 2019)  ||
      (TimeNow.day == 4 && TimeNow.mon == 10 && TimeNow.year == 2019)  ||
      (TimeNow.day == 6 && TimeNow.mon == 9 && TimeNow.year == 2019)  ||
      (TimeNow.day == 2 && TimeNow.mon == 8 && TimeNow.year == 2019)  ||
      (TimeNow.day == 5 && TimeNow.mon == 7 && TimeNow.year == 2019)  ||
      (TimeNow.day == 7 && TimeNow.mon == 6 && TimeNow.year == 2019)  ||
      (TimeNow.day == 3 && TimeNow.mon == 5 && TimeNow.year == 2019)  ||
      (TimeNow.day == 5 && TimeNow.mon == 4 && TimeNow.year == 2019)  ||
      (TimeNow.day == 8 && TimeNow.mon == 3 && TimeNow.year == 2019)  ||
      (TimeNow.day == 1 && TimeNow.mon == 2 && TimeNow.year == 2019)  ||
      (TimeNow.day == 4 && TimeNow.mon == 1 && TimeNow.year == 2019)  ||
      (TimeNow.day == 7 && TimeNow.mon == 12 && TimeNow.year == 2018)  ||
      (TimeNow.day == 2 && TimeNow.mon == 11 && TimeNow.year == 2018)  ||
      (TimeNow.day == 5 && TimeNow.mon == 10 && TimeNow.year == 2018)  ||
      (TimeNow.day == 7 && TimeNow.mon == 9 && TimeNow.year == 2018)  ||
      (TimeNow.day == 3 && TimeNow.mon == 8 && TimeNow.year == 2018)  ||
      (TimeNow.day == 6 && TimeNow.mon == 7 && TimeNow.year == 2018)  ||
      (TimeNow.day == 1 && TimeNow.mon == 6 && TimeNow.year == 2018)  ||
      (TimeNow.day == 4 && TimeNow.mon == 5 && TimeNow.year == 2018)  ||
      (TimeNow.day == 6 && TimeNow.mon == 4 && TimeNow.year == 2018)  ||
      (TimeNow.day == 9 && TimeNow.mon == 3 && TimeNow.year == 2018)  ||
      (TimeNow.day == 2 && TimeNow.mon == 2 && TimeNow.year == 2018)  ||
      (TimeNow.day == 5 && TimeNow.mon == 1 && TimeNow.year == 2018)  ||
      (TimeNow.day == 8 && TimeNow.mon == 12 && TimeNow.year == 2017)  ||
      (TimeNow.day == 3 && TimeNow.mon == 11 && TimeNow.year == 2017)  ||
      (TimeNow.day == 6 && TimeNow.mon == 10 && TimeNow.year == 2017)  ||
      (TimeNow.day == 1 && TimeNow.mon == 9 && TimeNow.year == 2017)  ||
      (TimeNow.day == 4 && TimeNow.mon == 8 && TimeNow.year == 2017)  ||
      (TimeNow.day == 7 && TimeNow.mon == 7 && TimeNow.year == 2017)  ||
      (TimeNow.day == 2 && TimeNow.mon == 6 && TimeNow.year == 2017)  ||
      (TimeNow.day == 5 && TimeNow.mon == 5 && TimeNow.year == 2017)  ||
      (TimeNow.day == 7 && TimeNow.mon == 4 && TimeNow.year == 2017)  ||
      (TimeNow.day == 10 && TimeNow.mon == 3 && TimeNow.year == 2017)  ||
      (TimeNow.day == 3 && TimeNow.mon == 2 && TimeNow.year == 2017)  ||
      (TimeNow.day == 6 && TimeNow.mon == 1 && TimeNow.year == 2017)  )
      check = true;

   return check;
  }

Thanks for your attention
Amir

 

Object Oriented Programming + mqh files

 
Vladislav Boyko #:
Object Oriented Programming

https://www.mql5.com/en/docs/basis/oop

https://www.mql5.com/en/docs/basis/types/classes

(These are just a couple of basic links to documentation. You will also need some educational literature to learn OOP from scratch)

Vladislav Boyko #:
mqh files

https://www.mql5.com/en/docs/basis/preprosessor/include

 
Vladislav Boyko #:

Object Oriented Programming + mqh files

I know nothing about object oriented programming but will look into it thanks for the info

 

Object-oriented programming is intended (among other things) to combine/link/wrap data (your global variable) and methods (your function) into one object.

Start simple - just combine your function and variable into a class or structure object. It's not difficult at all.

class CYourFirstClass
  {
private:
   bool NFP;
public:
   void initialize(bool a_nfpValue);
   bool NFPDate();
  };

bool CYourFirstClass::NFPDate(void)
  {
   //...
   bool check = false;
   if(NFP==false) return check;
   //...
   return check;
  }

void CYourFirstClass::initialize(bool a_nfpValue)
  {
   NFP = a_nfpValue;
  }
 
M. Amir #:

I know nothing about object oriented programming but will look into it thanks for the info

You are ready to start using OOP. Because you have independently come to the need to unite data and the code that works with it. You yourself have just formulated this need.

I started my acquaintance with OOP from a similar situation - I wanted to split the code into files and I had a problem with global variables.

 
Vladislav Boyko #:

You are ready to start using OOP. Because you have independently come to the need to unite data and the code that works with it. You yourself have just formulated this need.

I started my acquaintance with OOP from a similar situation - I wanted to split the code into files and I had a problem with global variables.

it is nice to hear someone once was in a similar path as me. Thanks for the kind word sir, it has been awhile

 

Now is the best time for you to do this. Your first task is to combine the NFP variable and the NFPDate function into a class.


Here's a good article to start with, in my opinion:

https://www.mql5.com/en/articles/351

Because you immediately start using OOP to implement a library of functions:

Creating Libraries of Functions

The first and the most simple application of OOP is creating your own libraries of frequently used functions. Of course, you can simply store these functions in an include file (mqh). When you well need a function, simply include a file and call this function. However, if you program long enough you can can collect a huge amount of functions, so that it would be difficult to remember their names and purpose.

You can collect functions in different files, splitting them into categories based on purpose. For example, functions of working with arrays, functions of working with string, functions of calculating orders, etc. In the last sentence the word "category" can be replaced by the word "classes". The meaning remains the same, but we will come closer to the topic - Object-Oriented Programming.


Below is an example of a bad article for a beginner (in my subjective opinion) because the author throughout the article simply describes some complex terms using other complex terms, confusing the beginner and creating the false impression that OOP is very difficult. It's like a student's essay who just combined information from Google without understanding it.

https://www.mql5.com/en/articles/12813

 
  1. Study this article:
  2. Complete this task:
    Vladislav Boyko #:
    Your first task is to combine the NFP variable and the NFPDate function into a class.

Once you've completed that task, you'll never be able to stop writing OOP code😄

 
Vladislav Boyko #:

Now is the best time for you to do this. Your first task is to combine the NFP variable and the NFPDate function into a class.


Here's a good article to start with, in my opinion:

https://www.mql5.com/en/articles/351

Because you immediately start using OOP to implement a library of functions:


Below is an example of a bad article for a beginner (in my subjective opinion) because the author throughout the article simply describes some complex terms using other complex terms, confusing the beginner and creating the false impression that OOP is very difficult. It's like a student's essay who just combined information from Google without understanding it.

https://www.mql5.com/en/articles/12813

Learning through it right now, apppreciate it for going this far for some random person