1

I try to use ChartSetSymbolPeriod() for my [ Custom Indicator ], but this indicator slows down my MT4 platform when I try to use it with another [ Expert Advisors ].

Specially while 'Order, Depth of Market' type of [ Expert Advisors ].

//+------------------------------------------------------------------+
//|                                       ChangeSymbol Indicator.mq4 |
//|                        Copyright 2016, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window

string    ChangeSP = "Where I go?";
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit() {
//---
    ObjectCreate     ( 0, ChangeSP, OBJ_BUTTON,           0, 0, 0            );
    ObjectSetInteger ( 0, ChangeSP, OBJPROP_XDISTANCE,    15                 );
    ObjectSetInteger ( 0, ChangeSP, OBJPROP_YDISTANCE,    100                );
    ObjectSetInteger ( 0, ChangeSP, OBJPROP_XSIZE,        200                );
    ObjectSetInteger ( 0, ChangeSP, OBJPROP_YSIZE,        40                 );
    ObjectSetString  ( 0, ChangeSP, OBJPROP_TEXT,         "Go to GBPUSD M15" );
    ObjectSetInteger ( 0, ChangeSP, OBJPROP_COLOR,        White              );
    ObjectSetInteger ( 0, ChangeSP, OBJPROP_BGCOLOR,      Red                );
    ObjectSetInteger ( 0, ChangeSP, OBJPROP_BORDER_COLOR, Red                );
    ObjectSetInteger ( 0, ChangeSP, OBJPROP_BORDER_TYPE,  BORDER_FLAT        );
    ObjectSetInteger ( 0, ChangeSP, OBJPROP_BACK,         false              );
    ObjectSetInteger ( 0, ChangeSP, OBJPROP_HIDDEN,       true               );
    ObjectSetInteger ( 0, ChangeSP, OBJPROP_STATE,        false              );
    ObjectSetInteger ( 0, ChangeSP, OBJPROP_FONTSIZE,     12                 );
//---
    return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start(){
    return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit(){
    return(0);
}
//+------------------------------------------------------------------+
void OnChartEvent( const int     id,
                   const long   &lparam,
                   const double &dparam,
                   const string &sparam
                   ) {
    if (  sparam == ChangeSP ) {
       ChangeSPClick( ChangeSP );
       ObjectSetInteger( 0, ChangeSP, OBJPROP_STATE, false );
    }
}
//+------------------------------------------------------------------+
void ChangeSPClick( bool   ChartSetSymbolPeriod ) {
    bool ChangeSP_action = ChartSetSymbolPeriod( 0, "GBPUSD", 15 );
}
user3666197
  • 1
  • 6
  • 50
  • 92
Max-Enrik
  • 61
  • 13

1 Answers1

1

Performance?
First: ALL [ Custom Indicators ] SHARE one SINGLE THREAD !

This New-MQL4.56789 architecture feature imposes even higher need for due care to be taken for non-blocking, performance focused code in [ Custom Indicators ].

An MQL4 documentation next states, that a call to ChartSetSymbolPeriod() is not synchronous, but just adds one more ticket into a TaskQueue.

ChartSetSymbolPeriod()

Changes the symbol and period of the specified chart. The function is asynchronous, i.e. it sends the command and does not wait for its execution completion. The command is added to chart message queue and executed only after all previous commands have been processed.

What else might be in the Queue already?
MQL4 recognises the following types of <ChartEVENT>-s :

OnChartEvent() is the handler of a group of ChartEvent events:

·CHARTEVENT_KEYDOWN — event of a keystroke, when the chart window is focused;

·CHARTEVENT_MOUSE_MOVE — mouse move events and mouse click events ( if CHART_EVENT_MOUSE_MOVE = true is set for the chart );

·CHARTEVENT_OBJECT_CREATE
— event of graphical object creation ( if CHART_EVENT_OBJECT_CREATE = true is set for the chart );

·CHARTEVENT_OBJECT_CHANGE
— event of change of an object property via the properties dialog;

·CHARTEVENT_OBJECT_DELETE
— event of graphical object deletion ( if CHART_EVENT_OBJECT_DELETE = true is set for the chart );

·CHARTEVENT_OBJECT_CLICK
— event of a mouse click in a graphical object belonging to the chart;

·CHARTEVENT_OBJECT_DRAG
— event of a graphical object move using the mouse;

·CHARTEVENT_OBJECT_ENDEDIT
— event of the finished text editing in the entry box of the LabelEdit graphical object;

·CHARTEVENT_CLICK
— event of a mouse click on the chart;

·CHARTEVENT_CHART_CHANGE
— event of chart changes; <<<<<<<<<<<<<<<<<<<<

·CHARTEVENT_CUSTOM + n
— ID of the user event, where n is in the range from 0 to 65535.

·CHARTEVENT_CUSTOM_LAST
— the last acceptable ID of a custom event == ( CHARTEVENT_CUSTOM +65535 ).

A change of Symbol and Period is a major chart undertaking, it makes [ MetaTrader Terminal 4 ] to throw away all current state of the instrument depicted inside the chart, next to move into the Back-of-the-House and to fetch all historically saved records from a [HistoryCentre] ( try F2 to see that facility in action ) and it has to repaint the GUI accordingly.

And guess what,
1) that takes some time
2) that makes a <ChartEVENT> which, again, triggers, the OnChartEvent() handler.
3) move back to the "Square No. 1"

Does it create a mouse-trap-wheel arrangement, to have to run infinitely in a loop?
Yes, it does.


Also, one might have already noticed a side-effect

A name in a function call signature masks the name of MQL4 Function

//+------------------------------------------------------------------+
    void ChangeSPClick( bool   ChartSetSymbolPeriod ) {
        bool ChangeSP_action = ChartSetSymbolPeriod( 0, "GBPUSD", 15 );
    }
user3666197
  • 1
  • 6
  • 50
  • 92