//+------------------------------------------------------------------+
//|                                             Auto scale chart.mq4 |
//+------------------------------------------------------------------+
#property version   "1.00"
#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 ChartSetInteger();
bool ChartSetDouble();

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- 
   Comment("");
   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);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   Comment("");
   ChartSetInteger(0,CHART_SCALEFIX,FALSE);   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| 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[])

  {
//---
   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*chart_space)*chart_space);
   double chart_price_min= bars_price_min - (( bars_price_max - bars_price_min ) /(100-2*chart_space)*chart_space);
   
   ChartSetDouble(0,CHART_FIXED_MAX,chart_price_max);
   ChartSetDouble(0,CHART_FIXED_MIN,chart_price_min);
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
