How to create a script which will draw a horizontal line when triggered ?
- How to execute script to draw horizontal lines on all charts
- How I assemble my advisor by trial and error
- Moving a line
To do that, use OBJ_HLINE
https://www.mql5.com/en/docs/constants/objectconstants/enum_object/obj_hline
- www.mql5.com
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); }
- www.mql5.com
Hello,
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?
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
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