Skip to content

Commit 1aae1ad

Browse files
committed
Support auto repo
1 parent 81cc543 commit 1aae1ad

File tree

6 files changed

+141
-1
lines changed

6 files changed

+141
-1
lines changed

README.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ ShiPanE-Python-SDK
1919

2020
- 简单的实盘易 HTTP API 封装,见 shipane_sdk/client.py
2121
- 多账号自动新股申购(自动打新)
22+
- 多账号自动逆回购
2223
- 聚宽(JoinQuant)集成
2324
- `米筐(RiceQuant)`_ 集成
2425
- 优矿(Uqer)集成
@@ -128,6 +129,7 @@ Mac/Linux
128129

129130
- examples/joinquant/simple\_strategy.py - 基本用法
130131
- examples/joinquant/new\_stocks\_purchase.py - 新股申购
132+
- examples/joinquant/repo.py - 逆回购
131133

132134
二. 抓取方式
133135
~~~~~~~~~~~~
@@ -169,6 +171,7 @@ Mac/Linux
169171

170172
- examples/ricequant/simple\_strategy.py - 基本用法
171173
- examples/ricequant/new\_stocks\_purchase.py - 新股申购
174+
- examples/ricequant/repo.py - 逆回购
172175

173176
二. 抓取方式
174177
~~~~~~~~~~~~

config/scheduler-example.ini

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
; 详见:https://apscheduler.readthedocs.io/en/v2.1.2/cronschedule.html
1313
;
1414

15+
1516
;
1617
; 实盘易配置
1718
;
@@ -20,6 +21,7 @@ host=localhost
2021
port=8888
2122
key=
2223

24+
2325
;
2426
; 交易客户端 client 参数别名列表
2527
;
@@ -28,6 +30,7 @@ key=
2830
client1=title:monijiaoyi
2931
client2=title:guotai
3032

33+
3134
;
3235
; 自动新股申购配置
3336
;
@@ -42,6 +45,24 @@ schedule=0 0 12 mon-fri * * * *
4245
; clients=client1,client2
4346
clients=client1
4447

48+
49+
;
50+
; 自动逆回购配置
51+
;
52+
[Repo]
53+
; 是否启用?
54+
enabled=false
55+
56+
; 默认设置为:星期一至星期五中午十二点
57+
schedule=0 55 14 mon-fri * * * *
58+
59+
; 需要自动新股申购的交易客户端列表,以,(半角逗号)分割
60+
; clients=client1,client2
61+
clients=client1
62+
63+
security=131810
64+
65+
4566
[JoinQuant]
4667
username=
4768
password=
@@ -63,6 +84,7 @@ schedule=30 */1 9-15 mon-fri * * * *
6384
; clients=client1,client2
6485
clients=client1
6586

87+
6688
[RiceQuant]
6789
username=
6890
password=
@@ -83,6 +105,7 @@ schedule=30 */1 9-15 mon-fri * * * *
83105
; clients=client1,client2
84106
clients=client1
85107

108+
86109
[Uqer]
87110
username=
88111
password=
@@ -103,6 +126,7 @@ schedule=30 */1 9-15 mon-fri * * * *
103126
; clients=client1,client2
104127
clients=client1
105128

129+
106130
;
107131
; 果仁自动同步设置
108132
; 自动同步采用分批下单,典型的下单步骤如下:

examples/joinquant/repo.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import tushare as ts
2+
3+
import shipane_sdk
4+
5+
6+
# 注意:需将回测调成分钟级别
7+
# 注意:用于回测没有意义,需挂到“我的交易”
8+
9+
def initialize(context):
10+
# 每天的收盘前5分钟进行逆回购,参数设置见:https://www.joinquant.com/api#定时运行
11+
run_daily(repo, '14:55')
12+
13+
14+
def process_initialize(context):
15+
# 创建 JoinQuantExecutor 对象
16+
# 可选参数包括:host, port, key, client, timeout 等
17+
# 请将下面的 IP 替换为实际 IP
18+
g.__executor = shipane_sdk.JoinQuantExecutor(
19+
host='xxx.xxx.xxx.xxx',
20+
port=8888,
21+
key='',
22+
client=''
23+
)
24+
25+
26+
def repo(context):
27+
if context.run_params.type == 'sim_trade':
28+
security = '131810'
29+
df = ts.get_realtime_quotes(security)
30+
order = {
31+
'action': 'SELL',
32+
'symbol': security,
33+
'type': 'LIMIT',
34+
'price': float(df['bid'][0]),
35+
'amountProportion': 'ALL'
36+
}
37+
g.__executor.client.execute(**order)
38+
else:
39+
log.info('回测中不进行逆回购')

examples/ricequant/repo.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import tushare as ts
2+
3+
import shipane_sdk
4+
5+
6+
# 注意:需将回测调成分钟级别
7+
# 注意:用于回测没有意义,需挂到“我的交易”
8+
9+
def initialize(context):
10+
# 每天的收盘前5分钟进行逆回购。参数设置见:https://www.ricequant.com/api/python/chn#scheduler
11+
scheduler.run_daily(repo, time_rule=market_close(minute=5))
12+
13+
14+
def process_initialize(context):
15+
# 创建 JoinQuantExecutor 对象
16+
# 可选参数包括:host, port, key, client, timeout 等
17+
# 请将下面的 IP 替换为实际 IP
18+
g.__executor = shipane_sdk.RiceQuantExecutor(
19+
host='xxx.xxx.xxx.xxx',
20+
port=8888,
21+
key='',
22+
client=''
23+
)
24+
25+
26+
def repo(context):
27+
if context.run_info.run_type == RUN_TYPE.PAPER_TRADING:
28+
security = '131810'
29+
quote_df = ts.get_realtime_quotes(security)
30+
order = {
31+
'action': 'SELL',
32+
'symbol': security,
33+
'type': 'LIMIT',
34+
'price': float(quote_df['bid'][0]),
35+
'amountProportion': 'ALL'
36+
}
37+
g.__executor.client.execute(**order)
38+
else:
39+
log.info('回测中不进行逆回购')

shipane_sdk/jobs/repo.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import tushare as ts
4+
5+
from shipane_sdk.jobs.basic_job import BasicJob
6+
7+
8+
class RepoJob(BasicJob):
9+
def __init__(self, client, client_aliases=None, name=None, **kwargs):
10+
super(RepoJob, self).__init__(name, kwargs.get('schedule', None), kwargs.get('enabled', False))
11+
12+
self._client = client
13+
self._client_aliases = client_aliases
14+
self._symbol = kwargs.get('security', '131810')
15+
16+
def __call__(self):
17+
df = ts.get_realtime_quotes(self._symbol)
18+
order = {
19+
'action': 'SELL',
20+
'symbol': self._symbol,
21+
'type': 'LIMIT',
22+
'price': float(df['bid'][0]),
23+
'amountProportion': 'ALL'
24+
}
25+
for client_alias in self._client_aliases:
26+
client = self._client_aliases[client_alias]
27+
self._client.execute(client, **order)

shipane_sdk/scheduler.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from shipane_sdk.jobs.new_stock_purchase import NewStockPurchaseJob
1818
from shipane_sdk.jobs.online_quant_following import OnlineQuantFollowingJob
1919
from shipane_sdk.jobs.online_quant_sync import OnlineQuantSyncJob
20+
from shipane_sdk.jobs.repo import RepoJob
2021
from shipane_sdk.joinquant.client import JoinQuantClient
2122
from shipane_sdk.ricequant.client import RiceQuantClient
2223
from shipane_sdk.uqer.client import UqerClient
@@ -39,6 +40,7 @@ def __init__(self):
3940

4041
def start(self):
4142
self.__add_job(self.__create_new_stock_purchase_job())
43+
self.__add_job(self.__create_repo_job())
4244
self.__add_job(self.__create_join_quant_following_job())
4345
self.__add_job(self.__create_rice_quant_following_job())
4446
self.__add_job(self.__create_uqer_following_job())
@@ -63,7 +65,13 @@ def __create_new_stock_purchase_job(self):
6365
section = 'NewStocks'
6466
options = self.__build_options(section)
6567
client_aliases = self.__filter_client_aliases(section)
66-
return NewStockPurchaseJob(self._client, client_aliases, '{}FollowingJob'.format(section), **options)
68+
return NewStockPurchaseJob(self._client, client_aliases, '{}Job'.format(section), **options)
69+
70+
def __create_repo_job(self):
71+
section = 'Repo'
72+
options = self.__build_options(section)
73+
client_aliases = self.__filter_client_aliases(section)
74+
return RepoJob(self._client, client_aliases, '{}Job'.format(section), **options)
6775

6876
def __create_join_quant_following_job(self):
6977
section = 'JoinQuant'

0 commit comments

Comments
 (0)