Move_Trand_line

Download script
Author: Copyright 2017, MetaQuotes Software Corp.

This script is designed as a utility tool for the MetaTrader 5 trading platform. Its primary purpose is to automatically reposition existing trend lines on your trading chart based on specific user-defined parameters.

Instead of manually dragging every line on your screen, this tool automates the process by scanning your chart and applying uniform adjustments to the "anchor points" (the start and end coordinates) of all trend lines simultaneously.

Core Functionality

The logic follows a systematic workflow once executed:

  • Scanning the Chart: The script identifies every object on your current main window that is classified as a "Trend Line." It creates an inventory of these items so it can apply changes to them in one go.
  • Dynamic Adjustments: For every trend line found, the script retrieves its current price and time coordinates. It then applies two specific types of movements:
    • Price Shift: It adjusts the vertical position (price) of the lines by a user-defined amount. This is useful if you need to offset multiple lines by a consistent number of pips or points.
    • Date/Time Shift: It calculates a time difference between the current date and the line's original start point, moving the lines horizontally across the timeline.
  • Automatic Anchor Management: If a trend line is missing specific coordinates (for example, if it hasn't been properly anchored), the script has a built-in "fail-safe." It will automatically snap those points to the current market price and the most recent time candle to ensure the movement logic remains consistent.
  • Error Reporting: The script includes a feedback system. If it encounters a specific line that it cannot move due to platform restrictions or technical conflicts, it logs an error code in the background so you can identify which objects may need manual attention.

Key User Benefits

  • Batch Processing: Rather than moving 20 lines one by one, you run this script once to shift the entire layout.
  • Precision: By using exact "Point" and "Date" shifts, it ensures that the relative spacing between your trend lines remains perfectly preserved after the move.
  • Workflow Efficiency: This is specifically designed for traders who perform large-scale structural analysis and need to "re-set" or "offset" their historical data quickly.
10 Views
0 Downloads
0 Favorites
Move_Trand_line
ÿþ//+------------------------------------------------------------------+

//|                                              Move Trand line.mq5 |

//|                        Copyright 2017, MetaQuotes Software Corp. |

//|                                             https://www.mql5.com |

//+------------------------------------------------------------------+

#property copyright "Copyright 2017, MetaQuotes Software Corp."

#property link      "https://www.mql5.com"

#property version   "1.004"

#property description "Move trend lines that were previously on the chart"

#property script_show_inputs

//--- input parameters

input int      shift=-90;                       // price shift

input datetime new_date=D'2017.04.20 14:00:37'; // new date

//+------------------------------------------------------------------+

//| Script program start function                                    |

//+------------------------------------------------------------------+

void OnStart()

  {

//---

   long  chart_id=0;       // Chart identifier. 0 means the current chart

   int   sub_window=0;     // Number of the chart subwindow. 0 means the main chart window ...

   int   type=OBJ_TREND;   // Type of the object. The value can be one of the values of the ENUM_OBJECT enumeration ...



   int  total=ObjectsTotal(chart_id,sub_window,type);



   for(int i=0;i<total;i++)

     {

      string name=ObjectName(chart_id,i,sub_window,type);

      double   new_price_0 =           ObjectGetDouble(chart_id,name,OBJPROP_PRICE,0);

      long     shift_date_0= new_date- ObjectGetInteger(chart_id,name,OBJPROP_TIME,0);

      long     date_1      =           ObjectGetInteger(chart_id,name,OBJPROP_TIME,1);

      //double   price_1     =           ObjectGetDouble(chart_id,name,OBJPROP_PRICE,1);



      TrendPointChange(chart_id,name,0,new_date,new_price_0+shift*Point());

      TrendPointChange(chart_id,name,1,date_1+shift_date_0,new_price_0+shift*Point());

     }

  }

//+------------------------------------------------------------------+ 

//| Move trend line anchor point                                     | 

//+------------------------------------------------------------------+ 

bool TrendPointChange(const long   chart_ID=0,       // chart's ID 

                      const string name="TrendLine", // line name 

                      const int    point_index=0,    // anchor point index 

                      datetime     time=0,           // anchor point time coordinate 

                      double       price=0)          // anchor point price coordinate 

  {

//--- if point position is not set, move it to the current bar having Bid price 

   if(!time)

      time=TimeCurrent();

   if(!price)

      price=SymbolInfoDouble(Symbol(),SYMBOL_BID);

//--- reset the error value 

   ResetLastError();

//--- move trend line's anchor point 

   if(!ObjectMove(chart_ID,name,point_index,time,price))

     {

      Print(__FUNCTION__,

            ": failed to move the anchor point! Error code = ",GetLastError());

      return(false);

     }

//--- successful execution 

   return(true);

  }

//+------------------------------------------------------------------+

Comments