Skip to content

phamdinhkhanh/vnquant

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

63 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

vnquant package

1. Introduction

This project offers comprehensive financial information and advanced visualization tools for the Vietnam stock market to researchers. Specifically, it provides extensive data, including historical prices, finacial, business, and cashflow reports for individual or multiple symbols over the same period. This enables investors to conduct in-depth quantitative analyses and forecasting. Additionally, the available stock prices can be utilized to create visualizations with advanced metrics such as Bollinger Bands and Relative Strength Index (RSI), aiding in identifying optimal buying and selling points.

2. Setting:

2.1. Setup on local machine

This project is in developing process, So it is only distributed on github channel. To install requiring you open the command line and type the below commands:

git clone https://github.com/phamdinhkhanh/vnquant
cd vnquant
python setup.py install

you must install git command line in your computer to run above command.

2.2. Google colab

To use package in google colab, you have to mount point to google drive folder first and setup the same as in local machine. Reference to google colab - vnquant example for detail.

3. Visualization: (0.0.2)

from version 0.0.2 vnquant enable to you visualize stock price from any symbols code at source cafe or vnd or pandas data frame which have OHLC type. OHLC type meaning that your data frame columns is enough ['open', 'high', 'low', 'close'] list. Below is general syntax of visualization function supported on vnquant package.

import vnquant.plot as pl
pl.vnquant_candle_stick(data,
                        title=None,
                        xlab='Date', ylab='Price',
                        start_date=None, end_date=None,
                        colors=['blue', 'red'],
                        width=800, height=600,
                        show_advanced=[],
                        data_source='cafe',
                        **kargs)

Arguments

  • data: is pandas data frame of OHLC type or OHLCV type, or string symbol of any VietNam stock index. in case symbol, data is automatically cloned from open source.
  • title: General title of candle stick chart. If data is a symbol, title is going to be created based on symbol and cloned datetime interval.
  • xlab: x label. Default Date.
  • ylab: y label. Default Price.
  • start_date: start date. Default None. Must to be declared when data is symbol.
  • end_date: end date. Default None. Must to be declared when data is symbol.
  • colors: list colors defines increasing and decreasing color stick candle in order.
  • width: with of plot frame. Default 800px
  • height: height of plot frame. Default 600px
  • show_advanced: list of advanced stock index to show up. Each element belongs to ['volume', 'macd', 'rsi'].
  • data_source: invalid when use symbol intead of data frame. Source to clone data, 'VND' or 'CAFE'.

3.1. Visualization from source VND or CAFE

In this way, you can visualize stock price clone from VND or CAFE source by pass symbol, start_date, end_date into module as below:

from vnquant import plot as plt
plt.vnquant_candle_stick(
    data='VND',
    title='VND symbol from 2019-09-01 to 2019-11-01',
    xlab='Date', ylab='Price',
    start_date='2019-09-01',
    end_date='2019-11-01',
    data_source='CAFE',
    show_advanced=['volume', 'macd', 'rsi'],
    width=1600,
    height=800
)

You can suppress volume by set up show_vol=False. Result as below:

3.2. Visualization from data frame

Data frame must be OHLC or OHLCV type. OHLC type when it includes ['open','high','low','close'] and OHLCV is ['open','high','low','close','volume'] or ['open','high','low','close','volume_match'] and index is DateTime. In case your data frame have columns with the same function, you should accordingly rename its.

from vnquant import plot as plt

plt.vnquant_candle_stick(
    data = data,
    title='Your data',
    ylab='Date', xlab='Price',
    show_advanced=['volume', 'macd', 'rsi']
)

To check whether data_vnd frame is OHLC or OHLCV type you can try:

from vnquant import utils
print(utils._isOHLC(data_vnd))
print(utils._isOHLCV(data_vnd))

Return True mean data frame is adapted types.

4. Clone Stock Prices: (0.1.2)

You can load the prices of one or more stocks in specific time interval according to syntax as below.

import vnquant.data as dt

loader = dt.DataLoader(
  symbols: Union[str, list], 
  start: Optional[Union[str, datetime]]=None,
  end: Optional[Union[str, datetime]]=None, 
  data_source: str='CAFE', 
  minimal: bool=True,
  table_style: str='levels')

For example:

loader = DataLoader(
  symbols='VND', 
  start='2018-01-10', end='2018-02-15', 
  data_source='CAFE', 
  minimal=True, 
  table_style='levels')

loader.download()

Arguments

  • symbols (Union[str, list]): A single stock symbol as a string or multiple stock symbols as a list of strings. The stock symbols regularly include 3 upper case letters except several special index such as: E1VFVN30, FUEVN100 for both data_source cafe and vnd, HNX-INDEX, HNX30-INDEX, UPCOM-INDEX for cafe.
  • start (Optional[Union[str, datetime]], default=None): The start date for the data. Can be a string in the format 'YYYY-MM-DD' or a datetime object.
  • end (Optional[Union[str, datetime]], default=None): The end date for the data. Can be a string in the format 'YYYY-MM-DD' or a datetime object.
  • data_source (str, default='CAFE'): The data source to be used for downloading stock data. Currently supports 'CAFE' and 'VND'.
  • minimal (bool, default=True): If True, returns a minimal set of columns. If False, returns all available columns.
  • table_style (str, default='levels'): The style of the returned table. Options are 'levels', 'prefix', and 'stack'.
  • 'levels': Returns the DataFrame with multi-level colums of symbols and list of basic arguments like ['high', 'low', 'open', 'close', 'adjust', 'volume', 'value']
  • 'prefix': Adds the stock symbol as a prefix to each column name.
  • 'stack': Returns the DataFrame and add one column 'code' to clarify each record belong to what stock symbol.

4.1. Clone one stock:

There are three return formats supported in the latest version 0.1.2.

  • Multiple-level: It will return a table with mutilple column indexes with two main levels Arguments (including many basical stock indexes) and Symbols (including list of stock codes).
import vnquant.data as dt
loader = dt.DataLoader('VND', '2018-02-02','2018-04-02')
data = loader.download()
data.head()
Attributes high low open close avg volume
Symbols VND VND VND VND VND VND
date
2018-02-02 28.95 27.60 28.5 28.95 28.28 1700670.0
2018-02-05 28.45 26.95 28.1 26.95 27.68 2150120.0
2018-02-06 26.95 25.10 25.1 26.40 25.25 3129690.0
2018-02-07 28.20 27.50 27.5 28.20 27.99 1985120.0
2018-02-08 29.20 27.70 28.0 28.00 28.47 943260.0
  • Prefix: The stock code is appended before each index to become a column name.
import vnquant.data as dt
loader = dt.DataLoader(['VND', 'FPT'], '2018-02-02','2018-04-02', table_style='prefix')
data = loader.download()
data.head()
VND_code FPT_code VND_high FPT_high VND_low FPT_low VND_open FPT_open VND_close FPT_close VND_adjust FPT_adjust VND_volume_match FPT_volume_match VND_value_match FPT_value_match
date
2018-04-02 VND FPT 29.80 61.7 29.10 61.0 29.10 61.5 29.55 61.5 6.52 21.98 2141980.0 2194820.0 6.320100e+10 1.347410e+11
2018-03-30 VND FPT 29.50 61.3 28.75 59.4 29.00 59.6 29.05 60.7 6.41 21.69 1688000.0 2434830.0 4.925300e+10 1.474940e+11
2018-03-29 VND FPT 29.00 59.7 28.25 59.0 28.50 59.4 29.00 59.5 6.40 21.26 1294580.0 827280.0 3.717900e+10 4.915900e+10
2018-03-28 VND FPT 28.65 59.4 27.60 58.8 27.75 59.1 28.65 58.9 6.32 21.05 1429910.0 660440.0 4.012300e+10 3.901500e+10
2018-03-27 VND FPT 28.55 60.1 27.75 59.0 28.55 59.9 28.00 59.5 6.18 21.26 2589800.0 993260.0 7.289400e+10 5.916600e+10
  • Stack: It will add one more column named code to demonstrate the stock symbols.
import vnquant.data as dt
loader = dt.DataLoader(['VND', 'FPT'], '2018-02-02','2018-04-02', table_style='stack')
data = loader.download()
data.head(4)
code high low open close adjust volume_match value_match
date
2018-04-02 FPT 61.7 61.00 61.5 61.50 21.98 2194820.0 1.347410e+11
2018-04-02 VND 29.8 29.10 29.1 29.55 6.52 2141980.0 6.320100e+10
2018-03-30 FPT 61.3 59.40 59.6 60.70 21.69 2434830.0 1.474940e+11
2018-03-30 VND 29.5 28.75 29.0 29.05 6.41 1688000.0 4.925300e+10

4.2. Clone more stocks

We need to set up symbols as a list.

loader = dt.DataLoader(symbols=["VND", "VCB"], start="2018-01-10", end="2018-02-15", minimal=True, data_source="cafe")
data = loader.download()
data.head()
Attributes high low open close avg volume
Symbols VND VCB VND VCB VND VCB VND VCB VND VCB VND VCB
date
2018-01-10 27.75 59.2 27.10 57.3 27.55 58.3 27.50 58.0 27.52 58.08 1466780.0 2842830.0
2018-01-11 27.50 58.8 26.80 57.2 27.30 57.5 27.20 58.8 27.21 58.04 1260720.0 1766240.0
2018-01-12 28.20 59.4 27.35 58.0 27.45 58.8 27.60 58.0 27.76 58.63 1730170.0 2525840.0
2018-01-15 28.40 60.0 27.35 57.0 27.60 58.0 28.25 60.0 28.11 58.76 1273740.0 2217420.0
2018-01-16 28.40 60.3 27.90 58.8 28.10 59.3 28.25 60.0 28.14 59.64 1163350.0 2218380.0

4.3. Clone full information:

To get more the others information about volume and value beside basical fields, we need to declare minimal=False (default True).

loader = dt.DataLoader(symbols=["VND"], start="2018-01-10", end="2018-02-15", minimal=False)
data = loader.download()
data.head()
Attributes change_perc1 change_perc2 open high low close avg volume_match volume_reconcile volume
Symbols VND VND VND VND VND VND VND VND VND VND
date
2018-01-10 0.00 0.000000 27.55 27.75 27.10 27.50 27.52 1382780.0 84000.0 1466780.0
2018-01-11 -0.30 0.010909 27.30 27.50 26.80 27.20 27.21 1260720.0 0.0 1260720.0
2018-01-12 0.40 0.014706 27.45 28.20 27.35 27.60 27.76 1730170.0 0.0 1730170.0
2018-01-15 0.65 0.023551 27.60 28.40 27.35 28.25 28.11 1273740.0 0.0 1273740.0
2018-01-16 0.00 0.000000 28.10 28.40 27.90 28.25 28.14 1077350.0 86000.0 1163350.0

Through this project, i hope you make your work being more covinient and easy by applying them. Though try hard, but there are many drawback, kindly comment and send me feed back to implement my project.

5. Get finance, cashflow, business and basic index reports (0.0.3)

In version 0.0.3 you can download finance, cashflow, business and basic index reports with class vnquant.data.FinanceLoader. Currently, we only support you clone one symbol per each time. To use this class you import as bellow:

import vnquant.data as dt
loader = dt.FinanceLoader(symbol = 'VND', 
                          start = '2019-06-02',
                          end = '2021-12-31')

Arguments

  • symbol: a string indicates the stock name. The stock symbol in regular includes 3 upper case letters.
  • start: start date time with format yyyy-mm-dd.
  • end: end date time with format yyyy-mm-dd.

5.1. Get finance report

Function get_finan_report() will help you get these finance indexes. For example:

import vnquant.data as dt
loader = dt.FinanceLoader('VND', '2019-06-02','2021-12-31')
data_finan = loader.get_finan_report()
data_finan.head()
2021-06 2021-03 2020-12 2020-09 2020-06 2020-03 2019-12 2019-09 2019-06
index
Tài sản ngắn hạn 21351441834807 17347112129802 12793253609747 11585099253268 11479005695043 10851511570130 11239350733660 11059560981795 10590145635691
Tài sản tài chính ngắn hạn 21339567743512 17332349291032 12770938291296 11570420145910 11465268994352 10826493556341 11222476803929 11036102039564 10550582164047
Tiền và các khoản tương đương tiền 1153684350307 590663521250 595786368281 175314778804 185532378242 368330609085 613548205346 298144380199 413837038988
Tiền 778884350307 460708745512 509970753138 141314778804 148847077857 346330609085 611548205346 158744380199 252137038988
Tiền gửi của người đầu tư về giao dịch chứng khoán 0 0 0 0 0 0 0 0 0

5.2. Get business report

To get business report of each symbol, you use get_business_report() function as below:

import vnquant.data as dt
loader = dt.FinanceLoader('VND', '2019-06-02','2021-12-31', data_source='VND', minimal=True)
data_bus = loader.get_business_report()
data_bus.head()
2021-06 2021-03 2020-12 2020-09 2020-06 2020-03 2019-12 2019-09 2019-06
index
Lãi từ các tài sản tài chính ghi nhận thông qua lãi/lỗ ( FVTPL) 384484114460 446903481873 348445083732 177846582328 126873424297 112699410946 69471930133 100788177914 96112568053
Lãi bán các tài sản tài chính PVTPL 187329856826 258061271830 321502700913 126020773731 120870780627 121996865833 77348559746 70332267851 62535937574
Chênh lệch tăng đánh giá lại các TSTC thông qua lãi/lỗ 192578271638 188425144043 -8160509655 33713693597 -848309517 -10832424389 -13633761575 28457177453 15139720013
Cổ tức, tiền lãi phát sinh từ tài sản tài chính PVTPL 4575985996 417066000 35102892474 18112115000 6850953187 1534969502 5757131962 1998732610 18436910466
Lãi từ các khoản đầu tư nắm giữ đến ngày đáo hạn 24948288940 108779296202 98753552528 84000482631 84108399683 105941161289 107029644615 100310037665 120061106620

5.3. Get cashflow report

Function get_cashflow_report() shall support to clone cashflow report:

import vnquant.data as dt
loader = dt.FinanceLoader('VND', '2019-06-02','2021-12-31', data_source='VND', minimal=True)
data_cash = loader.get_cashflow_report()
data_cash.head()
2021-06 2021-03 2020-09 2020-06 2020-03 2019-12 2019-09 2019-06
index
Điều chỉnh cho các khoản 117772457799 102102918502 39938806717 -2179656198 189614451308 77119446501 115901816119 310400347299
Chi phí khấu hao tài sản cố định 7121178573 5117724702 5200320730 4817922370 5082314726 4956658731 5024428635 5218835903
Phân bổ lợi thế thương mại 0 0 0 0 0 0 0 0
Dự phòng giảm giá các khoản đầu tư ngắn hạn, dài hạn 15351994938 7673582615 -50406427024 -97302010585 74769599906 -47666085953 -1958363587 194803059759
Lãi, lỗ chênh lệch tỷ giá hối đoái chưa thực hiện 2291430861 0 -855383375 -855383375 0 -535741671 -136318575 136318575

5.4. Get basic index report

This function provide to you basic and important index of each symbol such as: ROA, ROE, Net Profit Marget, Net Revenue Growth, Profit After tax Growth

import vnquant.data as dt
loader = dt.FinanceLoader('VND', '2019-06-02','2021-12-31', data_source='VND', minimal=True)
data_basic = loader.get_basic_index()
data_basic.head()
2020-12 2019-12
index
Profit After Tax Growth (YoY) 0.810405 0.025519
Net Revenue Growth (YoY) 0.421240 -0.023797
Net Profit Margin (Yr) 0.324553 0.254787
ROE last 4 quarters 0.195974 0.123374
ROA last 4 quarters 0.053917 0.033224