Percent change crosshair indicator for Strategy tester

 

Unfortunately, in the strategy tester of MQL5, the crosshair does not display the price change, as the crosshair does in the main chart.

This is critical to quantify changes in prices while debuging codes.

As such, I am exploring the option of creating an indicator that performs the function of the crosshair (percent chance calculation) to be added in an EA, which then can allow the manual quantifycation of the percent change in price between any two points of the chart in the strategy tester.

Reading across several pages I frankestain the code below for an indicator which  works .

But i would like to change the logic such as that:

1. the start price is collected upon left clicking on the chart,

2. then the current price  is dynamically gathered  while the left button of the mouse is kept pressed,

3. upon releasing the left mouse button the value should disapear..

can such a logic be implemented?


#property indicator_chart_window
#property indicator_buffers 1

// Buffers
double crossLineBuffer[];

int OnInit() {
   SetIndexBuffer(0,crossLineBuffer,INDICATOR_DATA);  
   return(INIT_SUCCEEDED);
}

int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[]) {
   return(rates_total);
}

void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam) {

datetime time; 
double price; 
int win;

// Check event mouse move
   if(id==CHARTEVENT_MOUSE_MOVE) {    
      int x = (int)lparam;
      int y = (int)dparam;
      
// Get current chart information
      ChartXYToTimePrice(0,x,y,win,time,price);
      
// Calculate and show percent variation      
      if ( sparam == "1" && crossLineBuffer[0] > 0) {
         double percent = ( ( price  - crossLineBuffer[0] ) / price ) * 100;
         Comment(percent);
      }
      
      // Check hold mouse click and save first price
      if ( sparam == "1" && crossLineBuffer[0] == 0) {
         crossLineBuffer[0] = price;
      }
   } else {
     crossLineBuffer[0] = 0;
     Comment("");
   }
}