ごくごく一部の例
jquantsapi.Client
クラスを継承しているので、jquants-api-client同じように使える
import jquants_derivatives
cli = jquants_derivatives.Client()
%%time
df_20230605 = cli.get_option_index_option("2023-06-05")
CPU times: user 343 ms, sys: 313 ms, total: 657 ms Wall time: 656 ms
pip install ipython-sql
%load_ext sql
%sql sqlite:////home/driller/.jquants-api/jquantsapi.db
%sql SELECT name FROM sqlite_master WHERE type='table';
* sqlite:////home/driller/.jquants-api/jquantsapi.db Done.
name |
---|
FINS_ANNOUNCEMENT |
FINS_DIVIDEND |
FINS_STATEMENTS |
INDICES_TOPIX |
LISTED_INFO |
MARKETS_BREAKDOWN |
MARKET_SEGMENT |
MARKET_SHORT_SELLING |
OPTION_INDEX_OPTION |
PRICES_DAILY_QUOTES |
PRICES_DAILY_QUOTES_PREMIUM |
PRICES_PRICES_AM |
SECTOR_17 |
SECTOR_33 |
OPTION_INDEX_OPTION_PROCESSED |
%sql SELECT Date, UnderlyingPrice, StrikePrice FROM OPTION_INDEX_OPTION LIMIT 3;
* sqlite:////home/driller/.jquants-api/jquantsapi.db Done.
Date | UnderlyingPrice | StrikePrice |
---|---|---|
2021-02-12 00:00:00 | 29520.07 | 20000.0 |
2021-02-12 00:00:00 | 29520.07 | 20250.0 |
2021-02-12 00:00:00 | 29520.07 | 20500.0 |
import sqlite3
import pandas as pd
with sqlite3.connect(jquants_derivatives.database.db) as con:
query_df = pd.read_sql(
"SELECT Date, UnderlyingPrice, StrikePrice FROM OPTION_INDEX_OPTION", con
)
query_df.head()
Date | UnderlyingPrice | StrikePrice | |
---|---|---|---|
0 | 2021-02-12 00:00:00 | 29520.07 | 20000.0 |
1 | 2021-02-12 00:00:00 | 29520.07 | 20250.0 |
2 | 2021-02-12 00:00:00 | 29520.07 | 20500.0 |
3 | 2021-02-12 00:00:00 | 29520.07 | 20750.0 |
4 | 2021-02-12 00:00:00 | 29520.07 | 21000.0 |
DataFrameの各列はjquants_derivatives/modelsで定義したデータ型に自動で型変換される
@dataclass
class IndexOption(DataFrameColumnsBase):
Date: np.dtype("datetime64[ns]")
Code: str
WholeDayOpen: float
WholeDayHigh: float
WholeDayLow: float
WholeDayClose: float
...
LastTradingDay: np.dtype("datetime64[ns]")
SpecialQuotationDay: np.dtype("datetime64[ns]")
SettlementPrice: float
TheoreticalPrice: float
BaseVolatility: float
UnderlyingPrice: float
ImpliedVolatility: float
InterestRate: float
@classmethod
def get_dtype(cls, field: str) -> Type[Any]:
key = field.replace("(", "").replace(")", "")
return cls.__annotations__[key]
オプション四本値(/option/index_option)
のエンドポイントにしか対応していないfrom jquants_derivatives import Option
op_20230605 = Option(df_20230605, contracts=2)
op_20230605.contract_month
['2023-06', '2023-07']
op_20230605.underlying_price
{'2023-06': 32217.43, '2023-07': 32217.43}
op_20230605.contracts_dfs["2023-06"].head()
Date | Code | WholeDayOpen | WholeDayHigh | WholeDayLow | WholeDayClose | NightSessionOpen | NightSessionHigh | NightSessionLow | NightSessionClose | ... | EmergencyMarginTriggerDivision | PutCallDivision | LastTradingDay | SpecialQuotationDay | SettlementPrice | TheoreticalPrice | BaseVolatility | UnderlyingPrice | ImpliedVolatility | InterestRate | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 2023-06-05 | 138067618 | 3.0 | 3.0 | 1.0 | 1.0 | 3.0 | 3.0 | 3.0 | 3.0 | ... | 002 | 1 | 2023-06-08 | 2023-06-09 | 1.0 | 1.0000 | 0.195121 | 32217.43 | 0.513807 | -0.000664 |
1 | 2023-06-05 | 188067718 | 3.0 | 4.0 | 1.0 | 2.0 | 3.0 | 4.0 | 3.0 | 3.0 | ... | 002 | 1 | 2023-06-08 | 2023-06-09 | 2.0 | 2.0000 | 0.195121 | 32217.43 | 0.535335 | -0.000664 |
2 | 2023-06-05 | 138067818 | 3.0 | 4.0 | 2.0 | 2.0 | 3.0 | 4.0 | 3.0 | 3.0 | ... | 002 | 1 | 2023-06-08 | 2023-06-09 | 2.0 | 2.0844 | 0.195121 | 32217.43 | 0.520748 | -0.000664 |
3 | 2023-06-05 | 188068018 | 3.0 | 4.0 | 1.0 | 2.0 | 3.0 | 4.0 | 3.0 | 3.0 | ... | 002 | 1 | 2023-06-08 | 2023-06-09 | 2.0 | 2.0000 | 0.195121 | 32217.43 | 0.506194 | -0.000664 |
4 | 2023-06-05 | 138068118 | 4.0 | 4.0 | 2.0 | 2.0 | 4.0 | 4.0 | 3.0 | 3.0 | ... | 002 | 1 | 2023-06-08 | 2023-06-09 | 2.0 | 2.0000 | 0.195121 | 32217.43 | 0.491672 | -0.000664 |
5 rows × 30 columns
Plotlyによる可視化
# スライド表示用なので、実際にノートブックで実行するときはスキップしてください
import plotly.io as pio
pio.renderers.default = "png"
jquants_derivatives.plot_volatility(op_20230605).update_layout(width=800)
df_20230602 = cli.get_option_index_option("2023-06-02")
op_20230602 = Option(df_20230602)
jquants_derivatives.plot_volatility(op_20230605, op_20230602).update_layout(width=800)
import pickle
with open("save_fig.pickle", "rb") as f:
save_fig = pickle.load(f)
save_fig["原資産価格(日経225)とインプライドボラティリティの時系列データ"]
save_fig["原資産価格とボラティリティの騰落率"]
save_fig["ボラティリティの上昇局面・下落局面においての原資産価格の分布"]
save_fig["原資産価格とボラティリティの騰落率(2023年5月以降のアゲモリ)"]
save_fig["アゲモリ局面の原資産価格の分布"]
save_fig["アゲモリ局面の原資産価格とボラティリティの分布"]
save_fig["原資産をロングした場合の損益と、ボラティリティのショートを組み合わせた損益"]
save_fig["2019年11月5日のボラティリティスマイル"]
save_fig["2019年11月6日のボラティリティスマイル"]