雪球模拟盘脚本化调仓

为啥干这个?

工作和个人需要,基于雪球模拟组合实现自动化的股票调仓,主要分几步:
1)登录雪球,创建模拟组合,手动添加股票,获取填写的 cURL;
2) 转化为 python 的 post 请求;
3)运行程序,检查结果即可;

咋干?

1)创建组合
图见公众号链接,下同
2)手动添加股票
3)开发者工具,找到添加股票的请求
4)获取 cURL

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
curl 'https://tc.xueqiu.com/tc/snowx/MONI/transaction/add.json' \
-H 'authority: tc.xueqiu.com' \
-H 'sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="96", "Google Chrome";v="96"' \
-H 'accept: application/json, text/plain, */*' \
-H 'content-type: application/x-www-form-urlencoded' \
-H 'sec-ch-ua-mobile: ?0' \
-H 'user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36' \
-H 'sec-ch-ua-platform: "macOS"' \
-H 'origin: https://xueqiu.com' \
-H 'sec-fetch-site: same-site' \
-H 'sec-fetch-mode: cors' \
-H 'sec-fetch-dest: empty' \
-H 'referer: https://xueqiu.com/performance' \
-H 'accept-language: zh-CN,zh;q=0.9,en;q=0.8,ja;q=0.7,zh-TW;q=0.6' \
-H 'cookie: your cookie here' \
--data-raw 'type=1&date=2021-12-15&gid=4383054580870984&symbol=SZ002157&price=9.49&shares=100&commission_rate=0.1' \
--compressed

5)转化python的格式
https://tool.lu/curl/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import requests
headers = {
'authority': 'tc.xueqiu.com',
'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="96", "Google Chrome";v="96"',
'accept': 'application/json, text/plain, */*',
'content-type': 'application/x-www-form-urlencoded',
'sec-ch-ua-mobile': '?0',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36',
'sec-ch-ua-platform': '"macOS"',
'origin': 'https://xueqiu.com',
'sec-fetch-site': 'same-site',
'sec-fetch-mode': 'cors',
'sec-fetch-dest': 'empty',
'referer': 'https://xueqiu.com/performance',
'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,ja;q=0.7,zh-TW;q=0.6',
'cookie': 'your cookie here',
}

data = {
'type': '1', # 1是买,2是卖
'date': '2021-12-15',
'gid': '4383054580870984',
'symbol': 'SZ002714',
'price': '9.49',
'shares': '100',
'tax_rate': '0',
'commission_rate': '0.1'
}

response = requests.post('https://tc.xueqiu.com/tc/snowx/MONI/transaction/add.json', headers=headers, data=data)

批量处理的时候,只需要脚本处理 data 的字典内容即可,太简单了,不展开说;

注意的点

1)cookies 一段时间可能失效,需要手动更新下;
2)基金,etf 都可以;

吃水不忘挖井人

https://github.com/yyri/stockScripts

公众号链接

https://mp.weixin.qq.com/s/rIYEBCV04xL1XMky57EtPA

(✪ω✪)