-
Notifications
You must be signed in to change notification settings - Fork 263
Expand file tree
/
Copy pathtrading_engine.py
More file actions
242 lines (196 loc) · 9.23 KB
/
Copy pathtrading_engine.py
File metadata and controls
242 lines (196 loc) · 9.23 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
from datetime import datetime
from typing import Dict
import json
class TradingEngine:
def __init__(self, model_id: int, db, market_fetcher, ai_trader, trade_fee_rate: float = 0.001):
self.model_id = model_id
self.db = db
self.market_fetcher = market_fetcher
self.ai_trader = ai_trader
self.coins = ['BTC', 'ETH', 'SOL', 'BNB', 'XRP', 'DOGE']
self.trade_fee_rate = trade_fee_rate # 从配置中传入费率
def execute_trading_cycle(self) -> Dict:
try:
market_state = self._get_market_state()
current_prices = {coin: market_state[coin]['price'] for coin in market_state}
portfolio = self.db.get_portfolio(self.model_id, current_prices)
account_info = self._build_account_info(portfolio)
decisions = self.ai_trader.make_decision(
market_state, portfolio, account_info
)
self.db.add_conversation(
self.model_id,
user_prompt=self._format_prompt(market_state, portfolio, account_info),
ai_response=json.dumps(decisions, ensure_ascii=False),
cot_trace=''
)
execution_results = self._execute_decisions(decisions, market_state, portfolio)
updated_portfolio = self.db.get_portfolio(self.model_id, current_prices)
self.db.record_account_value(
self.model_id,
updated_portfolio['total_value'],
updated_portfolio['cash'],
updated_portfolio['positions_value']
)
return {
'success': True,
'decisions': decisions,
'executions': execution_results,
'portfolio': updated_portfolio
}
except Exception as e:
print(f"[ERROR] Trading cycle failed (Model {self.model_id}): {e}")
import traceback
print(traceback.format_exc())
return {
'success': False,
'error': str(e)
}
def _get_market_state(self) -> Dict:
market_state = {}
prices = self.market_fetcher.get_current_prices(self.coins)
for coin in self.coins:
if coin in prices:
market_state[coin] = prices[coin].copy()
indicators = self.market_fetcher.calculate_technical_indicators(coin)
market_state[coin]['indicators'] = indicators
return market_state
def _build_account_info(self, portfolio: Dict) -> Dict:
model = self.db.get_model(self.model_id)
initial_capital = model['initial_capital']
total_value = portfolio['total_value']
total_return = ((total_value - initial_capital) / initial_capital) * 100
return {
'current_time': datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
'total_return': total_return,
'initial_capital': initial_capital
}
def _format_prompt(self, market_state: Dict, portfolio: Dict,
account_info: Dict) -> str:
return f"Market State: {len(market_state)} coins, Portfolio: {len(portfolio['positions'])} positions"
def _execute_decisions(self, decisions: Dict, market_state: Dict,
portfolio: Dict) -> list:
results = []
for coin, decision in decisions.items():
if coin not in self.coins:
continue
signal = decision.get('signal', '').lower()
try:
if signal == 'buy_to_enter':
result = self._execute_buy(coin, decision, market_state, portfolio)
elif signal == 'sell_to_enter':
result = self._execute_sell(coin, decision, market_state, portfolio)
elif signal == 'close_position':
result = self._execute_close(coin, decision, market_state, portfolio)
elif signal == 'hold':
result = {'coin': coin, 'signal': 'hold', 'message': 'Hold position'}
else:
result = {'coin': coin, 'error': f'Unknown signal: {signal}'}
results.append(result)
except Exception as e:
results.append({'coin': coin, 'error': str(e)})
return results
def _execute_buy(self, coin: str, decision: Dict, market_state: Dict,
portfolio: Dict) -> Dict:
quantity = float(decision.get('quantity', 0))
leverage = int(decision.get('leverage', 1))
price = market_state[coin]['price']
if quantity <= 0:
return {'coin': coin, 'error': 'Invalid quantity'}
# 计算交易额和交易费(按交易额的比例)
trade_amount = quantity * price # 交易额
trade_fee = trade_amount * self.trade_fee_rate # 交易费(0.1%)
required_margin = (quantity * price) / leverage # 保证金
# 总需资金 = 保证金 + 交易费
total_required = required_margin + trade_fee
if total_required > portfolio['cash']:
return {'coin': coin, 'error': 'Insufficient cash (including fees)'}
# 更新持仓
self.db.update_position(
self.model_id, coin, quantity, price, leverage, 'long'
)
# 记录交易(包含交易费)
self.db.add_trade(
self.model_id, coin, 'buy_to_enter', quantity,
price, leverage, 'long', pnl=0, fee=trade_fee # 新增fee参数
)
return {
'coin': coin,
'signal': 'buy_to_enter',
'quantity': quantity,
'price': price,
'leverage': leverage,
'fee': trade_fee, # 返回费用信息
'message': f'Long {quantity:.4f} {coin} @ ${price:.2f} (Fee: ${trade_fee:.2f})'
}
def _execute_sell(self, coin: str, decision: Dict, market_state: Dict,
portfolio: Dict) -> Dict:
quantity = float(decision.get('quantity', 0))
leverage = int(decision.get('leverage', 1))
price = market_state[coin]['price']
if quantity <= 0:
return {'coin': coin, 'error': 'Invalid quantity'}
# 计算交易额和交易费
trade_amount = quantity * price
trade_fee = trade_amount * self.trade_fee_rate
required_margin = (quantity * price) / leverage
# 总需资金 = 保证金 + 交易费
total_required = required_margin + trade_fee
if total_required > portfolio['cash']:
return {'coin': coin, 'error': 'Insufficient cash (including fees)'}
# 更新持仓
self.db.update_position(
self.model_id, coin, quantity, price, leverage, 'short'
)
# 记录交易(包含交易费)
self.db.add_trade(
self.model_id, coin, 'sell_to_enter', quantity,
price, leverage, 'short', pnl=0, fee=trade_fee # 新增fee参数
)
return {
'coin': coin,
'signal': 'sell_to_enter',
'quantity': quantity,
'price': price,
'leverage': leverage,
'fee': trade_fee,
'message': f'Short {quantity:.4f} {coin} @ ${price:.2f} (Fee: ${trade_fee:.2f})'
}
def _execute_close(self, coin: str, decision: Dict, market_state: Dict,
portfolio: Dict) -> Dict:
position = None
for pos in portfolio['positions']:
if pos['coin'] == coin:
position = pos
break
if not position:
return {'coin': coin, 'error': 'Position not found'}
current_price = market_state[coin]['price']
entry_price = position['avg_price']
quantity = position['quantity']
side = position['side']
# 计算平仓利润(未扣费)
if side == 'long':
gross_pnl = (current_price - entry_price) * quantity
else: # short
gross_pnl = (entry_price - current_price) * quantity
# 计算平仓交易费(按平仓时的交易额)
trade_amount = quantity * current_price
trade_fee = trade_amount * self.trade_fee_rate
net_pnl = gross_pnl - trade_fee # 净利润 = 毛利润 - 交易费
# 关闭持仓
self.db.close_position(self.model_id, coin, side)
# 记录平仓交易(包含费用和净利润)
self.db.add_trade(
self.model_id, coin, 'close_position', quantity,
current_price, position['leverage'], side, pnl=net_pnl, fee=trade_fee # 新增fee参数
)
return {
'coin': coin,
'signal': 'close_position',
'quantity': quantity,
'price': current_price,
'pnl': net_pnl,
'fee': trade_fee,
'message': f'Close {coin}, Gross P&L: ${gross_pnl:.2f}, Fee: ${trade_fee:.2f}, Net P&L: ${net_pnl:.2f}'
}