Create a Line with CHARTEVENT_CLICK

 

Hi guys,

I want to create a graphical object with every mouse click. For that I use the CHARTEVENT_CLICK function. But I always just draw e.g one H-Line and when I press the mouse again, it is deleted and another is drawn. But I want to prevent that and draw one line after the other without deleting any. As an example, I just took a code from the Codebase, which you can also see here. This indicator draws a crosshair everytime you click, at the specific coordinates from the mouse. But I want that the old crosshair stays and isn't deleted. How can I do that? Thanks for your help.

//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//--- Show the event parameters on the chart
   Comment(__FUNCTION__,": id=",id," lparam=",lparam," dparam=",dparam," sparam=",sparam);
//--- If this is an event of a mouse click on the chart
   if(id==CHARTEVENT_CLICK)
     {
      //--- Prepare variables
      int      x     =(int)lparam;
      int      y     =(int)dparam;
      datetime dt    =0;
      double   price =0;
      int      window=0;
      //--- Convert the X and Y coordinates in terms of date/time
      if(ChartXYToTimePrice(0,x,y,window,dt,price))
        {
         PrintFormat("Window=%d X=%d  Y=%d  =>  Time=%s  Price=%G",window,x,y,TimeToString(dt),price);
         //--- Perform reverse conversion: (X,Y) => (Time,Price)
         if(ChartTimePriceToXY(0,window,dt,price,x,y))
            PrintFormat("Time=%s  Price=%G  =>  X=%d  Y=%d",TimeToString(dt),price,x,y);
         else
            Print("ChartTimePriceToXY return error code: ",GetLastError());
         //--- delete lines
         ObjectDelete(0,"V Line");
         ObjectDelete(0,"H Line");
         //--- create horizontal and vertical lines of the crosshair
         ObjectCreate(0,"H Line",OBJ_HLINE,window,dt,price);
         ObjectCreate(0,"V Line",OBJ_VLINE,window,dt,price);
         ChartRedraw(0);
        }
      else
         Print("ChartXYToTimePrice return error code: ",GetLastError());
      Print("+--------------------------------------------------------------+");
     }
  }
 

Hi Timerider, it's my first time on this forum.

Excuse my English, I'm french

I would add an index in the call of the function incremented each time, delete the lines //---delete Lines; ObjectDelete(0,"V ... and replace

         ObjectCreate(0,"H Line",OBJ_HLINE,window,dt,price);
         ObjectCreate(0,"V Line",OBJ_VLINE,window,dt,price);
with:

         ObjectCreate("H Line"+index,OBJ_HLINE,window,0,0);

         ObjectSet("H Line"+index,OBJPROP_PRICE1,price);

         ObjectCreate("V Line"+index,OBJ_HLINE,window,0,0);
         ObjectSet("V Line"+index,OBJPROP_TIME1,dt);

Reason: