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: 2020/3/6 16:40
Desc:
"""
@@ -0,0 +1,184 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
Date: 2023/7/24 18:30
Desc: currencybeacon 提供的外汇数据
该网站需要先注册后获取 API 使用
https://currencyscoop.com/
"""
import pandas as pd
import requests
def currency_latest(
base: str = "USD", symbols: str = "", api_key: str = ""
) -> pd.DataFrame:
"""
Latest data from currencyscoop.com
https://currencyscoop.com/api-documentation
:param base: The base currency you would like to use for your rates
:type base: str
:param symbols: A list of currencies you will like to see the rates for. You can refer to a list all supported currencies here
:type symbols: str
:param api_key: Account -> Account Details -> API KEY (use as password in external tools)
:type api_key: str
:return: Latest data of base currency
:rtype: pandas.DataFrame
"""
params = {"base": base, "symbols": symbols, "api_key": api_key}
url = "https://api.currencyscoop.com/v1/latest"
r = requests.get(url, params=params)
temp_df = pd.DataFrame.from_dict(r.json()["response"])
temp_df["date"] = pd.to_datetime(temp_df["date"])
temp_df.reset_index(inplace=True)
temp_df.rename(columns={"index": "currency"}, inplace=True)
return temp_df
def currency_history(
base: str = "USD", date: str = "2023-02-03", symbols: str = "", api_key: str = ""
) -> pd.DataFrame:
"""
Latest data from currencyscoop.com
https://currencyscoop.com/api-documentation
:param base: The base currency you would like to use for your rates
:type base: str
:param date: Specific date, e.g., "2020-02-03"
:type date: str
:param symbols: A list of currencies you will like to see the rates for. You can refer to a list all supported currencies here
:type symbols: str
:param api_key: Account -> Account Details -> API KEY (use as password in external tools)
:type api_key: str
:return: Latest data of base currency
:rtype: pandas.DataFrame
"""
params = {"base": base, "date": date, "symbols": symbols, "api_key": api_key}
url = "https://api.currencyscoop.com/v1/historical"
r = requests.get(url, params=params)
temp_df = pd.DataFrame.from_dict(r.json()["response"])
temp_df["date"] = pd.to_datetime(temp_df["date"]).dt.date
temp_df.reset_index(inplace=True)
temp_df.rename(columns={"index": "currency"}, inplace=True)
return temp_df
def currency_time_series(
base: str = "USD",
start_date: str = "2023-02-03",
end_date: str = "2023-03-04",
symbols: str = "",
api_key: str = "",
) -> pd.DataFrame:
"""
Time-series data from currencyscoop.com
P.S. need special authority
https://currencyscoop.com/api-documentation
:param base: The base currency you would like to use for your rates
:type base: str
:param start_date: Specific date, e.g., "2020-02-03"
:type start_date: str
:param end_date: Specific date, e.g., "2020-02-03"
:type end_date: str
:param symbols: A list of currencies you will like to see the rates for. You can refer to a list all supported currencies here
:type symbols: str
:param api_key: Account -> Account Details -> API KEY (use as password in external tools)
:type api_key: str
:return: Latest data of base currency
:rtype: pandas.DataFrame
"""
params = {
"base": base,
"api_key": api_key,
"start_date": start_date,
"end_date": end_date,
"symbols": symbols,
}
url = "https://api.currencyscoop.com/v1/timeseries"
r = requests.get(url, params=params)
temp_df = pd.DataFrame.from_dict(r.json()["response"])
temp_df = temp_df.T
temp_df.reset_index(inplace=True)
temp_df.rename(columns={"index": "date"}, inplace=True)
temp_df["date"] = pd.to_datetime(temp_df["date"]).dt.date
return temp_df
def currency_currencies(c_type: str = "fiat", api_key: str = "") -> pd.DataFrame:
"""
currencies data from currencyscoop.com
https://currencyscoop.com/api-documentation
:param c_type: now only "fiat" can return data
:type c_type: str
:param api_key: Account -> Account Details -> API KEY (use as password in external tools)
:type api_key: str
:return: Latest data of base currency
:rtype: pandas.DataFrame
"""
params = {"type": c_type, "api_key": api_key}
url = "https://api.currencyscoop.com/v1/currencies"
r = requests.get(url, params=params)
data_json = r.json()
temp_df = pd.DataFrame(data_json["response"])
return temp_df
def currency_convert(
base: str = "USD",
to: str = "CNY",
amount: str = "10000",
api_key: str = "",
) -> pd.DataFrame:
"""
currencies data from currencyscoop.com
https://currencyscoop.com/api-documentation
:param base: The base currency you would like to use for your rates
:type base: str
:param to: The currency you would like to use for your rates
:type to: str
:param amount: The amount of base currency
:type amount: str
:param api_key: Account -> Account Details -> API KEY (use as password in external tools)
:type api_key: str
:return: Latest data of base currency
:rtype: pandas.Series
"""
params = {
"from": base,
"to": to,
"amount": amount,
"api_key": api_key,
}
url = "https://api.currencyscoop.com/v1/convert"
r = requests.get(url, params=params)
temp_se = pd.Series(r.json()["response"])
temp_se["timestamp"] = pd.to_datetime(temp_se["timestamp"], unit="s")
temp_df = temp_se.to_frame()
temp_df.reset_index(inplace=True)
temp_df.columns = ["item", "value"]
return temp_df
if __name__ == "__main__":
currency_latest_df = currency_latest(base="USD", api_key="")
print(currency_latest_df)
currency_history_df = currency_history(base="USD", date="2023-02-03", api_key="")
print(currency_history_df)
currency_time_series_df = currency_time_series(
base="USD",
start_date="2023-02-03",
end_date="2023-03-04",
symbols="",
api_key="",
)
print(currency_time_series_df)
currency_currencies_df = currency_currencies(c_type="fiat", api_key="")
print(currency_currencies_df)
currency_convert_se = currency_convert(
base="USD", to="CNY", amount="10000", api_key=""
)
print(currency_convert_se)
@@ -0,0 +1,117 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
Date: 2025/12/8 17:20
Desc: 新浪财经-中行人民币牌价历史数据查询
https://biz.finance.sina.com.cn/forex/forex.php?startdate=2012-01-01&enddate=2021-06-14&money_code=EUR&type=0
"""
from functools import lru_cache
from io import StringIO
import pandas as pd
import requests
from bs4 import BeautifulSoup
from tqdm import tqdm
@lru_cache()
def _currency_boc_sina_map(
start_date: str = "20210614", end_date: str = "20230810"
) -> dict:
"""
外汇 symbol 和代码映射
https://biz.finance.sina.com.cn/forex/forex.php?startdate=2012-01-01&enddate=2021-06-14&money_code=EUR&type=0
:param start_date: 开始交易日
:type start_date: str
:param end_date: 结束交易日
:type end_date: str
:return: 外汇 symbol 和代码映射
:rtype: dict
"""
url = "http://biz.finance.sina.com.cn/forex/forex.php"
params = {
"startdate": "-".join([start_date[:4], start_date[4:6], start_date[6:]]),
"enddate": "-".join([end_date[:4], end_date[4:6], end_date[6:]]),
"money_code": "EUR",
"type": "0",
}
r = requests.get(url, params=params)
r.encoding = "gbk"
soup = BeautifulSoup(r.text, "lxml")
data_dict = dict(
zip(
[
item.text
for item in soup.find(attrs={"id": "money_code"}).find_all("option")
],
[
item["value"]
for item in soup.find(attrs={"id": "money_code"}).find_all("option")
],
)
)
return data_dict
def currency_boc_sina(
symbol: str = "美元", start_date: str = "20230304", end_date: str = "20231110"
) -> pd.DataFrame:
"""
新浪财经-中行人民币牌价历史数据查询
https://biz.finance.sina.com.cn/forex/forex.php?startdate=2012-01-01&enddate=2021-06-14&money_code=EUR&type=0
:param symbol: choice of {'美元', '英镑', '欧元', '澳门元', '泰国铢', '菲律宾比索', '港币', '瑞士法郎', '新加坡元', '瑞典克朗', '丹麦克朗', '挪威克朗', '日元', '加拿大元', '澳大利亚元', '新西兰元', '韩国元'}
:type symbol: str
:param start_date: 开始交易日
:type start_date: str
:param end_date: 结束交易日
:type end_date: str
:return: 中行人民币牌价历史数据查询
:rtype: pandas.DataFrame
"""
data_dict = _currency_boc_sina_map(start_date=start_date, end_date=end_date)
url = "http://biz.finance.sina.com.cn/forex/forex.php"
params = {
"money_code": data_dict[symbol],
"type": "0",
"startdate": "-".join([start_date[:4], start_date[4:6], start_date[6:]]),
"enddate": "-".join([end_date[:4], end_date[4:6], end_date[6:]]),
"page": "1",
"call_type": "ajax",
}
r = requests.get(url, params=params)
soup = BeautifulSoup(r.text, features="lxml")
soup.find(attrs={"id": "money_code"})
page_element_list = soup.find_all("a", attrs={"class": "page"})
page_num = int(page_element_list[-2].text) if len(page_element_list) != 0 else 1
big_df = pd.DataFrame()
for page in tqdm(range(1, page_num + 1), leave=False):
params.update({"page": page})
r = requests.get(url, params=params)
temp_df = pd.read_html(StringIO(r.text), header=0)[0]
big_df = pd.concat(objs=[big_df, temp_df], ignore_index=True)
big_df.columns = [
"日期",
"中行汇买价",
"中行钞买价",
"中行钞卖价/汇卖价",
"央行中间价",
"中行折算价",
]
big_df["日期"] = pd.to_datetime(big_df["日期"], errors="coerce").dt.date
big_df["中行汇买价"] = pd.to_numeric(big_df["中行汇买价"], errors="coerce")
big_df["中行钞买价"] = pd.to_numeric(big_df["中行钞买价"], errors="coerce")
big_df["中行钞卖价/汇卖价"] = pd.to_numeric(
big_df["中行钞卖价/汇卖价"], errors="coerce"
)
big_df["央行中间价"] = pd.to_numeric(big_df["央行中间价"], errors="coerce")
big_df["中行折算价"] = pd.to_numeric(big_df["中行折算价"], errors="coerce")
big_df.sort_values(by=["日期"], inplace=True, ignore_index=True)
return big_df
if __name__ == "__main__":
currency_boc_sina_df = currency_boc_sina(
symbol="美元", start_date="20230304", end_date="20231110"
)
print(currency_boc_sina_df)
@@ -0,0 +1,60 @@
# -*- coding:utf-8 -*-
# !/usr/bin/env python
"""
Date: 2024/4/29 17:00
Desc: 人民币汇率中间价
https://www.safe.gov.cn/safe/rmbhlzjj/index.html
"""
import re
from datetime import datetime
from io import StringIO
import pandas as pd
import requests
from bs4 import BeautifulSoup
def currency_boc_safe() -> pd.DataFrame:
"""
人民币汇率中间价
https://www.safe.gov.cn/safe/rmbhlzjj/index.html
:return: 人民币汇率中间价
:rtype: pandas.DataFrame
"""
url = "https://www.safe.gov.cn/safe/2020/1218/17833.html"
r = requests.get(url)
r.encoding = "utf8"
soup = BeautifulSoup(r.text, features="lxml")
content = soup.find(name="a", string=re.compile("人民币汇率"))["href"]
url = f"https://www.safe.gov.cn{content}"
temp_df = pd.read_excel(url)
temp_df.sort_values(by=["日期"], inplace=True)
temp_df.reset_index(inplace=True, drop=True)
start_date = (
(pd.Timestamp(temp_df["日期"].tolist()[-1]) + pd.Timedelta(days=1))
.isoformat()
.split("T")[0]
)
end_date = datetime.now().isoformat().split("T")[0]
url = "https://www.safe.gov.cn/AppStructured/hlw/RMBQuery.do"
payload = {
"startDate": start_date,
"endDate": end_date,
"queryYN": "true",
}
r = requests.post(url, data=payload)
current_temp_df = pd.read_html(StringIO(r.text))[-1]
current_temp_df.sort_values(by=["日期"], inplace=True)
current_temp_df.reset_index(inplace=True, drop=True)
big_df = pd.concat(objs=[temp_df, current_temp_df], ignore_index=True)
column_name_list = big_df.columns[1:]
for item in column_name_list:
big_df[item] = pd.to_numeric(big_df[item], errors="coerce")
big_df["日期"] = pd.to_datetime(big_df["日期"], errors="coerce").dt.date
return big_df
if __name__ == "__main__":
currency_boc_safe_df = currency_boc_safe()
print(currency_boc_safe_df)