assetHistory(string Name, int Mode): int

Loads price history either from the currently selected broker or data feed, or from a specified online price source in CSV or JSON format. Stores the data in a dataset or in a .t6 or .t1 file in the History folder. Online price sources can be user defined; some popular sources such as Quandl, Stooq etc. are predefined.

Parameters:

Name

Asset symbol or code specifying the price source, or the asset name from the asset list, or 0 for the current asset symbol.

Mode

0 - download tick (T1) data from the selected broker (Zorro S required).
1
- download one-minute (M1) price data (all brokers, plus Bittrex, CryptoCompare)..
2 - download hourly (H1) data (Bittrex, CryptoCompare).
3 - download daily (D1) data (all online sources, and IB).
4 - download data in LookBackResolution (all brokers).
8 - download nothing, but get the data from History\history.csv (for test purposes).

+FROM_SOURCE - download data from a user defined online source (see assetSource; Zorro S required).
+FROM_GOOGLE
- download daily (D1) data from Google Finance (currently unavailable).
+FROM_QUANDL - download daily (D1) data from Quandl (Zorro S and Quandl key required).
+FROM_AV - download daily (D1) data from AlphaVantage.
+FROM_STOOQ - download daily (D1) data from Stooq.
+FROM_EOD - download daily (D1) data from EOD.
+FROM_YAHOO
- download daily (D1) data from Yahoo Finance.
+FROM_IEX - download daily (D1) data from IEX Cloud.
+FROM_BITTREX - download M1, H1, or D1 data from Bittrex (Zorro S required).
+FROM_CRYPTOC - download M1, H1, or D1 data from CryptoCompare (Zorro S required).
 (If no online source is set, the data is downloaded from the selected broker).

+UNADJUSTED - download unadjusted prices (some sources only; see remarks below).
+OVERRIDE - download price data even when history is up to date, i.e. not older than half the data period.
+IMMEDIATE - download at least one tick of recent price data; for getting a live price from an online source.
+VOLATILE - store the data in a temporary dataset whose handle is returned (only D1 data or online sources).
+FOR_ASSET - store the file under the name of the current asset selected with asset or assetAdd.

Returns:

0 when nothing was downloaded due to an error or because the data was up to date. Otherwise a nonzero value or the dataset handle.
 

assetSource(string Filename, string URL, string Header, string Body, string Format)

Sets up all parameters of a price source website or REST API for a subsequent assetHistory(...,FROM_SOURCE) call. See examples.

Parameters:

Filename

Name of the file for storing or appending the downloaded data, or 0 for using the default symbol.

URL

Target URL for the data request, including request parameters.

Header

Request header, or 0 if no header is required. Some online sources requre an API key or access troken in the header.

Body

Request body for a HTTP POST request, or 0 for a HTTP GET request.

Format

Format string for converting the received data, as described under dataParse. If the format begins with '[', JSON data format is assumed, otherwise CSV format. If 0 or empty, the data is not converted, but stored under history.csv or history.json.

Remarks:

Example:

// Update M1 price history of all assets from selected broker
function main()
{
  NumYears = 2;
  while(loop(Assets))
    assetHistory(Loop1,1);
}

// Download D1 prices from Quandl
string Name;
while(Name = loop("AAPL","MSFT","GOOGL"))
  assetHistory(strf("WIKI/%s",Name),FROM_QUANDL);

// Download and plot Bitcoin prices from Bitfinex
function run()
{
  BarPeriod = 1440;
  assetHistory("BITFINEX/BTCUSD",FROM_QUANDL);
  assetAdd("BTCUSD");
  asset("BTCUSD");
  set(PLOTNOW);
}

// Download unadjusted SPY data and store it in "SPYuna.t6"
History = "*una.t6";
assetHistory("SPY",FROM_STOOQ|UNADJUSTED);

// Download AAPL prices from Stooq, using assetSource
assetSource("AAPL.t6",
"https://stooq.pl/q/d/l/?s=AAPL.US&d1=20000101&d2=20201024&i=d",
0,0,"+%Y-%m-%d,f3,f1,f2,f4,f6");
assetHistory(0,FROM_SOURCE);

// Download Bitcoin OHLC prices from Coin.io
int Month, Year = 2019, MinutesPerMonth = 31*1440;
for(Month = 1; Month <= 12; Month++) {
  string URL = strf("https://rest.coinapi.io/v1/ohlcv/BTC/USD/history?period_id=1MIN&time_start=%04d-%02d-01T00:00:00&limit=%d",
    Year,Month,MinutesPerMonth);
  string Name = strf("BTCUSD_%04d.t6",Year);
  assetSource(Name,URL,
    "X-CoinAPI-Key: my-coin-api-key",0,
    "[,time_period_end,%Y-%m-%dT%H:%M:%SZ,price_high,price_low,price_open,price_close,,volume_traded");
  assetHistory(0,FROM_SOURCE);
}

// Download Bitcoin BBO ticks from Kraken via Coin.io
var From = dmy(20190101), To = dmy(20200101);
for(; From < To; From += 1.) {
  string Start = strdate("%Y-%m-%dT00:00:00",From),
    End = strdate("%Y-%m-%dT00:00:00",From+1.);
  assetSource("BTCUSD_2019.t2",
    strf("https://rest.coinapi.io/v1/quotes/KRAKEN_SPOT_BTC_USD/history?time_start=%s&time_end=%s&limit=100000",
      Start,End),
    "X-CoinAPI-Key: my-coin-api-key",0,
    "[,time_exchange,%Y-%m-%dT%H:%M:%SZ,ask_price,ask_size,bid_price,bid_size");
  assetHistory(0,FROM_SOURCE);
}

See also:

price, asset, UpdateDays, loadStatus, price history, dataDownload

► latest version online