Enumeration loops

for(current_trades) { ... }

A for loop that cycles through all open and pending trades with the current asset and algo, starting with the earliest trade; useful f.i. for closing trades in a FIFO compliant way.

for(last_trades) { ... }

A for loop that cycles backwards through the internal list of open, pending, closed, and cancelled trades with the current asset and algo, starting with the last trade.

for(open_trades) { ... }

A for loop that cycles through all open and pending trades of all assets and algos, starting with the earliest trade.

for(closed_trades) { ... }

A for loop that cycles backwards through all closed trades of all assets and algos, starting with the latest trade.

for(all_trades) { ... }

A for loop that cycles through the internal list of all trades; useful for a detailed trade statistic after the simulation.

for(past_trades) { ... }

A for loop that cycles backwards through the internal list of all trades, starting with the latest trade; useful for equity curve trading.

for(listed_assets) { ... }

A for loop that cycles through all assets from the asset list, in the same order as in the list. This is basically the same as for(i=0; Assets[i]; i++) { Asset = Assets[i]; ... } .

for(used_assets) { ... }

A for loop that cycles through all assets used in the script in alphabetical order, by selecting one after the other.

for(all_algos) { ... }

A for loop that cycles through all asset/algo combinations used in the script, by selecting one component after the other, 

break_trades

break_assets

break_algos

Special break macros for aborting a trade, asset, or algo enumeration loop. Use return_trades for returning from inside a trade loop. All macros are defined in variables.h.

Type:

Macro

Remarks:

Example:

// find all pending trades with the current asset and all algos
int numPending(bool isShort)
{
  int num = 0;
  for(open_trades)
    if(0 == strcmp(Asset,AssetPrev) 
      && TradeIsPending && TradeIsShort == isShort)
        num++;
  return num;
}
 
// sum up the profit/loss of all open trades with the current asset
var val = 0;
for(open_trades)
  if(0 == strcmp(Asset,AssetPrev) && TradeIsOpen && !TradeIsPhantom)
    val += TradeProfit;

// increase the stop of all winning trades slowly over time
for(open_trades) {
if(TradeIsOpen && TradeProfit > 0 && !TradeIsPool)
TradeStopLimit -= 0.02 * TradeStopDiff;
}
// lock 80% profit of all winning trades for(open_trades) { if(TradeIsOpen && !TradeIsPool && TradeProfit > 0) { TradeTrailLock = 0.80; if(TradeIsShort) TradeTrailLimit = max(TradeTrailLimit,TradePriceClose); else TradeTrailLimit = min(TradeTrailLimit,TradePriceClose); } } // sum up open profits of all assets and the current algo var ProfitSum = 0; for(used_assets) { asset(Asset); // select the current component ProfitSum += ProfitOpen; }

See also:

bar, enterLong/Short, trade variables, results, loop

 

► latest version online