-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrade.py
More file actions
93 lines (80 loc) · 3.52 KB
/
trade.py
File metadata and controls
93 lines (80 loc) · 3.52 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
# trade.py – FINAL – SYMBOLUSDT + NO positionSide ERROR
from api import bingx_api_request
async def execute_trade(client, signal, usdt_amount, leverage=10, config=None, dry_run=False):
if config is None:
from config import get_config
config = get_config()
# Symbol fix: BTC/USDT → BTCUSDT
symbol = signal['symbol'].replace('/', '').replace('-', '').upper()
direction = signal['direction']
entry = signal['entry']
targets = signal['targets']
stoploss = signal['stoploss']
if dry_run:
print(f"[DRY RUN] Would open {direction} {symbol} {leverage}x ${usdt_amount:.2f}")
return
qty = round((usdt_amount * leverage) / entry, 6)
# Set leverage & isolated mode
await bingx_api_request('POST', '/openApi/swap/v2/trade/leverage', client['api_key'], client['secret_key'], data={
'symbol': symbol, 'side': 'BOTH', 'leverage': leverage
})
await bingx_api_request('POST', '/openApi/swap/v2/trade/marginType', client['api_key'], client['secret_key'], data={
'symbol': symbol, 'marginType': 'ISOLATED'
})
# Entry order – NO positionSide (One-Way mode)
side = 'BUY' if direction == 'LONG' else 'SELL'
opposite = 'SELL' if direction == 'LONG' else 'BUY'
entry_payload = {
'symbol': symbol,
'side': side,
'type': 'LIMIT',
'quantity': f"{qty:.6f}",
'price': str(entry),
'timeInForce': 'GTC',
'workingType': 'MARK_PRICE'
}
print(f"SENDING ENTRY ORDER: {entry_payload}")
entry_resp = await bingx_api_request('POST', '/openApi/swap/v2/trade/order', client['api_key'], client['secret_key'], data=entry_payload)
print(f"ENTRY RESPONSE: {entry_resp}")
# 4 Take Profits
closed = 0.0
for i, tp in enumerate(targets):
percent = [config['tp1_close_percent'], config['tp2_close_percent'], config['tp3_close_percent'], config['tp4_close_percent']][i]
tp_qty = qty * (percent / 100)
closed += percent
tp_payload = {
'symbol': symbol,
'side': opposite,
'type': 'TAKE_PROFIT_MARKET',
'quantity': f"{tp_qty:.6f}",
'stopPrice': str(tp),
'workingType': 'MARK_PRICE',
'reduceOnly': 'false'
}
await bingx_api_request('POST', '/openApi/swap/v2/trade/order', client['api_key'], client['secret_key'], data=tp_payload)
# Trailing Stop on remaining
remaining_qty = qty * (100 - closed) / 100
if remaining_qty > 0:
trail_payload = {
'symbol': symbol,
'side': opposite,
'type': 'TRAILING_STOP_MARKET',
'quantity': f"{remaining_qty:.6f}",
'callbackRate': str(config['trailing_callback_rate']),
'workingType': 'MARK_PRICE',
'reduceOnly': 'false'
}
await bingx_api_request('POST', '/openApi/swap/v2/trade/order', client['api_key'], client['secret_key'], data=trail_payload)
print("TRAILING STOP PLACED")
# Stop Loss
sl_payload = {
'symbol': symbol,
'side': opposite,
'type': 'STOP_MARKET',
'quantity': f"{qty:.6f}",
'stopPrice': str(stoploss),
'workingType': 'MARK_PRICE',
'reduceOnly': 'false'
}
await bingx_api_request('POST', '/openApi/swap/v2/trade/order', client['api_key'], client['secret_key'], data=sl_payload)
print(f"REAL TRADE EXECUTED: {symbol} {direction} {leverage}x – ${usdt_amount:.2f}")