How to display Stop Loss values in the backtest trade list

Hi guys.
I tried to modify the example given here to display Stop Loss Price for each trade.
Let's assume I entered a trade at $22 and I set SL to $20 by ApplyStop function and TP to $25 for example:

ApplyStop(stopTypeLoss  , stopModePoint, SL, 1, 0);
ApplyStop(stopTypeProfit, stopModePoint, TP, 1, 0);

How can I display the value $20 and $25 values as SL and TP in the backtest trade list?
I tried to modify the below line in the above link example that gets the ATR value, but don't know how to get the SL and TP.

symbolATR = StaticVarGet( trade.Symbol + "ATR" );

Be grateful, if some can help.

Thanks.

@Tomasz Would you please advise?
I need to add a user-defined metric to backtest report, such as for example Stoploss level or any value I calculated within my code.
Be grateful if you kindly help.

You're probably not getting any help because you've provided no code so nobody can guess what the problem is. All you said is that you modified a working example and it doesn't do what you want. See this link: How to ask a good question

Where have you calculated the SL and TP values? How have you stored them? How have you retrieved them?

Hello and thanks for your reply.
I believe my question is clear, but here additional explanation:
Consider the following code:

SetCustomBacktestProc( "" );

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 ATR values and display as custom metric
        symbolATR = StaticVarGet( trade.Symbol + "ATR" );
        trade.AddCustomMetric( "Entry ATR", Lookup( symbolATR, trade.EntryDateTime ) );
    }

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

    // generate trade list
    bo.ListTrades( );
}

// your trading system here
Buy = Cross( MACD(), Signal() );
Sell = Cross( Signal(), MACD() );

// assign indicator values to ticker-specific variables
StaticVarSet( Name() + "ATR", ATR( 15 ) );


SL = IIf(  (BuyPrice - LLV(L,6)) < ATR(15), ATR(15), IIf( (BuyPrice - LLV(L,6)) > 5*ATR(15), 5*ATR(15), (BuyPrice - LLV(L,6)) ) );
TP = SL * 2.5; 
  
ApplyStop(stopTypeLoss  , stopModePoint, SL, 1, 0);
ApplyStop(stopTypeProfit, stopModePoint, TP, 1, 0);
  
SetPositionSize( 1, spsShares );

I want to add two columns to the backtest report that list the Sl and TP values of each trade.

Thanks.

Just do the same thing with SL and TP that the example did with ATR: Store the values in a static variable during phase 1, and then retrieve them in Phase 2 (CBT) and use the Lookup value to pluck out the right element from the array.

What you do is incorrect.

To store SL you have to use StaticVarSet() in 1st phase of BT.
StaticVarGet calls the stored static var in 2nd phase of BT.
And static var name has to be type string but not array.

// 1st phase
StaticVarSet( Name() + "SL", SL );

...

//2nd phase
symbolSL = StaticVarGet( trade.Symbol + "SL" );
1 Like

Thank you so much fxshrat for your generous support as always.
and SORRY for my stupid error!

If you have more than 1 position open at a time using this code you get an infinite loop. Or at least I do

Suggest you review these links: