Custom metrics: number of backtested bars and reference to equity curve

Hello,

I have two doubts related with custom metrics:

  1. How to obtain the number of backtested bars. For example, in the following code I assume that the backtest goes through a period of 120 bars (NumberOfBars = 120), I would like to have that value calculated depending on the range set in the Analysis Window.
// --- Custom Backtest ---

SetCustomBacktestProc("");
if( Status("action") == actionPortfolio )
{
    bo = GetBacktesterObject();
    bo.Backtest();
    st = bo.GetPerformanceStats(0); 
    AdjustedNumberOfWins   = st.GetValue("WinnersQty")  - sqrt(st.GetValue("WinnersQty"));
    AdjustedNumberOFLosses = st.GetValue("LosersQty")  + sqrt(st.GetValue("LosersQty"));   
    NumberOfBars = 120;
    bo.AddCustomMetric("PROM", IIf(st.GetValue("LosersQty") == 0, (st.GetValue("WinnersAvgProfit") * AdjustedNumberOfWins ), (st.GetValue("WinnersAvgProfit") * AdjustedNumberOFLosses) + (st.GetValue("LosersAvgLoss") * AdjustedNumberOFLosses))/NumberOfBars); 
 }
  1. Is it possible to use the final equity curve from a backtest to make further calculations at the end of the same backtest? For example, I would like to code a function that calculates the correlation between the backtest equity curve and other ticker. I do not know how to start, what I am trying to do is something like the following code (it does not work, I use it just for the explanation):
 // --- Custom Backtest ---

SetCustomBacktestProc("");
if( Status("action") == actionPortfolio )
{
    bo = GetBacktesterObject();
    bo.Backtest();
    AddToComposite(bo.EquityArray, "~~~BacktestEquity", "X", atcFlagDeleteValues | atcFlagEnableInPortfolio); 
    EquityCorrelation = Correlation(Foreign("~~~BacktestEquity", "Close"), Foreign("~~~TradedS&P500", "Close"), 120);
    bo.AddCustomMetric("Corr", EquityCorrelation);
 }

I have made some reasearch in the web and the forum, but I can't figure out how to code it yet. I should appreciate it if someone would help me with that.

By the way, just in case someone has read "The evaluation and Optimization of Trading Strategies" by Robert Pardo, I am trying to code the CECPP and PROM Methods, from Chapter 9, to use them as optimization targets. I recommend the read.

Regards.

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