@recluse 调仓后同步,规则你要写到调仓那里去,时间可以不写。同步雪球我后面又修正了一些问题
```
class Sync_to_xueqiu(Rule):
def handle_data(self, context, data):
# 限制只在某个时间执行同步
times = self._params.get('times', None)
if times is not None:
if [context.current_dt.hour, context.current_dt.minute] not in times:
return
try:
user = use('xq')
user.prepare(self._params.get('config_file', 'xq.json')) # 这个文件保存雪球的登录用户名,组合名啥的
# 获取雪球持仓
p = user.get_position()
old_stock_list = [normalize_code(x['stock_code']) for x in p]
new_stock_list = []
total_value = context.portfolio.total_value
for ps in context.portfolio.positions.values():
new_stock_list.append([ps.security, int(round(ps.total_amount * ps.price / total_value * 100))])
# 获取按比例的调仓列表
del_list = [[x, 0] for x in old_stock_list if x not in [stock for stock, rate in new_stock_list]]
new_stock_list = del_list + new_stock_list
info = []
total_rate = 0
for i, [stock, rate] in enumerate(new_stock_list):
rate = min(rate, 100 - total_rate)
info.append('[%s 调整为: %d%%]' % (show_stock(stock), rate))
total_rate += rate
# 雪球调仓
user.adjust_weight(stock[:6], rate)
self.log.info('雪球组合调仓:\n' + join_list(info, ' ', 1))
except:
self.log.error('同步持仓到雪球出错!')
def __str__(self):
return '%s [配置文件:%s]' % (self.memo, self._params.get('config_file', 'xq.json'))
```