I am trying to get the number of bars that have elapsed after buying in backtest. I am also trying to get the buying price.
Here is what I did. I am not sure if it works all the time.
bars_elapsed = BarsSince(buy);
buying_price = ValueWhen(Close, buy);
One problem I face is that buy array can be populated with true values after a buy transaction has taken place while the trade is still in place (sell transaction has yet to occur). This will distort the values of bars_elapsed
and buying_price
.
Did I miss out anything else? What are some better ways to get the number of bars elapsed after buy and buying price?