libfinance 0.0.2__tar.gz → 0.0.6__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- libfinance-0.0.6/PKG-INFO +83 -0
- {libfinance-0.0.2 → libfinance-0.0.6}/README.md +12 -1
- {libfinance-0.0.2 → libfinance-0.0.6}/libfinance/__init__.py +4 -2
- libfinance-0.0.6/libfinance/api/__init__.py +0 -0
- {libfinance-0.0.2 → libfinance-0.0.6}/libfinance/api/calendar.py +16 -16
- libfinance-0.0.2/libfinance/api/concept.py → libfinance-0.0.6/libfinance/api/concept_components.py +2 -5
- libfinance-0.0.6/libfinance/api/get_price.py +269 -0
- {libfinance-0.0.2 → libfinance-0.0.6}/libfinance/api/index_components.py +1 -6
- libfinance-0.0.6/libfinance/api/instrument.py +216 -0
- libfinance-0.0.6/libfinance/client.py +166 -0
- {libfinance-0.0.2/libfinance/api → libfinance-0.0.6/libfinance/utils}/__init__.py +2 -0
- {libfinance-0.0.2 → libfinance-0.0.6}/libfinance/utils/datetime_func.py +0 -2
- {libfinance-0.0.2 → libfinance-0.0.6}/libfinance/utils/decorators.py +1 -5
- libfinance-0.0.6/libfinance/utils/utils.py +271 -0
- libfinance-0.0.6/libfinance/utils/validators.py +142 -0
- libfinance-0.0.6/libfinance.egg-info/PKG-INFO +83 -0
- {libfinance-0.0.2 → libfinance-0.0.6}/libfinance.egg-info/SOURCES.txt +5 -5
- {libfinance-0.0.2 → libfinance-0.0.6}/libfinance.egg-info/dependency_links.txt +0 -0
- libfinance-0.0.6/libfinance.egg-info/requires.txt +3 -0
- {libfinance-0.0.2 → libfinance-0.0.6}/libfinance.egg-info/top_level.txt +0 -0
- libfinance-0.0.6/pyproject.toml +11 -0
- {libfinance-0.0.2 → libfinance-0.0.6}/setup.cfg +0 -0
- libfinance-0.0.6/setup.py +7 -0
- libfinance-0.0.2/PKG-INFO +0 -16
- libfinance-0.0.2/libfinance/api/get_price.py +0 -176
- libfinance-0.0.2/libfinance/client.py +0 -148
- libfinance-0.0.2/libfinance/libfinance.thrift +0 -16
- libfinance-0.0.2/libfinance/utils/__init__.py +0 -8
- libfinance-0.0.2/libfinance/utils/loader.py +0 -44
- libfinance-0.0.2/libfinance/utils/serialize.py +0 -38
- libfinance-0.0.2/libfinance.egg-info/PKG-INFO +0 -16
- libfinance-0.0.2/libfinance.egg-info/not-zip-safe +0 -1
- libfinance-0.0.2/libfinance.egg-info/requires.txt +0 -3
- libfinance-0.0.2/setup.py +0 -33
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: libfinance
|
|
3
|
+
Version: 0.0.6
|
|
4
|
+
Summary: libfinance is a modern finance API
|
|
5
|
+
Description-Content-Type: text/markdown
|
|
6
|
+
Requires-Dist: lz4
|
|
7
|
+
Requires-Dist: msgpack
|
|
8
|
+
Requires-Dist: pandas
|
|
9
|
+
|
|
10
|
+
# libfinance
|
|
11
|
+
\[ English | [中文](README_zh.md) \]
|
|
12
|
+
|
|
13
|
+
`libfinance` is python library for accessing financial data easily. The goal of `libfinance` is to provide HIGH quality financial data for scholars, researchers and developers in research and production environment.
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
# install
|
|
17
|
+
|
|
18
|
+
Method1: Install `libfinance` by `pip`
|
|
19
|
+
|
|
20
|
+
``` {.sourceCode .bash}
|
|
21
|
+
$ pip install libfinance
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
Method2: Install from source with a develop mode
|
|
25
|
+
~~~
|
|
26
|
+
git clone https://github.com/StateOfTheArt-quant/libfinance.git
|
|
27
|
+
cd libfinance
|
|
28
|
+
python -m pip install --editable .
|
|
29
|
+
~~~
|
|
30
|
+
|
|
31
|
+
# quick-start
|
|
32
|
+
|
|
33
|
+
~~~
|
|
34
|
+
from libfinance import get_trading_dates, get_price
|
|
35
|
+
|
|
36
|
+
trading_dates = get_trading_dates(start_date="2023-12-25", end_date="2024-01-11")
|
|
37
|
+
print(trading_dates)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
DatetimeIndex(['2023-12-25', '2023-12-26', '2023-12-27', '2023-12-28',
|
|
41
|
+
'2023-12-29', '2024-01-02', '2024-01-03', '2024-01-04',
|
|
42
|
+
'2024-01-05', '2024-01-08', '2024-01-09', '2024-01-10',
|
|
43
|
+
'2024-01-11'],
|
|
44
|
+
dtype='datetime64[ns]', freq=None)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
data = get_price(order_book_ids=["000001.XSHE","600000.XSHG"], start_date="2024-03-01", end_date="2024-03-11")
|
|
50
|
+
print(data)
|
|
51
|
+
|
|
52
|
+
open high low close volume
|
|
53
|
+
order_book_id datetime
|
|
54
|
+
000001.XSHE 2024-03-01 10.59 10.60 10.43 10.49 182810290.0
|
|
55
|
+
2024-03-04 10.45 10.50 10.32 10.33 165592954.0
|
|
56
|
+
2024-03-05 10.30 10.47 10.26 10.43 181731907.0
|
|
57
|
+
2024-03-06 10.40 10.45 10.33 10.33 134564016.0
|
|
58
|
+
2024-03-07 10.33 10.64 10.33 10.38 201616589.0
|
|
59
|
+
2024-03-08 10.35 10.44 10.30 10.38 111397428.0
|
|
60
|
+
2024-03-11 10.38 10.47 10.34 10.47 121067298.0
|
|
61
|
+
600000.XSHG 2024-03-01 7.13 7.16 7.10 7.11 29431801.0
|
|
62
|
+
2024-03-04 7.12 7.12 7.05 7.07 27855963.0
|
|
63
|
+
2024-03-05 7.05 7.18 7.04 7.16 41756232.0
|
|
64
|
+
2024-03-06 7.17 7.22 7.12 7.12 25918749.0
|
|
65
|
+
2024-03-07 7.12 7.20 7.11 7.14 24690348.0
|
|
66
|
+
2024-03-08 7.12 7.17 7.11 7.12 19861794.0
|
|
67
|
+
2024-03-11 7.13 7.17 7.06 7.11 26195498.0
|
|
68
|
+
~~~
|
|
69
|
+
|
|
70
|
+
# Documentation
|
|
71
|
+
|
|
72
|
+
More info for [Documentation](https://libfinance.readthedocs.io/zh/latest/)
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
# Join us community
|
|
76
|
+
|
|
77
|
+
Join us WeChat Official Accounts to get the [libfinance](https://github.com/StateOfTheArt-quant/libfinance) updated info:
|
|
78
|
+
|
|
79
|
+
<div>
|
|
80
|
+
<img alt="ds" src="/docs/source/_static/img/code.png" width="600" height="220">
|
|
81
|
+
</div>
|
|
82
|
+
|
|
83
|
+
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
# libfinance
|
|
2
|
+
\[ English | [中文](README_zh.md) \]
|
|
2
3
|
|
|
3
4
|
`libfinance` is python library for accessing financial data easily. The goal of `libfinance` is to provide HIGH quality financial data for scholars, researchers and developers in research and production environment.
|
|
4
5
|
|
|
@@ -15,7 +16,7 @@ Method2: Install from source with a develop mode
|
|
|
15
16
|
~~~
|
|
16
17
|
git clone https://github.com/StateOfTheArt-quant/libfinance.git
|
|
17
18
|
cd libfinance
|
|
18
|
-
pip install
|
|
19
|
+
python -m pip install --editable .
|
|
19
20
|
~~~
|
|
20
21
|
|
|
21
22
|
# quick-start
|
|
@@ -61,3 +62,13 @@ order_book_id datetime
|
|
|
61
62
|
|
|
62
63
|
More info for [Documentation](https://libfinance.readthedocs.io/zh/latest/)
|
|
63
64
|
|
|
65
|
+
|
|
66
|
+
# Join us community
|
|
67
|
+
|
|
68
|
+
Join us WeChat Official Accounts to get the [libfinance](https://github.com/StateOfTheArt-quant/libfinance) updated info:
|
|
69
|
+
|
|
70
|
+
<div>
|
|
71
|
+
<img alt="ds" src="/docs/source/_static/img/code.png" width="600" height="220">
|
|
72
|
+
</div>
|
|
73
|
+
|
|
74
|
+
|
|
File without changes
|
|
@@ -1,18 +1,22 @@
|
|
|
1
|
-
#!/usr/bin/env python3
|
|
2
|
-
# -*- coding: utf-8 -*-
|
|
3
1
|
import bisect
|
|
4
2
|
import datetime
|
|
5
3
|
import pandas as pd
|
|
6
|
-
|
|
4
|
+
import json
|
|
7
5
|
from libfinance.client import get_client
|
|
8
6
|
from libfinance.utils.decorators import export_as_api, ttl_cache, compatible_with_parm
|
|
9
7
|
|
|
8
|
+
|
|
10
9
|
def _to_timestamp(d):
|
|
11
10
|
return pd.Timestamp(d).replace(hour=0, minute=0, second=0, microsecond=0)
|
|
12
11
|
|
|
12
|
+
@export_as_api
|
|
13
13
|
@ttl_cache(24 * 3600)
|
|
14
|
-
def
|
|
15
|
-
|
|
14
|
+
def get_all_trading_dates():
|
|
15
|
+
trading_calendar_json = get_client().get_trading_calendar()
|
|
16
|
+
return pd.to_datetime(json.loads(trading_calendar_json))
|
|
17
|
+
|
|
18
|
+
def public_api():
|
|
19
|
+
return get_client().public_api()
|
|
16
20
|
|
|
17
21
|
@export_as_api
|
|
18
22
|
def get_trading_dates(start_date, end_date):
|
|
@@ -38,7 +42,7 @@ def get_trading_dates(start_date, end_date):
|
|
|
38
42
|
'2020-05-15', '2020-05-18', '2020-05-19', '2020-05-20'],
|
|
39
43
|
dtype='datetime64[ns]', freq=None)
|
|
40
44
|
"""
|
|
41
|
-
trading_dates =
|
|
45
|
+
trading_dates = get_all_trading_dates()
|
|
42
46
|
|
|
43
47
|
start_date = _to_timestamp(start_date)
|
|
44
48
|
end_date = _to_timestamp(end_date)
|
|
@@ -64,7 +68,7 @@ def get_previous_trading_date(date, n=1):
|
|
|
64
68
|
>>> get_previous_trading_date(date='2020-05-18', n=3)
|
|
65
69
|
Timestamp('2020-05-13 00:00:00')
|
|
66
70
|
"""
|
|
67
|
-
trading_dates =
|
|
71
|
+
trading_dates = get_all_trading_dates()
|
|
68
72
|
pos = trading_dates.searchsorted(_to_timestamp(date))
|
|
69
73
|
if pos >= n:
|
|
70
74
|
return trading_dates[pos - n]
|
|
@@ -88,7 +92,7 @@ def get_next_trading_date(date, n=1):
|
|
|
88
92
|
>>> get_next_trading_date(date='2020-05-13', n=3)
|
|
89
93
|
Timestamp('2020-05-18 00:00:00')
|
|
90
94
|
"""
|
|
91
|
-
trading_dates =
|
|
95
|
+
trading_dates = get_all_trading_dates()
|
|
92
96
|
pos = trading_dates.searchsorted(_to_timestamp(date), side='right')
|
|
93
97
|
if pos + n > len(trading_dates):
|
|
94
98
|
return trading_dates[-1]
|
|
@@ -98,13 +102,13 @@ def get_next_trading_date(date, n=1):
|
|
|
98
102
|
@export_as_api
|
|
99
103
|
def is_trading_date(date):
|
|
100
104
|
date = _to_timestamp(date)
|
|
101
|
-
trading_dates =
|
|
105
|
+
trading_dates = get_all_trading_dates()
|
|
102
106
|
pos = trading_dates.searchsorted(date)
|
|
103
107
|
return pos < len(trading_dates) and trading_dates[pos] == date
|
|
104
108
|
|
|
105
109
|
@export_as_api
|
|
106
110
|
def get_n_trading_dates_until(date, n):
|
|
107
|
-
trading_dates =
|
|
111
|
+
trading_dates = get_all_trading_dates()
|
|
108
112
|
pos = trading_dates.searchsorted(_to_timestamp(date), side='right')
|
|
109
113
|
if pos >= n:
|
|
110
114
|
return trading_dates[pos - n:pos]
|
|
@@ -114,7 +118,7 @@ def get_n_trading_dates_until(date, n):
|
|
|
114
118
|
def count_trading_dates(start_date, end_date):
|
|
115
119
|
start_date = _to_timestamp(start_date)
|
|
116
120
|
end_date = _to_timestamp(end_date)
|
|
117
|
-
trading_dates =
|
|
121
|
+
trading_dates = get_all_trading_dates()
|
|
118
122
|
return trading_dates.searchsorted(end_date, side='right') - trading_dates.searchsorted(start_date)
|
|
119
123
|
|
|
120
124
|
|
|
@@ -122,8 +126,4 @@ if __name__ == "__main__":
|
|
|
122
126
|
start_date = "2024-01-01"
|
|
123
127
|
end_date = "2024-02-27"
|
|
124
128
|
|
|
125
|
-
trading_dates = get_trading_dates(start_date, end_date)
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
129
|
+
trading_dates = get_trading_dates(start_date, end_date)
|
libfinance-0.0.2/libfinance/api/concept.py → libfinance-0.0.6/libfinance/api/concept_components.py
RENAMED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
#!/usr/bin/env python3
|
|
2
|
-
# -*- coding: utf-8 -*-
|
|
3
1
|
import datetime
|
|
4
2
|
from typing import Any, Union, Optional, Iterable, Dict, List, Sequence, Iterable
|
|
5
3
|
|
|
@@ -39,7 +37,7 @@ def get_concept_meta(source:str="THS") -> pd.DataFrame:
|
|
|
39
37
|
return get_client().get_concept_meta(source=source)
|
|
40
38
|
|
|
41
39
|
@export_as_api
|
|
42
|
-
def get_concept_weights(concept_ids:list, source:str="THS") -> pd.DataFrame:
|
|
40
|
+
def get_concept_weights(concept_ids:list, date=None, source:str="THS") -> pd.DataFrame:
|
|
43
41
|
"""
|
|
44
42
|
获取某一个概念的成分股及其权重数据
|
|
45
43
|
:param concept_ids: 概念id的列表
|
|
@@ -83,5 +81,4 @@ def get_concept_weights(concept_ids:list, source:str="THS") -> pd.DataFrame:
|
|
|
83
81
|
|
|
84
82
|
[25 rows x 7 columns]
|
|
85
83
|
"""
|
|
86
|
-
return get_client().get_concept_weights(concept_ids=concept_ids, source = source)
|
|
87
|
-
|
|
84
|
+
return get_client().get_concept_weights(concept_ids=concept_ids, source = source)
|
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
from typing import List, Union
|
|
4
|
+
import datetime
|
|
5
|
+
|
|
6
|
+
import pandas as pd
|
|
7
|
+
import json
|
|
8
|
+
import warnings
|
|
9
|
+
import pdb
|
|
10
|
+
|
|
11
|
+
from libfinance.client import get_client
|
|
12
|
+
from libfinance.utils.decorators import export_as_api, ttl_cache, compatible_with_parm
|
|
13
|
+
from libfinance.utils.datetime_func import convert_dateteime_to_timestamp
|
|
14
|
+
from libfinance.utils.utils import to_date_str
|
|
15
|
+
|
|
16
|
+
from libfinance.utils.validators import (
|
|
17
|
+
ensure_string,
|
|
18
|
+
ensure_list_of_string,
|
|
19
|
+
check_items_in_container,
|
|
20
|
+
ensure_instruments,
|
|
21
|
+
ensure_date_range,
|
|
22
|
+
is_panel_removed,
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
DAYBAR_FIELDS = {
|
|
26
|
+
"future": ["settlement", "prev_settlement", "open_interest", "limit_up", "limit_down",
|
|
27
|
+
"day_session_open"],
|
|
28
|
+
"common": ["open", "close", "high", "low", "total_turnover", "volume", "prev_close"],
|
|
29
|
+
"stock": ["limit_up", "limit_down"],
|
|
30
|
+
"fund": ["limit_up", "limit_down", "num_trades", "iopv"],
|
|
31
|
+
"spot": ["settlement", "prev_settlement", "open_interest", "limit_up", "limit_down"],
|
|
32
|
+
"option": ["open_interest", "strike_price", "contract_multiplier", "prev_settlement", "settlement", "limit_up",
|
|
33
|
+
"limit_down", "day_session_open"],
|
|
34
|
+
"convertible": ["limit_up", "limit_down", "num_trades"],
|
|
35
|
+
"index": [],
|
|
36
|
+
"repo": ["num_trades"],
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
WEEKBAR_FIELDS = {
|
|
40
|
+
"future": ["settlement", "prev_settlement", "open_interest", "day_session_open"],
|
|
41
|
+
"common": ["open", "close", "high", "low", "total_turnover", "volume"],
|
|
42
|
+
"stock": ["num_trades"],
|
|
43
|
+
"fund": ["num_trades", "iopv"],
|
|
44
|
+
"spot": ["settlement", "prev_settlement", "open_interest"],
|
|
45
|
+
"option": ["open_interest", "strike_price", "contract_multiplier", "settlement", "day_session_open"],
|
|
46
|
+
"convertible": ["num_trades"],
|
|
47
|
+
"index": [],
|
|
48
|
+
"repo": ["num_trades"],
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
MINBAR_FIELDS = {
|
|
52
|
+
"future": ["trading_date", "open_interest"],
|
|
53
|
+
"common": ["open", "close", "high", "low", "total_turnover", "volume"],
|
|
54
|
+
"stock": ["num_trades"],
|
|
55
|
+
"fund": ["num_trades", "iopv"],
|
|
56
|
+
"spot": ["trading_date", "open_interest"],
|
|
57
|
+
"option": ["trading_date", "open_interest"],
|
|
58
|
+
"convertible": ["num_trades"],
|
|
59
|
+
"index": [],
|
|
60
|
+
"repo": [],
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
def classify_order_book_ids(order_book_ids):
|
|
64
|
+
ins_list = ensure_instruments(order_book_ids)
|
|
65
|
+
_order_book_ids = []
|
|
66
|
+
stocks = []
|
|
67
|
+
funds = []
|
|
68
|
+
indexes = []
|
|
69
|
+
futures = []
|
|
70
|
+
futures_888 = {}
|
|
71
|
+
spots = []
|
|
72
|
+
options = []
|
|
73
|
+
convertibles = []
|
|
74
|
+
repos = []
|
|
75
|
+
for ins in ins_list:
|
|
76
|
+
if ins.order_book_id not in _order_book_ids:
|
|
77
|
+
_order_book_ids.append(ins.order_book_id)
|
|
78
|
+
if ins.type == "CS":
|
|
79
|
+
stocks.append(ins.order_book_id)
|
|
80
|
+
elif ins.type == "INDX":
|
|
81
|
+
indexes.append(ins.order_book_id)
|
|
82
|
+
elif ins.type in {"ETF", "LOF", "SF", "FUND"}:
|
|
83
|
+
funds.append(ins.order_book_id)
|
|
84
|
+
elif ins.type == "Future":
|
|
85
|
+
if ins.order_book_id.endswith(("88", "889")):
|
|
86
|
+
futures_888[ins.order_book_id] = ins.underlying_symbol
|
|
87
|
+
futures.append(ins)
|
|
88
|
+
elif ins.type == "Spot":
|
|
89
|
+
spots.append(ins.order_book_id)
|
|
90
|
+
elif ins.type == "Option":
|
|
91
|
+
options.append(ins.order_book_id)
|
|
92
|
+
elif ins.type == "Convertible":
|
|
93
|
+
convertibles.append(ins.order_book_id)
|
|
94
|
+
elif ins.type == "Repo":
|
|
95
|
+
repos.append(ins.order_book_id)
|
|
96
|
+
return _order_book_ids, stocks, funds, indexes, futures, futures_888, spots, options, convertibles, repos
|
|
97
|
+
|
|
98
|
+
def _ensure_date(start_date, end_date, stocks, funds, indexes, futures, spots, options, convertibles, repos):
|
|
99
|
+
default_start_date, default_end_date = ensure_date_range(start_date, end_date)
|
|
100
|
+
|
|
101
|
+
start_date = to_date_str(start_date) if start_date else default_start_date
|
|
102
|
+
end_date = to_date_str(end_date) if end_date else default_end_date
|
|
103
|
+
if start_date < "2000-01-04":
|
|
104
|
+
warnings.warn("start_date is earlier than 2000-01-04, adjusted to 2000-01-04")
|
|
105
|
+
start_date = "2000-01-04"
|
|
106
|
+
return start_date, end_date
|
|
107
|
+
|
|
108
|
+
def _ensure_fields(fields, fields_dict, stocks, funds, futures, futures888, spots, options, convertibles, indexes,
|
|
109
|
+
repos):
|
|
110
|
+
has_dominant_id = False
|
|
111
|
+
future_only = futures and not any([stocks, funds, spots, options, convertibles, indexes, repos])
|
|
112
|
+
all_fields = set(fields_dict["common"])
|
|
113
|
+
if futures:
|
|
114
|
+
all_fields.update(fields_dict["future"])
|
|
115
|
+
if stocks:
|
|
116
|
+
all_fields.update(fields_dict["stock"])
|
|
117
|
+
if funds:
|
|
118
|
+
all_fields.update(fields_dict["fund"])
|
|
119
|
+
if spots:
|
|
120
|
+
all_fields.update(fields_dict["spot"])
|
|
121
|
+
if options:
|
|
122
|
+
all_fields.update(fields_dict["option"])
|
|
123
|
+
if convertibles:
|
|
124
|
+
all_fields.update(fields_dict["convertible"])
|
|
125
|
+
if indexes:
|
|
126
|
+
all_fields.update(fields_dict["index"])
|
|
127
|
+
if repos:
|
|
128
|
+
all_fields.update(fields_dict["repo"])
|
|
129
|
+
if future_only and futures888 and len(futures) == len(futures888) and not fields:
|
|
130
|
+
has_dominant_id = True
|
|
131
|
+
|
|
132
|
+
if fields:
|
|
133
|
+
fields = ensure_list_of_string(fields, "fields")
|
|
134
|
+
fields_set = set(fields)
|
|
135
|
+
if len(fields_set) < len(fields):
|
|
136
|
+
warnings.warn("duplicated fields: %s" % [f for f in fields if fields.count(f) > 1])
|
|
137
|
+
fields = list(fields_set)
|
|
138
|
+
# 只有期货类型
|
|
139
|
+
if 'dominant_id' in fields:
|
|
140
|
+
fields.remove("dominant_id")
|
|
141
|
+
if not fields:
|
|
142
|
+
raise ValueError("can't get dominant_id separately, please use futures.get_dominant")
|
|
143
|
+
if futures888:
|
|
144
|
+
has_dominant_id = True
|
|
145
|
+
else:
|
|
146
|
+
warnings.warn(
|
|
147
|
+
"only if one of the order_book_id is future and contains 88/888/99/889 can the dominant_id be selected in fields")
|
|
148
|
+
check_items_in_container(fields, all_fields, "fields")
|
|
149
|
+
return fields, has_dominant_id
|
|
150
|
+
else:
|
|
151
|
+
return list(all_fields), has_dominant_id
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
@export_as_api
|
|
156
|
+
def get_price(
|
|
157
|
+
order_book_ids: list,
|
|
158
|
+
start_date: str,
|
|
159
|
+
end_date: str,
|
|
160
|
+
frequency: str="1d",
|
|
161
|
+
fields: List[str]=None,
|
|
162
|
+
skip_suspended: bool=True,
|
|
163
|
+
include_now: bool=True,
|
|
164
|
+
adjust_type: str="none",
|
|
165
|
+
adjust_orig:datetime.datetime = None) -> pd.DataFrame:
|
|
166
|
+
"""获取指定合约的历史 k 线行情,支持任意日频率xd(1d,5d)和任意分钟频率xm(1m,3m,5m,15m)的历史数据。
|
|
167
|
+
|
|
168
|
+
:param order_book_ids: 多个标的合约代码, 必填项
|
|
169
|
+
:param start_date: 开始日期,必填项
|
|
170
|
+
:param end_date: 结束日期,必填项
|
|
171
|
+
:param frequency: 获取数据什么样的频率进行。'1d'或'1m'分别表示每日和每分钟
|
|
172
|
+
:param fields: 返回数据字段
|
|
173
|
+
:param skip_suspended: 是否跳过停牌数据
|
|
174
|
+
:param include_now: 是否包含当前数据
|
|
175
|
+
:param adjust_type: 复权类型,默认为前复权 pre;可选 pre, none, post
|
|
176
|
+
|
|
177
|
+
========================= ===================================================
|
|
178
|
+
fields 字段名
|
|
179
|
+
========================= ===================================================
|
|
180
|
+
datetime 时间戳
|
|
181
|
+
open 开盘价
|
|
182
|
+
high 最高价
|
|
183
|
+
low 最低价
|
|
184
|
+
close 收盘价
|
|
185
|
+
volume 成交量
|
|
186
|
+
total_turnover 成交额
|
|
187
|
+
open_interest 持仓量(期货专用)
|
|
188
|
+
basis_spread 期现差(股指期货专用)
|
|
189
|
+
settlement 结算价(期货日线专用)
|
|
190
|
+
prev_settlement 结算价(期货日线专用)
|
|
191
|
+
========================= ===================================================
|
|
192
|
+
|
|
193
|
+
Example1::
|
|
194
|
+
|
|
195
|
+
获取中国平安和浦发银行 2024-03-01至2024-03-11之间的交易数据
|
|
196
|
+
|
|
197
|
+
.. code-block:: python3
|
|
198
|
+
|
|
199
|
+
import pandas as pd
|
|
200
|
+
from libfinance import get_price
|
|
201
|
+
|
|
202
|
+
>>> data = get_price(order_book_ids=["000001.XSHE","600000.XSHG"], start_date="2024-03-01", end_date="2024-03-11")
|
|
203
|
+
>>> print(data)
|
|
204
|
+
|
|
205
|
+
open high low close volume
|
|
206
|
+
order_book_id datetime
|
|
207
|
+
000001.XSHE 2024-03-01 10.59 10.60 10.43 10.49 182810290.0
|
|
208
|
+
2024-03-04 10.45 10.50 10.32 10.33 165592954.0
|
|
209
|
+
2024-03-05 10.30 10.47 10.26 10.43 181731907.0
|
|
210
|
+
2024-03-06 10.40 10.45 10.33 10.33 134564016.0
|
|
211
|
+
2024-03-07 10.33 10.64 10.33 10.38 201616589.0
|
|
212
|
+
2024-03-08 10.35 10.44 10.30 10.38 111397428.0
|
|
213
|
+
2024-03-11 10.38 10.47 10.34 10.47 121067298.0
|
|
214
|
+
600000.XSHG 2024-03-01 7.13 7.16 7.10 7.11 29431801.0
|
|
215
|
+
2024-03-04 7.12 7.12 7.05 7.07 27855963.0
|
|
216
|
+
2024-03-05 7.05 7.18 7.04 7.16 41756232.0
|
|
217
|
+
2024-03-06 7.17 7.22 7.12 7.12 25918749.0
|
|
218
|
+
2024-03-07 7.12 7.20 7.11 7.14 24690348.0
|
|
219
|
+
2024-03-08 7.12 7.17 7.11 7.12 19861794.0
|
|
220
|
+
2024-03-11 7.13 7.17 7.06 7.11 26195498.0
|
|
221
|
+
|
|
222
|
+
"""
|
|
223
|
+
if not frequency.endswith(("d", "w")):
|
|
224
|
+
return ValueError("current, only suport xd and xw frequency data")
|
|
225
|
+
|
|
226
|
+
# tick数据
|
|
227
|
+
if frequency == "tick":
|
|
228
|
+
return ValueError("current, only suport xd and xw data")
|
|
229
|
+
elif frequency.endswith(("d", "m", "w")):
|
|
230
|
+
duration = int(frequency[:-1])
|
|
231
|
+
_frequency = frequency[-1]
|
|
232
|
+
assert 1 <= duration <= 240, "frequency should in range [1, 240]"
|
|
233
|
+
if _frequency == "m" and duration not in (1, 5, 15, 30, 60):
|
|
234
|
+
raise ValueError("frequency should be str like 1m, 5m, 15m 30m,or 60m")
|
|
235
|
+
elif _frequency == 'w' and duration not in (1,):
|
|
236
|
+
raise ValueError("Weekly frequency should be str '1w'")
|
|
237
|
+
else:
|
|
238
|
+
raise ValueError("frequency should be str like 1d, 1m, 5m or tick")
|
|
239
|
+
|
|
240
|
+
valid_adjust = ["pre", "post", "none"]
|
|
241
|
+
ensure_string(adjust_type, "adjust_type")
|
|
242
|
+
check_items_in_container(adjust_type, valid_adjust, "adjust_type")
|
|
243
|
+
order_book_ids = ensure_list_of_string(order_book_ids, "order_book_ids")
|
|
244
|
+
|
|
245
|
+
assert isinstance(skip_suspended, bool), "'skip_suspended' should be a bool"
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
order_book_ids, stocks, funds, indexes, futures, futures888, spots, options, convertibles, repos = classify_order_book_ids(order_book_ids)
|
|
249
|
+
if not order_book_ids:
|
|
250
|
+
warnings.warn("no valid instrument")
|
|
251
|
+
return
|
|
252
|
+
|
|
253
|
+
start_date, end_date = _ensure_date(
|
|
254
|
+
start_date, end_date, stocks, funds, indexes, futures, spots, options, convertibles, repos
|
|
255
|
+
)
|
|
256
|
+
|
|
257
|
+
fields, has_dominant_id = _ensure_fields(fields, DAYBAR_FIELDS, stocks, funds, futures, futures888, spots, options, convertibles, indexes, repos)
|
|
258
|
+
#start_date = convert_dateteime_to_timestamp(start_date)
|
|
259
|
+
#end_date = convert_dateteime_to_timestamp(end_date)
|
|
260
|
+
#pdb.set_trace()
|
|
261
|
+
return get_client().get_price(order_book_ids=order_book_ids,
|
|
262
|
+
start_date=start_date,
|
|
263
|
+
end_date=end_date,
|
|
264
|
+
frequency=frequency,
|
|
265
|
+
fields=fields,
|
|
266
|
+
skip_suspended=skip_suspended,
|
|
267
|
+
include_now=include_now,
|
|
268
|
+
adjust_type=adjust_type,
|
|
269
|
+
adjust_orig=adjust_orig)
|
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
#!/usr/bin/env python3
|
|
2
|
-
# -*- coding: utf-8 -*-
|
|
3
|
-
|
|
4
1
|
import datetime
|
|
5
2
|
from typing import Any, Union, Optional, Iterable, Dict, List, Sequence, Iterable
|
|
6
3
|
|
|
@@ -69,6 +66,4 @@ def get_index_weights(index_id: str = "000300.XSHG", date: Union[str, datetime.d
|
|
|
69
66
|
[300 rows x 5 columns]
|
|
70
67
|
|
|
71
68
|
"""
|
|
72
|
-
return get_client().get_index_weights(index_id=index_id, date = date)
|
|
73
|
-
|
|
74
|
-
|
|
69
|
+
return get_client().get_index_weights(index_id=index_id, date = date)
|