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 open, pending, and closed 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 all open, pending, and closed trades, starting with the earliest trade; useful for a detailed trade statistic after the simulation.

for(past_trades) { ... }

A for loop that cycles backwards through all open, pending, and closed trades, starting with the latest trade; useful for equity curve trading.

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(listed_assets) { ... }

A for loop that cycles through all assets from the asset list, in the same order as in the list. Does not initialize the asset, but selects its parameters and name. Call asset(Asset) for initializing the asset and loading its history.

for(all_algos) { ... }

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

break_trades

A special break macro for aborting the trade enumeration loops.

Type:

Macro

Remarks:

Example:

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

// increase the stop of all winning trades slowly over time
for(open_trades) {
if(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); } }

See also:

bar, enterLong/Short, trade variables, results

 

► latest version online