Historical Prices

Historical market prices usually come in the form of OHLCV (i.e. open, high, low, close, volume). The granularity and amount of historical data will vary by provider.

Method historical()

The historical() function is located under a specific class for each asset type.

  • Common parameters have been standardized across all sources, start_date, end_date, interval.

  • The default end_date is the current date.

  • The default start_date is 60 days before the end_date.

  • The default interval is 1d.

  • The intervals follow the pattern as shown below. Though not all sources support all intervals.

    • 1m = one minute

    • 1h = one hour

    • 1d = one day

    • 1w = one week

    • 1mo = one month

Retrieve historical prices

For example, we will get historical prices of the stock ticker VNM.

[1]:
from vietfin import vf
import pandas as pd

vf.equity.price.historical(symbol="vnm")
end_date not provided, set to 2024-02-27
start_date not provided, set to 2023-12-29
Retrieved 36 historical price data point for symbol VNM.
[1]:
VfObject

results: [{'date': datetime.date(2023, 12, 29), 'open': 68700.0, 'high': 68700.0, '...
provider: tcbs
extra: {'command_run_at': '2024-02-27T01:10:17.464843+00:00', 'symbol': 'VNM', 'rec...
raw_data: [{'ticker': 'VNM', 'data': [{'open': 67795.0, 'high': 67894.0, 'low': 673...

A mutual fund with symbol like VESAF is also supported.

[4]:
vf.funds.historical(symbol="vesaf")
end_date not provided, set to 2024-02-24
start_date not provided, set to 2023-12-26
Retrieved 38 daily NAV data point for fund VESAF.
[4]:
VfObject

results: [{'date': '2023-12-26', 'nav_per_share': 25684.33, 'fund_id': 23}, {'date'...
provider: fmarket
extra: {'command_run_at': '2024-02-24T03:39:32.781606+00:00', 'symbol': 'VESAF', 'r...
raw_data: {'status': 200, 'code': 200, 'time': 1708745972441, 'message': 'Thành côn...

An ETF symbol like E1VFVN30 works as well.

[3]:
vf.etf.historical(symbol="e1vfvn30")
end_date not provided, set to 2024-02-24
start_date not provided, set to 2023-12-26
Retrieved 38 historical price data point for ticker E1VFVN30.
[3]:
VfObject

results: [{'date': datetime.datetime(2023, 12, 26, 0, 0, tzinfo=TzInfo(UTC)), 'open...
provider: tcbs
extra: {'command_run_at': '2024-02-24T03:39:01.152593+00:00', 'symbol': 'E1VFVN30',...
raw_data: [{'ticker': 'E1VFVN30', 'data': [{'open': 18800.0, 'high': 18800.0, 'low'...

Or the symbol of an index like vn30 is also retrievable.

[5]:
vf.index.price.historical(symbol="vn30")
end_date not provided, set to 2024-02-24
start_date not provided, set to 2023-12-26
Retrieved 38 historical price data point for ticker VN30.
[5]:
VfObject

results: [{'date': datetime.datetime(2023, 12, 26, 0, 0, tzinfo=TzInfo(UTC)), 'open...
provider: tcbs
extra: {'command_run_at': '2024-02-24T03:42:41.671788+00:00', 'symbol': 'VN30', 're...
raw_data: [{'ticker': 'VN30', 'data': [{'open': 1085.26, 'high': 1087.34, 'low': 10...

Differences Between Sources

Let’s try to compare the values for daily volume of a stock ticker from several sources, to see the variation between providers.

[2]:
# Collect historical prices of stock ticker "FPT" from three providers, TCBS, SSI and DNSE.
tcbs = vf.equity.price.historical(symbol="fpt", provider="tcbs").to_df()
dnse = vf.equity.price.historical(symbol="fpt", provider="dnse").to_df()
ssi = vf.equity.price.historical(symbol="fpt", provider="ssi").to_df()

# Compare the last 5 rows of "volume" column
compare = pd.DataFrame()
compare["tcbs"] = tcbs["volume"].tail(5)
compare["dnse"] = dnse["volume"].tail(5)
compare["ssi"] = ssi["volume"].tail(5)
compare ["[tcbs-dnse]"] = compare["tcbs"] - compare["dnse"]
compare ["[ssi-dnse]"] = compare["ssi"] - compare["dnse"]

compare
end_date not provided, set to 2024-02-27
start_date not provided, set to 2023-12-29
Retrieved 36 historical price data point for symbol FPT.
end_date not provided, set to 2024-02-27
start_date not provided, set to 2023-12-29
Retrieved 36 historical price data point for symbol FPT.
end_date not provided, set to 2024-02-27
start_date not provided, set to 2023-12-29
Retrieved 36 historical price data for symbol FPT.
[2]:
tcbs dnse ssi [tcbs-dnse] [ssi-dnse]
date
2024-02-20 1848111 1844300 1844300 3811 0
2024-02-21 3141692 3137500 3137500 4192 0
2024-02-22 1722092 1718700 1718700 3392 0
2024-02-23 4108378 4103500 4103500 4878 0
2024-02-26 4472548 4466700 4466700 5848 0