Skip to content

Commit

Permalink
Dev (#5414)
Browse files Browse the repository at this point in the history
* 修改future_basis的错误,请帮忙同步 (#5411)

* 修改futures_basis和新增品种

* 修改future_basis.py

* .\akshare\futures\symbol_var.py

* .\akshare\futures\symbol_var.py

---------

Co-authored-by: jason udu <gjx_best@hotmail.com>

* fix: fix futures_spot_price_previous

* fix: fix stock_gdfx_free_top_10_em

* fix: fix stock_jgdy_detail_em

* docs: update date

* feat: add version 1.15.45

---------

Co-authored-by: jasonudu <106946718+jasonudu@users.noreply.github.com>
Co-authored-by: jason udu <gjx_best@hotmail.com>
  • Loading branch information
3 people authored Dec 12, 2024
1 parent becb53d commit 66db8f9
Show file tree
Hide file tree
Showing 7 changed files with 284 additions and 169 deletions.
3 changes: 2 additions & 1 deletion akshare/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2954,9 +2954,10 @@
1.15.42 fix: fix get_roll_yield_bar interface
1.15.43 fix: fix macro_china_urban_unemployment interface
1.15.44 fix: fix index_detail_hist_adjust_cni interface
1.15.45 fix: fix stock_jgdy_detail_em interface
"""

__version__ = "1.15.44"
__version__ = "1.15.45"
__author__ = "AKFamily"

import sys
Expand Down
34 changes: 27 additions & 7 deletions akshare/futures/futures_basis.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
Date: 2024/11/28 22:00
Date: 2024/12/12 17:00
Desc: 生意社网站采集大宗商品现货价格及相应基差数据, 数据时间段从 20110104-至今
备注:现期差 = 现货价格 - 期货价格(这里的期货价格为结算价)
黄金为 元/克, 白银为 元/千克, 玻璃现货为 元/平方米, 鸡蛋现货为 元/公斤, 鸡蛋期货为 元/500千克, 其余为 元/吨.
Expand Down Expand Up @@ -124,9 +124,9 @@ def futures_spot_price(
return temp_df
else:
time.sleep(3)
except: # noqa: E722
except Exception as e: # noqa: E722
print(
f"{date.strftime('%Y-%m-%d')}日生意社数据连接失败,第{str(i)}次尝试,最多5次"
f"{date.strftime('%Y-%m-%d')}日生意社数据连接失败[错误信息:{e}],第{str(i)}次尝试,最多5次"
)
i += 1
if i > 5:
Expand Down Expand Up @@ -187,6 +187,8 @@ def _check_information(df_data, date):
"郑州商品交易所",
"大连商品交易所",
"广州期货交易所",
# 某些天网站没有数据,比如 20180912,此时返回"暂无数据",但并不是网站被墙了
"暂无数据",
]:
symbol = chinese_to_english(news)
record = pd.DataFrame(df_data[df_data["symbol"] == string])
Expand All @@ -206,6 +208,16 @@ def _check_information(df_data, date):
record.loc[:, "spot_price"] = float(record["spot_price"].iloc[0]) * 1000
records = pd.concat([records, record])

# 20241129:如果某日没有数据,直接返回返回空表
if records.empty:
records = df_data.iloc[0:0]
records["near_basis"] = pd.Series(dtype="float")
records["dom_basis"] = pd.Series(dtype="float")
records["near_basis_rate"] = pd.Series(dtype="float")
records["dom_basis_rate"] = pd.Series(dtype="float")
records["date"] = pd.Series(dtype="object")
return records

records.loc[:, ["near_contract_price", "dominant_contract_price", "spot_price"]] = (
records.loc[
:, ["near_contract_price", "dominant_contract_price", "spot_price"]
Expand Down Expand Up @@ -259,6 +271,7 @@ def _check_information(df_data, date):
records["dom_basis_rate"] = (
records["dominant_contract_price"] / records["spot_price"] - 1
)
# records.loc[:, "date"] = date.strftime("%Y%m%d")
records.insert(0, "date", date.strftime("%Y%m%d"))
return records

Expand Down Expand Up @@ -300,8 +313,15 @@ def futures_spot_price_previous(date: str = "20240430") -> pd.DataFrame:
values = main[main[4].str.endswith("%")]
values.columns = header
# Basis
basis = pd.concat(content[2:-1])
# 对于没有数据的天,xml文件中没有数据,所以content[2:-1]可能为空
if len(content[2:-1]) > 0:
basis = pd.concat(content[2:-1])
else:
basis = pd.DataFrame(columns=["主力合约基差", "主力合约基差(%)"])

basis.columns = ["主力合约基差", "主力合约基差(%)"]
# 20241125(jasonudu):因为部分日期,存在多个品种的现货价格,比如20151125的白糖、豆粕、豆油等,如果用商品名来merge,会出现重复列名,所以改用index来merge
# basis["商品"] = values["商品"].tolist()
basis.index = values.index
basis = pd.merge(
values[["商品", "现货价格", "主力合约代码", "主力合约价格"]],
Expand Down Expand Up @@ -338,12 +358,12 @@ def futures_spot_price_previous(date: str = "20240430") -> pd.DataFrame:

if __name__ == "__main__":
futures_spot_price_daily_df = futures_spot_price_daily(
start_day="20241028", end_day="20241128", vars_list=["CU", "RB"]
start_day="20240415", end_day="20240418", vars_list=["CU", "RB"]
)
print(futures_spot_price_daily_df)

futures_spot_price_df = futures_spot_price(date="20241128")
futures_spot_price_df = futures_spot_price(date="20240430")
print(futures_spot_price_df)

futures_spot_price_previous_df = futures_spot_price_previous(date="20241128")
futures_spot_price_previous_df = futures_spot_price_previous(date="20240430")
print(futures_spot_price_previous_df)
Loading

0 comments on commit 66db8f9

Please sign in to comment.