assetHistory(string Name, int Mode): int

Loads price history from predefined or user defined online price sources, or from a broker's price server. Stores the data in a dataset or in a .t6 or .t1 file in the History folder.

Parameters:

Name

Asset symbol or code of 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 currently selected broker (Zorro S required).
FROM_SOURCE
- download data from a user defined online source (see assetSource; Zorro S required).
FROM_GOOGLE
- download daily (D1) data from Google Finance.
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_YAHOO - download daily (D1) data from Yahoo Finance ('backdoor' method).
FROM_IEX - download daily (D1) data provided by IEX Cloud.
FROM_BITTREX - download M1, H1, or D1 cryptocurrency data from Bittrex (Zorro S required).
FROM_CRYPTOC - download M1, H1, or D1 cryptocurrency data from CryptoCompare (Zorro S required).
 
+1 - download one-minute (M1) price data (all brokers, Bittrex, CryptoCompare)..
+2 - download H1 data (Bittrex, CryptoCompare).
+3 - download daily (D1) price data (all online sources, and IB).
+UNADJUSTED - download unadjusted prices (some sources only; see remarks below).
+OVERRIDE - download price data even when history is up to date, i.e. newer than 12 hours.
+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 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
function run()
{
  NumYears = 2;
  while(loop(Assets))
    assetHistory(Loop1,1);
  quit();
}

// 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 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 prices from Coin.io
var Date = dmy(20190101), EndDate = dmy(20200101);
for(; Date < EndDate; Date += 1.) {
  string Start = strdate("%Y-%m-%dT00:00:00",Date),
    End = strdate("%Y-%m-%dT00:00:00",Date+1.);
  assetSource("BTCUSD_2020.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