Ejemplo de uso de la biblioteca "News Filter"

Ejemplo de uso de la biblioteca "News Filter"

14 febrero 2025, 15:53
Niquel Mendoza
0
34

Bienvenido a este blog. En esta guía, te mostraré un ejemplo de cómo utilizar la biblioteca gratuita News Filter.

Introducción

Esta biblioteca te permite para filtrar noticias en vivo según su importancia, pero tiene ciertas limitaciones:

  • Sólo funciona en tiempo real (no en modo de prueba).
  • Utiliza OnTimer, por lo que No es compatible con programas que también utilizan este evento.

Teniendo esto en cuenta, si su programa ya utiliza OnTimer, No recomiendo usar esta librería, ya que puede interferir con su funcionamiento.

Con estas aclaraciones, veamos cómo implementarlo en un Asesor Experto simple .

1. Definir variables e importar la biblioteca

Primero, necesitamos definir las banderas de importancia del evento y Importar cuatro funciones necesarias para su correcto uso.

 // Define flags to be able to choose the importance of events
 #define FLAG_IMPORTANCE_HIGH 16 
#define FLAG_IMPORTANCE_LOW 8
 #define FLAG_IMPORTANCE_MODERATE 4
 #define FLAG_IMPORTANCE_NONE 2

 // Import the external library "Filters News.ex5"
 #import "Filters_News.ex5"
 void SetMinutesBeforeAndAfter( int ); //Function to set the minutes before and after for filtering
 void OnTimerEvent(); //Function that must be executed in the OnTimer event
 inline bool TradingHastobeSuspended(); //Funcoin that will verify if your strategy can be executed or not
 int OnNewDay( int flags); //Function that is executed every new day
 #import 

Luego, creamos lo siguiente variables globales:

 // Input parameter to define minutes before and after an event to consider
input int MinutosBeforeAfter = 10 ;

// Variable to store the latest daily candle timestamp
datetime new_vela;

Explicación :

  • MinutosBeforeAfter: Define cuántos minutos antes y después de un evento se suspenderá el trading.
  • new_vela: Almacena la apertura de la vela diaria (D1).
2. Configuración en OnInit

En la función OnInit definiremos cuántos minutos antes y después del evento se filtrarán las noticias.

 // Initialization function
int OnInit () {
   // Set the number of minutes before and after an event for filtering
   SetMinutesBeforeAndAfter(MinutosBeforeAfter);
   return ( INIT_SUCCEEDED );
}
3. Implementación en OnTick

En OnTick, se definirán indicadores de importancia para filtrar las noticias y verificar si se pueden negociar o no.

 // Function executed on every tick
void OnTick () {
   // Check if a new daily candle has formed
   if (new_vela != iTime ( _Symbol , PERIOD_D1 , 0 )) {
       int flags = FLAG_IMPORTANCE_LOW | FLAG_IMPORTANCE_MODERATE | FLAG_IMPORTANCE_HIGH;   // Set event importance 
      OnNewDay(flags); // Process new day events based on the importance level
      new_vela = iTime ( _Symbol , PERIOD_D1 , 0 ); // Update the last daily candle timestamp
   }
   
   // Check if trading needs to be suspended
   if (TradingHastobeSuspended() == false ) {
       // Trading strategy logic goes here
   }
}

Explicación:

  1. La apertura de una nueva vela diaria (D1) se detecta.
  2. Se definen las banderas de importancia para filtrar eventos relevantes.
  3. Se comprueba si se debe suspender la negociación antes de ejecutar la estrategia.

IMPORTANTE:   Dentro del bloque { } de:

 if (TradingHastobeSuspended() == false )

Debes escribir tu Código de estrategia.

4.OnDeinit y    Configuración de OnTimer

Ejecutar el evento OnTimer

La función OnTimer ejecutará OnTimerEvent() cada vez que se active el temporizador.

 void OnTimer () {
   OnTimerEvent();
}

Eliminar el temporizador en OnDeinit

Cuando se cierra o elimina el Asesor Experto, necesitamos:

detener el evento del temporizador Para evitar problemas.

 void OnDeinit ( const int reason) {
   EventKillTimer (); 
}

Conclusión

Con esta implementación, ahora tenemos una filtro de noticias Integrado en nuestro sistema comercial.

Notas importantes:

  • El filtro sólo funciona para el símbolo actual.
    • Ejemplo: Si estás en EURUSD, solo filtrará EUR y USD   noticias.
    • No filtrará eventos de otros compañeros.