Custom Indicator (Separate Window) AUTO-SCALE LIMIT

 

I have wanted to stop my indicator auto-scaling on the M1 time-scale as the result is more difficult to read.

The ...

#property indicator_maximum 5.0

is no good as it just scales the top of the window to that fixed value rather than using it as a minimum limit of the top of the window (which is what I want).

To be clear, if my indicator is in the range of +2 to -2 I want the window to be say +5 to -10. If the indicator value becomes -9 to -12 within the visible range I want the window scale to now be +5 to -12. In other words I am setting a limit below which the upper and lower values are not allowed to auto-scale. There does not seem to be any direct method to do this within MT4 so the task is to perform the function with the least amount of difficulty.

I have used an extra indicator buffer to perform this task, which seems pretty wasteful but it is the best I could think of. Now this buffer contains no useful information so I don’t want to see it. Obviously using EMPTY_VALUE for most of it is the answer. However all the values can’t be empty or it won’t prevent the graph auto-scaling.

I have defined two values ...

extern double indicatorHigh =   5.0;
extern double indicatorLow  = -10.0;

which are my auto-scale limits. I need at least one of each of these visibly drawn on the chart in order to be effective. Of course I don’t want to see them! So I have set that buffer colour as Black since my charts are on a Black background. I don’t see any programmatic way of either

a) reading the chart background colour

b) programmatically setting the indicator colour at run-time (ie not using the start-up colour selector)

There is another important point. The indicators seem to have a built-in Z-ordering so that indicator 1 is at the back of the display. Therefore you need to use indicator 1 for the scaling so that the real indicators over-write it.

And here is the final code snippet ...

   int limit = Bars - counted_bars;
             
   for(int n=limit; n>=0; n--){
                           
      SCALE[n] = EMPTY_VALUE;  // my dummy indicator which limits the autoscaling
      // put the real indicator work here
   }
   
   SCALE[1] = indicatorHigh;
   SCALE[0] = indicatorLow;

EDIT: This code is not quite right. If you turn off auto-scroll and look back in history the auto-scale limit fails since the limit points are at bar[0] and bar[1] which are not visible.

   SCALE[WindowFirstVisibleBar()]   = indicatorHigh;
   SCALE[WindowFirstVisibleBar()-1] = indicatorLow;

The above works better but is still not quite right as you have to wait for a new tick before the auto-scale limit works, which again is just not quite right. So here is my latest (working) version.

   for( int n=Bars-counted_bars; n>=0; n-- ){
                      
      // do the real indicator work in here
      
      if( n % 50 == 0 )
         SCALE[n] = indicatorHigh;
      else if( n % 50 == 1)
         SCALE[n] = indicatorLow;
      else
         SCALE[n] = EMPTY_VALUE;
   }




 
dabbler:To be clear, if my indicator is in the range of +2 to -2 I want the window to be say +5 to -10. If the indicator value becomes -9 to -12 within the visible range I want the window scale to now be +5 to -12.
Auto scale and add a pair of (min/max) buffers set to the limits you want.
 
dabbler:
I'm sorry, I am having trouble understanding your terse comment. I have solved the problem and was merely presenting the complete solution above. Are you suggesting it is easier to use two buffers, rather than the one buffer I am using?


The question has come up before https://www.mql5.com/en/forum/114824

and was not answered then.

That was almost 3 years ago . . this question has come up much more recently and been answered.
 
RaptorUK:
That was almost 3 years ago . . this question has come up much more recently and been answered.

Never did find that one :-(


However, this recent post by Raptor

https://www.mql5.com/en/forum/138343

has revealed the missing "secret code"

CLR_NONE

which makes the indicator line invisible without removing its scaling effects and without having it as the background color, which really doesn't work nicely.

Now that the buffer can be made invisible, and still scale the window, WHR's suggestion of using two buffers is arguably simpler.

Reason: