Video by via Dailymotion
Source
To achieve this the program ascertains the start date and time and end date and time of the visible window. It looks for pivots on this part of the chart and stores them in a vector. It then sorts the vector. In addition the initial position of the horizontal line is set to the center price.
The horizontal line moving event updates a variable with the current price of the line. When the user releases his mouse the chart element click event of the charting host fires. The price of the line is then compared with the values in the vector of pivots to ascertain which is the closest. The price of the line is then set to this value.
N.B As the chart develops and new pivots are added, the vector is updated, however pivots that were to the left of the chart and are now no longer visible are left in the vector. This can result in the line being adjusted to a pivot level that is no longer visible. If the window is changed the user will need to turn off and on the program so that it recalculates the pivots for the current window.
See https://markplex.com/free-tutorials/tutorial-210-make-a-horizontal-line-snap-to-pivots/