Static Variables leading to longer hold period on last trade

Wondering if anyone might be able to spot an issue with my logic or code.

I’m getting some odd behavior when running a back test using static variables. My code has a 10 bar stop applied, but my last trade shows it has been on for 155,003 daily bars. I’m guessing it’s some combination of a memory allocation error in combination with the number of static variable indicators I’m outputting to columns in the back test (35), but that’s an ill-informed guess.

The tests are on just a single symbol at a time (NASDAQ 100 symbols), and the entry criteria doesn’t use any indicators; it’s just simple price calculation:

BreakPointHigh3 = Ref(HHV(High, 3), -1); // High price of last three days
BreakPointLow3 = Ref(LLV(Low, 3), -1); // Low price of last three days
MidPoint3 = (BreakPointHigh3 + BreakPointLow3) / 2; // mid point of the high - low point last three days
AboveBreakPoint3 = Close > MidPoint3; // condition met for being above mid point of past three days

Buy = AboveBreakPoint3;	


Walking through it by commenting out sections of the code, it’s clear my use of static variables is the issue as the trades all show the expected hold period of 10 days when I eliminate the static variables.

I am also using code to exit a trade before it becomes de-listed:

OnSecondLastBarOfDelistedSecurity = !IsNull(GetFnData("DelistingDate")) 
	AND (BarIndex() == (LastValue(BarIndex()) -1) OR DateTime() >= GetFnData("DelistingDate") ) ;
OnLastTwoBarsOfDelistedSecurity = !IsNull(GetFnData("DelistingDate")) 
	AND (BarIndex() >= (LastValue(BarIndex()) -1) OR DateTime() >= GetFnData("DelistingDate") );
Buy = Buy AND NOT OnLastTwoBarsOfDelistedSecurity;
Sell = Sell OR OnSecondLastBarOfDelistedSecurity;	

OnSecondLastBarOfDelistedSecurity = !IsNull(GetFnData("DelistingDate")) 
	AND (BarIndex() == (LastValue(BarIndex()) -1) OR DateTime() >= GetFnData("DelistingDate") ) ;
OnLastTwoBarsOfDelistedSecurity = !IsNull(GetFnData("DelistingDate")) 
	AND (BarIndex() >= (LastValue(BarIndex()) -1) OR DateTime() >= GetFnData("DelistingDate") );
Short = Short AND NOT OnLastTwoBarsOfDelistedSecurity;
Cover = Cover OR OnSecondLastBarOfDelistedSecurity;

The section with all the static variables is close to 1,000 lines long so I won’t post it here. But most them are very similar to this one:

//////////////////// ABOVE 10 SMA
if ( Status( "action" ) == actionPortfolio )
{
    bo = GetBacktesterObject();
    // run default backtest procedure without generating the trade list
    bo.Backtest( True );

    // iterate through closed trades
    for ( trade = bo.GetFirstTrade( ); trade; trade = bo.GetNextTrade( ) )
    {
        // read 10 SMA values and display as custom metric
        symbol10SMA = StaticVarGet( trade.Symbol + "Above10SMA" );
        trade.AddCustomMetric( "Entry Above 10SMA", Lookup( symbol10SMA, trade.EntryDateTime ) );
    }

    // iterate through open positions
    for ( trade = bo.GetFirstOpenPos( ); trade; trade = bo.GetNextOpenPos( ) )
    {
        // read 10SMA values and display as custom metric
        symbol10SMA = StaticVarGet( trade.Symbol + "Above10SMA" );
        trade.AddCustomMetric( "Entry Above 10SMA", Lookup( symbol10SMA, trade.EntryDateTime ) );
    }
    
}

In my experience, unexpectedly long hold times are often due to your delisting logic not working the way you think it does. If the offending trade is on a delisted symbol, then use an exploration to check the values of the variables relating to delisting. For example, is OnSecondLastBarOfDelistedSecurity getting set to true 2 days prior to the delisting date?