# Combining two Custom Backtest Procedures

Hello,

I have the following two custom backtest procedures, the first one is related with additional metrics, the second one with scaling out positions. They work fine separately, but when I try to combine them in the same code, something doesn't work properly, I get weird results.

I have tried to mix them in several ways, but I was unable to find the correct one.

I guess the solution is quite simple for many expert users in this forum, but I can't figure it out.

I'll appreciate if someone would help me with this.

Thanks on beforehand.

Regards.

1)

``````SetCustomBacktestProc("");
if( Status("action") == actionPortfolio )
{
bo = GetBacktesterObject();
bo.Backtest();
st = bo.GetPerformanceStats(0);
bo.AddCustomMetric("Score", (st.GetValue("CAR") * (1 + (st.GetValue("MaxSystemDrawdownPercent")/100))^1.5)*(100/15.000));
}
``````

2)

``````EachPosPercent = 10;

SetOption("UseCustomBacktestProc", True );

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

bo.PreProcess();

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

for(Pos = bo.GetFirstOpenPos(); Pos; Pos = bo.GetNextOpenPos() )
{
Posval = Pos.GetPositionValue();
Diff   = Posval - 0.01 * EachPosPercent * CurEquity;
Price  = Pos.GetPrice( bar, "C" );
if(Diff != 0 AND
Diff       > 0.05 * CurEquity AND
abs(Diff ) > Price )
{
bo.ScaleTrade(bar, Pos.Symbol, Diff < 0, Price, abs(Diff ) );
}}}
bo.PostProcess();}
``````

Hello,

Finally, I could figure out how to solve the problem, so I'm going to answer myself, just in case someone else would have a similar doubt.

The code works fine as it is shown below

Regards

``````SetCustomBacktestProc("");

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

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

for(Pos = bo.GetFirstOpenPos(); Pos; Pos = bo.GetNextOpenPos() )
{
Posval = Pos.GetPositionValue();
Diff   = Posval - 0.01 * InitialPosition * CurEquity;
Price  = Pos.GetPrice( bar, "C" );
if(Diff != 0 AND Diff > (ScaleOutSignal/100) * CurEquity AND abs(Diff) > Price)
{
bo.ScaleTrade(bar, Pos.Symbol, Diff < 0, Price, abs(Diff ));
}
}
}
bo.PostProcess();
st = bo.GetPerformanceStats(0);