Is there a way to get the same screen measure at any zoom level?

 

I want to draw many horizontal lines across the chart and use them as some kind of grid. I want to have a certain distance between any two of them. That "gap size" has been determined by eye. I look at it and decide whether it's good or not.

My first attempt at it was to divide the chart area into a given number of "slices". That exact number of slices results in perfect gap sizes.

The problem is that all of that varies according to the zoom level. The "perfect gap size" is obtained at zoom level 2 or 3. It will not be good at other zoom levels. I don't mind that the gaps shrink or enlarge when other zoom levels are applied, that's actually expected. The problem is that I need a certain measure that is determined by eye at intermediate zoom levels.

I know that the EA can be made to manipulate the chart's zoom level, but I've had the joy of discovering that ChartSetInteger() is asynchronous and will not really change the zoom level until everything else in the code has been executed, which is of course too late and useless to my interests.

My second attempt was to draw the lines at zoom level 3, capture the difference in X/Y coordinates between two of them, translate that difference into a price difference, then use that price difference as the actual step value to determine the distance between any two lines. That works and scales very well when the zoom level is changed after the lines have been drawn, BUT the chart has to be set at the right zoom level when the indicator is loaded, or it will not work as expected.

Is there a way around this limitation and make the right measures apply even when the initial zoom level is wrong?

TIA

 
I have the same issue. Can anyone help?
 

I am the original poster. I never found the solution I wanted so I worked my way around it the best way I could.

That way is finding the value manually for each pair I trade often and keeping a record of those values in a file. The EA or indicator then looks up that value in the file as soon as it runs.

As stated above, the "perfect gap size" is obtained at zoom level 2 or 3. So I set the zoom to level 2 or 3 manually then I load the EA or indicator, which contains this function:


void FindSlices(bool LinesDrawnCheck)  {
   if (LinesDrawnCheck == true) {return;}

   int LineNumber;
   double LinePrice100MinusTen; double LinePrice100;
   LineStepSize = 999999;

   LineStepSize = getTableValue("StepSize", GridFile);
   LinePrice100 = getTableValue("Price100", GridFile);

   // IF GRID FILE NOT FOUND, DETECT VALUES -------
   if (LineStepSize == 999999)   {
      int subwindow; datetime coordtime;
      ChartXYToTimePrice(0, 10, ChartHeight - 10, subwindow, coordtime, LinePrice100MinusTen);
      ChartXYToTimePrice(0, 10, ChartHeight, subwindow, coordtime, LinePrice100);

      LineStepSize = LinePrice100MinusTen - LinePrice100;
      Print("grid file NOT found, LineStepSize is " + LineStepSize);
      int filehandle=FileOpen(GridFile,FILE_READ|FILE_SHARE_READ|FILE_WRITE|FILE_TXT|FILE_COMMON);
      FileSeek(filehandle,0,SEEK_END);
      FileWrite(filehandle, "StepSize|" + Symbol() + "|" + LineStepSize);
      FileWrite(filehandle, "Price100|" + Symbol() + "|" + LinePrice100);
      FileClose(filehandle);

   }
   // IF GRID FILE EXISTS, READ IT ----------------
   else  {
      Print("grid file FOUND, LineStepSize is " + LineStepSize);
   }

   for(LineNumber = 100; LineNumber <= ArraySize(SliceNames); LineNumber++)   {
      SliceNames[LineNumber] = "SliceLine_" + LineNumber;
      SliceValues[LineNumber] = LinePrice100;
      LinePrice100 = LinePrice100 + LineStepSize;
   }
   LinePrice100 = (SliceValues[100] - LineStepSize);
   for(LineNumber = 99; LineNumber > 0; LineNumber--)   {
      SliceNames[LineNumber] = "SliceLine_" + LineNumber;
      SliceValues[LineNumber] = LinePrice100;
      LinePrice100 = LinePrice100 - LineStepSize;
   }
   drawSlices();
   LinesDrawn = true;
}


The function above will find the value for the global variable LineStepSize, then draw the lines that delimit each slice.


For that to work, you're going to need two additional functions:

void drawSlices()  {
   if (LinesDrawn == false)   {return;}
   for(int i = 1; i < ArraySize(SliceValues); i++)   {
      drawLines(SliceNames[i], SliceValues[i]);
   }
}


double getTableValue(string value, string filename)   {
   string   Line;
   string   Result[];
   double   retvalue = 999999;
   bool     found = false;
   int      fh = 0;

   if (FileIsExist(filename, FILE_COMMON))   {
           fh = FileOpen(filename, FILE_READ|FILE_TXT|FILE_COMMON);
           while (found == false && !FileIsEnding(fh))  {
           Line = FileReadString(fh);
           if ( StringFind(Line,value,0) != -1 && StringFind(Line,_Symbol,0) != -1 )   {
              found = true;
              int i = StringSplit(Line,StringGetCharacter("|", 0), Result);
              if (i > 0)  {retvalue = StrToDouble(Result[2]);}
           }
           if ( StringFind(Line,value,0) != -1 && StringFind(Line, "XXXXXX", 0) != -1 )   {
              found = true;
              i = StringSplit(Line,StringGetCharacter("|", 0), Result);
              if (i > 0)  {retvalue = StrToDouble(Result[2]);}
           }
      }
        }
   FileClose(fh);
   return retvalue;
}



I hope that helps. If something doesn't work, you may ask, but I may not be able to help because I wrote that several months ago and I don't code often, in fact I haven't coded in about three months so I don't quite remember exactly how everything works and I need some time to make heads and tails of my own code.