Van Tharp`s R-multiple column in the backtest report

Hello, group members,
I am trying to modify a code I have found here - How to add user-defined metrics to backtest/optimization report

I want to use Example 3 and add Van Tharps R-multiple column in the backtest report. The example from the link works when the Stop Loss is % of the entry price and my desire is to make the code work when the SL is defined by ATR from the previous bar. I know that I have to change lines 19 and 20 but I dont know do it. Can somebody help me?

SetCustomBacktestProc("");
// original Stop Loss
//MaxLossPercentStop = 10; // 10% max. loss stop
StopLossAmount = Ref(ATR(20),-1); // my version of Stop Loss

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

    bo.Backtest(1); 

   SumProfitPerRisk = 0;
   NumTrades = 0;

   for( trade = bo.GetFirstTrade(); trade; trade = bo.GetNextTrade() )
   {
     

       Risk = StopLossAmount; // is that correct?
       RMultiple = // I need help here

       trade.AddCustomMetric("Initial risk $", Risk  );
       trade.AddCustomMetric("R-Multiple", RMultiple  );

       SumProfitPerRisk = SumProfitPerRisk + RMultiple;
       NumTrades++;
   }

    expectancy3 = SumProfitPerRisk / NumTrades;

    bo.AddCustomMetric( "Expectancy (per risk)", expectancy3 );

    bo.ListTrades();

Read here

Use Ref() function for previous bar ATR.

Thank you for your suggestion. How can I make ATR to be displayed in the Results List with 5 digits after the decimal point instead of 2 as it is now?

See Custom Backtester.

trade.AddCustomMetric("ATR", 'Your ATR Value', 5);

How I can unite the code from the link you sent me with the code from the link I sent? Now I am getting ATR, also I am getting R-multiple but could not get R-multiple based on ATR as a stop loss.

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.