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; or an empty string "" for creating a dummy asset. 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 sets up its parameters. Must be called in the initial run of the strategy before the asset can be selected. Assets contained in the asset list are automatically added.
||Name of the asset
||Optional asset parameters as described under asset list. When omitted, they are set to default values.
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 .csv file. The path can be omitted for asset lists in the History folder.
Number of the loaded assets, or 0 when no assets were loaded.
assetType (string Name) : int
Attempts to determine the type of an asset from its name; f.i. if the 3-letter abbreviation of a currency appears in the asset name, it is identified as Forex.
0 when the type can not be identified, f.i. stock; otherwise FOREX (1), INDEX (2), CMDTY (3), or BOND (5).
- 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 correctly 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, the gap is filled with price data from the previous bar.
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.
- 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:.
- For selecting all assets of the asset list in a loop, use while(asset(loop(Assets))). For counting them, 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, follow the step by step description under Data Import.
- 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 MT4, need a long time after subscribing an asset before prices are available.
// 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