Hello,

My function below is part of a program that compiles and runs. The core of the program is in the function below. The issue is that the variable named "PNL" below as well as "CumLoss" and "CumProfit" don't reset to 0 when the loop "L" restarts. So the output in the output file ends up being a cumulative PNL and cumulative CumLoss and CumProfit instead of the PNL for each individual run of Loop "L".

So the output looks like this:

262.75 60.7542 0.832699 1.28905

735.5 61.9823 0.875953 1.42811

1074.5 62.2238 0.857014 1.41165

Focus on the first column above only, which is the PNL column. What it should look like is this:

262.75

472.75 (which is 735.5 - 262.75)

339 (which is 1074.5 - 735.5)

So as you can see the current output is cumulative, instead of outputting each individual run of Loop L (I've set to 3 loops only here to simplify)

Any ideas what I've got wrong in the code below?

Thanks much!

TR

```
int BackTest (PriceList& pl, std::vector <Metrics>& R, std::vector <unsigned>& ra, std::vector <unsigned>& h )
{
for( size_t i = 1; i < pl.size(); ++ i )
{
if (random conditions)
{
h.push_back (pl[i].Index);
}
}
int WinCount = 0;
int LossCount = 0;
double CumLoss = 0;
double CumProfit = 0;
double PNL = 0;
for (size_t L = 0; L < 3; ++L) // Monte Carlo loop starts
{
// set variables to 0 for each loop
PNL = 0;
CumLoss = 0;
CumProfit = 0;
//randomize the vector of prices
std::srand(std::time(0));
std::random_shuffle (h.begin(), h.end());
for (size_t r = 0; r < 717; ++ r )
{
ra.push_back (h[r]);
std::sort (ra.begin(), ra.end() );
}
for (std::size_t i=1; i< ra.size(); ++i)
{
unsigned r = ra[i];
for (unsigned z=1; z<6; ++z)
{
if ( r+z < pl.size() )
{
double SEntry = (pl[r-1].Low - 0.5);
double StopPrice = (pl[r-1].High + 0.5);
if (pl[r+z].High >= StopPrice)
{
++LossCount;
double Loss = SEntry - StopPrice;
CumLoss+= Loss;
break;
}
if(z!=5)
{
if((pl[r+z].High < StopPrice) && (abs(SEntry - pl[r+z].Close) >= 0) )
{
++z;
}
}
else // else 1
{
if((pl[r+z].High < StopPrice) && (abs(SEntry - pl[r+z].Close) >= 0) )
{
++WinCount;
double Profit = abs(SEntry - pl[r+z].Close);
CumProfit+= Profit;
break;
}
else // else 2
{
if( (pl[r+z].High > StopPrice) )
{
++LossCount;
double Loss = SEntry - StopPrice;
CumLoss+= Loss;
break;
}
} //else2
} //else1
} //if r+z
} //for (unsigned z=1; z<6; ++z)
} // for (std::size_t i=1; i< ra.size(); ++i)
PNL = CumLoss + CumProfit;
double numerator = (double) WinCount + LossCount;
double WinLoss = (double) WinCount / numerator * 100;
double WL = (double) WinCount / numerator;
double AvgWin = CumProfit / WinCount;
double AvgLoss = abs(CumLoss / LossCount);
double Payoff = AvgWin/AvgLoss;
double ProfitFactor =( (WL*AvgWin)/ ((abs(1-WL))*AvgLoss) );
std::cout << PNL << std::endl;
std::cout << CumLoss << std::endl;
std::cout << CumProfit << std::endl;
R.push_back (Metrics ( PNL, WinLoss, Payoff, ProfitFactor));
std::ofstream fout( "BackTestResults.txt");
for( size_t i = 0; i < R.size(); ++ i )
{
fout << R[i].PNL << "\t" << R[i].WinLoss << "\t" << R[i].Payoff << "\t" << R[i].ProfitFactor << std::endl;
}
} //big Loop ends (intended for monte carlo sim)
return 0;
}
```