Creating a Buy/Sell array based on discrete signals in Explore

I’m unsophisticated in coding and have looked through the documentation and user group, but I can’t figure out how to do this operation.

Once I have created a discrete Buy (1) or Sell (-1) signal in my code (I might get a Buy or Sell trigger every 20 bars), I want to create an array in Explore where the most recent signal carries through until the next Buy or Sell Signal.

So in the Explore function, I want it to go 1,1,1,1,-1,-1,-1,1,1,-1,-1, etc., instead of getting 0s in the interim periods when I'm not getting a Buy or Sell signal.

I’d appreciate it if you could show me how to do this. Thanks!

@paulgraham you could do this either with the Flip() function: https://www.amibroker.com/guide/afl/flip.html or with the ValueWhen() function: https://www.amibroker.com/guide/afl/valuewhen.html. For example, with the latter function you might use something like this untested snippet:

// Put the discrete Buy and Sell signals into a single array. Assumes that both start out with 
// values of 1 when true and 0 otherwise.
stateArray = Buy - Sell; 

// Maintain the Buy/Sell state until another signal occurs
stateArray = ValueWhen(stateArray != 0, stateArray);
3 Likes

@mradtke Both of your suggestions work great. You're the master! Thanks so much!