having problem with ChartTimePriceToXY function

 

hi every one,

i am trying to write a code to draw some graphical objects on chart, because one of the objects is circle, i have fixed the chart scale to 1:1

      if(!ChartSetInteger(0, CHART_SCALEFIX_11, 0, true)) 
        { 
         //--- display the error message in Experts journal 
         Print(__FUNCTION__+", Error Code = ",GetLastError()); 
         return(false);   
        } 

now i am trying to draw a square base on a trend line:

void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
   // Draw base line button
   if(id==CHARTEVENT_OBJECT_CLICK && sparam=="GGP_Button_Draw_BaseLine" && ObjectGetInteger(0, "GGP_Button_Draw_BaseLine", OBJPROP_STATE)==true)
     {
      Rnd_Index= rand();
      Trend_Line(0, "GGP_Drawing_BaseLine"+ (string)Rnd_Index, 0, Time[40], Low[40], Time[1], High[1], InpBaseLineColor, STYLE_DASHDOT, 1);

      sq_x1= (datetime)(ObjectGet("GGP_Drawing_BaseLine"+ (string)Rnd_Index, OBJPROP_TIME1));
      sq_y1= ObjectGetDouble(0, "GGP_Drawing_BaseLine"+ (string)Rnd_Index, OBJPROP_PRICE1);
      
      sq_x2= (datetime)(ObjectGet("GGP_Drawing_BaseLine"+ (string)Rnd_Index, OBJPROP_TIME2));
      sq_y2= ObjectGetDouble(0, "GGP_Drawing_BaseLine"+ (string)Rnd_Index, OBJPROP_PRICE2);
      
      // Unpressed the button
      ObjectSetInteger(0, "GGP_Button_Draw_BaseLine", OBJPROP_STATE, false);
     }

   // Reading the base line points coordinates
   if(id==CHARTEVENT_OBJECT_CLICK && sparam=="GGP_Drawing_BaseLine"+ (string)Rnd_Index)
     {
      sq_x1= (datetime)(ObjectGet("GGP_Drawing_BaseLine"+ (string)Rnd_Index, OBJPROP_TIME1));
      sq_y1= ObjectGetDouble(0, "GGP_Drawing_BaseLine"+ (string)Rnd_Index, OBJPROP_PRICE1);
      
      sq_x2= (datetime)(ObjectGet("GGP_Drawing_BaseLine"+ (string)Rnd_Index, OBJPROP_TIME2));
      sq_y2= ObjectGetDouble(0, "GGP_Drawing_BaseLine"+ (string)Rnd_Index, OBJPROP_PRICE2);
      
      // Redraw the new base line
      Trend_Line(0, "GGP_Drawing_BaseLine"+ (string)Rnd_Index, 0, sq_x1, sq_y1, sq_x2, sq_y2, InpBaseLineColor, STYLE_DASHDOT, 1);      
     }
     
   // Draw square button
   if(id==CHARTEVENT_OBJECT_CLICK && sparam=="GGP_Button_Draw_Square" && ObjectGetInteger(0, "GGP_Button_Draw_Square", OBJPROP_STATE)==true)
     {
      Draw_Main_Square();
      
      // Unpressed the button
      ObjectSetInteger(0, "GGP_Button_Draw_Square", OBJPROP_STATE, false);
     }

  }

and the main square function is:

void Draw_Main_Square()
   {
    if(ChartTimePriceToXY(0, 0, sq_x1, sq_y1, x1, y1) && ChartTimePriceToXY(0, 0, sq_x2, sq_y2, x2, y2))
      {
       int delta_y= 0;
       int delta_x= 0;
       int window= 0;
       
       if(y2>= y1)
         {
          delta_y= y2- y1;
          delta_x= x2- x1;
          
          x3= x1+ delta_y;
          y3= y1+ (-1* delta_x);
          x4= x2+ delta_y;
          y4= y2+ (-1* delta_x);
         }
       else
         {
          delta_y= y2- y1;
          delta_x= x2- x1;
          
          x3= x1- delta_y;
          y3= y1- (-1* delta_x);
          x4= x2- delta_y;
          y4= y2- (-1* delta_x);
         }
       
       bool ch1= ChartXYToTimePrice(0, x3, y3, window, sq_x3, sq_y3);
       bool ch2= ChartXYToTimePrice(0, x4, y4, window, sq_x4, sq_y4);  
      }

    // Draw square
    Trend_Line(0, "GGP_Drawing_Square_L1"+ (string)Rnd_Index, 0, sq_x1, sq_y1, sq_x2, sq_y2, InpSquareColor, STYLE_SOLID, 1);
    Trend_Line(0, "GGP_Drawing_Square_L2"+ (string)Rnd_Index, 0, sq_x1, sq_y1, sq_x3, sq_y3, InpSquareColor, STYLE_SOLID, 1);
    Trend_Line(0, "GGP_Drawing_Square_L3"+ (string)Rnd_Index, 0, sq_x2, sq_y2, sq_x4, sq_y4, InpSquareColor, STYLE_SOLID, 1);
    Trend_Line(0, "GGP_Drawing_Square_L4"+ (string)Rnd_Index, 0, sq_x3, sq_y3, sq_x4, sq_y4, InpSquareColor, STYLE_SOLID, 1);
   }

everything works great when all vertices of the square are inside the chart window, but when one vertex placed outside the chart window, the coordinates becomes negative and the code can not draw a correct square

can anybody help me with this

thanks a lot

Files:
f1.png  40 kb
f2.png  40 kb
Reason: