How to use the profit obtained in the function?

function MyCBTColumns() {
/// example to user question being asked here
/// @link How can I write my own "Cum profit" in CBT?
global bo;

sumprofit = 0; // initialize cum. profit


// Iterating through closed trades
for ( trade = bo.GetFirstTrade(); trade; trade = bo.GetNextTrade() ) {      
	profit  = trade.GetProfit(); // get P/L of closed trade
	sumprofit += profit;  // sum up P/L 
}

// Iterating through open trades
for (trade = bo.GetFirstOpenPos(); trade; trade = bo.GetNextOpenPos()) {      
	profit  = trade.GetProfit(); // get P/L of open trade
	sumprofit += profit;  // sum up P/L 
}

return sumprofit;

}

if ( Status( “action” ) == actionPortfolio ) {
bo = GetBacktesterObject(); // Retrieving the interface to portfolio backtester
bo.Backtest( 1 ); // Set to 1 to turn off automatic calling of trade list so per-trade variables can be added

MyCBTColumns();// My column(s) output function
bo.ListTrades(); // calling the trade list procedure after adding the variables wanted

}

RiskPorContrato = 2 * ATR(20);
PositionRiskEquity = Optimize (“PositionRiskEquity”, 0.012, 1, 100, 1);
PositionRiskLucro = Optimize (“PositionRiskLucro”, 0.008, 1, 100, 1);
PctSize = (PositionRiskEquity100000 + (PositionRiskLucrosumprofit))/( RiskPorContrato * PointValue);
SetPositionSize( PctSize, spsShares );

I need to use the variable sumprofit to determine o PctSize.
Any suggestion? Very grateful…

Besides the fact that your code is totally all over the place, this will probably do what you intend:

The snippet below is intended for you to trade on the OPEN with TRADEDELAYS set to 1.

RiskPerContract = ATR( 20 );
PctSize =    PositionRisk * Open / (RiskPerContract);//

SetOption("maxopenpositions",1000);//
SetPositionSize( PctSize, spsPercentOfEquity );
SetOption("ActivateStopsImmediately",true);//
SetTradeDelays(1,1,1,1);// 
BuyPrice=O; 
SellPrice=O;