- Implemented a function to fetch futures data from the API with error handling and response validation. - Added example usage for fetching and saving K-line data to CSV. - Updated CSV files with new data entries for specified date ranges. - Enhanced the structure of the data retrieval function to include parameters for depth and adjust type.
229 lines
8.0 KiB
Plaintext
229 lines
8.0 KiB
Plaintext
{
|
||
"cells": [
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"from ssquant.SQDATA import TakeData\n",
|
||
"\n",
|
||
"#注意首先需要pip install ssquant\n",
|
||
"#否则链接不到数据库\n",
|
||
"#输入俱乐部的账号密码即可调用,注意保密。\n",
|
||
"#目前数据是2019年1月-至今\n",
|
||
"#每日下午收盘后3点30分录入当天数据。\n",
|
||
"#有任何疑问可以再群里提出,或者私信我(慕金龙)\n",
|
||
"#官网: quant789.com\n",
|
||
"#公众号:松鼠Quant\n",
|
||
"#客服微信: viquant01\n",
|
||
"\n",
|
||
"#只能调取分钟及以上数据,tick数据每月底更新到百度网盘下载\n",
|
||
"\n",
|
||
"'''\n",
|
||
"获取数据-\n",
|
||
"品种:symbol,不区分大小写\n",
|
||
"起始时间:start_date,\n",
|
||
"结束时间:end_date(包含当天),\n",
|
||
"周期kline_period:1M..5M..NM(分钟),1D(天),1W(周),1Y(月)\n",
|
||
"复权adjust_type:0(不复权)1(后复权)\n",
|
||
"注意:\n",
|
||
"1.请正确输入账号密码\n",
|
||
"2.不要挂代理访问数据库\n",
|
||
"3.暂时没有股指数据,下个月补齐。\n",
|
||
"'''\n",
|
||
" \n",
|
||
"# username='俱乐部账号' password='密码'\n",
|
||
"client = TakeData(username='77777@qq.com', password='7777')\n",
|
||
"data = client.get_data(\n",
|
||
" symbol='rb888',\n",
|
||
" start_date='2023-01-02',\n",
|
||
" end_date='2024-01-03',\n",
|
||
" kline_period='60M',\n",
|
||
" adjust_type=1\n",
|
||
")\n",
|
||
"print(data)\n",
|
||
"\n",
|
||
"\n",
|
||
"\n",
|
||
"'''\n",
|
||
"datetime:时间,\n",
|
||
"\n",
|
||
"symbol:品种,\n",
|
||
"\n",
|
||
"open:开盘价,\n",
|
||
"\n",
|
||
"high:最高价,\n",
|
||
"\n",
|
||
"low:最低价,\n",
|
||
"\n",
|
||
"close:收盘价,\n",
|
||
"\n",
|
||
"volume:成交量(单bar),\n",
|
||
"\n",
|
||
"amount:成交金额(单bar),\n",
|
||
"\n",
|
||
"openint:持仓量(单bar),\n",
|
||
"\n",
|
||
"cumulative_openint:累计持仓量,\n",
|
||
"\n",
|
||
"open_bidp , open_askp: K线第一个价格的买一价格和卖一价格\n",
|
||
"\n",
|
||
"close_bidp , close_askp: K线最后一个价格的买一价格和卖一价格\n",
|
||
"\n",
|
||
"\n",
|
||
" datetime symbol open high low close volume amount cumulative_openint openint open_bidp open_askp close_bidp close_askp\n",
|
||
"0 2023-01-03 10:00:00 rb2305 4081.0 4081.0 4016.0 4037.0 737537 29782187220 1883481 -48415 4081.0 4084.0 4037.0 4038.0\n",
|
||
"1 2023-01-03 11:00:00 rb2305 4038.0 4056.0 4037.0 4042.0 158548 6415696920 1887716 4235 4037.0 4038.0 4042.0 4044.0\n",
|
||
"2 2023-01-03 12:00:00 rb2305 4044.0 4054.0 4037.0 4051.0 67448 2728130300 1890125 2409 4043.0 4044.0 4050.0 4051.0\n",
|
||
"3 2023-01-03 14:00:00 rb2305 4055.0 4065.0 4045.0 4058.0 110181 4469698600 1895841 5723 4050.0 4051.0 4058.0 4059.0\n",
|
||
"4 2023-01-03 15:00:00 rb2305 4059.0 4074.0 4056.0 4063.0 167932 6824213940 1882723 -13125 4058.0 4059.0 4062.0 4063.0\n",
|
||
"... ... ... ... ... ... ... ... ... ... ... ... ... ... ...\n",
|
||
"1689 2024-01-03 12:00:00 rb2405 4055.0 4057.0 4044.0 4049.0 79745 3229361570 1597387 -6515 4054.0 4055.0 4049.0 4050.0\n",
|
||
"1690 2024-01-03 14:00:00 rb2405 4050.0 4056.0 4046.0 4049.0 55040 2229498750 1598566 1179 4050.0 4051.0 4049.0 4050.0\n",
|
||
"1691 2024-01-03 15:00:00 rb2405 4050.0 4064.0 4048.0 4055.0 148845 6038835190 1583796 -14770 4049.0 4050.0 4055.0 4056.0\n",
|
||
"1692 2024-01-03 22:00:00 rb2405 4054.0 4054.0 4040.0 4049.0 181753 7354584770 1582419 990 4053.0 4054.0 4048.0 4049.0\n",
|
||
"1693 2024-01-03 23:00:00 rb2405 4049.0 4057.0 4042.0 4049.0 104712 4240341050 1574287 -8132 4048.0 4049.0 4049.0 4050.0"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 3,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"ename": "ModuleNotFoundError",
|
||
"evalue": "No module named 'ssquant.SQDATA'",
|
||
"output_type": "error",
|
||
"traceback": [
|
||
"\u001b[31m---------------------------------------------------------------------------\u001b[39m",
|
||
"\u001b[31mModuleNotFoundError\u001b[39m Traceback (most recent call last)",
|
||
"\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[3]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mssquant\u001b[39;00m\u001b[34;01m.\u001b[39;00m\u001b[34;01mSQDATA\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m TakeData\n",
|
||
"\u001b[31mModuleNotFoundError\u001b[39m: No module named 'ssquant.SQDATA'"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"from ssquant.SQDATA import TakeData"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"out_path = r'D:/data'\n",
|
||
"symbol_name = 'rb888' #主力连续888 次主力合约777\n",
|
||
"time_period = '1M'\n",
|
||
"start_time = '2000-01-01'\n",
|
||
"end_time = '2019-01-31'\n",
|
||
"adjust_k = 'Faj' #Naj:Non adjust,Faj:Forward adjust,后复权\n",
|
||
"\n",
|
||
"if adjust_k == 'Naj':\n",
|
||
" adjust_tmp = 0\n",
|
||
"elif adjust_k == 'Faj':\n",
|
||
" adjust_tmp = 1"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"client = TakeData(username='240884432@qq.com', password='Zj123!@#')\n",
|
||
"data = client.get_data(\n",
|
||
" symbol=symbol_name,\n",
|
||
" start_date=start_time,\n",
|
||
" end_date=end_time,\n",
|
||
" kline_period=time_period,\n",
|
||
" adjust_type= adjust_tmp\n",
|
||
")"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"print('头部文件为:--------------------')\n",
|
||
"data.head()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"from datetime import datetime\n",
|
||
" \n",
|
||
"# 假设你有一个字符串,表示时间,格式为 'YYYY-MM-DD HH:MM:SS'\n",
|
||
"real_start_time = data.iloc[0,0]\n",
|
||
" \n",
|
||
"# 使用datetime.strptime将字符串转换为时间\n",
|
||
"time_obj = datetime.strptime(real_start_time, '%Y-%m-%d %H:%M:%S')\n",
|
||
" \n",
|
||
"# 获取年月日\n",
|
||
"year = time_obj.year\n",
|
||
"month = time_obj.month\n",
|
||
"day = time_obj.day\n",
|
||
" \n",
|
||
"print(f'年: {year}, 月: {month}, 日: {day}')"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"print('尾部文件为:--------------------')\n",
|
||
"data.tail()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"import pandas as pd\n",
|
||
"import datetime\n",
|
||
"real_start_time = pd.to_datetime(data.iloc[0,0]).date().strftime('%Y-%m-%d')\n",
|
||
"real_end_time = pd.to_datetime(data.iloc[-1,0]).date().strftime('%Y-%m-%d')"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"data.to_csv('%s/%s_%s_%s(%s_%s).csv'%(out_path,symbol_name,time_period,adjust_k,real_start_time,real_end_time), index=False)"
|
||
]
|
||
}
|
||
],
|
||
"metadata": {
|
||
"kernelspec": {
|
||
"display_name": "Python 3",
|
||
"language": "python",
|
||
"name": "python3"
|
||
},
|
||
"language_info": {
|
||
"codemirror_mode": {
|
||
"name": "ipython",
|
||
"version": 3
|
||
},
|
||
"file_extension": ".py",
|
||
"mimetype": "text/x-python",
|
||
"name": "python",
|
||
"nbconvert_exporter": "python",
|
||
"pygments_lexer": "ipython3",
|
||
"version": "3.13.2"
|
||
}
|
||
},
|
||
"nbformat": 4,
|
||
"nbformat_minor": 2
|
||
}
|