The difficult part is defining the "back to the buy rate or near" conditions. That can be a very tricky group of conditions. I think you should try to choose what indicators (and in which conditions) you would consider the condition is met. Then we can implement it into your project.
M