asset (string Name) : int
Selects an asset, and opens its price history in the initial run. Price and trade functions, and asset related variables (Spread, Symbol, AssetVar etc.) are automatically adapted to the new asset. This way different assets can be compared for arbitraging or correlation indicators, or a script can trade multiple assets simultaneously. For loading price history, the asset function must be called in the initial run of the strategy.
||The name of the asset, as in the [Asset] selector.
An empty string "" creates a dummy asset,
a name beginning with '#' prevents loading price history for
test purposes. Up to 15 characters, with no blanks and no special characters except for slash '/' and underline '_'.
0 when the Name string is NULL or empty, or when no prices are available for the asset; otherwise nonzero.
asset("EUR/USD"); switches to the EUR/USD currency.
assetAdd (string Name, var Price, var Spread, var RollLong, var RollShort, var PipVal, var PipCost, var MarginCost, var Leverage, var LotAmount, var Commission, string Symbol)
Adds a new asset to the current strategy and selects it, or modifies parameters of an existing asset.
The asset is also added to the [Asset] scrollbox. To be called in the initial run of the strategy. Assets contained in the asset list are automatically added.
For creating a dummy asset for test purposes, set Name to
"DUMMY" - this generates artificial bars with a flat price
history. Can also be used for selecting an asset before loading its price
history, f.i. when asset specific parameters like
Centage must be set that affect the history download.
||Name of the asset
||Optional asset parameters as described under asset list. When at 0, the parameter is not changed.
assetList (string Filename): int
Loads a new asset list in the first run of the script, and adds all assets to the [Asset] scrollbox and to the current strategy. Must be called before any of the new assets can be selected.
||File name of the asset list, f.i. "AssetsIB". The
.csv extension and the path can be omitted for asset lists in the History folder.
Number of loaded assets, or 0 when no assets were loaded.
The number of assets is also available through
assetType (string Name) : int
Attempts to determine the type of an asset from its name. If the
name begins with the 3-letter abbreviation of a currency, it is identified as Forex;
if it ends with a 2- or 3-digt number, it is identified as an index.
0 when the type can not be identified; otherwise FOREX
(1) or INDEX (2).
- If the asset function is used, it must be called in the first run (INITRUN) of the script. All variables and flags that affect the price data array, such as BarPeriod, BarZone, LookBack, Detrend, StartDate, EndDate, TICKS, Weekend, UpdateDays, AssetList, History etc. must be set
_before_ calling asset. Otherwise the simulation period can not be set up, and the script will produce an Error 030 message.
- Calling asset() by script is not exactly the same as selecting the asset with the Asset scroll box. If the script contains no asset call, the scroll box asset is selected
_after_ the first run, and its name is appended to the training files for being able to train different assets separately.
- The order of asset() calls does matter. The first asset in a script is used to create the array of bars. Gaps in the price history of the first asset are thus reflected in the price data of all further assets. Price histories have no data for periods when the asset is not traded, for instance during the weekend, or at night for assets that are only traded during business hours. Therefore set first the asset with the most complete price history (for instance, a currency pair that is traded 24 hours). When a subsequent asset has a gap where the first asset has none
- for instance, outside market hours - the gap is filled with price data from the previous bar.
This produces a different price curve and can cause indicators to
behave differently in a portfolio. For avoiding such an effect,
don't combine assets with different market hours, or skip
out-of-market periods with the TimeFrame
mechanism or by not shifting series.
- Every asset() call switches the trade statistics parameters and the OptimalF factors to the current statistics and factors of the selected asset. All asset parameters are loaded from the asset list. If the asset is not found in the list, an error message will be printed and defaults are substituted for the asset parameters.
- In [Trade] mode, asset() causes the asset prices to be downloaded from the broker. If this is not desired or if the broker does not support that asset, add a '#' to the Symbol field in the asset list, or add an external price source such as QUANDL:, GOOGLE:, or AV:.
- If a bar does not receive any price ticks during trading,
for instance because of an Internet connection problem, a
holiday, or outside business hours, the bar is also filled with
price data from the previous bar. This ensures for portfolio
systems the same behavior in live trading as in the simulation
with historic price data.
- For selecting all assets of the asset list in a loop, use while(asset(loop(Assets))). For counting them
in a variable int N, use for(N = 0; Assets[N]; N++).
- The trading time of an asset can be set up with AssetZone and AssetFrame.
- The current asset name is stored in the Asset string; its full symbol, including counter currency and exchange, is stored in the Symbol string. Call asset(Asset) for loading the asset selected by the Scrollbox already in the INITRUN. Otherwise it's automatically loaded after the INITRUN.
- For running the same strategy with multiple assets, use the loop function; for running the strategy with all available assets from the asset list, use loop(Assets). The Assets array contains the names of all available assets.
- Artificial assets can be created by combining the prices from several real assets (see example).
- By passing an empty string (asset("")), a dummy asset is created with all prices at 1. This is useful when a real asset is not needed, f.i. for testing filters or indicators with artificial price curves.
- When loading price data, the prices are checked for plausibility. If a bar has invalid data, such as extreme outliers or prices at zero, it is automatically corrected. Setting Detrend = 16; before calling asset() prevents that asset and price data is checked and outliers are removed.
- If only a single asset is selected in the script, the [Asset] scrollbox is automatically set to that asset. If multiple assets are selected, the [Asset] scrollbox is unchanged and can be used to determine the price curve in the chart.
- For adding a new asset to the available asset set, see the description under Asset List.
- Assets must be subscribed before their prices are available. The asset function subscribes the asset automatically, but some brokers have a limit to the number of subscribed assets. Some platforms, for instance MTR4, need a long time after subscribing an asset before prices are available.
- Any asset requires memory. The memory requirement in bytes
can be estimated with the formula Years / BarPeriod * 15
MB, where Years is the number of
backtest years (or 1 for live trading). The LEAN
flag reduces the memory requirement by about 25%, the
TICKS flag increases it by 32 bytes per historical data
// trade multiple strategies and assets in a single script
BarPeriod = 240;
StartDate = 2010;
set(TICKS); // set relevant variables and flags before calling asset()
// call different strategy functions with different assets
// For adding a new asset to the [Assets] scrollbox,
// click [Trade] with the script below.
// Then edit History\AssetsFix.csv and copy the line
// beginning with "Copper" over from History\Assets.csv.
asset("Copper"); // add "Copper" to assets.csv
// Basket trading - generate a snythetic asset "USD"
// combined from the USD value of EUR, GBP, and AUD
var p = 0;
asset("GBP/USD"); p += price();
asset("AUD/USD"); p += price();
asset("EUR/USD"); p += price();
// basket trade function with stop limit
int tradeUSD(var StopUSD)
if((TradeIsLong && priceUSD() <= StopUSD)
or (TradeIsShort && priceUSD() >= StopUSD))
return 1; // exit the trade
else return 0; // continue the trade
// open a trade with the synthetic asset and a stop loss
void enterLongUSD(var StopDistUSD)
var StopUSD = priceUSD()-StopDistUSD;
void enterShortUSD(var StopDistUSD)
var StopUSD = priceUSD()+StopDistUSD;
// plot a price curve of the synthetic asset
// (the plot command is linked to the last used asset -
// so "EUR/USD" must be selected in the scrollbox)
enterLong/Short, loop, algo, Asset, AssetZone, AssetVar, Detrend, assetHistory, price