Shift an array to the right/left by n positions

I have the following code:

Buy = ....;
Sell = ....;
BuyPrice = SellPrice = Open;

Since there is a trade delay, I cannot use ValueWhen( Buy, BuyPrice ) to pick the actual Buy/Sell price.
One solution would be to shift Buy/Sell array by the SetTradeDelays and then use ValueWhen. I am wondering if there any solution to get the actual buy/sell price (same as what reported in backtesting report). Otherwise, how can I shift the Buy/Sell array and use ValueWhen?


You can shift an array using the Ref() function, but there are other challenges that you may not be considering. For example, what if you have Buy signals on both Monday and Tuesday? Regardless of any shifting that you do, if you use ValueWhen on Friday you will get pricing related to the Tuesday entry signal, not the Monday entry signal. And in fact, in Phase 1 you won't know whether the back tester used the Monday entry, the Tuesday entry, or neither entry due to max position constraints, capital constraints, position scoring, etc.

1 Like

Hi, thanks for your reply.
I think Ref() references to the previous or subsequent element in an array, not shifting. For example, if in an occasion you get the 5th element of the array, Ref(Array,-1) gives you the 4th element in the array!
Consider the below array for example:

Sell = [0 0 1 0 0]

I want to get:

Sell = [0 0 0 1 0]

Thanks again!

The Ref() function returns an entire array. So you can just do this:

priorSell = Ref(Sell, -1);

I suggest you give this a try and use an Exploration to confirm that it's behaving as expected.

It references past or future data BY SHIFTING the array. Instead of "thinking" and "guessing" why don't you read this crucial document: that precisely shows what Ref is doing.