Problem with a pixel/pip-raster calculation.

 

Hey guys,

a picture says more than 1000 words. Please have a look at both charts. The script works when I use 30 rows but when I use 100 the height of the rows is wrong. But I don't see an error in the calculation.

Can someone help me with this issue?

This is the script:

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   double dbl2pips=MathPow(10, _Digits-1);
   for (int i=0; i<ObjectsTotal(); i++) {
      if (ObjectType(ObjectName(0, i))==OBJ_RECTANGLE_LABEL) ObjectDelete(0, ObjectName(0, i));
   }
   int rows=3;
   double priceMax=ChartGetDouble(0, CHART_PRICE_MAX, 0);
   double priceMin=ChartGetDouble(0, CHART_PRICE_MIN, 0);
   double dblStep=(priceMax-priceMin)/rows;
   double pipStep=dblStep*dbl2pips;
   double pixelPerPip=ChartGetInteger(0, CHART_HEIGHT_IN_PIXELS, 0)/((priceMax-priceMin)*dbl2pips);
   double ySize=pixelPerPip*pipStep;
   int yDistance=0;
   double hiRange=priceMax;
   double loRange=priceMax-dblStep;
   for (int i=0; i<=rows; i++) {
      ObjectCreate(0, (string)i, OBJ_RECTANGLE_LABEL, 0, 0, 0);
      ObjectSetInteger(0, (string)i, OBJPROP_CORNER, CORNER_RIGHT_UPPER);
      ObjectSetInteger(0, (string)i, OBJPROP_XDISTANCE, 1);
      ObjectSetInteger(0, (string)i, OBJPROP_YDISTANCE, priceMax+i*yDistance);
      ObjectSetInteger(0, (string)i, OBJPROP_XSIZE, -i*10);
      ObjectSetInteger(0, (string)i, OBJPROP_YSIZE, (int)ySize);
      ObjectSetInteger(0, (string)i, OBJPROP_BORDER_TYPE, BORDER_FLAT);
      ObjectSetInteger(0, (string)i, OBJPROP_COLOR, clrSilver);
      ObjectSetInteger(0, (string)i, OBJPROP_BGCOLOR, clrSilver);
      ObjectSetInteger(0, (string)i, OBJPROP_BACK, true);
      ObjectSetInteger(0, (string)i, OBJPROP_HIDDEN, false);
      hiRange=loRange;
      loRange=loRange-dblStep;
      yDistance=ySize;
   }
  }
Files:
30rows.png  34 kb
100rows.png  36 kb
 
Do not use "OBJ_RECTANGLE_LABEL" for this.
Try to change the size of the chart and look at these objects.
 
Taras Slobodyanik:
Do not use "OBJ_RECTANGLE_LABEL" for this.
Try to change the size of the chart and look at these objects.


or use a smaller step)

#property strict
#property script_show_inputs

input int rows=100;

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   string prefix="rectangle";
   ObjectsDeleteAll(0,prefix);
   
   int ySize=(int)MathCeil((double)ChartGetInteger(0, CHART_HEIGHT_IN_PIXELS, 0)/(double)rows);

   for (int i=0; i<rows; i++) 
      {
      string name=prefix+(string)i;
      
      ObjectCreate(0, name, OBJ_RECTANGLE_LABEL, 0, 0, 0);
      ObjectSetInteger(0, name, OBJPROP_CORNER, CORNER_RIGHT_UPPER);
      ObjectSetInteger(0, name, OBJPROP_XDISTANCE, 1);
      ObjectSetInteger(0, name, OBJPROP_YDISTANCE, i*ySize);
      ObjectSetInteger(0, name, OBJPROP_XSIZE, -i*3);
      ObjectSetInteger(0, name, OBJPROP_YSIZE, ySize);
      ObjectSetInteger(0, name, OBJPROP_BORDER_TYPE, BORDER_FLAT);
      ObjectSetInteger(0, name, OBJPROP_COLOR, clrSilver);
      ObjectSetInteger(0, name, OBJPROP_BGCOLOR, clrSilver);
      ObjectSetInteger(0, name, OBJPROP_BACK, true);
      ObjectSetInteger(0, name, OBJPROP_HIDDEN, false);
      }
  }
 
Taras Slobodyanik:


or use a smaller step)

This is brilliant!!! Thank you so much! Exactly what I wanted to see.

Obviously I am very bad at maths... :D


Thank you!!

Reason: