forked from yijixiuxin/chanlun-pro
-
Notifications
You must be signed in to change notification settings - Fork 3
/
trader_currency.py
82 lines (71 loc) · 3.44 KB
/
trader_currency.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
from cl_v2 import exchange_binance
from cl_v2 import rd
from cl_v2 import fun
from cl_v2 import trader
class CurrencyTrader(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)
self.exchange = exchange_binance.ExchangeBinance()
# 做多买入
def open_buy(self, code, opt):
# 固定买入 1000 USDT 的
open_usdt = 1000
leverage = 5
ticks = self.exchange.ticks([code])
amount = (open_usdt / ticks[code].last) * leverage
res = self.exchange.order(code, 'open_long', amount, {'leverage': leverage})
if res is False:
fun.send_dd_msg('currency', '%s 下单失败' % code)
return False
msg = '开多仓 %s 价格 %s 数量 %s 原因 %s' % (code, res['price'], res['amount'], opt['msg'])
fun.send_dd_msg('currency', msg)
rd.currency_opt_record_save(code, '策略交易:' + msg)
return {'price': res['price'], 'amount': res['amount']}
# 做空卖出
def open_sell(self, code, opt):
# 固定卖出 1000 USDT 的
open_usdt = 1000
leverage = 5
ticks = self.exchange.ticks([code])
amount = (open_usdt / ticks[code].last) * leverage
res = self.exchange.order(code, 'open_short', amount, {'leverage': leverage})
if res is False:
fun.send_dd_msg('currency', '%s 下单失败' % code)
return False
msg = '开空仓 %s 价格 %s 数量 %s 原因 %s' % (code, res['price'], res['amount'], opt['msg'])
fun.send_dd_msg('currency', msg)
rd.currency_opt_record_save(code, '策略交易:' + msg)
return {'price': res['price'], 'amount': res['amount']}
# 做多平仓
def close_buy(self, code, pos: trader.POSITION, opt):
hold_position = self.exchange.positions(code)
if len(hold_position) == 0:
return {'price': pos.price, 'amount': pos.amount}
hold_position = hold_position[0]
res = self.exchange.order(code, 'close_long', pos.amount)
if res is False:
fun.send_dd_msg('currency', '%s 下单失败' % code)
return False
msg = '平多仓 %s 价格 %s 数量 %s 盈亏 %s (%.2f%%) 原因 %s' % (
code, res['price'], res['amount'], hold_position['unrealizedPnl'], hold_position['percentage'], opt['msg'])
fun.send_dd_msg('currency', msg)
rd.currency_opt_record_save(code, '策略交易:' + msg)
return {'price': res['price'], 'amount': res['amount']}
# 做空平仓
def close_sell(self, code, pos: trader.POSITION, opt):
hold_position = self.exchange.positions(code)
if len(hold_position) == 0:
return {'price': pos.price, 'amount': pos.amount}
hold_position = hold_position[0]
res = self.exchange.order(code, 'close_short', pos.amount)
if res is False:
fun.send_dd_msg('currency', '%s 下单失败' % code)
return False
msg = '平空仓 %s 价格 %s 数量 %s 盈亏 %s (%.2f%%) 原因 %s' % (
code, res['price'], res['amount'], hold_position['unrealizedPnl'], hold_position['percentage'], opt['msg'])
fun.send_dd_msg('currency', msg)
rd.currency_opt_record_save(code, '策略交易:' + msg)
return {'price': res['price'], 'amount': res['amount']}