//+------------------------------------------------------------------+
//|                                                       PeterE.mq4 | https://www.forexfactory.com/showthread.php?t=890930
//|                                                           .....h |
//|                                                    hayseedfx.com |
//+------------------------------------------------------------------+
#property copyright ".....h"
#property link      "hayseedfx.com"
#property version   "1.20" //delete "scale"-button on deinig is added by MikeFT https://www.forexfactory.com/thread/post/14482421#post14482421
#property strict
#property indicator_chart_window


extern string Explanation   = "Chart Space is a percent of the vertical axis as blank space above & below price";
extern string Inputs        = "Input percent number between 0 and 49 below";
extern int    chart_space   = 25;

       bool   adjusted      = false;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
     
    button("scale","scale",40,20,20,20,Lime,Black);            // calls button function below

   if(chart_space<0 || chart_space>49)
     {
      Comment("Please enter chart_space value between 0 and 49");
      Print("Please enter chart_space value between 0 and 49");
      return(0);
     }
     
     ChartSetInteger(0,CHART_SCALEFIX,TRUE);
   
  // Print(ChartGetInteger(0,CHART_HEIGHT_IN_PIXELS,0)+" height");
  // Print(ChartGetInteger(0,CHART_WIDTH_IN_PIXELS,0)+" width");
   
//---
   return(INIT_SUCCEEDED);
  }
  
  //+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
  
  void OnDeinit(const int reason)
  {
//---
   ChartSetInteger(0,CHART_SCALEFIX,FALSE);
   ObjectDelete(0, "scale");

  }
//+------------------------------------------------------------------+
//| Custom indicator iteration 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[])
  {
//---



//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

//-----
//-----

void button(string name,string label,int xsize,int ysize,int xdist,int ydist,int bcolor,int fcolor)
{
    
   if(ObjectFind(0,name)<0)
   {
      if(!ObjectCreate(0,name,OBJ_BUTTON,0,0,0))
        {
         Print(__FUNCTION__,
               ": failed to create the button! Error code = ",GetLastError());
         return;
        }
      ObjectSetString(0,name,OBJPROP_TEXT,label);
      ObjectSetInteger(0,name,OBJPROP_XSIZE,xsize);
      ObjectSetInteger(0,name,OBJPROP_YSIZE,ysize);
      ObjectSetInteger(0,name,OBJPROP_CORNER,CORNER_LEFT_LOWER);     
      ObjectSetInteger(0,name,OBJPROP_XDISTANCE,xdist);      
      ObjectSetInteger(0,name,OBJPROP_YDISTANCE,ydist);         
      ObjectSetInteger(0,name,OBJPROP_BGCOLOR,bcolor);
      ObjectSetInteger(0,name,OBJPROP_COLOR,fcolor);
      ObjectSetInteger(0,name,OBJPROP_FONTSIZE,9);
      ObjectSetInteger(0,name,OBJPROP_HIDDEN,true);
      //ObjectSetInteger(0,name,OBJPROP_BORDER_COLOR,ChartGetInteger(0,CHART_COLOR_FOREGROUND));
      ObjectSetInteger(0,name,OBJPROP_BORDER_TYPE,BORDER_RAISED);
      
      ChartRedraw();      
   }

}

//----
//----



void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam)  
     {
   
      if(id==CHARTEVENT_OBJECT_CLICK)  
      {
          
           if((sparam == "scale") && !adjusted)
             {  
              adjust(chart_space); adjusted = true;
             }
             else
             {
             adjust(3); adjusted = false;
             }
      }
                           
      }
      
//-----
//-----

 void adjust(int x)
 
 {
//---
   int iLeft   = WindowFirstVisibleBar();
   int iRight  = iLeft-WindowBarsPerChart();
   if(iRight < 0) iRight = 0;     // Chart shifted

   double bars_price_max=High[iHighest(NULL,0,MODE_HIGH,iLeft-iRight+1,iRight)];
   double bars_price_min= Low[ iLowest(NULL,0,MODE_LOW, iLeft-iRight+1,iRight)];
   
   double chart_price_max= bars_price_max + (( bars_price_max - bars_price_min ) /(100-2*x)*x);
   double chart_price_min= bars_price_min - (( bars_price_max - bars_price_min ) /(100-2*x)*x);
   
   ChartSetDouble(0,CHART_FIXED_MAX,chart_price_max);
   ChartSetDouble(0,CHART_FIXED_MIN,chart_price_min);


//--- return value of prev_calculated for next call
   
  }