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:
- The above enumeration loops are
macros defined in include\variables.h.
Unlike a normal for loop, enumeration loops cannot be nested, and they must not be
aborted by break or
return statements. The break_trades,
break_assets, break_algos
macros can be used to abort these loops. The asset list can alternatively be enumerated,
with no restrictions, with a normal loop
like this: for(i=0; Assets[i]; i++) { asset(Assets[i]); ... }
).
- The predefined Itor variable starts at 0
and is incremented at any loop. It can be used for counting cycles or
indexing arrays.
- The asset that was selected at loop begin is stored in
AssetPrev and automatically re-selected after the
end of the loop.
- Trades are sorted in the order of entering, either forwards or
backwards dependent on loop type.
- The code inside a for(...trades) loop sets the
ThisTrade pointer to the loop trade and has access to all
its trade variables.
When called inside a TMF, all trade variables
inside the enumeration loop refer to the current loop trade, while outside the loop they refer to the trade of the TMF. All asset specific variables
in the trade loop - Asset, Spread, etc. - are set to the
trade asset. Algo specific variables (f.i. AlgoVar)
and component specific variables (f.i. trade statistics) are
not set
up in the loop; if this is desired, call algo(TradeAlgo) inside
a trade loop.
- Asset loops select the asset name and parameters, but don't initialize the asset
or load its history. Call asset(Asset) and/or algo(...) for
evaluating
component specific trade statistics in the loop.
- Use an if(TradeIsPending),
if(TradeIsOpen), or if(TradeIsClosed) condition,
or a combination of them, to filter out the desired trades in a trade
enumeration loop. When virtual hedging is used, open_trades
includes phantom as well as pool trades. Make sure to distinguish them with
if(TradeIsPool). You don't want to modify pool trades.
- Enumeration loops must normally not modify themselves. For instance, a
loop over all open trades must not change the number of open trades.
As an exception, a new trade can be opened inside a trade enumeration loop.
But since this will add a new cycle to that loop, be careful not to create an endless loop this way.
It is also allowed to close the current trade in the loop by
exitTrade(ThisTrade).
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