I need help getting started on a variation of a Donchian style stop. Instead of a n-day lowest low/highest high, I am trying to use the n-th lower low/higher high. To complicate things I would like to make this level a trailing stop based on new highs/lows.
So for example, if I am long and n=5, the initial stop would calculated from the entry bar as the 1st low and look back each bar to find the 2nd low and so on until the 5th lower low. It could be 5 bars back, it could be 25 bars back. Assuming a long position and the market makes a new high, the stop is recalculated. The stop may not change, if this new high bar's low is equal to the entry bar low and there are no occurences of a lower low in between entry bar and the new high bar.
Concievably it could calculate even lower if you had an outside bar that say dropped down to the 4th low but then reversed and made a new high. In that case, counting back 5 lower lows could take you back further than the intial stop which you probably would not want to do, but who knows, that could also be tested.
So I see two main parts here that I do not know how to code, 1.) calculating the initial nth lower low and 2.) making it a trailing stop.
Any help, anything just to get me started will be greatly appreciated.
Thanks in advance,
James