Download MetaTrader 5
To add comments, please log in or register
Publish your task in Freelance service and developers will execute your order.
Timerider 2015.07.07 13:40 

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
      //--- 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
         PrintFormat("Window=%d X=%d  Y=%d  =>  Time=%s  Price=%G",window,x,y,TimeToString(dt),price);
         //--- Perform reverse conversion: (X,Y) => (Time,Price)
            PrintFormat("Time=%s  Price=%G  =>  X=%d  Y=%d",TimeToString(dt),price,x,y);
            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);
         Print("ChartXYToTimePrice return error code: ",GetLastError());
Patrice LARIVIERE 2015.10.09 08:53  

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);

         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);

To add comments, please log in or register