Number of days since entry with Switch-statement

Hi everyone!

I have a very simple task that I can't find a good neat solution to, and I'm sure there is one.

I want to use a Switch-statement with the input of how many bars there is since the last buy. This is to use different kind of stops depending on how long since the entry.

The BarsSince() looks right, but that returns an array and won´t work with the switch. So I need to drill it down to an integer, but I just can't find a way. And I would very much like to avoid for-loops.

I have searched the forum and there are similar posts, but none that goes from an array to a specific integer.

I'm probably missing something very obvious, so sorry about that, but thanks for any help :slight_smile:

But you would have to create looping code to properly execute for both state and impulse signals.

If you have impulse signals then you may use barssince with Iif function.

Buy = Cross(C, MA(C,20));// Impulse signal

exit_cond1 = ...;
exit_cond2 = ...;
exit_cond3 = ...;

bars_since = BarsSince(Buy);

Sell = IIf(bars_since < 50, exit_cond1, 
    IIf(bars_since < 100, exit_cond2, 
    IIf(bars_since < 150, exit_cond3, 0)));

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.