I'm trying to run a simple backtest with the Equity() function inside a chart. The array returned by Equity() is constant as if no trades are ever taken. What am I doing wrong?
Buy/Sell/Short/Cover must be global as Equity() reads global variables. If they are not used outside Backtest function before, they must be explicitly declared global
function Backtest(...)
{
global Buy, Sell, Short, Cover; // required by Equity()
...
}
Error 10: Array subscript out of range. You must not acces array elements outside 0,,,(BacCount-1). You attempted to access non-existing -1st element of array.
It's something to do with lastVisibleBarIndex. The error disappear when I changed the previous line as below:
@bursaware I did not observe negative lastVisibleBarIndex, but I did notice values greater than (BarCount-1). I'm not sure how either of those conditions could happen. Maybe @Tomasz can clarify what's happening.
firstvisiblebarindex lastvisiblebarindex may include right blank margin area of chart! They do not stop at barcount-1. So for example if you enter that area via zoom in at the end of chart or multiple times pressing END key (so only blank area is visible) then your subscript (i.e. [toIndex] ) goes outside of barcount-1 because toindex becomes larger (barcount-1+number of blank bars). That is why you get error.