How to create a script which will draw a horizontal line when triggered ?

 
Hello,
I'm new to mql5. I'm doing this as a practice project.

Is it possible to create a script, when triggered it'll draw 1. A horizontal line at the Y axis value of the crosshair and
2. It draws only on the current time frame ?
 
Documentation on MQL5: Constants, Enumerations and Structures / Objects Constants / Object Types / OBJ_HLINE
Documentation on MQL5: Constants, Enumerations and Structures / Objects Constants / Object Types / OBJ_HLINE
  • www.mql5.com
OBJ_HLINE - Object Types - Objects Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 

for making a line appear where you click the mouse, you should study here:

https://www.mql5.com/en/docs/event_handlers/onchartevent


I made this which creates a horizontal line at whichever price you click on (y-axis):


//+------------------------------------------------------------------+
//|                                          CustomHorizontalLine.mq5 |
//|                                                     Copyright 2023, |
//|                                              https://www.example.com |
//+------------------------------------------------------------------+
#property indicator_chart_window
#define OBJ_PREFIX MQLInfoString(MQL_PROGRAM_NAME)


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{

   // Return INIT_SUCCEEDED to indicate successful initialization
   return INIT_SUCCEEDED;
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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 0;
  }
//+------------------------------------------------------------------+



//+------------------------------------------------------------------+
//| Custom chart events                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
{
   if (id == CHARTEVENT_CLICK)
   {
      // Calculate the Y-axis value (price level) from the mouse click position
      double chartHeightInPixels = ChartGetInteger(0, CHART_HEIGHT_IN_PIXELS);
      double priceRange = ChartGetDouble(0, CHART_PRICE_MAX) - ChartGetDouble(0, CHART_PRICE_MIN);
      double pixelsPerPrice = chartHeightInPixels / priceRange;
      double mouseYValue = ChartGetDouble(0, CHART_PRICE_MAX) - dparam / pixelsPerPrice;
      
      
     // Generate a unique object name based on the current timestamp
      string objectName = OBJ_PREFIX + "_HorizontalLine_" + TimeToString(TimeCurrent(), TIME_DATE | TIME_MINUTES | TIME_SECONDS);

      // Check if the object with the specified name already exists
      int objectHandle = ObjectFind(0, objectName);

      if (objectHandle != -1)
      {
         // If the object exists, move it to the new price level
         ObjectMove(0, objectName, 0, TimeCurrent(), mouseYValue);
      }
      else
      {
         // If the object doesn't exist, create a new horizontal line at the clicked price level
         ObjectCreate(0, objectName, OBJ_HLINE, 0, TimeCurrent(), mouseYValue);
         ObjectSetInteger(0, objectName, OBJPROP_COLOR, clrRed); // You can change the color here
         ObjectSetInteger(0, objectName, OBJPROP_WIDTH, 1);      // You can change the line width here
      }

      // Print the mouse click coordinates (X and Y) on the chart
      Print("Mouse click coordinates on a chart: x =", lparam, " y =", dparam, " => Price Level =", mouseYValue);
   }
}


void OnDeinit(const int reason)
{
    ObjectsDeleteAll(0, OBJ_PREFIX);
}
Documentation on MQL5: Event Handling / OnChartEvent
Documentation on MQL5: Event Handling / OnChartEvent
  • www.mql5.com
OnChartEvent - Event Handling - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
Files:
Drawline.mq5  4 kb
 
Majestic du du:
Hello,
I'm new to mql5. I'm doing this as a practice project.

Is it possible to create a script, when triggered it'll draw 1. A horizontal line at the Y axis value of the crosshair and
2. It draws only on the current time frame ?
Yes its easy.
How do you want to use it? What is the trigger condition? Draw a line on every click or just once?
Maybe you need a button after activation of which one line will be drawn at the place of the click?
 
Ezatrimeo #:

Maybe you need a button after activation of which one line will be drawn at the place of the click?

I agree that this would be necessary, otherwise it will keep drawing lines with click anywhere. I tried optimizing the code above to prevent line from forming when there is a mouse click & drag, but this didn't seem possible

 
phade #:

I agree that this would be necessary, otherwise it will keep drawing lines with click anywhere. I tried optimizing the code above to prevent line from forming when there is a mouse click & drag, but this didn't seem possible

plz check.
one click on button, next click on chart. 

Files:
 
Ezatrimeo #:

plz check.
one click on button, next click on chart. 

the funny thing is we already have this button to do the same thing on the navigation bar. but nice code, I think OP has more than enough to work off now

Reason: