forked from yijixiuxin/chanlun-pro
-
Notifications
You must be signed in to change notification settings - Fork 3
/
trader_hk_stock.py
97 lines (84 loc) · 3.96 KB
/
trader_hk_stock.py
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
from cl_v2 import exchange_futu
from cl_v2 import fun
from cl_v2 import trader
# 交易所对象放到外面,不然无法进行序列化
futu_ex = exchange_futu.ExchangeFutu()
class HKStockTrader(trader.Trader):
"""
港股股票交易对象
"""
def __init__(self, name, is_stock=True, is_futures=False, mmds=None, log=None, is_test=False):
super().__init__(name, is_stock, is_futures, mmds, log, is_test=False)
self.b_space = 3 # 资金分割数量
# 做多买入
def open_buy(self, code, opt):
positions = futu_ex.positions()
if len(positions) >= self.b_space:
return False
stock_info = futu_ex.stock_info(code)
if stock_info is None:
return False
can_tv = futu_ex.can_trade_val(code)
if can_tv is None:
return False
max_amount = (can_tv['max_margin_buy'] / (self.b_space - len(positions)))
max_amount = max_amount - (max_amount % stock_info['lot_size'])
if max_amount == 0:
return False
order = futu_ex.order(code, 'buy', max_amount)
if order is False:
fun.send_dd_msg('hk', '%s 下单失败 买入数量 %s' % (code, max_amount))
return False
msg = '股票买入 %s 价格 %s 数量 %s 原因 %s' % (code, order['dealt_avg_price'], order['dealt_amount'], opt['msg'])
fun.send_dd_msg('hk', msg)
return {'price': order['dealt_avg_price'], 'amount': order['dealt_amount']}
# 做空卖出
def open_sell(self, code, opt):
positions = futu_ex.positions()
if len(positions) >= self.b_space:
return False
stock_info = futu_ex.stock_info(code)
if stock_info is None:
return False
can_tv = futu_ex.can_trade_val(code)
if can_tv is None:
return False
max_amount = (can_tv['max_margin_short'] / (self.b_space - len(positions)))
max_amount = max_amount - (max_amount % stock_info['lot_size'])
if max_amount == 0:
return False
order = futu_ex.order(code, 'sell', max_amount)
if order is False:
fun.send_dd_msg('hk', '%s 下单失败 卖出数量 %s' % (code, max_amount))
return False
msg = '股票卖空 %s 价格 %s 数量 %s 原因 %s' % (code, order['dealt_avg_price'], order['dealt_amount'], opt['msg'])
fun.send_dd_msg('hk', msg)
return {'price': order['dealt_avg_price'], 'amount': order['dealt_amount']}
# 做多平仓
def close_buy(self, code, pos: trader.POSITION, opt):
positions = futu_ex.positions(code)
if len(positions) == 0:
return {'price': pos.price, 'amount': pos.amount}
order = futu_ex.order(code, 'sell', pos.amount)
if order is False:
fun.send_dd_msg('hk', '%s 下单失败 平仓卖出 %s' % (code, pos.amount))
return False
msg = '股票卖出 %s 价格 %s 数量 %s 盈亏 %s (%.2f%%) 原因 %s' % (
code, order['dealt_avg_price'], order['dealt_amount'], positions[0]['profit_val'], positions[0]['profit'],
opt['msg'])
fun.send_dd_msg('hk', msg)
return {'price': order['dealt_avg_price'], 'amount': order['dealt_amount']}
# 做空平仓
def close_sell(self, code, pos: trader.POSITION, opt):
positions = futu_ex.positions(code)
if len(positions) == 0:
return {'price': pos.price, 'amount': pos.amount}
order = futu_ex.order(code, 'buy', pos.amount)
if order is False:
fun.send_dd_msg('hk', '%s 下单失败 平仓买入 %s' % (code, pos.amount))
return False
msg = '股票平空 %s 价格 %s 数量 %s 盈亏 %s (%.2f%%) 原因 %s' % (
code, order['dealt_avg_price'], order['dealt_amount'], positions[0]['profit_val'], positions[0]['profit'],
opt['msg'])
fun.send_dd_msg('hk', msg)
return {'price': order['dealt_avg_price'], 'amount': order['dealt_amount']}