mt4 - ChartSetSymbolPeriod - slow down

 

Hi,

I try to use 'ChartSetSymbolPeriod' for my indicator, but this indicator slow down my MT4 platform when I try to use it with another 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()
    {
//----
    //ObjectsDeleteAll();
//----
    return(0);
    }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
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 );
}

 Please help me.

 
Max-Enrik: I try to use 'ChartSetSymbolPeriod' for my indicator, but this indicator slow down my MT4 platform when I try to use it with another Expert Advisors.
Why?? Just run on whatever chart your put on. Let the human decide what symbol/period. Done.
 

( If I right, you would like to say, do not use any code for that, is that right? )

Sorry did you try (/ test) it?

I mean if you tried that code that does not slow down your platform?

Specially while 'Order, Depth of Market' type of Expert Adivsors.

 

(I need to make similar like below picture)

 

 

Hope to hear from you soon.

Kind regards.

 

If someone can give advise me about that, do it, please.

I need any good advise about that.

 

Kind regards. 

 

The function itself doesn't slow the system down.

But MT4 may load prices from the brokers server and calls OnDeinit and OnInit for all indicators and experts on the chart and have to calculate all indicators.

 
When you change the time-frame, all indicators and EAs on the chart are de-initialised, then initialised again. So if there is a lot to do, the platform can "hang" at times.
 

Big thanks for the information.

I am still researching about that, so I has been found 'Scripts Change Time Frame All.mq4' it works as quickly as possible.

(Posted - Change Time Frame All)

Is there any chance to write a good code for indicator won't "hang" anything?

After your answer I will decided, either I will continue my researching or not.


(Really I need your answer / advice.) 

Kind regards. 

 
Max-Enrik: Is there any chance to write a good code for indicator won't "hang" anything?
  1. Reduce max bars on chart. Or,
  2. Break the initial update into multiple ticks. See my last comment on: how to do your lookbacks correctly.
       int      iMA     = rates_total - 1 - MathMax(LB_MA, prev_calculated);
       int      iLast   = MathMax(0, iMA - 2000);            // Do in groups
    
     
    WHRoeder:
    1. Reduce max bars on chart. Or,
    2. Break the initial update into multiple ticks. See my last comment on: how to do your lookbacks correctly.

      First of all thanks for the reply.

      I do not sure I understand correctly or not, so I tried like below but I do not get any effects.

      //+------------------------------------------------------------------+
      //|                                             Custom 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
      
      int             lookback = 0;
      
      //string          ChangeSP        = "Where I go?";
      string          ChangeSP        = Symbol();
      //+------------------------------------------------------------------+
      //| Custom indicator initialization function                         |
      //+------------------------------------------------------------------+
      int OnInit()
          {
      //---
          ObjectCreate     ( 0, ChangeSP, OBJ_BUTTON,           0, 0, 0                  );
          ObjectSetInteger ( 0, ChangeSP, OBJPROP_XDISTANCE,    1600                     );
          ObjectSetInteger ( 0, ChangeSP, OBJPROP_YDISTANCE,    200                      );
          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()
      {
      //----
      //  Print(ChangeSP);
      //----
          return(0);
      }
      //+------------------------------------------------------------------+
      //| expert deinitialization function                                 |
      //+------------------------------------------------------------------+
      int deinit()
      {
      //----
          ObjectsDeleteAll(0, ChangeSP);
      //----
          return(0);
      }
      //+------------------------------------------------------------------+
      //| ChartEvent function                                              |
      //+------------------------------------------------------------------+
      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 );
          }
          return;
      }
      //+------------------------------------------------------------------+
      void ChangeSPClick(bool ChartSetSymbolPeriod)
      {
          bool ChangeSP_action = ChartSetSymbolPeriod(0, "GBPUSD", 15 );
      }
      //+------------------------------------------------------------------+
      //| OnCalculate function                                             |
      //+------------------------------------------------------------------+
      int OnCalculate(const int      rates_total     ,
                      const int      prev_calculated ,
                      const datetime &time[]         ,
                      const double   &open[]         ,
                      const double   &high[]         ,
                      const double   &low[]          ,
                      const double   &close[]        ,
                      const long     &tick_volume[]  ,
                      const long     &volume[]       ,
                      const int      &spread[]       )
      {
          for( int iBar = Bars - 1 - MathMax( lookback, prev_calculated ); iBar >= 0; --iBar )
          {
              int ChangeSP_action = rates_total - 1 - MathMax( lookback, prev_calculated);
              int iLast = MathMax( 0, ChangeSP_action - 1 );
          }
          return rates_total - 1;
      }
      //+------------------------------------------------------------------+

      I need your good reply and advice, please.

      Thanks. 

       

      Clear up yor code. You are mixing start ( ) and OnCalculate ( ).

      Check the value of ChangeSP.

       
      eddie:

      Clear up yor code. You are mixing start ( ) and OnCalculate ( ).

      Check the value of ChangeSP.

      Thanks for the reply, and I already cleaned up below code.

      Right now I have not any warnings.

      int start()
      {
      //----
      //  Print(ChangeSP);
      //----
          return(0);
      }

      But still I do not got any effects.

      Still I need good reply, please.

      Reason: