Limited backtest range

Hi, I have a code with a Buy line inside a loop which is then referenced outside the loop as the Buy signal. This seems to create a situation where only the most recent trades are taken. Can someone please tell me what I need to change? Please keep it basic if possible, since that's where I'm at. Thanks.

Adding to the above. The most recent trade executes as desired, and an exploration shows all variables to be correct. When I add 'plotshapes' for the buy signals, only the last one shows up on the chart unless I scroll back or bar replay (they flash on when they are the most recent signal only).

I'd like to backtest all signals.

It would be much easier to help you if you posted your code...

Hi mradtke, thanks for getting back. Is there a generic guide I could use to understand what I'm wanting to do?

if 
        {
               
                Buy=Cross(C,line);
       
         }

        
Buy = Buy;

The code you just posted does not have a loop, and there's nothing in it that helps me understand what you're trying to do. What's your comfort level with arrays, and AFL in general?

I just took everything out because I assumed there was an easy answer, and there may not be. It looks like so. Konrad was the author of most of it - I just modified it slightly.

My comfort level - I'm comfortable, just not particularly skilled!

function GetVisibleBarCount()
{
    lvb=Status("lastvisiblebar");
    fvb=Status("firstvisiblebar");
    return Min(lvb-fvb,BarCount-fvb);
}
AllVisibleBars= GetVisibleBarCount();

la = BuySignal = Buy = Null;

_HHVlkbkBarDistance = HHVBars(H, lkbk);
fvb = 0;

if (Status("action") == actionIndicator)
{
    LoopStart = Status("firstvisiblebar");
    LoopEnd = Min(Status("lastvisiblebar"), BarCount);
}
else
{
    LoopStart = Max(1, BarCount - 200); /// is this line the issue?  
    LoopEnd = BarCount;
}

for(i = LoopStart; i < LoopEnd; i++)
{
    if (ww[i + fvb] == False)
        continue;
   
    if (_HHVlkbkBarDistance[i + fvb] >0)
    {
        dHighestAngle = 0;
        iHighestAngleBar = i;
       
        for (j = i - 1; j >= 0 && j > (i - lkbk); j--)
        {
            if ( (H[j+fvb]-wwE[i+fvb])/(i-j) > dHighestAngle)
            {
                dHighestAngle = (H[j+fvb]-wwE[i+fvb])/(i-j);
                iHighestAngleBar = j;
            }
        }

        if (dHighestAngle > 0)
        {

            _LineArray = LA = LineArray(iHighestAngleBar + fvb, H[iHighestAngleBar + fvb], i + fvb, wwe[i + fvb],Param("extend",1,0,1,1));//
                
                              
                Buy=Cross(C,LA) ;
                                                                
                
               if (Status("action") == actionIndicator) Plot(la,"", colordarkgrey, styleLine |stylenorescale,0,0,0,1); 
          
         }
         }
         }   
         
      
Buy = Buy;
Sell = False;


If I understand your code correctly, the for loop is there so that you can build up the values in your line array, although what that line array is supposed to be is a mystery to me. But when you do this inside the loop:

Buy=Cross(C,LA) ;

You are assigning the entire Buy array every time. That's simply inefficient, and I suggest you move that statement outside the loop. And this statement which is currently outside the loop:

Buy = Buy;

Does absolutely nothing, so you should remove it.

Neither of those changes will actually change the output of your code. You mentioned that an Exploration "shows all variables to be correct", yet I don't see any code for an Exploration. I would use an Explore and/or the built-in debugger to check the value of your line array and your Buy signal.

Thanks.

"If I understand your code correctly, the for loop is there so that you can build up the values in your line array...".

Yes. They are just trendlines with some extra conditions. I moved the Buy line outside the loop as you suggest, but the same thing is occuring.

I left the Filter & Addcolumns out of the code I posted, but it's all correct when I check that aspect. When a buy is triggered, there's no problem at all. It's just that I can only get signals from the cross of the most recent plotted line to trigger. For any previous lines it doesn't trigger in a backtest. Pls see chart below.

If I scroll back or use bar replay, the missing buy arrows will appear when the latest line is off screen. I'm sure this must have a simple solution.

x

You should check out this site Understanding how AFL works

Ah, I see now. You are (potentially) creating an entirely new Line Array each time through the loop. I thought you were creating the line array one "segment" at a time.

Perhaps something like this would work for your FOR loop:

Buy = 0;
for(i = LoopStart; i < LoopEnd; i++)
{
    if (ww[i + fvb] == False)
        continue;
   
    if (_HHVlkbkBarDistance[i + fvb] >0)
    {
        dHighestAngle = 0;
        iHighestAngleBar = i;
       
        for (j = i - 1; j >= 0 && j > (i - lkbk); j--)
        {
            if ( (H[j+fvb]-wwE[i+fvb])/(i-j) > dHighestAngle)
            {
                dHighestAngle = (H[j+fvb]-wwE[i+fvb])/(i-j);
                iHighestAngleBar = j;
            }
        }

        if (dHighestAngle > 0)
        {

            _LineArray = LA = LineArray(iHighestAngleBar + fvb, H[iHighestAngleBar + fvb], i + fvb, wwe[i + fvb],Param("extend",1,0,1,1));//
               
                                                    
               if (Status("action") == actionIndicator) Plot(la,"", colordarkgrey, styleLine |stylenorescale,0,0,0,1); 
          
         }
     }

    if (i > 0)
    {
        Buy[i] = C[i] > LA[i] AND C[i-1] < LA[i-1];
    }
 }   

Thanks for trying mradtke. Visually it's the same result. Backtest also just testing the most recent line crosses.

Outside the loop, do I then write Buy = ....?

No, you should not have to assign Buy outside the loop. Can you post your entire AFL file?

Finally worked it out.

VarSet / VarGet.