Backtest empty (AFL on SPY)

I have displayed SPY on the Chart and run backtest on 'Current' and dates 01/01/94 to 30/01/2021
But I am getting No results on Backtest. I have set positions to 'Long and Short' in Settings->General
I have included the AFL code I am backtesting
Not sure what I am doing wrong...

Thanks

#include_once "Formulas\Norgate Data\Norgate Data Functions.afl"
Delis = DateTime()>=GetFnData("DelistingDate");

NP=90; // 20 Positions as not many stocks may be available
SetOption("MaxOpenPositions",NP);
SetPositionSize(1,spsPercentOfEquity);//5% as only 20 positions

RS = Optimize("RSI",2,2,30,1);
RSISig = RSIa(Ref(C,-1),RS) ;
MAS = Optimize("MA",9,5,10,1);
FA = MA(C,MAS);
MA1=MA(C,200);
//Marketcap = Outstanding shares x Share Price
mc = IIf ((GetFnData("SharesOut")*C)>1000000000,1,0);
//Set up Index MA
//Index = Foreign("SPY","C",True);
//IndexMA=MA(Index,200);

BuyPrice = Open;
ShortPrice = Open;
SellPrice = Open;
CoverPrice = Open;

Buy = Ref(C,-1) > MA1 AND Cross(10,RSISig) AND NorgateIndexConstituentTimeSeries("$SPX") AND NOT Delis AND mc;
//Buy = Ref(C,-1) > MA1 AND Index > IndexMA AND Cross(RSIlimit,RSISig) AND NorgateIndexConstituentTimeSeries("$SPX") AND NOT Delis AND mc;
Sell = Ref(C,-1) > FA OR Delis ;
Short = Ref(C,-1) < MA1 AND Cross(RSISig,90) AND NorgateIndexConstituentTimeSeries("$SPX") AND NOT Delis AND mc;
Cover = FA > Ref(C,-1);
ApplyStop(stopTypeTrailing,stopModePercent,40,True);
Buy = ExRem(Buy,Sell);
Short = ExRem (Short, Cover);

@ativecoloradan, the values that you use to generate your buy/sell rules are different than those that you think!

I always suggest using an Exploration to display ALL intermediate values to get better understanding of what is happening in your formula: please, see How do I debug my formula?

I mean that to debug the code you should avoid using functions directly in the conditions, but instead assign to each rule a separate variable like:

closePrevBar =  Ref( C, -1 );
aboveMA1 = closePrevBar > MA1;
crossRsiAt10 = Cross( 10, RSISig );
inSPX = NorgateIndexConstituentTimeSeries( "$SPX" );
sharesOut = GetFnData( "SharesOut" );
// etc...
Buy = aboveMa1 AND crossRsiAt10 AND inSPX AND( NOT Delis ) AND mc;

if( status( "action" ) == actionExplore )
{
    Filter = 1;
    AddColumn( ClosePrevBar, "Close prev. Bar" );
    AddColumn( MA1, "MA1" );
    AddColumn( aboveMA1, "Above MA1", 1 );
    AddColumn( crossRsiAt10, "Cross RSS at 10", 1 );
    AddColumn( inSPX, "SPX constit.", 1 );
    AddColumn( delis, "Delisted", 1 );
    AddColumn( sharesOut, "Shares", 1 );
    AddColumn( C, "Close", 1 );
    AddColumn( mc, "Share * C", 1.2 );
    // and so on...
    AddColumn( Buy, "Buy", 1 );
    AddColumn( Sell, "Sell", 1 );
    // etc.

    SetSortColumns( -2, 1 );
}

In any case, the main culprit here seems to be the line:

Delis = DateTime()>=GetFnData("DelistingDate");

Change it to:

Delis = NZ(DateTime() >= GetFnData("DelistingDate"), 0);

Happy debugging!

2 Likes