Custom events possible?

 

Hello again.

I'm building my strategy and it's becoming big, and I need a better way to manage how the objects interact with each other. I'm new to OOP.

Can I generate signals—events like—instead of manually checking for conditions on every signal object from the trading class? Then, the trade class would have a mechanism to catch the events and perform actions.

class PSARSignalEmitter
  {
private:
   BasePSAR          trendFilter;
   BasePSAR          entrySignal;

//..
//..
// this is the current implimentation. I have to check for a buy and sell signal int all the time
   int               GenerateSignal()
     {
      int trendPosition = trendFilter.CheckPosition();
      int trendSwitch = trendFilter.CheckPositionSwitch();
      int entrySwitch = entrySignal.CheckPositionSwitch();

      if(trendPosition > 0 && entrySwitch > 0)
         return 1;  // Buy signal
      if(trendPosition < 0 && entrySwitch < 0)
         return -1; // Sell signal

      return 0;     // No signal
     }

   //..
   // What is on my mind: I should generate an event instead
   //...

   void               GenerateSignal()
     {
      int trendPosition = trendFilter.CheckPosition();
      int trendSwitch = trendFilter.CheckPositionSwitch();
      int entrySwitch = entrySignal.CheckPositionSwitch();

      if(trendPosition > 0 && entrySwitch > 0)
         event.emit(Buy_Signal);  // Something like this for Buy signal
      if(trendPosition < 0 && entrySwitch < 0)
         event.emit(Sell_Signal);  // Something like this for Buy signal
      if(trendSwitch == 1)
         event.emit(Trend_Switch_Buy);  // Something like this for Buy trend switch
      if(trendSwitch == -1)
         event.emit(Trend_Switch_Sell);  // Something like this for Sell trend switch
     }
};
   //+------------------------------------------------------------------+

   // then what i would do on the trading class
   void              ProcessTradeSignals()
     {
      OnEvent(Buy_Signal){BuyNow();}
      OnEvent(Sell_Signal){SellNow();}
      OnEvent(Trend_Switch_Buy){Notifications("Buy Trend Started");}
      OnEvent(Trend_Switch_Sell){Notifications("Sell Trend Started");}
     }

Two reasons I want a better approach:

1. I'm checking the signal on new bar on the first tick and I miss the switch signal around 40% of the time. I think the dot is not created yet on the first tick when I miss it. If I use events I can run ontick and reduce missed signals. using flags seems cumbersome to me.
2. I want to use a vast number of signal gen objects, and I want the trade class to only watch for the events without manually checking conditions in each.

I'm not sure if it is something I could do. Maybe I don't have the right words to search through to get ideas on this. I will appreciate pointers to the right direction. Thanks

 
Kevin Onsongo:

Hello again.

I'm building my strategy and it's becoming big, and I need a better way to manage how the objects interact with each other. I'm new to OOP.

Can I generate signals—events like—instead of manually checking for conditions on every signal object from the trading class? Then, the trade class would have a mechanism to catch the events and perform actions.

Two reasons I want a better approach:

1. I'm checking the signal on new bar on the first tick and I miss the switch signal around 40% of the time. I think the dot is not created yet on the first tick when I miss it. If I use events I can run ontick and reduce missed signals. using flags seems cumbersome to me.
2. I want to use a vast number of signal gen objects, and I want the trade class to only watch for the events without manually checking conditions in each.

I'm not sure if it is something I could do. Maybe I don't have the right words to search through to get ideas on this. I will appreciate pointers to the right direction. Thanks

I achieved this by using https://www.mql5.com/en/docs/eventfunctions/eventchartcustom

And then using OnChartEvent on my trading logic to watch for and catch the custom event ID.

Documentation on MQL5: Working with Events / EventChartCustom
Documentation on MQL5: Working with Events / EventChartCustom
  • www.mql5.com
The function generates a custom event for the specified chart. Parameters chart_id [in] Chart identifier. 0 means the current chart...