Using Portfolio PNL in the custom backtester

Greetings for the day.
@Tomasz I would like to exit all intraday trades based on the portfolio PNL , If possible can you some exit option based on the portfolio based in Amibroker 7.

SetPositionSize(1000000, spsValue); 
SetCustomBacktestProc("");       

// Trading System Logic
ShortEMAPeriod = 10; 
LongEMAPeriod = 50;  
ShortEMA = EMA(Close, ShortEMAPeriod);
LongEMA = EMA(Close, LongEMAPeriod);

Buy = Cross(ShortEMA, LongEMA) AND TimeNum() < 151500; 
Sell = TimeNum() >= 151500;                          

if (Status("action") == actionPortfolio)
{
    bo = GetBacktesterObject(); // Get the backtester object
    bo.PreProcess();           

    for (bar = 0; bar < BarCount; bar++)
    {
        bo.ProcessTradeSignals(bar); 

        CurEquity = bo.Equity;

        if (CurEquity > HighestEquity)
        {
            HighestEquity = CurEquity; 
        }

        // Check if the equity drops below 1% of the highest equity
        if (CurEquity < 0.99 * HighestEquity)
        {
            for (openpos = bo.GetFirstOpenPos(); openpos; openpos = bo.GetNextOpenPos())
            {
                bo.ExitTrade(bar, openpos.Symbol, openpos.GetPrice(bar, "C"));
            }
        }
    }

    bo.PostProcess(); 
}

// Plot signals and portfolio metrics
Plot(Close, "Close", colorBlack, styleCandle);
Plot(ShortEMA, "Short EMA", colorBlue, styleLine);
Plot(LongEMA, "Long EMA", colorRed, styleLine);


It is already possible with the code you just posted. Portfolio PNL is just bo.Equity minus initial equity.

@Tomasz Thank you for your response and support; it is working well. Could you please suggest a way to prevent placing new orders after the portfolio exits for the day, while allowing new orders to be entered starting the next day

SetOption( "InitialEquity", 100000 );
InitialEquity = 100000;

SetPositionSize( 1000000, spsValue );
SetCustomBacktestProc( "" );

ShortEMAPeriod = 10;
LongEMAPeriod = 50;

ShortEMA = EMA( Close, ShortEMAPeriod );
LongEMA = EMA( Close, LongEMAPeriod );

Buy = Cross( ShortEMA, LongEMA ) AND TimeNum() < 151500;
Sell = TimeNum() >= 151500;

if( Status( "action" ) == actionPortfolio )
{
    bo = GetBacktesterObject();
    bo.PreProcess();

    HighestEquity = 0;

    for( bar = 0; bar < BarCount; bar++ )
    {
        bo.ProcessTradeSignals( bar );

        CurEquity = bo.Equity;
        PortfolioPNL = CurEquity - InitialEquity;

        if( PortfolioPNL > 5000 )
        {
            for( openpos = bo.GetFirstOpenPos();
                    openpos;
                    openpos = bo.GetNextOpenPos() )
            {
                bo.ExitTrade( bar, openpos.Symbol, openpos.GetPrice( bar, "C" ) );
            }
		InitialEquity = CurEquity;
        }
    }

    bo.PostProcess();
}

Plot( Close, "Close", colorBlack, styleCandle );
Plot( ShortEMA, "Short EMA", colorBlue, styleLine );
Plot( LongEMA, "Long EMA", colorRed, styleLine );


Please use guidance from Knowledge Base: