Title: | Korea Investment & Securities (KIS) Open Trading API |
---|---|
Description: | API Wrapper to use Korea Investment & Securities (KIS) trading system that provides various financial services like stock price check, orders and balance check <https://apiportal.koreainvestment.com/>. |
Authors: | Seokhoon Joo [aut, cre] |
Maintainer: | Seokhoon Joo <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.0.2 |
Built: | 2025-02-13 04:42:37 UTC |
Source: | https://github.com/seokhoonj/kisopenapi |
if you wrote live trading and paper trading evironment variables both in your .Renviron file, you can change trading environment.
change_trading_env()
change_trading_env()
"live" or "paper" string value
## Not run: change_trading_environment()
## Not run: change_trading_environment()
Download kospi_code.mst (kosdaq_code.mst) file.
download_kospi_master(base_dir) download_kosdaq_master(base_dir)
download_kospi_master(base_dir) download_kosdaq_master(base_dir)
base_dir |
destination folder, if missing, current working directory. |
no return value
# download kospi_code.mst ## Not run: download_kospi_master() ## End(Not run) # download kosdaq_code.mst ## Not run: download_kosdaq_master() ## End(Not run)
# download kospi_code.mst ## Not run: download_kospi_master() ## End(Not run) # download kosdaq_code.mst ## Not run: download_kosdaq_master() ## End(Not run)
These dataset contains KIS Open API errors
errors
errors
data.frame
errors
errors
Get your account balance.
get_balance(prdt_code, rt_cash_flag = FALSE)
get_balance(prdt_code, rt_cash_flag = FALSE)
prdt_code |
A string specifying account product code |
rt_cash_flag |
A boolean specifying deposit or details |
balance data.frame
## get account balance ## Not run: # details get_balance(rt_cash_flag = FALSE) # deposit get_balance(rt_cash_flag = TRUE) ## End(Not run)
## get account balance ## Not run: # details get_balance(rt_cash_flag = FALSE) # deposit get_balance(rt_cash_flag = TRUE) ## End(Not run)
Get buyable amount of cash of the account
get_buyable_cash(prdt_code)
get_buyable_cash(prdt_code)
prdt_code |
A string specifying account product code |
A numeric specifying buyable cash
# get buyable cash ## Not run: get_buyable_cash() ## End(Not run)
# get buyable cash ## Not run: get_buyable_cash() ## End(Not run)
Get current index.
get_current_index(index_code = c("0001", "1001", "2001"))
get_current_index(index_code = c("0001", "1001", "2001"))
index_code |
A string specifying an index code (KOSPI: "0001", KOSDAQ: "1001", KOSPI200: "2001") |
a data frame of current index
# get current index ## Not run: get_current_index("0001") ## End(Not run)
# get current index ## Not run: get_current_index("0001") ## End(Not run)
Get current stock price.
get_current_price(stock_code)
get_current_price(stock_code)
stock_code |
A string specifying stock number (stock code) |
current stock price
# get current price ## Not run: get_current_price("005930") ## End(Not run)
# get current price ## Not run: get_current_price("005930") ## End(Not run)
Download KOSPI (KOSDAQ) master file in temp directory and get the data from the file
get_kospi_master() get_kosdaq_master()
get_kospi_master() get_kosdaq_master()
a data.frame
# get kospi master ## Not run: kospi <- get_kospi_master() View(kospi) ## End(Not run) # get kosdaq master ## Not run: kosdaq <- get_kosdaq_master() View(kosdaq) ## End(Not run)
# get kospi master ## Not run: kospi <- get_kospi_master() View(kospi) ## End(Not run) # get kosdaq master ## Not run: kosdaq <- get_kosdaq_master() View(kosdaq) ## End(Not run)
Get data from kospi_code.mst (kosdaq_code.mst) file.
get_kospi_master_dataframe(base_dir) get_kosdaq_master_dataframe(base_dir)
get_kospi_master_dataframe(base_dir) get_kosdaq_master_dataframe(base_dir)
base_dir |
destination folder, if missing, current working directory. |
KOSPI (KOSDAQ) master dataframe
# get kospi master dataframe ## Not run: download_kospi_master() get_kospi_master_dataframe() ## End(Not run) # get kosdaq master dataframe ## Not run: download_kosdaq_master() get_kosdaq_master_dataframe() ## End(Not run)
# get kospi master dataframe ## Not run: download_kospi_master() get_kospi_master_dataframe() ## End(Not run) # get kosdaq master dataframe ## Not run: download_kosdaq_master() get_kosdaq_master_dataframe() ## End(Not run)
Get order history from start date to end date.
get_order_history(sdt, edt, prdt_code, zip_flag = TRUE)
get_order_history(sdt, edt, prdt_code, zip_flag = TRUE)
sdt |
A string specifying start date "YYYYMMDD" |
edt |
A string specifying end date "YYYYMMDD" |
prdt_code |
A string specifying account product code |
zip_flag |
A boolean specifying choosing important columns |
Order history data frame
# get order history ## Not run: get_order_history("20") ## End(Not run)
# get order history ## Not run: get_order_history("20") ## End(Not run)
Return a list of orders that can be revised or canceled.
get_orders()
get_orders()
data.frame of orders
# get a list of orders ## Not run: get_orders() ## End(Not run)
# get a list of orders ## Not run: get_orders() ## End(Not run)
Get stock history.
get_stock_history(stock_code, unit = c("D", "W", "M"))
get_stock_history(stock_code, unit = c("D", "W", "M"))
stock_code |
A string specifying stock code |
unit |
A string specifying day, week, month |
stock history data frame
# get stock history ## Not run: get_stock_history("005930") ## End(Not run)
# get stock history ## Not run: get_stock_history("005930") ## End(Not run)
Get stock history by open, high, low, close, volume.
get_stock_history_by_ohlcv(stock_code, unit = "D", add_var = FALSE)
get_stock_history_by_ohlcv(stock_code, unit = "D", add_var = FALSE)
stock_code |
A string specifying stock code |
unit |
A string specifying day, week, month |
add_var |
A boolean adding volitility and percentage change |
stock history by ohlcv data frame
# get stock history ## Not run: get_stock_history_by_ohlcv("005930") ## End(Not run)
# get stock history ## Not run: get_stock_history_by_ohlcv("005930") ## End(Not run)
Get stock investor data using KIS API.
get_stock_investor(stock_code)
get_stock_investor(stock_code)
stock_code |
A string specifying stock code |
stock investor data frame
# get stock investor ## Not run: get_stock_investor("005930") ## End(Not run)
# get stock investor ## Not run: get_stock_investor("005930") ## End(Not run)
Get current stock qutoes.
get_stock_quotes(stock_code)
get_stock_quotes(stock_code)
stock_code |
A string specifying stock code |
current stock quotes data frame
# get stock quotes ## Not run: get_stock_quotes("005930") ## End(Not run)
# get stock quotes ## Not run: get_stock_quotes("005930") ## End(Not run)
Order stocks.
kis_order( stock_code, order_qty, order_price, prdt_code, order_type = "00", buy_flag = TRUE ) kis_buy(stock_code, order_qty, order_price, prdt_code, order_type = "00") kis_sell(stock_code, order_qty, order_price, prdt_code, order_type = "00")
kis_order( stock_code, order_qty, order_price, prdt_code, order_type = "00", buy_flag = TRUE ) kis_buy(stock_code, order_qty, order_price, prdt_code, order_type = "00") kis_sell(stock_code, order_qty, order_price, prdt_code, order_type = "00")
stock_code |
A string specifying stock code |
order_qty |
A numeric or string specifying order quantity |
order_price |
A numeric or string specifying order price |
prdt_code |
A string specifying account product code |
order_type |
A string specifying order type |
buy_flag |
A boolean specifying flag |
A list contains rt_cd: return code, msg_cd: message code, msg1: message
An order result
## Not run: ## buy kis_buy( stock_code = "stock code", order_qty = "your order quantity", order_price = "your order price" ) ## sell kis_sell( stock_code = "stock code", order_qty = "your order quantity", order_price = "your order price" ) ## End(Not run)
## Not run: ## buy kis_buy( stock_code = "stock code", order_qty = "your order quantity", order_price = "your order price" ) ## sell kis_sell( stock_code = "stock code", order_qty = "your order quantity", order_price = "your order price" ) ## End(Not run)
Revise and cancel orders.
kis_revise_cancel( order_no, order_branch, order_qty, order_price, prdt_code, order_dv = "00", cncl_dv = c("01", "02"), qty_all_yn = c("Y", "N") ) kis_revise( order_no, order_branch, order_qty, order_price, prdt_code, order_dv = "00", cncl_dv = "01", qty_all_yn = c("Y", "N") ) kis_cancel( order_no, order_branch, order_qty, order_price, prdt_code, order_dv = "00", cncl_dv = "02", qty_all_yn = c("Y", "N") ) kis_cancel_all()
kis_revise_cancel( order_no, order_branch, order_qty, order_price, prdt_code, order_dv = "00", cncl_dv = c("01", "02"), qty_all_yn = c("Y", "N") ) kis_revise( order_no, order_branch, order_qty, order_price, prdt_code, order_dv = "00", cncl_dv = "01", qty_all_yn = c("Y", "N") ) kis_cancel( order_no, order_branch, order_qty, order_price, prdt_code, order_dv = "00", cncl_dv = "02", qty_all_yn = c("Y", "N") ) kis_cancel_all()
order_no |
A string specifying order number |
order_branch |
A string specifying branch code |
order_qty |
A numeric or string specifying order quantity |
order_price |
A numeric or string specifying order price |
prdt_code |
A string specifying account product code |
order_dv |
A string specifying limit order(00) or market order(01) |
cncl_dv |
A string specifying revise(01) or cancel(02) |
qty_all_yn |
A string specifying total order quantity or not |
A list contains rt_cd: return code, msg_cd: message code, msg1: message
response
## revise ## Not run: kis_revise( order_no = "your order number", order_branch = "your order branch", order_qty = "your order quantity", order_price = "your order price" ) ## End(Not run) ## cancel ## Not run: kis_cancel( order_no = "your order number", order_branch = "your order branch", order_qty = "your order quantity", order_price = "your order price" ) ## End(Not run) ## cancel all ## Not run: kis_cancel_all() ## End(Not run)
## revise ## Not run: kis_revise( order_no = "your order number", order_branch = "your order branch", order_qty = "your order quantity", order_price = "your order price" ) ## End(Not run) ## cancel ## Not run: kis_cancel( order_no = "your order number", order_branch = "your order branch", order_qty = "your order quantity", order_price = "your order price" ) ## End(Not run) ## cancel all ## Not run: kis_cancel_all() ## End(Not run)
These datasets contain both English and Korean column names.
kospi_master_columns kosdaq_master_columns
kospi_master_columns kosdaq_master_columns
data.frame
kospi_master_columns kosdaq_master_columns
kospi_master_columns kosdaq_master_columns
Print your account product code in the current trading environemnt. it changes depending on the trading environment.
print_acnt_prdt_cd()
print_acnt_prdt_cd()
account product code
## Not run: print_acnt_prdt_cd()
## Not run: print_acnt_prdt_cd()
Print your app key in the current trading environemnt. it changes depending on the trading environment.
print_app_key()
print_app_key()
app key
## Not run: print_app_key()
## Not run: print_app_key()
Print your app secret in the current trading environemnt. it changes depending on the trading environment.
print_app_secret()
print_app_secret()
app secret
## Not run: print_app_secret()
## Not run: print_app_secret()
Print your base url in the current trading environemnt. it changes depending on the trading environment.
print_base_url()
print_base_url()
base url of current trading environment
## Not run: print_base_url()
## Not run: print_base_url()
Print your account number in the current trading environemnt. it changes depending on the trading environment.
print_cano()
print_cano()
account number
## Not run: print_cano()
## Not run: print_cano()
Print the current trading environment.
print_trading_env()
print_trading_env()
"live" or "paper" string value
## Not run: print_trading_env()
## Not run: print_trading_env()
Set access token (hash key) using kis app key and kis app secret.
set_auth() get_auth() print_auth()
set_auth() get_auth() print_auth()
An access token (access token is stored in environment variable at the same time)
# set authentification ## Not run: set_auth() ## End(Not run)
# set authentification ## Not run: set_auth() ## End(Not run)
Save environment variables for the current session. To set it permanently,
please add the following line to your .Renvrion file:
set_trading_env(cano, acnt_prdt_cd, app_key, app_secret, is_paper = FALSE)
set_trading_env(cano, acnt_prdt_cd, app_key, app_secret, is_paper = FALSE)
cano |
A string specifying KIS common account number |
acnt_prdt_cd |
A string specifying KIS account product code |
app_key |
A string specifying KIS app key |
app_secret |
A string specifying KIS app secret |
is_paper |
A string specifying paper trading or not, default |
No return value, called to set environment variables
KIS_CANO="YOUR ACCOUNT NUMBER" (first 8 digits of your account)
KIS_ACNT_PRDT_CD="01" (last 2 digits of your account)
KIS_APP_KEY="YOUR APP KEY"
KIS_APP_SECRET="YOUR APP SECRET"
KIS_PAPER_CANO="YOUR PAPER ACCOUNT NUMBER" (first 8 digits of your paper account)
KIS_PAPER_ACNT_PRDT_CD="01" (last 2 digits of your paper account)
KIS_PAPER_APP_KEY="YOUR PAPER APP KEY"
KIS_PAPER_APP_SECRET="YOUR PAPER APP SECRET"
## Set app key for the current session ## Not run: ## Set trading environment only for the current session set_trading_env( cano = "your account number", acnt_prdt_no = "your account product code", app_key = "your app key", app_secret = "your app secret" ) ## Check environment variables print_cano() print_acnt_prdt_cd() print_app_key() print_app_secret() ## End(Not run)
## Set app key for the current session ## Not run: ## Set trading environment only for the current session set_trading_env( cano = "your account number", acnt_prdt_no = "your account product code", app_key = "your app key", app_secret = "your app secret" ) ## Check environment variables print_cano() print_acnt_prdt_cd() print_app_key() print_app_secret() ## End(Not run)