-
Notifications
You must be signed in to change notification settings - Fork 70
Tutorial
์ํฌ๋ฆฟ ํค๋ฅผ ๊ด๋ฆฌํ๋ ๋ฐฉ๋ฒ์ ํฌ๊ฒ 2๊ฐ์ง๊ฐ ์์ต๋๋ค.
-
์ํฌ๋ฆฟ ํค๋ฅผ ํ์ผ๋ก ๊ด๋ฆฌํ๋ ๋ฐฉ๋ฒ
# ๋จผ์ ์ํฌ๋ฆฟ ํค๋ฅผ ํ์ผ๋ก ์ ์ฅํฉ๋๋ค. from pykis import KisAuth auth = KisAuth( # HTS ๋ก๊ทธ์ธ ID ์) soju06 id="YOUR_HTS_ID", # ์ฑ ํค ์) Pa0knAM6JLAjIa93Miajz7ykJIXXXXXXXXXX appkey="YOUR_APP_KEY", # ์ฑ ์ํฌ๋ฆฟ ํค ์) V9J3YGPE5q2ZRG5EgqnLHn7XqbJjzwXcNpvY . . . secretkey="YOUR_APP_SECRET", # ์ฑ ํค์ ์ฐ๊ฒฐ๋ ๊ณ์ข๋ฒํธ ์) 00000000-01 account="00000000-01", # ๋ชจ์ํฌ์ ์ฌ๋ถ virtual=False, ) # ์์ ํ ๊ฒฝ๋ก์ ์ํฌ๋ฆฟ ํค๋ฅผ ํ์ผ๋ก ์ ์ฅํฉ๋๋ค. auth.save("secret.json")
๊ทธ ํ, ์ ์ฅ๋ ์ํฌ๋ฆฟ ํค๋ฅผ ์ฌ์ฉํ์ฌ PyKis ๊ฐ์ฒด๋ฅผ ์์ฑํฉ๋๋ค.
from pykis import PyKis, KisAuth # ์ค์ ํฌ์์ฉ PyKis ๊ฐ์ฒด๋ฅผ ์์ฑํฉ๋๋ค. kis = PyKis("secret.json", keep_token=True) kis = PyKis(KisAuth.load("secret.json"), keep_token=True) # ๋ชจ์ํฌ์์ฉ PyKis ๊ฐ์ฒด๋ฅผ ์์ฑํฉ๋๋ค. kis = PyKis("secret.json", "virtual_secret.json", keep_token=True) kis = PyKis(KisAuth.load("secret.json"), KisAuth.load("virtual_secret.json"), keep_token=True)
-
์ง์ ์ํฌ๋ฆฟ ํค๋ฅผ ์ ๋ ฅํ๋ ๋ฐฉ๋ฒ
from pykis import PyKis # ์ค์ ํฌ์์ฉ ํ๊ตญํฌ์์ฆ๊ถ API๋ฅผ ์์ฑํฉ๋๋ค. kis = PyKis( id="soju06", # HTS ๋ก๊ทธ์ธ ID account="00000000-01", # ๊ณ์ข๋ฒํธ appkey="PSED321z...", # AppKey 36์๋ฆฌ secretkey="RR0sFMVB...", # SecretKey 180์๋ฆฌ keep_token=True, # API ์ ์ ํ ํฐ ์๋ ์ ์ฅ ) # ๋ชจ์ํฌ์์ฉ ํ๊ตญํฌ์์ฆ๊ถ API๋ฅผ ์์ฑํฉ๋๋ค. kis = PyKis( id="soju06", # HTS ๋ก๊ทธ์ธ ID account="00000000-01", # ๋ชจ์ํฌ์ ๊ณ์ข๋ฒํธ appkey="PSED321z...", # ์ค์ ํฌ์ AppKey 36์๋ฆฌ secretkey="RR0sFMVB...", # ์ค์ ํฌ์ SecretKey 180์๋ฆฌ virtual_id="soju06", # ๋ชจ์ํฌ์ HTS ๋ก๊ทธ์ธ ID virtual_appkey="PSED321z...", # ๋ชจ์ํฌ์ AppKey 36์๋ฆฌ virtual_secretkey="RR0sFMVB...", # ๋ชจ์ํฌ์ SecretKey 180์๋ฆฌ keep_token=True, # API ์ ์ ํ ํฐ ์๋ ์ ์ฅ )
ํ๊ตญํฌ์์ฆ๊ถ ๊ฐ์ธ ๊ณ ๊ฐ์ ๊ฒฝ์ฐ, ์์ธ์ค ํ ํฐ์ ๋ง๋ฃ ๊ธฐ๊ฐ์ 1์ผ์ด๋ฉฐ, ์์ธ์ค ํ ํฐ์ ๋ฐ๊ธ๋ฐ์ผ๋ฉด, ๊ณ์ข์ ์ฐ๊ฒฐ๋ ์นด์นด์คํก์ผ๋ก ์๋ฆผ์ด ์ ์ก๋ฉ๋๋ค.
PyKis๋ ์์ธ์ค ํ ํฐ์ ์๋์ผ๋ก ๊ด๋ฆฌํ๊ธฐ ๋๋ฌธ์ ์ผ๋ฐ์ ์ธ ํ๊ฒฝ์์๋ ๋ฐ๊ธ์ด๋ ๋ง๋ฃ๋ฅผ ์ง์ ๊ด๋ฆฌํ ํ์๊ฐ ์์ต๋๋ค.
ํ์ง๋ง ๋ง์ฝ, PyKis ๊ฐ์ฒด๋ฅผ 1์ผ ์ด์ ์ ์งํ๊ธฐ ์ด๋ ค์ด ํ๊ฒฝ์ด๋ผ๋ฉด, ์์ธ์ค ํ ํฐ์ ํ์ผ๋ก ์ ์ฅํ์ฌ ๊ธฐ๊ฐ์ด ๋จ์ ํ ํฐ์ ์ฌ์ฌ์ฉํ ์ ์์ต๋๋ค.
-
ํ ํฐ ์๋ ๊ด๋ฆฌ ํ์ฑํ ๋ฐฉ๋ฒ
from pykis import PyKis # keep_token=True๋ฅผ ์ฌ์ฉํ์ฌ ํ ํฐ์ ์๋์ผ๋ก ์ ์ฅํฉ๋๋ค. # ๊ธฐ๋ณธ ์ ์ฅ ๊ฒฝ๋ก๋ ~/.pykis/ ์ ๋๋ค. ์ ๋ขฐํ ์ ์๋ ํ๊ฒฝ์์ ์ฌ์ฉํ์ง ๋ง์ธ์. kis = PyKis("secret.json", keep_token=True)
-
ํ ํฐ์ ์๋์ผ๋ก ๊ด๋ฆฌํ๋ ๋ฐฉ๋ฒ
from pykis.kis import PyKis, KisAccessToken # ์ ์ฅ๋ ํ ํฐ์ ์ฌ์ฉํ๋ ค๋ฉด ์๋์ ๊ฐ์ด PyKis ๊ฐ์ฒด๋ฅผ ์์ฑํฉ๋๋ค. kis = PyKis("secret.json", token="token.json") kis = PyKis("secret.json", token=KisAccessToken.load("token.json")) # ๋๋ ์๋์ ๊ฐ์ด PyKis ๊ฐ์ฒด๋ฅผ ์์ฑํ ํ ํ ํฐ์ ์ค์ ํฉ๋๋ค. kis = PyKis("secret.json") kis.token = KisAccessToken.load("token.json") # ์๋ ํ๋กํผํฐ๋ฅผ ํธ์ถํ๋ฉด ๋ง๋ฃ๊ธฐ๊ฐ์ด ๋ณด์ฅ๋ ํ ํฐ์ ๋ฐ๊ธ๋ฐ์ ์ ์์ต๋๋ค. token = kis.token # ํ ํฐ์ ์ ๋ณด๋ฅผ ํ์ธํฉ๋๋ค. print(repr(token), str(token)) # ์์ ํ ๊ฒฝ๋ก์ ํด๋น ํ ํฐ์ ํ์ผ๋ก ์ ์ฅํฉ๋๋ค. token.save("token.json") # ํ์ผ๋ก ์ ์ฅ๋ ํ ํฐ์ด ๋ง๋ฃ๋๋๋ผ๋ PyKis ๊ฐ์ฒด์์ ์๋์ผ๋ก ๊ฐฑ์ ํฉ๋๋ค. print(f"๋จ์ ์ ํจ๊ธฐ๊ฐ: {kis.token.remaining}")
๊ธฐ๋ณธ์ ์ผ๋ก ์ด๋ ํ ์ข
๋ชฉ์ด๋ ์์ธ๋ฅผ ์กฐํํ๋ ค๋ฉด kis.stock('AAPL')
์ฒ๋ผ ์ข
๋ชฉ์ ๊ฐ์ฒด๋ฅผ ๋ฐ์์์ผ ํฉ๋๋ค.
๊ตญ๋ด์ฃผ์, ํด์ธ์ฃผ์์ ์๊ด ์์ด ๋ชจ๋ ๋์ผํ ๋ฐฉ๋ฒ์ผ๋ก ์กฐํํ ์ ์์ต๋๋ค.
from pykis import KisStock
# ๊ตญ๋ด ์ฃผ์
hynix: KisStock = kis.stock("000660") # SKํ์ด๋์ค (์ฝ์คํผ)
ecopro: KisStock = kis.stock("247540") # ์์ฝํ๋ก๋น์ (์ฝ์ค๋ฅ)
# ํด์ธ ์ฃผ์ (๋ฏธ๊ตญ)
nvida: KisStock = kis.stock("NVDA") # ์๋น๋์ (๋์ค๋ฅ)
coupang: KisStock = kis.stock("CPNG") # ์ฟ ํก (๋ด์)
API์ ํ๊ณ๋ก ์ข ๋ชฉ์ด ๋ฐ๊ฒฌ๋ ๋๊น์ง ๋ชจ๋ ์์ฅ์ ์ํํ๋ฉฐ ์กฐํํ๋ฏ๋ก ์์ฅ ์ ๋ณด๋ฅผ ํจ๊ป ์ ๋ ฅํ๋ฉด ๋ ๋น ๋ฅด๊ฒ ์กฐํํ ์ ์์ต๋๋ค.
๊ธฐ๋ณธ๊ฐ์ ํ๊ตญ -> ๋ฏธ๊ตญ -> ์ผ๋ณธ -> ํ์ฝฉ -> ์ค๊ตญ -> ๋ฒ ํธ๋จ ์์ผ๋ก ์กฐํํฉ๋๋ค.
tokyo_electric: KisStock = kis.stock("9501", market="TYO") # ๋์ฟ ์ ๋ ฅ (๋์ฟ)
# ๋๋ ๊ตญ๊ฐ์ฝ๋๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค.
tokyo_electric: KisStock = kis.stock("9501", market="JP") # ๋์ฟ ์ ๋ ฅ (๋์ฟ)
PyKis์ stockํจ์๋ก ์ป์ ์ข
๋ชฉ ์ค์ฝํ(KisStock
)๋ ๊ธฐ๋ณธ์ ์ธ ์ข
๋ชฉ ์ ๋ณด๋ฅผ ๋ด๊ณ ์์ต๋๋ค.
print(
f"""
์ข
๋ชฉ์ฝ๋: {hynix.symbol}
์ข
๋ชฉ๋ช
: {hynix.name}
์ข
๋ชฉ์์ฅ: {hynix.market}
"""
)
# ๋ํ info ํ๋กํผํฐ๋ฅผ ํตํด ์์ธ ์ ๋ณด๋ฅผ ์ป์ ์ ์์ต๋๋ค.
print(
f"""
์ข
๋ชฉ์ฝ๋: {hynix.info.symbol}
์ข
๋ชฉํ์ค์ฝ๋: {hynix.info.std_code}
์ข
๋ชฉ๋ช
: {hynix.info.name}
์ข
๋ชฉ์๋ฌธ๋ช
: {hynix.info.name_eng}
์ข
๋ชฉ์์ฅ: {hynix.info.market}
์ข
๋ชฉ์์ฅํ๊ธ๋ช
: {hynix.info.market_name}
"""
)
์ด๋ ๊ฒ ์ป์ ์ข ๋ชฉ ์ค์ฝํ๋ฅผ ์ด์ฉํด ์์ธ, ์ฃผ๋ฌธ๊ฐ๋ฅ ์๋ ์กฐํ, ๋งค๋งค ์ฃผ๋ฌธ์ ํ ์ ์์ต๋๋ค.
stock.quote()
ํจ์๋ฅผ ์ด์ฉํ์ฌ ๊ตญ๋ด์ฃผ์ ๋ฐ ํด์ธ์ฃผ์์ ์์ธ๋ฅผ ์กฐํํ ์ ์์ต๋๋ค.
from pykis import KisQuote
quote: KisQuote = kis.stock("CPNG").quote()
print(
f"""
์ข
๋ชฉ์ฝ๋: {quote.symbol}
์ข
๋ชฉ๋ช
: {quote.name}
์ข
๋ชฉ์์ฅ: {quote.market}
์
์ข
๋ช
: {quote.sector_name}
ํ์ฌ๊ฐ: {quote.price}
๊ฑฐ๋๋: {quote.volume}
๊ฑฐ๋๋๊ธ: {quote.amount}
์๊ฐ์ด์ก: {quote.market_cap}
๋๋น๋ถํธ: {quote.sign}
์ํ๋: {quote.risk}
๊ฑฐ๋์ ์ง: {quote.halt}
๋จ๊ธฐ๊ณผ์ด๊ตฌ๋ถ: {quote.overbought}
์ ์ผ์ข
๊ฐ: {quote.prev_price}
์ ์ผ๊ฑฐ๋๋: {quote.prev_volume}
์ ์ผ๋๋น: {quote.change}
์ํ๊ฐ: {quote.high_limit}
ํํ๊ฐ: {quote.low_limit}
๊ฑฐ๋๋จ์: {quote.unit}
ํธ๊ฐ๋จ์: {quote.tick}
์์์ ์๋ฆฌ์: {quote.decimal_places}
ํตํ์ฝ๋: {quote.currency}
๋น์ผํ์จ: {quote.exchange_rate}
๋น์ผ์๊ฐ: {quote.open}
๋น์ผ๊ณ ๊ฐ: {quote.high}
๋น์ผ์ ๊ฐ: {quote.low}
๋ฑ๋ฝ์จ: {quote.rate}
๋๋น๋ถํธ๋ช
: {quote.sign_name}
==== ์ข
๋ชฉ ์งํ ====
EPS (์ฃผ๋น์์ด์ต): {quote.indicator.eps}
BPS (์ฃผ๋น์์์ฐ): {quote.indicator.bps}
PER (์ฃผ๊ฐ์์ต๋น์จ): {quote.indicator.per}
PBR (์ฃผ๊ฐ์์์ฐ๋น์จ): {quote.indicator.pbr}
52์ฃผ ์ต๊ณ ๊ฐ: {quote.indicator.week52_high}
52์ฃผ ์ต์ ๊ฐ: {quote.indicator.week52_low}
52์ฃผ ์ต๊ณ ๊ฐ ๋ ์ง: {quote.indicator.week52_high_date.strftime("%Y-%m-%d")}
52์ฃผ ์ต์ ๊ฐ ๋ ์ง: {quote.indicator.week52_low_date.strftime("%Y-%m-%d")}
"""
)
๊ตญ๋ด์ฃผ์ ๋ฐ ํด์ธ์ฃผ์์ ๋น์ผ ๋ถ๋ด๊ณผ ๊ธฐ๊ฐ ๋ด ์ฐจํธ๋ฅผ ์กฐํํฉ๋๋ค.
์ด 3๊ฐ์ง์ ํจ์(chart
, daily_chart
, day_chart
) ๊ฐ ์์ง๋ง, chart
ํจ์๋ฅผ ์ด์ฉํ์ฌ ๋ชจ๋ ์กฐํํ ์ ์์ต๋๋ค.
from datetime import date, time
from pykis import KisChart
chart: KisChart = coupang.chart() # ๊ธฐ๋ณธ๊ฐ์ ์์ฅ ์ด๋์ ์ผ๋ด์
๋๋ค.
# ์ต๊ทผ ๊ธฐ๊ฐ ์กฐํ๋ ์๋์ ๊ฐ์ด ์๊ฐ ํํ์์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
# 1m: 1๋ถ
# 1h: 1์๊ฐ
# 1d: 1์ผ
# 1w: 1์ฃผ
# 1M: 1๊ฐ์ (๊ฐ์ ๋จ์๋ ๋๋ฌธ์ M)
# 1y: 1๋
# 1y6M: 1๋
6๊ฐ์
chart: KisChart = coupang.chart("3d") # ์ต๊ทผ 3์ผ ์ผ๋ด์
๋๋ค.
chart: KisChart = coupang.chart("1y", period="month") # ์ต๊ทผ 1๋
๊ฐ์ ์๋ด์
๋๋ค.
chart: KisChart = coupang.chart(period="year") # ์์ฅ ์ด๋์ ์ฐ๊ฐ ์ผ๋ด์
๋๋ค.
chart: KisChart = coupang.chart(start=date(2023, 1, 1)) # 2023๋
1์ 1์ผ๋ถํฐ ํ์ฌ๊น์ง์ ์ผ๋ด์
๋๋ค.
chart: KisChart = coupang.chart(
start=date(2023, 1, 1),
end=date(2024, 1, 1),
) # 2023๋
1์ 1์ผ๋ถํฐ 2023๋
12์ 31์ผ๊น์ง์ ์ผ๋ด์
๋๋ค.
chart: KisChart = coupang.chart("1h", period=1) # ์ต๊ทผ 1์๊ฐ์ 1๋ถ๋ด์
๋๋ค.
chart: KisChart = coupang.chart(period=5) # ๋น์ผ 5๋ถ๋ด์
๋๋ค.
chart: KisChart = coupang.chart(period=1, end=time(12, 30)) # ๋น์ผ 12์ 30๋ถ๊น์ง์ 1๋ถ๋ด์
๋๋ค.
PyKis์ ๋ชจ๋ ๊ฐ์ฒด๋ repr๋ฅผ ํตํด ๊ฐ์ฒด์ ์ฃผ์ ๋ด์ฉ์ ํ์ธํ ์ ์์ต๋๋ค.
print(repr(hynix.chart("7d"))) # SKํ์ด๋์ค์ ์ต๊ทผ 7์ผ ์ผ๋ด ์ฐจํธ
KisDomesticDailyChart(
market='KRX',
symbol='000660',
bars=[
KisDomesticDailyChartBar(time='2024-07-19T00:00:00+09:00', open=208500, close=209500, high=214000, low=207000, volume=4519170, amount=949039126250, change=-3000),
KisDomesticDailyChartBar(time='2024-07-22T00:00:00+09:00', open=209000, close=205000, high=209500, low=200500, volume=6662441, amount=1363039398518, change=-4500),
KisDomesticDailyChartBar(time='2024-07-23T00:00:00+09:00', open=208500, close=205000, high=209500, low=200500, volume=5976519, amount=1224619868908, change=0),
KisDomesticDailyChartBar(time='2024-07-24T00:00:00+09:00', open=202000, close=208500, high=212500, low=200000, volume=4838734, amount=1003813878000, change=3500),
KisDomesticDailyChartBar(time='2024-07-25T00:00:00+09:00', open=196200, close=190000, high=198800, low=189000, volume=12503762, amount=2411730871900, change=-18500),
KisDomesticDailyChartBar(time='2024-07-26T00:00:00+09:00', open=190800, close=191800, high=194500, low=186100, volume=8769107, amount=1670363205934, change=1800)
]
)
์๋๋ ์ฐจํธ์ ๊ฐ๋จํ ์๊ฐํ ์์์ ๋๋ค.
๋จผ์ ํ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ค์นํฉ๋๋ค.
pip install pandas lightweight-charts
from datetime import datetime, timedelta
from lightweight_charts import JupyterChart
chart_view = JupyterChart(width=1280, height=720)
chart = nvida.chart("1y")
chart_view.set(chart.df()) # pykis์ ์ฐจํธ ๊ฐ์ฒด๋ dfํจ์๋ฅผ ์ด์ฉํ์ฌ pandas DataFrame์ผ๋ก ๋ณํํ ์ ์์ต๋๋ค.
chart_view.set_visible_range(datetime.now() - timedelta(days=365), datetime.now())
chart_view.load()
stock.orderbook()
ํจ์๋ฅผ ์ด์ฉํ์ฌ ๊ตญ๋ด์ฃผ์ ๋ฐ ํด์ธ์ฃผ์์ ํธ๊ฐ๋ฅผ ์กฐํํ ์ ์์ต๋๋ค.
from pykis import KisOrderbook
orderbook: KisOrderbook = hynix.orderbook()
print("๋งค๋1ํธ๊ฐ:", orderbook.ask_price, orderbook.ask_volumn)
print("๋งค์1ํธ๊ฐ:", orderbook.bid_price, orderbook.bid_volumn)
print(repr(orderbook)) # repr์ ํตํด ๊ฐ์ฒด์ ์ฃผ์ ๋ด์ฉ์ ํ์ธํ ์ ์์ต๋๋ค.
๋งค๋1ํธ๊ฐ: 192500 127
๋งค์1ํธ๊ฐ: 192400 1384
KisDomesticOrderbook(
market='KRX',
symbol='000660',
asks=[
KisDomesticOrderbookItem(price=192500, volume=127),
KisDomesticOrderbookItem(price=192600, volume=3630),
KisDomesticOrderbookItem(price=192700, volume=559),
KisDomesticOrderbookItem(price=192800, volume=693),
KisDomesticOrderbookItem(price=192900, volume=461),
KisDomesticOrderbookItem(price=193000, volume=2634),
KisDomesticOrderbookItem(price=193100, volume=1151),
KisDomesticOrderbookItem(price=193200, volume=379),
KisDomesticOrderbookItem(price=193300, volume=842),
KisDomesticOrderbookItem(price=193400, volume=1159)
],
bids=[
KisDomesticOrderbookItem(price=192400, volume=1384),
KisDomesticOrderbookItem(price=192300, volume=2598),
KisDomesticOrderbookItem(price=192200, volume=7793),
KisDomesticOrderbookItem(price=192100, volume=12525),
KisDomesticOrderbookItem(price=192000, volume=13471),
KisDomesticOrderbookItem(price=191900, volume=10903),
KisDomesticOrderbookItem(price=191800, volume=31044),
KisDomesticOrderbookItem(price=191700, volume=930),
KisDomesticOrderbookItem(price=191600, volume=1280),
KisDomesticOrderbookItem(price=191500, volume=1921)
]
)
kis.trading_hours()
ํจ์๋ฅผ ์ด์ฉํ์ฌ ๊ตญ๋ด ๋ฐ ํด์ธ ์ฅ์ด์ ์๊ฐ์ ์กฐํํ ์ ์์ต๋๋ค.
from pykis import KisTradingHours
trading_hours: KisTradingHours = kis.trading_hours("US")
print(repr(trading_hours)) # repr์ ํตํด ๊ฐ์ฒด์ ์ฃผ์ ๋ด์ฉ์ ํ์ธํ ์ ์์ต๋๋ค.
KisForeignTradingHours(
market='NASDAQ',
open='09:30:00',
open_kst='22:30:00',
close='16:00:00',
close_kst='05:00:00'
)
๊ณ์ข์ ์๊ณ ๋ฅผ ์กฐํํ๋ ค๋ฉด kis.account()
ํจ์๋ฅผ ์ด์ฉํ์ฌ ์๊ณ ์ค์ฝํ ๊ฐ์ฒด๋ฅผ ๋ฐ์์์ผ ํฉ๋๋ค.
from pykis import KisAccount
account: KisAccount = kis.account()
account.balance()
ํจ์๋ฅผ ์ด์ฉํ์ฌ ์์๊ธ ๋ฐ ๋ณด์ ์ข
๋ชฉ์ ์กฐํํ ์ ์์ต๋๋ค.
from pykis import KisBalance
balance: KisBalance = account.balance()
print(repr(balance)) # repr์ ํตํด ๊ฐ์ฒด์ ์ฃผ์ ๋ด์ฉ์ ํ์ธํ ์ ์์ต๋๋ค.
KisIntegrationBalance(
account_number=KisAccountNumber('50113500-01'),
deposits={
'KRW': KisDomesticDeposit(account_number=KisAccountNumber('50113500-01'), currency='KRW', amount=2447692, exchange_rate=1),
'USD': KisForeignPresentDeposit(account_number=KisAccountNumber('50113500-01'), currency='USD', amount=0, exchange_rate=1384.6),
},
stocks=[
KisDomesticBalanceStock(account_number=KisAccountNumber('50113500-01'), market='KRX', symbol='000660', qty=14, price=192600, amount=2696400, profit=22900, profit_rate=0.856555077613615111277351786),
KisDomesticBalanceStock(account_number=KisAccountNumber('50113500-01'), market='KRX', symbol='039200', qty=118, price=39600, amount=4672800, profit=-199500, profit_rate=-4.094575457176282248630010467)
],
purchase_amount=7545800,
current_amount=7369200,
profit=-176600,
profit_rate=-2.340374778022211031302181346
)
account.profits()
ํจ์๋ฅผ ์ด์ฉํ์ฌ ๊ธฐ๊ฐ ์์ต์ ์กฐํํ ์ ์์ต๋๋ค. (๋ชจ์ํฌ์์์๋ ์กฐํ๊ฐ ๋ถ๊ฐ๋ฅํฉ๋๋ค.)
from datetime import date
from pykis import KisOrderProfits
profits: KisOrderProfits =account.profits(start=date(2023, 8, 1))
print(repr(profits)) # repr์ ํตํด ๊ฐ์ฒด์ ์ฃผ์ ๋ด์ฉ์ ํ์ธํ ์ ์์ต๋๋ค.
KisIntegrationOrderProfits(
account_number=KisAccountNumber('00000000-01'),
buy_amount=8456747.364,
sell_amount=8458122.90431,
profit=1375.54031,
orders=[
KisDomesticOrderProfit(time_kst='2024-07-11T00:00:00+09:00', market='KRX', symbol='462870', name='์ํํธ์
', buy_price=60000, sell_price=85000, qty=1, profit=25000, profit_rate=41.66666666666666666666666667),
KisForeignOrderProfit(time_kst='2024-06-18T00:00:00+09:00', market='NASDAQ', symbol='ARM', name='์์ด์์ ํ๋ฉ์ค(ADR)', buy_price=135.39, sell_price=161.14, qty=7, profit=180.25, profit_rate=19.01912992096905236723539405),
KisForeignOrderProfit(time_kst='2024-05-23T00:00:00+09:00', market='NYSE', symbol='CPNG', name='์ฟ ํก', buy_price=17.55, sell_price=22.3601, qty=1, profit=4.8101, profit_rate=27.40797720797720797720797721),
KisDomesticOrderProfit(time_kst='2024-04-01T00:00:00+09:00', market='KRX', symbol='005930', name='์ผ์ฑ์ ์', buy_price=81700, sell_price=83200, qty=7, profit=10500, profit_rate=1.835985312117503059975520196),
KisForeignOrderProfit(time_kst='2024-02-29T00:00:00+09:00', market='NASDAQ', symbol='MARA', name='๋งค๋ฌ์ ๋์งํธ ํ๋ฉ์ค', buy_price=32.8718, sell_price=29.41, qty=37, profit=-128.09, profit_rate=-10.53146531169322348839886209),
KisForeignOrderProfit(time_kst='2024-02-28T00:00:00+09:00', market='NASDAQ', symbol='NVDA', name='์๋น๋์', buy_price=738, sell_price=787.2, qty=1, profit=49.2, profit_rate=6.666666666666666666666666667),
KisForeignOrderProfit(time_kst='2024-02-28T00:00:00+09:00', market='NASDAQ', symbol='SOUN', name='์ฌ์ด๋ํ์ด๋ AI', buy_price=7.028, sell_price=6.72, qty=45, profit=-13.86, profit_rate=-4.382470119521912350597609562),
KisForeignOrderProfit(time_kst='2024-02-16T00:00:00+09:00', market='NASDAQ', symbol='AAPL', name='์ ํ', buy_price=194, sell_price=184.46, qty=11, profit=-104.94, profit_rate=-4.917525773195876288659793814),
KisForeignOrderProfit(time_kst='2024-02-16T00:00:00+09:00', market='NYSE', symbol='CPNG', name='์ฟ ํก', buy_price=17.71, sell_price=15.76, qty=8, profit=-15.6, profit_rate=-11.01072840203274985883681536),
KisForeignOrderProfit(time_kst='2023-12-13T00:00:00+09:00', market='NYSE', symbol='WMT', name='์๋งํธ', buy_price=159.09, sell_price=151.36, qty=1, profit=-7.73, profit_rate=-4.858884907913759507197183984),
KisDomesticOrderProfit(time_kst='2023-09-04T00:00:00+09:00', market='KRX', symbol='389500', name='์์ค๋น๋นํ
ํฌ', buy_price=55400, sell_price=59200, qty=1, profit=3800, profit_rate=6.859205776173285198555956679),
KisDomesticOrderProfit(time_kst='2023-08-09T00:00:00+09:00', market='KRX', symbol='228760', name='์ง๋
ธ๋ฏนํธ๋ฆฌ', buy_price=22200, sell_price=22250, qty=4, profit=200, profit_rate=0.2252252252252252252252252252),
KisDomesticOrderProfit(time_kst='2023-08-09T00:00:00+09:00', market='KRX', symbol='008930', name='ํ๋ฏธ์ฌ์ด์ธ์ค', buy_price=39600, sell_price=40200, qty=2, profit=1200, profit_rate=1.515151515151515151515151515)
]
)
account.daily_orders()
ํจ์๋ฅผ ์ด์ฉํ์ฌ ์ผ๋ณ ์ฒด๊ฒฐ ๋ด์ญ์ ์กฐํํ ์ ์์ต๋๋ค.
from datetime import date
from pykis import KisDailyOrders
daily_orders: KisDailyOrders = account.daily_orders(start=date(2024, 4, 2), end=date(2024, 6, 1))
print(repr(daily_orders)) # repr์ ํตํด ๊ฐ์ฒด์ ์ฃผ์ ๋ด์ฉ์ ํ์ธํ ์ ์์ต๋๋ค.
KisIntegrationDailyOrders(
account_number=KisAccountNumber('00000000-01'),
orders=[
KisForeignDailyOrder(
order_number=KisOrderBase(
kis=kis,
account_number=KisAccountNumber('00000000-01'),
code='CPNG',
market='NYSE',
branch='01790',
number='0030677379'
),
type='sell',
price=22.3601,
qty=1,
executed_qty=1
),
KisForeignDailyOrder(
order_number=KisOrderBase(
kis=kis,
account_number=KisAccountNumber('00000000-01'),
code='NVDA',
market='NASDAQ',
branch='01790',
number='0000018511'
),
type='buy',
price=0,
qty=1,
executed_qty=0
),
KisForeignDailyOrder(
order_number=KisOrderBase(
kis=kis,
account_number=KisAccountNumber('00000000-01'),
code='NVDA',
market='NASDAQ',
branch='01790',
number='0000011574'
),
type='buy',
price=0,
qty=1,
executed_qty=0
)
]
)
account.orderable_amount()
๋๋ stock.orderable_amount()
ํจ์๋ฅผ ์ด์ฉํ์ฌ ๋งค์ ๊ฐ๋ฅ ๊ธ์ก/์๋์ ์กฐํํ ์ ์์ต๋๋ค.
from pykis import KisOrderableAmount
# ์๋น๋์ ์ฃผ๊ฐ๊ฑฐ๋ ์์ฅ๊ฐ ๋งค์ ๊ฐ๋ฅ ๊ธ์ก ์กฐํ
orderable_amount: KisOrderableAmount = account.orderable_amount(
market="NASDAQ",
symbol="NVDA",
condition="extended"
)
print(repr(orderable_amount)) # repr์ ํตํด ๊ฐ์ฒด์ ์ฃผ์ ๋ด์ฉ์ ํ์ธํ ์ ์์ต๋๋ค.
# ์ค์ค์ฝํ
๋จ๊ฐ 40,950์ ๋งค์ ๊ฐ๋ฅ ๊ธ์ก ์กฐํ
orderable_amount: KisOrderableAmount = oscotec.orderable_amount(price=40950)
print(repr(orderable_amount)) # repr์ ํตํด ๊ฐ์ฒด์ ์ฃผ์ ๋ด์ฉ์ ํ์ธํ ์ ์์ต๋๋ค.
KisForeignOrderableAmount(
account_number=KisAccountNumber('50113500-01'),
symbol='NVDA',
market='NASDAQ',
unit_price=109.18,
qty=906,
amount=100000,
condition='extended',
execution=None
)
KisDomesticOrderableAmount(
account_number=KisAccountNumber('50113500-01'),
symbol='039200',
market='KRX',
unit_price=40950,
qty=59,
amount=2435453,
condition=None,
execution=None
)
๋งค๋ ๊ฐ๋ฅ ์๋์ account.balance()
์ KisBalanceStock
๊ฐ์ฒด ๋๋ KisStock
์ ๋จ์ถ ํ๋กํผํฐ๋ฅผ ์ด์ฉํ์ฌ ์กฐํํ ์ ์์ต๋๋ค.
-
์๊ณ ์ค์ฝํ์
KisBalanceStock
๊ฐ์ฒด๋ฅผ ์ด์ฉํ์ฌ ์กฐํํ๋ ๋ฐฉ๋ฒfrom pykis import KisBalanceStock # ์ค์ค์ฝํ ๋งค๋ ๊ฐ๋ฅ ์๋ ์กฐํ balance_stock: KisBalanceStock = account.balance().stocks[1] balance_stock: KisBalanceStock | None = account.balance().stock("039200") print(balance_stock.orderable) # ๋งค๋ ๊ฐ๋ฅ ์๋
Decimal('118')
-
์ข ๋ชฉ ์ค์ฝํ์ ๋จ์ถ ํ๋กํผํฐ๋ฅผ ์ด์ฉํ์ฌ ์กฐํํ๋ ๋ฐฉ๋ฒ
oscotec = kis.stock("039200") # ์ค์ค์ฝํ ๋งค๋ ๊ฐ๋ฅ ์๋ ์กฐํ print(oscotec.orderable) # ๋งค๋ ๊ฐ๋ฅ ์๋
Decimal('118')
account.pending_orders()
๋๋ stock.pending_orders()
ํจ์๋ฅผ ์ด์ฉํ์ฌ ๋ฏธ์ฒด๊ฒฐ ์ฃผ๋ฌธ์ ์กฐํํ ์ ์์ต๋๋ค. (๊ตญ๋ด ๋ชจ์ํฌ์๋ ์กฐํ๊ฐ ๋ถ๊ฐ๋ฅํฉ๋๋ค.)
from pykis import KisPendingOrders
# ๊ณ์ข์ ๋ฏธ์ฒด๊ฒฐ ์ฃผ๋ฌธ ์กฐํ
pending_orders: KisPendingOrders = account.pending_orders()
# ํน์ ์ข
๋ชฉ์ ๋ฏธ์ฒด๊ฒฐ ์ฃผ๋ฌธ ์กฐํ
pending_orders: KisPendingOrders = oscotec.pending_orders()
print(repr(pending_orders)) # repr์ ํตํด ๊ฐ์ฒด์ ์ฃผ์ ๋ด์ฉ์ ํ์ธํ ์ ์์ต๋๋ค.
KisSimplePendingOrders(
account_number=KisAccountNumber('00000000-01'),
orders=[
KisDomesticPendingOrder(
order_number=KisOrderBase(
kis=kis,
account_number=KisAccountNumber('00000000-01'),
code='039200',
market='KRX',
branch='91253',
number='0000157444'
),
type='buy',
price=41300,
qty=2,
executed_qty=0,
condition=None,
execution=None
)
]
)
stock.order()
, stock.buy()
, stock.sell()
, stock.modify()
, stock.cancel()
ํจ์๋ฅผ ์ด์ฉํ์ฌ ๋งค์/๋งค๋ ์ฃผ๋ฌธ ๋ฐ ์ ์ /์ทจ์๋ฅผ ํ ์ ์์ต๋๋ค.
from pykis import KisOrder
# SKํ์ด๋์ค 1์ฃผ ์์ฅ๊ฐ ๋งค์ ์ฃผ๋ฌธ
order: KisOrder = hynix.buy(qty=1)
# SKํ์ด๋์ค 1์ฃผ ์ง์ ๊ฐ ๋งค์ ์ฃผ๋ฌธ
order: KisOrder = hynix.buy(price=194700, qty=1)
# SKํ์ด๋์ค ์ ๋ ์์ฅ๊ฐ ๋งค๋ ์ฃผ๋ฌธ
order: KisOrder = hynix.sell()
# SKํ์ด๋์ค ์ ๋ ์ง์ ๊ฐ ๋งค๋ ์ฃผ๋ฌธ
order: KisOrder = hynix.sell(price=194700)
stock.buy(price=100, condition=None, execution=None) # ์ ์ฒด ์ง์ ๊ฐ ๋งค์
stock.buy(price=None, condition=None, execution=None) # ์ ์ฒด ์์ฅ๊ฐ ๋งค์
stock.buy(price=100, condition=None, execution=None) # ์ง์ ๊ฐ ๋งค์
stock.buy(price=None, condition=None, execution=None) # ์์ฅ๊ฐ ๋งค์
stock.buy(price=100, condition='condition', execution=None) # ์กฐ๊ฑด๋ถ์ง์ ๊ฐ ๋งค์
stock.buy(price=100, condition='best', execution=None) # ์ต์ ๋ฆฌ์ง์ ๊ฐ ๋งค์
stock.buy(price=100, condition='priority', execution=None) # ์ต์ฐ์ ์ง์ ๊ฐ ๋งค์
stock.buy(price=100, condition='extended', execution=None) # ์๊ฐ์ธ๋จ์ผ๊ฐ ๋งค์ (๋ชจ์ํฌ์ ๋ฏธ์ง์)
stock.buy(price=None, condition='before', execution=None) # ์ฅ์ ์๊ฐ์ธ ๋งค์ (๋ชจ์ํฌ์ ๋ฏธ์ง์)
stock.buy(price=None, condition='after', execution=None) # ์ฅํ์๊ฐ์ธ ๋งค์ (๋ชจ์ํฌ์ ๋ฏธ์ง์)
stock.buy(price=100, condition=None, execution='IOC') # IOC์ง์ ๊ฐ ๋งค์ (๋ชจ์ํฌ์ ๋ฏธ์ง์)
stock.buy(price=100, condition=None, execution='FOK') # FOK์ง์ ๊ฐ ๋งค์ (๋ชจ์ํฌ์ ๋ฏธ์ง์)
stock.buy(price=None, condition=None, execution='IOC') # IOC์์ฅ๊ฐ ๋งค์ (๋ชจ์ํฌ์ ๋ฏธ์ง์)
stock.buy(price=None, condition=None, execution='FOK') # FOK์์ฅ๊ฐ ๋งค์ (๋ชจ์ํฌ์ ๋ฏธ์ง์)
stock.buy(price=100, condition='best', execution='IOC') # IOC์ต์ ๋ฆฌ ๋งค์ (๋ชจ์ํฌ์ ๋ฏธ์ง์)
stock.buy(price=100, condition='best', execution='FOK') # FOK์ต์ ๋ฆฌ ๋งค์ (๋ชจ์ํฌ์ ๋ฏธ์ง์)
stock.buy(price=100, condition='LOO', execution=None) # ๋์ค๋ฅ, ๋ด์, ์๋ฉ์ค ์ฅ๊ฐ์์ง์ ๊ฐ ๋งค์ (๋ชจ์ํฌ์ ๋ฏธ์ง์)
stock.buy(price=100, condition='LOC', execution=None) # ๋์ค๋ฅ, ๋ด์, ์๋ฉ์ค ์ฅ๋ง๊ฐ์ง์ ๊ฐ ๋งค์ (๋ชจ์ํฌ์ ๋ฏธ์ง์)
stock.buy(price=None, condition='MOO', execution=None) # ๋์ค๋ฅ, ๋ด์, ์๋ฉ์ค ์ฅ๊ฐ์์์ฅ๊ฐ ๋งค์ (๋ชจ์ํฌ์ ๋ฏธ์ง์)
stock.buy(price=None, condition='MOC', execution=None) # ๋์ค๋ฅ, ๋ด์, ์๋ฉ์ค ์ฅ๋ง๊ฐ์์ฅ๊ฐ ๋งค์ (๋ชจ์ํฌ์ ๋ฏธ์ง์)
stock.buy(price=None, condition='extended', execution=None) # ๋์ค๋ฅ, ๋ด์, ์๋ฉ์ค ์ฃผ๊ฐ๊ฑฐ๋ ์์ฅ๊ฐ ๋งค์ (๋ชจ์ํฌ์ ๋ฏธ์ง์)
stock.buy(price=100, condition='extended', execution=None) # ๋์ค๋ฅ, ๋ด์, ์๋ฉ์ค ์ฃผ๊ฐ๊ฑฐ๋ ์ง์ ๊ฐ ๋งค์ (๋ชจ์ํฌ์ ๋ฏธ์ง์)
stock.sell(price=100, condition=None, execution=None) # ์ ์ฒด ์ง์ ๊ฐ ๋งค๋
stock.sell(price=None, condition=None, execution=None) # ์ ์ฒด ์์ฅ๊ฐ ๋งค๋
stock.sell(price=100, condition=None, execution=None) # ์ง์ ๊ฐ ๋งค๋
stock.sell(price=None, condition=None, execution=None) # ์์ฅ๊ฐ ๋งค๋
stock.sell(price=100, condition='condition', execution=None) # ์กฐ๊ฑด๋ถ์ง์ ๊ฐ ๋งค๋
stock.sell(price=100, condition='best', execution=None) # ์ต์ ๋ฆฌ์ง์ ๊ฐ ๋งค๋
stock.sell(price=100, condition='priority', execution=None) # ์ต์ฐ์ ์ง์ ๊ฐ ๋งค๋
stock.sell(price=100, condition='extended', execution=None) # ์๊ฐ์ธ๋จ์ผ๊ฐ ๋งค๋ (๋ชจ์ํฌ์ ๋ฏธ์ง์)
stock.sell(price=None, condition='before', execution=None) # ์ฅ์ ์๊ฐ์ธ ๋งค๋ (๋ชจ์ํฌ์ ๋ฏธ์ง์)
stock.sell(price=None, condition='after', execution=None) # ์ฅํ์๊ฐ์ธ ๋งค๋
stock.sell(price=100, condition=None, execution='IOC') # IOC์ง์ ๊ฐ ๋งค๋ (๋ชจ์ํฌ์ ๋ฏธ์ง์)
stock.sell(price=100, condition=None, execution='FOK') # FOK์ง์ ๊ฐ ๋งค๋ (๋ชจ์ํฌ์ ๋ฏธ์ง์)
stock.sell(price=None, condition=None, execution='IOC') # IOC์์ฅ๊ฐ ๋งค๋ (๋ชจ์ํฌ์ ๋ฏธ์ง์)
stock.sell(price=None, condition=None, execution='FOK') # FOK์์ฅ๊ฐ ๋งค๋ (๋ชจ์ํฌ์ ๋ฏธ์ง์)
stock.sell(price=100, condition='best', execution='IOC') # IOC์ต์ ๋ฆฌ ๋งค๋ (๋ชจ์ํฌ์ ๋ฏธ์ง์)
stock.sell(price=100, condition='best', execution='FOK') # FOK์ต์ ๋ฆฌ ๋งค๋ (๋ชจ์ํฌ์ ๋ฏธ์ง์)
stock.sell(price=100, condition='LOO', execution=None) # ๋์ค๋ฅ, ๋ด์, ์๋ฉ์ค ์ฅ๊ฐ์์ง์ ๊ฐ ๋งค๋ (๋ชจ์ํฌ์ ๋ฏธ์ง์)
stock.sell(price=100, condition='LOC', execution=None) # ๋์ค๋ฅ, ๋ด์, ์๋ฉ์ค ์ฅ๋ง๊ฐ์ง์ ๊ฐ ๋งค๋ (๋ชจ์ํฌ์ ๋ฏธ์ง์)
stock.sell(price=None, condition='MOO', execution=None) # ๋์ค๋ฅ, ๋ด์, ์๋ฉ์ค ์ฅ๊ฐ์์์ฅ๊ฐ ๋งค๋ (๋ชจ์ํฌ์ ๋ฏธ์ง์)
stock.sell(price=None, condition='MOC', execution=None) # ๋์ค๋ฅ, ๋ด์, ์๋ฉ์ค ์ฅ๋ง๊ฐ์์ฅ๊ฐ ๋งค๋ (๋ชจ์ํฌ์ ๋ฏธ์ง์)
stock.sell(price=None, condition='extended', execution=None) # ๋์ค๋ฅ, ๋ด์, ์๋ฉ์ค ์ฃผ๊ฐ๊ฑฐ๋ ์์ฅ๊ฐ ๋งค๋ (๋ชจ์ํฌ์ ๋ฏธ์ง์)
stock.sell(price=100, condition='extended', execution=None) # ๋์ค๋ฅ, ๋ด์, ์๋ฉ์ค ์ฃผ๊ฐ๊ฑฐ๋ ์ง์ ๊ฐ ๋งค๋ (๋ชจ์ํฌ์ ๋ฏธ์ง์)
from pykis import KisOrder
order: KisOrder = hynix.buy(price=194700, qty=1) # ๋งค์ ์ฃผ๋ฌธ
print(order.pending) # ๋ฏธ์ฒด๊ฒฐ ์ฃผ๋ฌธ์ธ์ง ์ฌ๋ถ
print(order.pending_order.pending_qty) # ๋ฏธ์ฒด๊ฒฐ ์๋
order: KisOrder = order.modify(price=195000) # ๋จ๊ฐ ์ ์
order: KisOrder = order.modify(qty=10) # ์๋ ์ ์
order.cancel() # ์ฃผ๋ฌธ ์ทจ์
# ๋ฏธ์ฒด๊ฒฐ ์ฃผ๋ฌธ ์ ์ฒด ์ทจ์
for order in account.pending_orders():
order.cancel()
PyKis์ ์น์์ผ ์ฐ๊ฒฐ๊ณผ ๊ตฌ๋ ๊ด๋ฆฌ๋ ๋ชจ๋ ์๋์ผ๋ก ์ด๋ฃจ์ด์ง๋๋ค. ๋ผ์ด๋ธ๋ฌ๋ฆฌ ํ์ฉ์ ์ด๋ฒคํธ ๋ฆฌ์ค๋์ ๋ฐ๊ธ๋๋ ํฐ์ผ๋ง ๊ด๋ฆฌํ๋ฉด ๋ฉ๋๋ค.
ํฐ์ผ์ Python GC์ ์ํด ์๋ฉธ๋๊ธฐ ์ ๊น์ง ์น์์ผ ๊ตฌ๋ ์ ์ ์งํฉ๋๋ค. ๋์ผ ์ด๋ฒคํธ๋ฅผ ๋ค์ค ์์ ํ๋๋ผ๋ ๋ํผ๋ฐ์ค ์นด์ดํฐ์ ์ํด ์์ ํ๊ฒ ๊ด๋ฆฌ๋ฉ๋๋ค.
์ด๋ฒคํธ ์์ ์ ํ๋๋ฐ, ๋ฐ๋ก ์ทจ์๋๋ ๊ฒฝ์ฐ๊ฐ ์์ต๋๋ค. ์ด๋ ์ด๋ฒคํธ ์์ ์ ์ํ ํฐ์ผ์ด GC์ ์ํด ์๋ฉธ๋์๊ธฐ ๋๋ฌธ์ ๋๋ค.
์๋์ ๊ฐ์ ์ฝ๋์์ ํฐ์ผ์ด ์๋ฉธ๋ ์ ์์ต๋๋ค.
from pykis import PyKis, KisWebsocketClient, KisSubscriptionEventArgs, KisRealtimePrice
kis = PyKis("secret.json", keep_token=True)
def on_price(sender: KisWebsocketClient, e: KisSubscriptionEventArgs[KisRealtimePrice]):
print(e.response)
def add_event():
# ์ด๋ฒคํธ ๋ฆฌ์ค๋ ํจ์๋ ๋ชจ๋ KisEventTicket์ ๋ฐํํฉ๋๋ค.
# ํด๋น ํฐ์ผ์ ๊ด๋ฆฌํ์ง ์์ผ๋ฉด GC์ ์ํด ์๋ฉธ๋ฉ๋๋ค.
kis.stock("000660").on("price", on_price)
kis.stock("005930").on("price", on_price)
kis.stock("039200").on("price", on_price)
add_event()
print(kis.websocket.subscriptions) # ํ์ฌ ๊ตฌ๋
์ค์ธ ์ด๋ฒคํธ ๋ชฉ๋ก
input("Press Enter to exit...")
[07/31 16:51:27] INFO: RTC Connected to real server
[07/31 16:51:27] INFO: RTC Subscribed to H0STCNT0.005930
[07/31 16:51:27] INFO: RTC Unsubscribed from H0STCNT0.005930
[07/31 16:51:27] INFO: RTC Subscribed to H0STCNT0.039200
set() # ๊ตฌ๋
์ค์ธ ์ด๋ฒคํธ ๋ชฉ๋ก์ด ๋น์ด์์ต๋๋ค.
Press Enter to exit...
[07/31 16:51:27] INFO: RTC Unsubscribed from H0STCNT0.039200
ํด๋น ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด 2๊ฐ์ง ๋ฐฉ๋ฒ์ด ์์ต๋๋ค.
- ํฐ์ผ์ ์ ์ญ ๋ณ์๋ก ์ ์งํ์ฌ GC์ ์ํด ์๋ฉธ๋์ง ์๋๋ก ํฉ๋๋ค.
tickets = [] def add_event(): tickets.append(kis.stock("000660").on("price", on_price)) tickets.append(kis.stock("005930").on("price", on_price)) tickets.append(kis.stock("039200").on("price", on_price)) add_event() print(kis.websocket.subscriptions) # ํ์ฌ ๊ตฌ๋ ์ค์ธ ์ด๋ฒคํธ ๋ชฉ๋ก input("Press Enter to exit...") for ticket in tickets: ticket.unsubscribe()
-
with
๊ตฌ๋ฌธ์ ์ด์ฉํ์ฌ ๋ฒ์ ๋ด์์๋ง ํฐ์ผ์ ์ ์งํฉ๋๋ค.def add_event(): with kis.stock("000660").on("price", on_price) as ticket: input("Press Enter to exit...") add_event()
- ํฐ์ผ์ด GC์ ์ํด ๊ตฌ๋
์ทจ์๋์ง ์๋๋ก ํ๋๊ทธ๋ฅผ ์ง์ ํฉ๋๋ค.
def add_event(): kis.stock("000660").on("price", on_price).suppress() kis.stock("005930").on("price", on_price).suppress() kis.stock("039200").on("price", on_price).suppress() add_event() print(kis.websocket.subscriptions) # ํ์ฌ ๊ตฌ๋ ์ค์ธ ์ด๋ฒคํธ ๋ชฉ๋ก input("Press Enter to exit...")
๊ตญ๋ด์ฃผ์ ๋ฐ ํด์ธ์ฃผ์์ ์ค์๊ฐ ์ฒด๊ฒฐ๊ฐ ์กฐํ๋ stock.on("price", callback)
ํจ์๋ฅผ ์ด์ฉํ์ฌ ์์ ํ ์ ์์ต๋๋ค.
from pykis import KisRealtimePrice, KisSubscriptionEventArgs, KisWebsocketClient, PyKis
def on_price(sender: KisWebsocketClient, e: KisSubscriptionEventArgs[KisRealtimePrice]):
print(e.response)
ticket = hynix.on("price", on_price)
print(kis.websocket.subscriptions) # ํ์ฌ ๊ตฌ๋
์ค์ธ ์ด๋ฒคํธ ๋ชฉ๋ก
input("Press Enter to exit...")
ticket.unsubscribe()
{KisWebsocketTR(id='H0STCNT0', key='000660')}
Press Enter to exit...
[08/02 13:50:42] INFO: RTC Connected to real server
[08/02 13:50:42] INFO: RTC Restoring subscriptions... H0STCNT0.000660
[08/02 13:50:42] INFO: RTC Subscribed to H0STCNT0.000660
KisDomesticRealtimePrice(market='KRX', symbol='000660', time='2024-08-02T13:50:44+09:00', price=174900, change=-18400, volume=8919304, amount=1587870362300)
KisDomesticRealtimePrice(market='KRX', symbol='000660', time='2024-08-02T13:50:44+09:00', price=174800, change=-18500, volume=8919354, amount=1587879102300)
KisDomesticRealtimePrice(market='KRX', symbol='000660', time='2024-08-02T13:50:45+09:00', price=174800, change=-18500, volume=8919358, amount=1587879801500)
KisDomesticRealtimePrice(market='KRX', symbol='000660', time='2024-08-02T13:50:45+09:00', price=174900, change=-18400, volume=8920313, amount=1588046831000)
KisDomesticRealtimePrice(market='KRX', symbol='000660', time='2024-08-02T13:50:45+09:00', price=174800, change=-18500, volume=8920319, amount=1588047879800)
[08/02 13:50:48] INFO: RTC Unsubscribed from H0STCNT0.000660
๊ตญ๋ด์ฃผ์ ๋ฐ ํด์ธ์ฃผ์์ ์ค์๊ฐ ํธ๊ฐ ์กฐํ๋ stock.on("orderbook", callback)
ํจ์๋ฅผ ์ด์ฉํ์ฌ ์์ ํ ์ ์์ต๋๋ค.
from pykis import KisRealtimeOrderbook, KisSubscriptionEventArgs, KisWebsocketClient, PyKis
def on_orderbook(sender: KisWebsocketClient, e: KisSubscriptionEventArgs[KisRealtimeOrderbook]):
print(e.response)
ticket = hynix.on("orderbook", on_orderbook)
print(kis.websocket.subscriptions) # ํ์ฌ ๊ตฌ๋
์ค์ธ ์ด๋ฒคํธ ๋ชฉ๋ก
input("Press Enter to exit...")
ticket.unsubscribe()
{KisWebsocketTR(id='H0STASP0', key='000660')}
Press Enter to exit...
[08/02 14:22:52] INFO: RTC Connected to real server
[08/02 14:22:52] INFO: RTC Restoring subscriptions... H0STASP0.000660
[08/02 14:22:52] INFO: RTC Subscribed to H0STASP0.000660
KisDomesticRealtimeOrderbook(
market='KRX',
symbol='000660',
asks=[
KisDomesticRealtimeOrderbookItem(price=174000, volume=10689),
KisDomesticRealtimeOrderbookItem(price=174100, volume=7197),
KisDomesticRealtimeOrderbookItem(price=174200, volume=3430),
KisDomesticRealtimeOrderbookItem(price=174300, volume=3120),
KisDomesticRealtimeOrderbookItem(price=174400, volume=4865),
KisDomesticRealtimeOrderbookItem(price=174500, volume=477),
KisDomesticRealtimeOrderbookItem(price=174600, volume=1818),
KisDomesticRealtimeOrderbookItem(price=174700, volume=3344),
KisDomesticRealtimeOrderbookItem(price=174800, volume=2184),
KisDomesticRealtimeOrderbookItem(price=174900, volume=2266)
],
bids=[
KisDomesticRealtimeOrderbookItem(price=173900, volume=12054),
KisDomesticRealtimeOrderbookItem(price=173800, volume=15792),
KisDomesticRealtimeOrderbookItem(price=173700, volume=12568),
KisDomesticRealtimeOrderbookItem(price=173600, volume=19204),
KisDomesticRealtimeOrderbookItem(price=173500, volume=71514),
KisDomesticRealtimeOrderbookItem(price=173400, volume=6470),
KisDomesticRealtimeOrderbookItem(price=173300, volume=9029),
KisDomesticRealtimeOrderbookItem(price=173200, volume=8204),
KisDomesticRealtimeOrderbookItem(price=173100, volume=15445),
KisDomesticRealtimeOrderbookItem(price=173000, volume=36000)
]
)
[08/02 14:22:56] INFO: RTC Unsubscribed from H0STASP0.000660
๊ตญ๋ด์ฃผ์ ๋ฐ ํด์ธ์ฃผ์์ ์ค์๊ฐ ์ฒด๊ฒฐ๋ด์ญ ์กฐํ๋ account.on("execution", callback)
ํจ์๋ฅผ ์ด์ฉํ์ฌ ์์ ํ ์ ์์ต๋๋ค.
from pykis import KisRealtimeExecution, KisSubscriptionEventArgs, KisWebsocketClient
account = kis.account()
def on_execution(sender: KisWebsocketClient, e: KisSubscriptionEventArgs[KisRealtimeExecution]):
print(e.response)
ticket = account.on("execution", on_execution)
print(kis.websocket.subscriptions) # ํ์ฌ ๊ตฌ๋
์ค์ธ ์ด๋ฒคํธ ๋ชฉ๋ก
input("Press Enter to exit...")
ticket.unsubscribe()
{KisWebsocketTR(id='H0GSCNI9', key='soju06'), KisWebsocketTR(id='H0STCNI9', key='soju06')}
Press Enter to exit...
[08/02 14:27:24] INFO: RTC Connected to real server
[08/02 14:27:24] INFO: RTC Connected to virtual server
[08/02 14:27:24] INFO: RTC Restoring subscriptions... H0GSCNI9.soju06
[08/02 14:27:24] INFO: RTC Restoring subscriptions... H0STCNI9.soju06
[08/02 14:27:24] INFO: RTC Subscribed to H0STCNI9.soju06
[08/02 14:27:24] INFO: RTC Subscribed to H0GSCNI9.soju06
KisDomesticRealtimeOrderExecution(account_number=KisAccountNumber('50113500-01'), market='KRX', symbol='000660', time='2024-08-02T14:28:25+09:00', type='buy', price=173600, executed_qty=0)
KisDomesticRealtimeOrderExecution(account_number=KisAccountNumber('50113500-01'), market='KRX', symbol='000660', time='2024-08-02T14:28:25+09:00', type='buy', price=173600, executed_qty=10)
KisDomesticRealtimeOrderExecution(account_number=KisAccountNumber('50113500-01'), market='KRX', symbol='000660', time='2024-08-02T14:28:25+09:00', type='buy', price=173600, executed_qty=10)