Discussion of article "Library for easy and quick development of MetaTrader programs (part XXIV): Base trading class - auto correction of invalid parameters"

 

New article Library for easy and quick development of MetaTrader programs (part XXIV): Base trading class - auto correction of invalid parameters has been published:

In this article, we will have a look at the handler of invalid trading order parameters and improve the trading event class. Now all trading events (both single ones and the ones occurred simultaneously within one tick) will be defined in programs correctly.

Generally, the EA should be able to act according to the circumstances while following the user-defined logic of handling errors in trading orders. Thus, we may give the following instructions to the EA when a trading order error is detected:

  1. Simply exit the trading method letting the user create the handler of invalid parameters of an erroneous order on their own.
  2. If an invalid value of a trading order can be fixed, do that right away and send it to the server,
  3. If a situation and an error are appropriate, repeat a trading request after a pause or simply repeat the request with the same parameters.

Handling errors in trading order parameters may lead to one of several outcomes:

  • Inability to continue trading using the EA before an error source is eliminated by the user.
  • Inability to send a trading order — exit from the trading method.
  • Correcting invalid values and sending a fixed trading order.
  • Immediate sending of a trading order with the initial parameters (here the assumption is made that trading conditions have improved).
  • Waiting, updating quote data and sending a trading order with the initial parameters.

In this article, we are going to develop the trading order error handler that will check errors and their sources, as well as return the error handling method:

  • disabling trading operations,
  • interrupting a trading operation,
  • correcting invalid parameters,
  • trading request with initial parameters,
  • trading request after waiting (temporary solution),
  • creating a pending trading request (in subsequent articles)

Author: Artyom Trishkin

 

Hi Artyom -- in working closer with this code, I noticed this peculiarity of the 'shift' value implemented in EventsHandling() and OnDoEasyEvent() for correctly handling trade events when running in the tester... I understand, as you point out in the article, when running live trade events are delivered one by one from OnChartEvent() as they are triggered by the engine, whereas in testing mode they are grouped together and delivered as a list...

My question is: wouldn't it be better to implement a dedicated function parameter in the event-handler rather than sacrificing 'lparam' which can contain useful information for the event-handler? I also think it makes the code simpler / more readable; do you agree? 

PS: Anyway, I am finding this library to be really powerful but also complex and difficult to wrap one's head around, however once mastered it should enable developing all sorts of EA-powered strategies very quickly... Beside the huge learning curve, I am also noticing the back-testing performance to be rather slow, so I hope you can address this point once you complete the feature-set you envisioned for DoEasy.

void EventsHandling(void)
  {
//--- If a trading event is present
   if(engine.IsTradeEvent())
     {
      //--- Number of trading events occurred simultaneously
      int total = engine.GetTradeEventsTotal();
      for (int i = 0; i < total; i++)
        {
         //--- Get the next event from the list of simultaneously occurred events by index
         CEventBaseObj *event = engine.GetTradeEventByIndex(i);
         if(event == NULL)
            continue;
         int   shift  = i;
         long   lparam = event.LParam();
         double dparam = event.DParam();
         string sparam = event.SParam();
         OnDoEasyEvent(CHARTEVENT_CUSTOM+event.ID(), lparam, dparam, sparam, shift);
        }
     }
   //...
   //...
  }

void OnDoEasyEvent(const int id, const long &lparam, const double &dparam, const string &sparam, int shift=0)
  {
   //...
   //...
//--- Handling trading events
   if(idx > TRADE_EVENT_NO_EVENT && idx < TRADE_EVENTS_NEXT_CODE)
     {
      //--- Get the list of trading events
      CArrayObj *list = engine.GetListAllOrdersEvents();
      if(list == NULL) return;

      //--- get the event index shift relative to the end of the list
      //--- in the tester, the shift is passed by the lparam parameter to the event handler
      //--- outside the tester, events are sent one by one and handled in OnChartEvent()
      int shift=(testing ? (int)lparam : 0);
      
      CEvent *event=list.At(list.Total()-1-shift);
      if(event==NULL) return;
   //...
   //...
  }


 
Dima Diall :

Hi Artyom -- in working closer with this code, I noticed this peculiarity of the ' shift ' value implemented in  EventsHandling()  and  OnDoEasyEvent()  for correctly handling trade events when running in the tester... I understand, as you point out in the article, when running live trade events are delivered one by one from  OnChartEvent()  as they are triggered by the engine, whereas in testing mode they are grouped together and delivered as a list...

My question is: wouldn't it be better to implement a dedicated function parameter in the event-handler rather than sacrificing ' lparam ' which can contain useful information for the event-handler? I also think it makes the code simpler / more readable; do you agree? 

PS: Anyway, I am finding this library to be really powerful but also complex and difficult to wrap one's head around, however once mastered it should enable developing all sorts of EA-powered strategies very quickly... Beside the huge learning curve, I am also noticing the back-testing performance to be rather slow, so I hope you can address this point once you complete the feature-set you envisioned for DoEasy.


No. Here I did not plan to redo anything, and most likely I will not. All the necessary data is already delivered to event objects, and the rest of the data is already taken from those objects whose event was registered.

 
Artyom Trishkin:

No. Here I did not plan to redo anything, and most likely I will not. All the necessary data is already delivered to event objects, and the rest of the data is already taken from those objects whose event was registered.

OK, fair enough... I agree that all necessary data is in the event objects.