My indicator doesn't work from within a template

 

I am toying with some ideas for a future indicator and I am stumped at this problem:

The indicator draws some lines across the screen as soon as it is loaded, as expected. Then I save the chart as a template. Then I load the template in another chart and the indicator is loaded, but it doesn't draw the lines. I've spent a long time analysing this and can't find the reason. Can someone please help?

TIA


int OnInit()   {
   PriceMax    =  WindowPriceMax();
   PriceMin    =  WindowPriceMin();
   Slices      =  10;
   SliceSize   =  (PriceMax - PriceMin) / Slices;
   drawSlices();
   return(INIT_SUCCEEDED);
}

//--- My values
double   PriceMax;
double   PriceMin;
int      Slices;
double   SliceSize;
double   SliceValue;
string   AllObjects[100];

void OnDeinit(const int reason)  {
   int i;
   EventKillTimer();
   for(i = 1; i <= 10; i++)   {AllObjects[i]="SliceLine_" + i;}
   for (i=1; i <= ArraySize(AllObjects) - 1; i++)   {if (ObjectFind(AllObjects[i]) != -1)       {ObjectDelete(AllObjects[i]);}}
}

// FUNCTION drawSlices ---------------
void drawSlices()  {
   string LineName;
   SliceValue = PriceMin;
   for(int i = 1; i < (Slices); i++)   {
      SliceValue = SliceValue + SliceSize;
      LineName = "SliceLine_" + i;
      AllObjects[i] = LineName;
      drawLines(LineName, SliceValue);
   }
}

// FUNCTION drawLines ---------------
void drawLines(string name, double price)  {
   if (ObjectFind(name) != -1) {ObjectDelete(name);}
   ObjectCreate(name, OBJ_HLINE, 0,Time[0], price);
   ObjectSet(name, OBJPROP_STYLE, STYLE_SOLID);
   ObjectSet(name, OBJPROP_WIDTH, 0);
   ObjectSet(name, OBJPROP_COLOR, LightGray);
   ObjectSetString(0,name,OBJPROP_TEXT,name);
   ObjectSet(name,OBJPROP_BACK,true);

}

//=============================================================
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[])
                        {
//--- return value of prev_calculated for next call
        return(rates_total);
}
 
whoowl:

I am toying with some ideas for a future indicator and I am stumped at this problem:

The indicator draws some lines across the screen as soon as it is loaded, as expected. Then I save the chart as a template. Then I load the template in another chart and the indicator is loaded, but it doesn't draw the lines. I've spent a long time analysing this and can't find the reason. Can someone please help?

TIA


You can delete the remaining objects from the old chart in the Inıt function.

  ObjectsDeleteAll ();

    Or

      LineName = "SliceLine_" + i;

   You can try to run the code as follows.

LineName = Symbol () + string (Period ()) + "SliceLine_" + i;

 
Mehmet Bastem:

You can delete the remaining objects from the old chart in the Inıt function.

  ObjectsDeleteAll ();

    Or

      LineName = "SliceLine_" + i;

   You can try to run the code as follows.

LineName = Symbol () + string (Period ()) + "SliceLine_" + i;

Neither suggestion works.
I can't understand them either.

 
int OnInit()   {
   PriceMax    =  WindowPriceMax();
   PriceMin    =  WindowPriceMin();

Do not use price or server related calls on OnInit. Waiit for OnCalc.

 
whoowl:

Neither suggestion works.
I can't understand them either.

I wonder if it gives any errors in the Expert tab?

 
Mehmet Bastem:

I wonder if it gives any errors in the Expert tab?

No errors. I planted a few Print statements that only proved that everything was running as expected, except the lines weren't drawn as expected.

William's solution does the job, but only if the market is opened. That is another thing for me not to understand very well, because my code never fails to detect the current price, for example, when the market is either opened or closed.

There are many things in MQL4 that I don't quite understand.

 
whoowl:

No errors. I planted a few Print statements that only proved that everything was running as expected, except the lines weren't drawn as expected.

William's solution does the job, but only if the market is opened. That is another thing for me not to understand very well, because my code never fails to detect the current price, for example, when the market is either opened or closed.

There are many things in MQL4 that I don't quite understand.


  drawSlices () and drawLines (string name, double price)

Can you write the code for calling functions for 

OnCalculate()
 
Mehmet Bastem:


  drawSlices () and drawLines (string name, double price)

Can you write the code for calling functions for 

I'm sorry, are you asking me? Do you want me to post something? Your comment is not clear enough to me.
 
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[])

  {

//---

   

//--- return value of prev_calculated for next call

//I want the code to call these functions here. Since the code is not all,

// it will be difficult for us to find the error.

 drawSlices ();

 drawLines();

   return(rates_total);

  }
whoowl
:

I'm sorry, are you asking me? Do you want me to post something? Your comment is not clear enough to me.


@ whoowl

whoowl
whoowl
  • 2021.05.03
  • www.mql5.com
Trader's profile
 
Mehmet Bastem:

Please edit your post and use the code button (Alt+S) when pasting code.

EDIT your original post, please do not just post the code correctly in a new post.

Reason: