news filter and other stuff

 


I am sharing this code with you in hope that you can help me to add the features for it to close all  open trades x minutes before the news. Its MT5

If you can advice me on a feature to backtest news it would be a plus.


Code

Input:

input string snev = "------------ News Event Filter -----------";
input bool           UseNewsFilter= false; // Use News filter :
input bool           FilterHigh  = false; // Filter High Events :
input bool           FilterMed  = false; // Filter Med Events :
input int            MinBeforeHighEvent = 15; // Minutes before High news :
input int            MinAfterHighEvent = 15; // Minutes after High news :
input int            MinBeforeMedEvent = 15; // Minutes before Med news :
input int            MinAfterMedEvent = 15; // Minutes after Med news :

Logic:

string GetMainCurr(string pSymbol)
  {
   int numberstr        = StringLen(pSymbol);
   string MainCurr      = StringSubstr(pSymbol,0, numberstr/2);
   return MainCurr;
  }

string GetSecCurr(string pSymbol)
  {
   int numberstr        = StringLen(pSymbol);
   string SecondaryCurr = StringSubstr(pSymbol, numberstr/2);
   return SecondaryCurr;
  }

bool HighEventDodge(string pCurrency, datetime pTimeBefore, datetime pTimeAfter)
  {
   MqlCalendarEvent events[];
   bool EventStop    = false;
   int countMain = CalendarEventByCurrency(pCurrency,events);
   ArrayResize(events,countMain);
   datetime times[];
   int counter1 = 0;
   int counter2 = 0;
   MqlCalendarValue Value[];
   int ValueCount = CalendarValueHistory(Value,TimeTradeServer(),TimeTradeServer()+pTimeBefore,NULL,pCurrency);
   ArrayResize(Value,ValueCount);

   for(int i=0; i < countMain; i++)
     {
      int importance = events[i].importance;
      if(importance == CALENDAR_IMPORTANCE_HIGH)
        {
         counter1++;
         ulong  EventId   = events[i].id;
         string EventName = events[i].name;
         for(int j=0; j < ValueCount; j++)
           {
            if(EventId == Value[j].event_id)
              {
               datetime EventTime = Value[j].time;
               ArrayResize(times,counter2+1);
               times[counter2] = EventTime;
               counter2++;
              }
           }
        }
     }
   int Items = ArraySize(times);
   ArraySort(times);
   for(int i=0; i<Items; i++)
     {
      if(times[i] > TimeTradeServer() && times[i] < (TimeTradeServer() + pTimeBefore + PeriodSeconds(PERIOD_M5)) && (TimeTradeServer() < (times[i]+pTimeAfter)))
        {
         EventStop = true;
        }
     }
   return EventStop;
  }

bool MedEventDodge(string pCurrency, datetime pTimeBefore, datetime pTimeAfter)
  {
   MqlCalendarEvent events[];
   bool EventStop    = false;

   int countMain = CalendarEventByCurrency(pCurrency,events);
   ArrayResize(events,countMain);
   datetime times[];
   int counter1 = 0;
   int counter2 = 0;
   MqlCalendarValue Value[];
   int ValueCount = CalendarValueHistory(Value,TimeTradeServer(),TimeTradeServer()+pTimeBefore,NULL,pCurrency);
   ArrayResize(Value,ValueCount);
   for(int i=0; i < countMain; i++)
     {
      int importance = events[i].importance;
      if(importance  == CALENDAR_IMPORTANCE_MODERATE)
        {
         counter1++;
         ulong  EventId   = events[i].id;
         string EventName = events[i].name;
         for(int j=0; j < ValueCount; j++)
           {
            if(EventId == Value[j].event_id)
              {
               datetime EventTime = Value[j].time;
               ArrayResize(times,counter2+1);
               times[counter2] = EventTime;
               counter2++;
              }
           }
        }
     }
   int Items = ArraySize(times);
   ArraySort(times);
   for(int i=0; i<Items; i++)
     {
      if(times[i] > TimeTradeServer() && times[i] < (TimeTradeServer() + pTimeBefore + PeriodSeconds(PERIOD_M5)) && (TimeTradeServer() < (times[i]+pTimeAfter)))
        {
         EventStop = true;
        }
     }
   return EventStop;
  }
  
Testing trading strategies on real ticks
Testing trading strategies on real ticks
  • www.mql5.com
The article provides the results of testing a simple trading strategy in three modes: "1 minute OHLC", "Every tick" and "Every tick based on real ticks" using actual historical data.
 
Your topic has been moved to the section: Expert Advisors and Automated Trading — In the future, please consider which section is most appropriate for your query.
MQL5 forum: Expert Advisors and Automated Trading
MQL5 forum: Expert Advisors and Automated Trading
  • www.mql5.com
How to create an Expert Advisor (a trading robot) for Forex trading
 
Fernando Carreiro #:
Your topic has been moved to the section: Expert Advisors and Automated Trading — In the future, please consider which section is most appropriate for your query.

Ok noted, its most appropriate so am waiting for your opinion on it.

I see so many EA'S Without a news filter, are they implying that their EA would win even if there is news?

 
Henry Asaba Achankeng #:

Ok noted, its most appropriate so am waiting for your opinion on it.

I see so many EA'S Without a news filter, are they implying that their EA would win even if there is news?

Why not ? It depends of the strategy.
 
Alain Verleyen #:
Why not ? It depends of the strategy.

well if it was designed to work in the news then yes.

I noticed you are a pro developer.

No help for the needy :) ?

 
Henry Asaba Achankeng #:

well if it was designed to work in the news then yes.

I noticed you are a pro developer.

No help for the needy :) ?

Your approach to solve this issue is very unintuitive.

You should hold an array of datetime with events.

This array should be created once and only updated if....

Then you check the array of datetime if TimeCurrent is approaching your events to avoid.

While you are in such a phase and all positions are closed/the EA is waiting, you could use that time to update your array of datetimes.

By splitting your functionality into smaller sub-pieces, it's much easier to manage your code.

The all in one function, you are using at the moment is a little messy and inefficient.
 
Dominik Christian Egert #:
Your approach to solve this issue is very unintuitive.

You should hold an array of datetime with events.

This array should be created once and only updated if....

Then you check the array of datetime if TimeCurrent is approaching your events to avoid.

While you are in such a phase and all positions are closed/the EA is waiting, you could use that time to update your array of datetimes.

By splitting your functionality into smaller sub-pieces, it's much easier to manage your code.

The all in one function, you are using at the moment is a little messy and inefficient.

okay i missed part of the code written by my partner. Actually the issue is already solved in the code but I did not notice.


Thanks for the advice


////
   if(FilterHigh == true && UseNewsFilter == true)
     {
      DodgeMainHigh  = HighEventDodge(MainCurrency,minutesBeforeHigh,minutesAfterHigh);
      DodgeSecHigh   = HighEventDodge(SecondaryCurrency,minutesBeforeHigh,minutesAfterHigh);
     }
   if(FilterMed == true && UseNewsFilter == true)
     {
      DodgeMainMed   = MedEventDodge(MainCurrency,minutesBeforeMed,minutesAfterMed);
      DodgeSecMed    = MedEventDodge(SecondaryCurrency,minutesBeforeMed,minutesAfterMed);
     }

   
   if(UseNewsFilter == true && MinBeforeHighEvent > 0 && MinAfterHighEvent > 0 && FilterHigh == true)
     {
      EventStop = DodgeMainHigh || DodgeSecHigh;
      if(EventStop == true)
        {
         Print("--- Stoping for High news events ---");
         sqCloseAllPositions("Any", MagicNumber, 1, "");
         sqCloseAllPositions("Any", MagicNumber,-1, "");
        }
     }
   if(UseNewsFilter == true && MinBeforeMedEvent > 0 && MinAfterMedEvent > 0 && FilterMed == true)
     {
      EventStop = DodgeMainMed || DodgeSecMed;
      if(EventStop == true)
        {
         Print("--- Stoping for Med news events ---");
         sqCloseAllPositions("Any", MagicNumber, 1, "");
         sqCloseAllPositions("Any", MagicNumber,-1, "");
        }
     }
////
Reason: