stock-tracker

This commit is contained in:
C菌
2026-07-04 00:17:11 +08:00
commit 6087341a48
6463 changed files with 1929869 additions and 0 deletions
@@ -0,0 +1,6 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
Date: 2019/10/27 19:45
Desc:
"""
@@ -0,0 +1,142 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
Date: 2021/8/20 16:13
Desc: 期货衍生指标配置文件
西本新干线-指数数据
"""
# xgx
symbol_dict = {
"钢材指数": "65",
"铁矿指数": "61",
"焦炭指数": "64",
"煤炭指数": "1002",
"水泥指数": "1003",
"FTZ指数": "1100",
"钢铁行业PMI指数": "118",
"生产指数": "119",
"新订单指数": "120",
"新出口订单指数": "121",
"产成品库存指数": "122",
"原材料库存指数": "123",
"沪市终端线螺每周采购量监控": "74",
"沪螺纹钢社会库存": "72",
"国内螺纹钢社会库存量": "67",
"国内线材社会库存量": "68",
"国内主要城市热轧卷板库存": "69",
"国内主要城市冷轧卷板库存": "70",
"国内主要城市中厚板库存": "73",
"全国主要钢材品种库存总量": "117",
"热轧价格走势": "108",
"冷轧价格走势": "109",
"中板价格走势": "110",
"型材价格走势": "111",
"沪二级螺纹钢价格走势": "127",
"螺纹钢主力合约收盘价格": "179",
"铁矿石主力合约收盘价格": "180",
"热轧板卷主力合约收盘价格": "181",
"焦煤主力合约收盘价格": "182",
"焦炭主力合约收盘价格": "183",
"存款基准利率": "52",
"贷款基准利率": "53",
"存款准备金率": "105",
"人民币新增贷款(亿元)": "174",
"广义货币供应量增速(M2%": "175",
"狭义货币供应量增速(M1%)": "176",
"上海大额银行承兑汇票()": "129",
"上海大额银行承兑汇票(Ⅱ)": "130",
"上海大额银行承兑汇票(Ⅲ)": "131",
"上海大额商业承兑汇票()": "132",
"上海大额商业承兑汇票(II)": "133",
"上海大额商业承兑汇票(III)": "134",
"重点企业粗钢日均产量(旬报)": "99",
"重点企业钢材库存量(旬报)": "124",
"国内月度粗钢日均产量": "159",
"国内月度粗钢产量": "35",
"国内月度钢材产量": "88",
"国内月度螺纹钢产量": "40",
"国内月度线材产量": "41",
"国内月度热轧板卷产量": "114",
"国内月度冷轧板卷产量": "115",
"国内月度中厚板产量": "116",
"国内月度生铁产量": "177",
"国内月度焦炭产量": "37",
"国内月度铁矿石原矿产量": "36",
"国内月度铁矿石进口量": "42",
"国内月度钢材出口量": "38",
"国内月度钢材进口量": "39",
"国内铁矿石港口存量": "43",
"唐山地区钢坯库存量": "161",
"印度矿港口库存": "100",
"波罗的海干散货指数(BDI": "77",
"废钢价格走势": "78",
"钢坯价格走势": "79",
"钢材成本指数": "178",
"铁矿石进口月度均价": "93",
"巴西图巴朗-北仑铁矿海运价": "94",
"西澳-北仑铁矿海运价": "95",
"澳大利亚粉矿价格(56.5%,日照港)": "1006",
"澳大利亚粉矿价格(61.5%青岛港,元/吨)": "106",
"巴西粉矿价格( 65% 日照港,元/吨)": "107",
"62%铁矿石指数": "125",
"63.5%印度粉矿外盘报价": "126",
"国民生产总值季度增速(GDP": "166",
"居民消费物价指数(CPI": "30",
"工业生产者出厂价格指数(PPI": "165",
"制造业采购经理指数(PMI": "104",
"月度建筑安装工程投资额": "91",
"月度固定资产投资额": "32",
"月度房地产建设投资额": "34",
"城填固定资产投资增速(累计值,%": "171",
"房地产开发投资增速(累计值,%": "167",
"土地购置面积同比增速(累计值,%": "168",
"房屋新开工面积同比增速(累计值,%": "169",
"商品房销售面积同比增速(累计值,%": "170",
"钢铁业固定资产投资增速(累计值,%": "172",
"CRU全球": "80",
"CRU长材": "81",
"CRU扁平材": "82",
"CRU北美": "83",
"CRU欧洲": "84",
"CRU亚洲": "85",
"全球粗钢月度产量(万吨)": "162",
"全球粗钢日均产量(万吨)": "163",
"全球粗钢产能利用率(%": "164",
}
xgx_code_url = "http://www.96369.net/Other/ValidateCode.aspx"
xgx_main_url = "http://www.96369.net/indices/{}"
xgx_headers = {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8",
"Cache-Control": "no-cache",
"Connection": "keep-alive",
"Content-Length": "57",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "www.96369.net",
"Origin": "http://www.96369.net",
"Pragma": "no-cache",
"Referer": "http://www.96369.net/indices/67",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36",
}
xgx_short_headers = {
"Accept": "image/webp,image/apng,image/*,*/*;q=0.8",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8",
"Cache-Control": "no-cache",
"Connection": "keep-alive",
"Host": "www.96369.net",
"Pragma": "no-cache",
"Referer": "http://www.96369.net/indices/67",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36",
}
# csa
csa_params_url = "https://www.jiaoyifamen.com/tools/nav/spread"
csa_url_spread = "https://www.jiaoyifamen.com/tools/future/spread/free"
csa_url_ratio = "https://www.jiaoyifamen.com/tools/future/valence/free"
csa_url_customize = "https://www.jiaoyifamen.com/tools/future/customize"
csa_payload = {"type1": "RB", "code1": "01", "type2": "RB", "code2": "05"}
@@ -0,0 +1,102 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
Date: 2024/2/28 17:00
Desc: 中国金融期货交易所-数据-交易参数
http://www.cffex.com.cn/jycs/
"""
import xml.etree.ElementTree as ET
import pandas as pd
import requests
def futures_contract_info_cffex(date: str = "20240228") -> pd.DataFrame:
"""
中国金融期货交易所-数据-交易参数
http://www.gfex.com.cn/gfex/hyxx/ywcs.shtml
:param date: 查询日期
:type date: str
:return: 交易参数汇总查询
:rtype: pandas.DataFrame
"""
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36",
}
url = f"http://www.cffex.com.cn/sj/jycs/{date[:6]}/{date[6:]}/index.xml"
r = requests.get(url, headers=headers)
xml_data = r.text
# 解析 XML
tree = ET.ElementTree(ET.fromstring(xml_data))
root = tree.getroot()
# 获取所有的记录
records = root.findall(".//INDEX")
# 解析数据并填充到列表中
data = []
for record in records:
# 对于每个记录,创建一个字典
row_data = {}
for field in record:
row_data[field.tag] = field.text
# 将字典添加到数据列表中
data.append(row_data)
temp_df = pd.DataFrame(data)
temp_df.rename(
columns={
"TRADING_DAY": "查询交易日",
"PRODUCT_ID": "品种",
"INSTRUMENT_ID": "合约代码",
"INSTRUMENT_MONTH": "合约月份",
"BASIS_PRICE": "挂盘基准价",
"OPEN_DATE": "上市日",
"END_TRADING_DAY": "最后交易日",
"UPPER_VALUE": "涨停板幅度",
"LOWER_VALUE": "跌停板幅度",
"UPPERLIMITPRICE": "涨停板价位",
"LOWERLIMITPRICE": "跌停板价位",
"LONG_LIMIT": "持仓限额",
},
inplace=True,
)
temp_df["挂盘基准价"] = pd.to_numeric(temp_df["挂盘基准价"], errors="coerce")
temp_df["涨停板价位"] = pd.to_numeric(temp_df["涨停板价位"], errors="coerce")
temp_df["跌停板价位"] = pd.to_numeric(temp_df["跌停板价位"], errors="coerce")
temp_df["持仓限额"] = pd.to_numeric(temp_df["持仓限额"], errors="coerce")
temp_df["查询交易日"] = pd.to_datetime(
temp_df["查询交易日"], format="%Y%m%d", errors="coerce"
).dt.date
temp_df["上市日"] = pd.to_datetime(
temp_df["上市日"], format="%Y%m%d", errors="coerce"
).dt.date
temp_df["最后交易日"] = pd.to_datetime(
temp_df["最后交易日"], format="%Y%m%d", errors="coerce"
).dt.date
temp_df["查询交易日"] = pd.to_datetime(
temp_df["查询交易日"], format="%Y%m%d", errors="coerce"
).dt.date
temp_df = temp_df[
[
"合约代码",
"合约月份",
"挂盘基准价",
"上市日",
"最后交易日",
"涨停板幅度",
"跌停板幅度",
"涨停板价位",
"跌停板价位",
"持仓限额",
"品种",
"查询交易日",
]
]
temp_df.sort_values(["合约代码"], ascending=False, ignore_index=True, inplace=True)
return temp_df
if __name__ == "__main__":
futures_contract_info_cffex_df = futures_contract_info_cffex(date="20240228")
print(futures_contract_info_cffex_df)
@@ -0,0 +1,108 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
Date: 2024/4/11 16:30
Desc: 郑州商品交易所-交易数据-参考数据
http://www.czce.com.cn/cn/jysj/cksj/H770322index_1.htm
"""
import xml.etree.ElementTree as ET
import pandas as pd
import requests
def futures_contract_info_czce(date: str = "20240228") -> pd.DataFrame:
"""
郑州商品交易所-交易数据-参考数据
http://www.czce.com.cn/cn/jysj/cksj/H770322index_1.htm
:param date: 查询日期
:type date: str
:return: 交易参数汇总查询
:rtype: pandas.DataFrame
"""
headers = {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,"
"image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/84.0.4147.89 Safari/537.36",
"Host": "www.czce.com.cn",
}
url = f"http://www.czce.com.cn/cn/DFSStaticFiles/Future/{date[:4]}/{date}/FutureDataReferenceData.xml"
r = requests.get(url, headers=headers)
xml_data = r.text
# 解析 XML
tree = ET.ElementTree(ET.fromstring(xml_data))
root = tree.getroot()
# 获取所有的记录
records = root.findall(".//Contract")
# 解析数据并填充到列表中
data = []
for record in records:
# 对于每个记录,创建一个字典
row_data = {}
for field in record:
row_data[field.tag] = field.text
# 将字典添加到数据列表中
data.append(row_data)
temp_df = pd.DataFrame(data)
temp_df.rename(
columns={
"Name": "产品名称",
"CtrCd": "合约代码",
"PrdCd": "产品代码",
"PrdTp": "产品类型",
"ExchCd": "交易所MIC编码",
"SegTp": "交易场所",
"TrdHrs": "交易时间节假日除外",
"TrdCtyCd": "交易国家ISO编码",
"TrdCcyCd": "交易币种ISO编码",
"ClrngCcyCd": "结算币种ISO编码",
"ExpiryTime": "到期时间待国家公布2025年节假日安排后进行调整",
"SettleTp": "结算方式",
"Duration": "挂牌频率",
"TckSz": "最小变动价位",
"TckVal": "最小变动价值",
"CtrSz": "交易单位",
"MsrmntUnt": "计量单位",
"MaxOrdSz": "最大下单量",
"MnthPosLmt": "日持仓限额期货公司会员不限仓",
"MinBlckTrdSz": "大宗交易最小规模",
"CesrEaaFl": "是否受CESR监管",
"FlexElgblFl": "是否为灵活合约",
"ListCy": "上市周期该产品的所有合约月份",
"DlvryNtcDt": "交割通知日",
"FrstTrdDt": "第一交易日",
"LstTrdDt": "最后交易日待国家公布2025年节假日安排后进行调整",
"DlvrySettleDt": "交割结算日",
"MnthCd": "月份代码",
"YrCd": "年份代码",
"LstDlvryDt": "最后交割日",
"LstDlvryDtBoard": "车(船)板最后交割日",
"DlvryMnth": "合约交割月份本合约交割月份",
"Margin": "交易保证金率",
"PxLim": "涨跌停板",
"FeeCcy": "费用币种ISO编码",
"TrdFee": "交易手续费",
"FeeCollectionType": "手续费收取方式",
"DlvryFee": "交割手续费",
"IntraDayTrdFee": "平今仓手续费",
"TradingLimit": "交易限额",
},
inplace=True,
)
temp_df["交易手续费"] = pd.to_numeric(temp_df["交易手续费"], errors="coerce")
temp_df["交割手续费"] = pd.to_numeric(temp_df["交割手续费"], errors="coerce")
temp_df["平今仓手续费"] = pd.to_numeric(temp_df["平今仓手续费"], errors="coerce")
temp_df["交易限额"] = pd.to_numeric(temp_df["交易限额"], errors="coerce")
temp_df["车(船)板最后交割日"] = pd.to_datetime(
temp_df["车(船)板最后交割日"], errors="coerce"
).dt.date
return temp_df
if __name__ == "__main__":
futures_contract_info_czce_df = futures_contract_info_czce(date="20240228")
print(futures_contract_info_czce_df)
@@ -0,0 +1,70 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
Date: 2025/8/18 11:00
Desc: 大连商品交易所-业务/服务-业务参数-交易参数-合约信息查询
http://www.dce.com.cn/dalianshangpin/ywfw/ywcs/jycs/hyxxcx/index.html
"""
import pandas as pd
import requests
def futures_contract_info_dce() -> pd.DataFrame:
"""
大连商品交易所-数据中心-业务数据-交易参数-合约信息
http://www.dce.com.cn/dce/channel/list/180.html
:return: 交易参数汇总查询
:rtype: pandas.DataFrame
"""
url = "http://www.dce.com.cn/dcereport/publicweb/tradepara/contractInfo"
payload = {
"lang": "zh",
"tradeType": "1",
"varietyId": "all",
}
r = requests.post(url, json=payload)
data_json = r.json()
temp_df = pd.DataFrame(data_json["data"])
temp_df.rename(
columns={
"contractId": "合约",
"variety": "品种名称",
"varietyOrder": "品种代码",
"unit": "交易单位",
"tick": "最小变动价位",
"startTradeDate": "开始交易日",
"endTradeDate": "最后交易日",
"endDeliveryDate": "最后交割日",
"tradeType": "",
},
inplace=True,
)
temp_df = temp_df[
[
"品种名称",
"合约",
"交易单位",
"最小变动价位",
"开始交易日",
"最后交易日",
"最后交割日",
]
]
temp_df["交易单位"] = pd.to_numeric(temp_df["交易单位"], errors="coerce")
temp_df["最小变动价位"] = pd.to_numeric(temp_df["最小变动价位"], errors="coerce")
temp_df["开始交易日"] = pd.to_datetime(
temp_df["开始交易日"], format="%Y%m%d", errors="coerce"
).dt.date
temp_df["最后交易日"] = pd.to_datetime(
temp_df["最后交易日"], format="%Y%m%d", errors="coerce"
).dt.date
temp_df["最后交割日"] = pd.to_datetime(
temp_df["最后交割日"], format="%Y%m%d", errors="coerce"
).dt.date
return temp_df
if __name__ == "__main__":
futures_contract_info_dce_df = futures_contract_info_dce()
print(futures_contract_info_dce_df)
@@ -0,0 +1,72 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
Date: 2024/2/28 16:00
Desc: 广州期货交易所-业务/服务-合约信息
http://www.gfex.com.cn/gfex/hyxx/ywcs.shtml
"""
import pandas as pd
import requests
def futures_contract_info_gfex() -> pd.DataFrame:
"""
广州期货交易所-业务/服务-合约信息
http://www.gfex.com.cn/gfex/hyxx/ywcs.shtml
:return: 交易参数汇总查询
:rtype: pandas.DataFrame
"""
url = "http://www.gfex.com.cn/u/interfacesWebTtQueryContractInfo/loadList"
params = {
"variety": "",
"trade_type": "0",
}
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36"
}
r = requests.post(url, params=params, headers=headers)
data_json = r.json()
temp_df = pd.DataFrame(data_json["data"])
temp_df.rename(
columns={
"tradeType": "-",
"variety": "品种",
"varietyOrder": "-",
"contractId": "合约代码",
"unit": "交易单位",
"tick": "最小变动单位",
"startTradeDate": "开始交易日",
"endTradeDate": "最后交易日",
"endDeliveryDate0": "最后交割日",
},
inplace=True,
)
temp_df["交易单位"] = pd.to_numeric(temp_df["交易单位"], errors="coerce")
temp_df["最小变动单位"] = pd.to_numeric(temp_df["最小变动单位"], errors="coerce")
temp_df["开始交易日"] = pd.to_datetime(
temp_df["开始交易日"], format="%Y%m%d", errors="coerce"
).dt.date
temp_df["最后交易日"] = pd.to_datetime(
temp_df["最后交易日"], format="%Y%m%d", errors="coerce"
).dt.date
temp_df["最后交割日"] = pd.to_datetime(
temp_df["最后交割日"], format="%Y%m%d", errors="coerce"
).dt.date
temp_df = temp_df[
[
"品种",
"合约代码",
"交易单位",
"最小变动单位",
"开始交易日",
"最后交易日",
"最后交割日",
]
]
return temp_df
if __name__ == "__main__":
futures_contract_info_gfex_df = futures_contract_info_gfex()
print(futures_contract_info_gfex_df)
@@ -0,0 +1,69 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
Date: 2024/11/30 19:00
Desc: 上海国际能源交易中心-业务指南-交易参数汇总(期货)
https://www.ine.cn/bourseService/summary/?name=currinstrumentprop
"""
import pandas as pd
import requests
def futures_contract_info_ine(date: str = "20241129") -> pd.DataFrame:
"""
上海国际能源交易中心-业务指南-交易参数汇总(期货)
https://www.ine.cn/bourseService/summary/?name=currinstrumentprop
:param date: 查询日期; 交易日
:type date: str
:return: 交易参数汇总查询
:rtype: pandas.DataFrame
"""
url = f"https://www.ine.cn/data/busiparamdata/future/ContractBaseInfo{date}.dat"
params = {"rnd": "0.8312696798757147"}
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/119.0.0.0 Safari/537.36"
}
r = requests.get(url, params=params, headers=headers)
data_json = r.json()
temp_df = pd.DataFrame(data_json["ContractBaseInfo"])
temp_df.rename(
columns={
"BASISPRICE": "挂牌基准价",
"ENDDELIVDATE": "最后交割日",
"EXPIREDATE": "到期日",
"INSTRUMENTID": "合约代码",
"OPENDATE": "上市日",
"STARTDELIVDATE": "开始交割日",
"TRADINGDAY": "交易日",
},
inplace=True,
)
temp_df = temp_df[
[
"合约代码",
"上市日",
"到期日",
"开始交割日",
"最后交割日",
"挂牌基准价",
"交易日",
]
]
temp_df["上市日"] = pd.to_datetime(temp_df["上市日"], errors="coerce").dt.date
temp_df["到期日"] = pd.to_datetime(temp_df["到期日"], errors="coerce").dt.date
temp_df["开始交割日"] = pd.to_datetime(
temp_df["开始交割日"], errors="coerce"
).dt.date
temp_df["最后交割日"] = pd.to_datetime(
temp_df["最后交割日"], errors="coerce"
).dt.date
temp_df["交易日"] = pd.to_datetime(temp_df["交易日"], errors="coerce").dt.date
temp_df["挂牌基准价"] = pd.to_numeric(temp_df["挂牌基准价"], errors="coerce")
return temp_df
if __name__ == "__main__":
futures_contract_info_ine_df = futures_contract_info_ine(date="20241129")
print(futures_contract_info_ine_df)
@@ -0,0 +1,71 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
Date: 2024/5/13 17:45
Desc: 上海期货交易所-交易所服务-业务数据-交易参数汇总查询
https://tsite.shfe.com.cn/bourseService/businessdata/summaryinquiry/
"""
import pandas as pd
import requests
def futures_contract_info_shfe(date: str = "20240513") -> pd.DataFrame:
"""
上海期货交易所-交易所服务-业务数据-交易参数汇总查询
https://tsite.shfe.com.cn/bourseService/businessdata/summaryinquiry/
:param date: 查询日期; 交易日
:type date: str
:return: 交易参数汇总查询
:rtype: pandas.DataFrame
"""
url = (
f"https://www.shfe.com.cn/data/busiparamdata/future/ContractBaseInfo{date}.dat"
)
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/119.0.0.0 Safari/537.36"
}
r = requests.get(url, headers=headers)
data_json = r.json()
temp_df = pd.DataFrame(data_json["ContractBaseInfo"])
temp_df.rename(
columns={
"BASISPRICE": "挂牌基准价",
"ENDDELIVDATE": "最后交割日",
"EXPIREDATE": "到期日",
"INSTRUMENTID": "合约代码",
"OPENDATE": "上市日",
"STARTDELIVDATE": "开始交割日",
"TRADINGDAY": "交易日",
},
inplace=True,
)
temp_df = temp_df[
[
"合约代码",
"上市日",
"到期日",
"开始交割日",
"最后交割日",
"挂牌基准价",
"交易日",
]
]
temp_df["上市日"] = pd.to_datetime(temp_df["上市日"], errors="coerce").dt.date
temp_df["到期日"] = pd.to_datetime(temp_df["到期日"], errors="coerce").dt.date
temp_df["开始交割日"] = pd.to_datetime(
temp_df["开始交割日"], errors="coerce"
).dt.date
temp_df["最后交割日"] = pd.to_datetime(
temp_df["最后交割日"], errors="coerce"
).dt.date
temp_df["交易日"] = pd.to_datetime(temp_df["交易日"], errors="coerce").dt.date
temp_df["挂牌基准价"] = pd.to_numeric(temp_df["挂牌基准价"], errors="coerce")
temp_df["更新时间"] = data_json["update_date"]
return temp_df
if __name__ == "__main__":
futures_contract_info_shfe_df = futures_contract_info_shfe(date="20250611")
print(futures_contract_info_shfe_df)
@@ -0,0 +1,76 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
Date: 2025/3/5 18:00
Desc: 新浪财经-期货-成交持仓
https://vip.stock.finance.sina.com.cn/q/view/vFutures_Positions_cjcc.php
"""
from io import StringIO
import pandas as pd
import requests
def futures_hold_pos_sina(
symbol: str = "成交量", contract: str = "OI2501", date: str = "20240223"
) -> pd.DataFrame:
"""
新浪财经-期货-成交持仓
https://vip.stock.finance.sina.com.cn/q/view/vFutures_Positions_cjcc.php
:param symbol: choice of {"成交量", "多单持仓", "空单持仓"}
:type symbol: str
:param contract: 期货合约
:type contract: str
:param date: 查询日期
:type date: str
:return: 成交持仓
:rtype: pandas.DataFrame
"""
date = "-".join([date[:4], date[4:6], date[6:]])
url = "https://vip.stock.finance.sina.com.cn/q/view/vFutures_Positions_cjcc.php"
params = {"t_breed": contract, "t_date": date}
r = requests.get(url, params=params)
if symbol == "成交量":
temp_df = pd.read_html(StringIO(r.text))[2].iloc[:-1, :]
temp_df["名次"] = pd.to_numeric(temp_df["名次"], errors="coerce")
temp_df["成交量"] = pd.to_numeric(temp_df["成交量"], errors="coerce")
temp_df["比上交易增减"] = pd.to_numeric(
temp_df["比上交易增减"], errors="coerce"
)
return temp_df
elif symbol == "多单持仓":
temp_df = pd.read_html(StringIO(r.text))[3].iloc[:-1, :]
temp_df["名次"] = pd.to_numeric(temp_df["名次"], errors="coerce")
temp_df["多单持仓"] = pd.to_numeric(temp_df["多单持仓"], errors="coerce")
temp_df["比上交易增减"] = pd.to_numeric(
temp_df["比上交易增减"], errors="coerce"
)
return temp_df
elif symbol == "空单持仓":
temp_df = pd.read_html(StringIO(r.text))[4].iloc[:-1, :]
temp_df["名次"] = pd.to_numeric(temp_df["名次"], errors="coerce")
temp_df["空单持仓"] = pd.to_numeric(temp_df["空单持仓"], errors="coerce")
temp_df["比上交易增减"] = pd.to_numeric(
temp_df["比上交易增减"], errors="coerce"
)
return temp_df
else:
raise ValueError("请输入正确的 symbol 参数")
if __name__ == "__main__":
futures_hold_pos_sina_df = futures_hold_pos_sina(
symbol="成交量", contract="IC2403", date="20240203"
)
print(futures_hold_pos_sina_df)
futures_hold_pos_sina_df = futures_hold_pos_sina(
symbol="多单持仓", contract="OI2501", date="20241016"
)
print(futures_hold_pos_sina_df)
futures_hold_pos_sina_df = futures_hold_pos_sina(
symbol="空单持仓", contract="OI2501", date="20241016"
)
print(futures_hold_pos_sina_df)
@@ -0,0 +1,278 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
Date: 2024/3/18 14:00
Desc: 玄田数据
https://zhujia.zhuwang.com.cn
"""
import pandas as pd
import requests
def futures_hog_core(symbol: str = "外三元") -> pd.DataFrame:
"""
玄田数据-核心数据
https://zhujia.zhuwang.com.cn
:param symbol: choice of {"外三元", "内三元", "土杂猪"}
:type symbol: str
:return: 玄田数据-核心数据
:rtype: pandas.DataFrame
"""
if symbol == "外三元":
url = "https://xt.yangzhu.vip/data/getzhujiahitsdata"
params = {"ptype": "1", "areano": "-1", "datetype": "0"}
r = requests.post(url, params=params)
data_json = r.json()
temp_df = pd.DataFrame(data_json["data"])
temp_df.columns = ["value", "date"]
temp_df = temp_df[["date", "value"]]
temp_df["date"] = pd.to_datetime(temp_df["date"], errors="coerce").dt.date
temp_df["value"] = pd.to_numeric(temp_df["value"], errors="coerce")
return temp_df
elif symbol == "内三元":
url = "https://xt.yangzhu.vip/data/getzhujiahitsdata"
params = {"ptype": "2", "areano": "-1", "datetype": "0"}
r = requests.post(url, params=params)
data_json = r.json()
temp_df = pd.DataFrame(data_json["data"])
temp_df.columns = ["value", "date"]
temp_df = temp_df[["date", "value"]]
temp_df["date"] = pd.to_datetime(temp_df["date"], errors="coerce").dt.date
temp_df["value"] = pd.to_numeric(temp_df["value"], errors="coerce")
return temp_df
elif symbol == "土杂猪":
url = "https://xt.yangzhu.vip/data/getzhujiahitsdata"
params = {"ptype": "3", "areano": "-1", "datetype": "0"}
r = requests.post(url, params=params)
data_json = r.json()
temp_df = pd.DataFrame(data_json["data"])
temp_df.columns = ["value", "date"]
temp_df = temp_df[["date", "value"]]
temp_df["date"] = pd.to_datetime(temp_df["date"], errors="coerce").dt.date
temp_df["value"] = pd.to_numeric(temp_df["value"], errors="coerce")
return temp_df
def futures_hog_cost(symbol: str = "玉米") -> pd.DataFrame:
"""
玄田数据-成本维度
https://zhujia.zhuwang.com.cn
:param symbol: choice of {"玉米", "豆粕", "二元母猪价格", "仔猪价格"}
:type symbol: str
:return: 玄田数据-成本维度
:rtype: pandas.DataFrame
"""
if symbol == "玉米":
url = "https://xt.yangzhu.vip/data/getzhujiahitsdata"
params = {"ptype": "4", "areano": "-1", "datetype": "0"}
r = requests.post(url, params=params)
data_json = r.json()
temp_df = pd.DataFrame(data_json["data"])
temp_df.columns = ["value", "date"]
temp_df = temp_df[["date", "value"]]
temp_df["date"] = pd.to_datetime(temp_df["date"], errors="coerce").dt.date
temp_df["value"] = pd.to_numeric(temp_df["value"], errors="coerce")
return temp_df
elif symbol == "豆粕":
url = "https://xt.yangzhu.vip/data/getzhujiahitsdata"
params = {"ptype": "5", "areano": "-1", "datetype": "0"}
r = requests.post(url, params=params)
data_json = r.json()
temp_df = pd.DataFrame(data_json["data"])
temp_df.columns = ["value", "date"]
temp_df = temp_df[["date", "value"]]
temp_df["date"] = pd.to_datetime(temp_df["date"], errors="coerce").dt.date
temp_df["value"] = pd.to_numeric(temp_df["value"], errors="coerce")
return temp_df
elif symbol == "二元母猪价格":
url = "https://xt.yangzhu.vip/data/getmapdata"
params = {
"ptype": "1",
"areano": "-1",
}
r = requests.post(url, params=params)
data_json = r.json()
temp_df = pd.DataFrame(data_json["data"])
temp_df.columns = ["date", "value"]
temp_df["date"] = pd.to_datetime(temp_df["date"], errors="coerce").dt.date
temp_df["value"] = pd.to_numeric(temp_df["value"], errors="coerce")
return temp_df
elif symbol == "仔猪价格":
url = "https://xt.yangzhu.vip/data/getmapdata"
params = {
"ptype": "2",
"areano": "-1",
}
r = requests.post(url, params=params)
data_json = r.json()
temp_df = pd.DataFrame(data_json["data"])
temp_df.columns = ["date", "value"]
temp_df["date"] = pd.to_datetime(temp_df["date"], errors="coerce").dt.date
temp_df["value"] = pd.to_numeric(temp_df["value"], errors="coerce")
return temp_df
def futures_hog_supply(symbol: str = "猪肉批发价") -> pd.DataFrame:
"""
玄田数据-供应维度
https://zhujia.zhuwang.com.cn
:param symbol: choice of {"猪肉批发价", "储备冻猪肉", "饲料原料数据", "白条肉",
"生猪产能", "育肥猪", "肉类价格指数", "猪粮比价"}
:type symbol: str
:return: 玄田数据-供应维度
:rtype: pandas.DataFrame
"""
if symbol == "猪肉批发价":
url = "https://xt.yangzhu.vip/data/getmapdata"
params = {"ptype": "3", "areano": "-1"}
r = requests.post(url, params=params)
data_json = r.json()
temp_df = pd.DataFrame(data_json["data"])
temp_df.columns = ["date", "item", "value"]
del temp_df["item"]
temp_df["date"] = pd.to_datetime(temp_df["date"], errors="coerce").dt.date
temp_df["value"] = pd.to_numeric(temp_df["value"], errors="coerce")
return temp_df
elif symbol == "储备冻猪肉":
url = "https://xt.yangzhu.vip/data/getmapdata"
params = {"ptype": "4", "areano": "-1"}
r = requests.post(url, params=params)
data_json = r.json()
temp_df = pd.DataFrame(data_json["data"])
temp_df.columns = ["date", "value"]
temp_df["date"] = pd.to_datetime(temp_df["date"], errors="coerce").dt.date
temp_df["value"] = pd.to_numeric(temp_df["value"], errors="coerce")
return temp_df
elif symbol == "饲料原料数据":
url = "https://xt.yangzhu.vip/data/getmapdata"
params = {"ptype": "5", "areano": "-1"}
r = requests.post(url, params=params)
data_json = r.json()
temp_df = pd.DataFrame(data_json["data"])
temp_df.columns = [
"周期",
"大豆进口金额",
"大豆播种面积",
"玉米进口金额",
"玉米播种面积",
]
temp_df["周期"] = temp_df["周期"].astype(int).astype(str)
temp_df["大豆进口金额"] = pd.to_numeric(
temp_df["大豆进口金额"], errors="coerce"
)
temp_df["大豆播种面积"] = pd.to_numeric(
temp_df["大豆播种面积"], errors="coerce"
)
temp_df["玉米进口金额"] = pd.to_numeric(
temp_df["玉米进口金额"], errors="coerce"
)
temp_df["玉米播种面积"] = pd.to_numeric(
temp_df["玉米播种面积"], errors="coerce"
)
return temp_df
elif symbol == "白条肉":
url = "https://xt.yangzhu.vip/data/getmapdata"
params = {"ptype": "6", "areano": "-1"}
r = requests.post(url, params=params)
data_json = r.json()
temp_df = pd.DataFrame(data_json["data"])
temp_df.columns = ["周期", "白条肉平均出厂价格", "环比", "同比"]
temp_df["白条肉平均出厂价格"] = pd.to_numeric(
temp_df["白条肉平均出厂价格"], errors="coerce"
)
temp_df["环比"] = pd.to_numeric(temp_df["环比"], errors="coerce")
temp_df["同比"] = pd.to_numeric(temp_df["同比"], errors="coerce")
return temp_df
elif symbol == "生猪产能":
url = "https://xt.yangzhu.vip/data/getmapdata"
params = {"ptype": "7", "areano": "-1"}
r = requests.post(url, params=params)
data_json = r.json()
temp_df = pd.DataFrame(data_json["data"])
temp_df.columns = ["周期", "能繁母猪存栏", "猪肉产量", "生猪存栏", "生猪出栏"]
temp_df["能繁母猪存栏"] = pd.to_numeric(
temp_df["能繁母猪存栏"], errors="coerce"
)
temp_df["猪肉产量"] = pd.to_numeric(temp_df["猪肉产量"], errors="coerce")
temp_df["生猪存栏"] = pd.to_numeric(temp_df["生猪存栏"], errors="coerce")
temp_df["生猪出栏"] = pd.to_numeric(temp_df["生猪出栏"], errors="coerce")
return temp_df
elif symbol == "育肥猪":
url = "https://xt.yangzhu.vip/data/getmapdata"
params = {"ptype": "9", "areano": "-1"}
r = requests.post(url, params=params)
data_json = r.json()
temp_df = pd.DataFrame(data_json["data"])
temp_df = temp_df[["date", "benzhou"]]
temp_df["date"] = pd.to_datetime(temp_df["date"], errors="coerce").dt.date
temp_df["benzhou"] = pd.to_numeric(temp_df["benzhou"], errors="coerce")
return temp_df
elif symbol == "肉类价格指数":
url = "https://xt.yangzhu.vip/data/getmapdata"
params = {"ptype": "10", "areano": "-1"}
r = requests.post(url, params=params)
data_json = r.json()
temp_df = pd.DataFrame(data_json["data"])
temp_df.columns = ["date", "item", "value"]
del temp_df["item"]
temp_df["date"] = pd.to_datetime(temp_df["date"], errors="coerce").dt.date
temp_df["value"] = pd.to_numeric(temp_df["value"], errors="coerce")
return temp_df
elif symbol == "猪粮比价":
url = "https://xt.yangzhu.vip/data/getmapdata"
params = {"ptype": "11", "areano": "-1"}
r = requests.post(url, params=params)
data_json = r.json()
temp_df = pd.DataFrame(data_json["data"])
temp_df.columns = ["date", "value"]
temp_df["date"] = pd.to_datetime(temp_df["date"], errors="coerce").dt.date
temp_df["value"] = pd.to_numeric(temp_df["value"], errors="coerce")
return temp_df
if __name__ == "__main__":
futures_hog_core_df = futures_hog_core(symbol="外三元")
print(futures_hog_core_df)
futures_hog_core_df = futures_hog_core(symbol="内三元")
print(futures_hog_core_df)
futures_hog_core_df = futures_hog_core(symbol="土杂猪")
print(futures_hog_core_df)
futures_hog_cost_df = futures_hog_cost(symbol="玉米")
print(futures_hog_cost_df)
futures_hog_cost_df = futures_hog_cost(symbol="豆粕")
print(futures_hog_cost_df)
futures_hog_cost_df = futures_hog_cost(symbol="二元母猪价格")
print(futures_hog_cost_df)
futures_hog_cost_df = futures_hog_cost(symbol="仔猪价格")
print(futures_hog_cost_df)
futures_hog_supply_df = futures_hog_supply(symbol="猪肉批发价")
print(futures_hog_supply_df)
futures_hog_supply_df = futures_hog_supply(symbol="储备冻猪肉")
print(futures_hog_supply_df)
futures_hog_supply_df = futures_hog_supply(symbol="饲料原料数据")
print(futures_hog_supply_df)
futures_hog_supply_df = futures_hog_supply(symbol="白条肉")
print(futures_hog_supply_df)
futures_hog_supply_df = futures_hog_supply(symbol="生猪产能")
print(futures_hog_supply_df)
futures_hog_supply_df = futures_hog_supply(symbol="育肥猪")
print(futures_hog_supply_df)
futures_hog_supply_df = futures_hog_supply(symbol="肉类价格指数")
print(futures_hog_supply_df)
futures_hog_supply_df = futures_hog_supply(symbol="猪粮比价")
print(futures_hog_supply_df)
@@ -0,0 +1,160 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
Date: 2024/3/1 23:00
Desc: 新浪财经-期货的主力合约数据
https://finance.sina.com.cn/futuremarket/index.shtml
"""
from io import StringIO
import pandas as pd
import requests
from akshare.futures.cons import (
zh_subscribe_exchange_symbol_url,
zh_match_main_contract_url,
zh_match_main_contract_payload,
)
from akshare.utils import demjson
def zh_subscribe_exchange_symbol(symbol: str = "dce") -> pd.DataFrame:
"""
订阅指定交易所品种的代码
https://finance.sina.com.cn/futuremarket/index.shtml
:param symbol: choice of {"dce", "czce", "shfe", "cffex", "gfex"}
:type symbol: str
:return: 订阅指定交易所品种的代码
:rtype: pandas.DataFrame
"""
r = requests.get(zh_subscribe_exchange_symbol_url)
r.encoding = "gb2312"
data_text = r.text
data_json = demjson.decode(
data_text[data_text.find("{") : data_text.find("};") + 1]
)
if symbol == "czce":
data_json["czce"].remove("郑州商品交易所")
return pd.DataFrame(data_json["czce"])
if symbol == "dce":
data_json["dce"].remove("大连商品交易所")
return pd.DataFrame(data_json["dce"])
if symbol == "shfe":
data_json["shfe"].remove("上海期货交易所")
return pd.DataFrame(data_json["shfe"])
if symbol == "cffex":
data_json["cffex"].remove("中国金融期货交易所")
return pd.DataFrame(data_json["cffex"])
if symbol == "gfex":
data_json["gfex"].remove("广州期货交易所")
return pd.DataFrame(data_json["gfex"])
def match_main_contract(symbol: str = "shfe") -> pd.DataFrame:
"""
指定交易所的所有可以提供数据的合约
https://finance.sina.com.cn/futuremarket/index.shtml
:param symbol: choice of {"dce", "czce", "shfe", "cffex", "gfex"}
:type symbol: str
:return: 指定交易所的所有可以提供数据的合约
:rtype: pandas.DataFrame
"""
subscribe_list = []
exchange_symbol_list = zh_subscribe_exchange_symbol(symbol).iloc[:, 1].tolist()
for item in exchange_symbol_list:
zh_match_main_contract_payload.update({"node": item})
res = requests.get(
zh_match_main_contract_url, params=zh_match_main_contract_payload
)
data_json = demjson.decode(res.text)
data_df = pd.DataFrame(data_json)
try:
main_contract = data_df[
data_df["name"].str.contains("连续")
& data_df["symbol"]
.str.extract(r"([\w])(\d)")
.iloc[:, 1]
.str.contains("0")
].iloc[0, :3]
subscribe_list.append(main_contract)
except: # noqa: E722
# print(item, "无主力连续合约")
continue
# print("主力连续合约获取成功")
temp_df = pd.DataFrame(subscribe_list)
return temp_df
def futures_display_main_sina() -> pd.DataFrame:
"""
新浪主力连续合约品种一览表
https://finance.sina.com.cn/futuremarket/index.shtml
:return: 新浪主力连续合约品种一览表
:rtype: pandas.DataFrame
"""
temp_df = pd.DataFrame()
for item in ["dce", "czce", "shfe", "cffex", "gfex"]:
temp_df = pd.concat([temp_df, match_main_contract(symbol=item)])
temp_df.reset_index(inplace=True, drop=True)
return temp_df
def futures_main_sina(
symbol: str = "V0",
start_date: str = "19900101",
end_date: str = "22220101",
) -> pd.DataFrame:
"""
新浪财经-期货-主力连续日数据
https://vip.stock.finance.sina.com.cn/quotes_service/view/qihuohangqing.html#titlePos_1
:param symbol: 通过 ak.futures_display_main_sina() 函数获取 symbol
:type symbol: str
:param start_date: 开始时间
:type start_date: str
:param end_date: 结束时间
:type end_date: str
:return: 主力连续日数据
:rtype: pandas.DataFrame
"""
trade_date = "20210817"
trade_date = trade_date[:4] + "_" + trade_date[4:6] + "_" + trade_date[6:]
url = f"https://stock2.finance.sina.com.cn/futures/api/jsonp.php/var%20_{symbol}{trade_date}=/InnerFuturesNewService.getDailyKLine?symbol={symbol}&_={trade_date}"
r = requests.get(url)
data_text = r.text
data_json = data_text[data_text.find("([") + 1 : data_text.rfind("])") + 1]
temp_df = pd.read_json(StringIO(data_json))
temp_df.columns = [
"日期",
"开盘价",
"最高价",
"最低价",
"收盘价",
"成交量",
"持仓量",
"动态结算价",
]
temp_df["日期"] = pd.to_datetime(temp_df["日期"], errors="coerce").dt.date
temp_df.set_index(keys=["日期"], inplace=True)
temp_df.index = pd.to_datetime(temp_df.index)
temp_df = temp_df[start_date:end_date]
temp_df.reset_index(inplace=True)
temp_df["日期"] = pd.to_datetime(temp_df["日期"], errors="coerce").dt.date
temp_df["开盘价"] = pd.to_numeric(temp_df["开盘价"], errors="coerce")
temp_df["最高价"] = pd.to_numeric(temp_df["最高价"], errors="coerce")
temp_df["最低价"] = pd.to_numeric(temp_df["最低价"], errors="coerce")
temp_df["收盘价"] = pd.to_numeric(temp_df["收盘价"], errors="coerce")
temp_df["成交量"] = pd.to_numeric(temp_df["成交量"], errors="coerce")
temp_df["持仓量"] = pd.to_numeric(temp_df["持仓量"], errors="coerce")
temp_df["动态结算价"] = pd.to_numeric(temp_df["动态结算价"], errors="coerce")
return temp_df
if __name__ == "__main__":
futures_display_main_sina_df = futures_display_main_sina()
print(futures_display_main_sina_df)
futures_main_sina_hist = futures_main_sina(
symbol="CF0", start_date="20240124", end_date="20240301"
)
print(futures_main_sina_hist)
@@ -0,0 +1,92 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
Date: 2024/4/5 20:20
Desc: 生意社-商品与期货-现期图
https://www.100ppi.com/sf/792.html
"""
from io import StringIO
import pandas as pd
import requests
from bs4 import BeautifulSoup
def __get_sys_spot_futures_dict() -> dict:
"""
生意社-商品与期货-现期图: 品种和网址字典
https://www.100ppi.com/sf/792.html
:return: 品种和网址字典
:rtype: dict
"""
url = "https://www.100ppi.com/sf/792.html"
res = requests.get(url)
soup = BeautifulSoup(res.text, features="lxml")
temp_item = soup.find(name="div", attrs={"class": "q8"}).find_all("li")
name_url_dict = dict(
zip(
[item.find("a").get_text().strip() for item in temp_item],
[item.find("a")["href"] for item in temp_item],
)
)
return name_url_dict
def futures_spot_sys(symbol: str = "", indicator: str = "市场价格") -> pd.DataFrame:
"""
生意社-商品与期货-现期图
https://www.100ppi.com/sf/792.html
:param symbol: 期货品种
:type symbol: str
:param indicator: 市场价格; choice of {"市场价格", "基差率", "主力基差"}
:type indicator: str
:return: pandas.DataFrame
:rtype: dict
"""
name_url_dict = __get_sys_spot_futures_dict()
url = name_url_dict[symbol]
r = requests.get("https://www.100ppi.com" + url)
if indicator == "市场价格":
table_df_one = pd.read_html(StringIO(r.text), header=0, index_col=0)[1].T
table_df_one["现货价格"] = pd.to_numeric(
table_df_one["现货价格"], errors="coerce"
)
table_df_one["主力合约"] = pd.to_numeric(
table_df_one["主力合约"], errors="coerce"
)
table_df_one["最近合约"] = pd.to_numeric(
table_df_one["最近合约"], errors="coerce"
)
table_df_one.reset_index(inplace=True)
table_df_one.columns.name = None
table_df_one.rename(columns={"index": "日期"}, inplace=True)
return table_df_one
elif indicator == "基差率":
table_df_two = pd.read_html(StringIO(r.text), header=0, index_col=0)[2].T
table_df_two["基差率"] = table_df_two["基差率"].str.replace("%", "")
table_df_two["基差率"] = pd.to_numeric(table_df_two["基差率"], errors="coerce")
table_df_two.reset_index(inplace=True)
table_df_two.columns.name = None
table_df_two.rename(columns={"index": "日期"}, inplace=True)
return table_df_two
else:
table_df_three = pd.read_html(StringIO(r.text), header=0, index_col=0)[3].T
table_df_three["主力基差"] = pd.to_numeric(
table_df_three["主力基差"], errors="coerce"
)
table_df_three.reset_index(inplace=True)
table_df_three.columns.name = None
table_df_three.rename(columns={"index": "日期"}, inplace=True)
return table_df_three
if __name__ == "__main__":
futures_spot_sys_df = futures_spot_sys(symbol="", indicator="市场价格")
print(futures_spot_sys_df)
futures_spot_sys_df = futures_spot_sys(symbol="", indicator="基差率")
print(futures_spot_sys_df)
futures_spot_sys_df = futures_spot_sys(symbol="", indicator="主力基差")
print(futures_spot_sys_df)