I want to use the ValueWhen function to compare the value of a condition at the time of the most recent Buy to the current bar (for a sell signal). I know how to use ValueWhen but my problem is that if there are other Buy signals between my actual Buy and the current bar then it references the wrong bar.
Buy = Diff >= Optimize("Diff", 13, 2, 20, 1) AND RSI3 > RSI1;
Sell = RSI3 < ValueWhen(Buy, RSI(14));
Any pointers on how to achieve this?
Your best bet is to turn the
Sell into impulse signals rather than a state signals.
Buy = Diff >= Optimize("Diff", 13, 2, 20, 1) AND Cross(RSI3, RSI1);
Sell = Cross(ValueWhen(Buy, RSI(14)), RSI3);
That way the
Buy will only be true on the bar that the two RSIs cross, and your
ValueWhen will then work as expected.
If you want to preserve the multiple
Buy signals for all bars the criteria are true (known as a state signals), then if the exit is dependent on a value when the trade is entered at the initial
Buy, you’ll have to resort to a loop for the
Sell. Such as in this example: http://www.amibroker.com/kb/2007/03/24/how-to-plot-a-trailing-stop-in-the-price-chart/
Thanks, HelixTrader. I’ll give it a go.
I have read about state signals before but didn’t consider how they would be useful until now.