Interesting coding problem
My two cents: put all the lows in an array and let your code check if a certain low has 'neighbours' that are higher / lower.
An example: these are the bar lows of the last 15 bars:
[ 5, 7 ,7,
4 ,5 ,6 ,6 ,7 ,6 ,
11, 5, 5, 6, 7, 6]
A for-loop would check each number. When it arrives at
4, the code checks 2 neighbours on the left and finds those are higher. So this
4 is considered a possible 'DrawCandidate'. For now at least.
Because immediately afterwards the same for-loop also checks the
4's neighbours on the right. Both of those are also higher, so that
4 now becomes an 'official Low', and is used as starting coordinate for a new line.
Code continues its calculations until it arrives at the
11: all four of it's neighbours are lower, so the 11 becomes an 'official High', being used as endpoint for a line (and also as starting point for the following line)
Obviously, you would have to take into account the 'outliers', and the 'weight' (number of neighbours to check), so include those early on in your code.
Good luck !