-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheksekusi_demo.py
More file actions
125 lines (100 loc) · 3.74 KB
/
eksekusi_demo.py
File metadata and controls
125 lines (100 loc) · 3.74 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
import os
import ccxt
from dotenv import load_dotenv
load_dotenv()
API_KEY = os.getenv("BINANCE_TESTNET_API_KEY", "").strip()
SECRET_KEY = os.getenv("BINANCE_TESTNET_SECRET_KEY", "").strip()
def _validate_credentials():
if not API_KEY or not SECRET_KEY:
raise RuntimeError(
"Missing BINANCE_TESTNET_API_KEY or BINANCE_TESTNET_SECRET_KEY. "
"Copy .env.example to .env and fill the Binance Futures testnet credentials first."
)
def inisialisasi_exchange():
_validate_credentials()
exchange = ccxt.binanceusdm(
{
"apiKey": API_KEY,
"secret": SECRET_KEY,
"enableRateLimit": True,
"options": {
"defaultType": "future",
"fetchCurrencies": False,
},
}
)
exchange.urls["api"] = {
"fapiPublic": "https://testnet.binancefuture.com/fapi/v1",
"fapiPrivate": "https://testnet.binancefuture.com/fapi/v1",
"fapiPrivateV2": "https://testnet.binancefuture.com/fapi/v2",
"fapiPrivateV3": "https://testnet.binancefuture.com/fapi/v3",
"public": "https://testnet.binancefuture.com/fapi/v1",
"private": "https://testnet.binancefuture.com/fapi/v1",
}
return exchange
def eksekusi_order(symbol_input, side, entry_price, tp_price, sl_price, modal_usd=100):
exchange = inisialisasi_exchange()
try:
markets = exchange.load_markets()
symbol_fix = symbol_input
if symbol_input not in markets:
print(f"⚠️ Symbol '{symbol_input}' tidak ditemukan langsung.")
for market_symbol in markets:
if market_symbol.startswith(symbol_input):
symbol_fix = market_symbol
print(f"✅ Menggunakan symbol alternatif: '{symbol_fix}'")
break
print(f"⚙️ Set leverage 1x untuk {symbol_fix}...")
exchange.set_leverage(1, symbol_fix)
ticker = exchange.fetch_ticker(symbol_fix)
last_price = ticker["last"]
quantity = modal_usd / last_price
quantity = float(exchange.amount_to_precision(symbol_fix, quantity))
print(f"🤖 EKSEKUSI DEMO: {side.upper()} {quantity} {symbol_fix}...")
order_entry = exchange.create_order(
symbol=symbol_fix,
type="market",
side=side,
amount=quantity,
)
print(f"✅ Entry sukses. Order ID: {order_entry['id']}")
side_exit = "sell" if side == "buy" else "buy"
exchange.create_order(
symbol=symbol_fix,
type="TAKE_PROFIT_MARKET",
side=side_exit,
amount=quantity,
params={"stopPrice": tp_price},
)
print(f"🎯 TP terpasang di: ${tp_price:,.2f}")
exchange.create_order(
symbol=symbol_fix,
type="STOP_MARKET",
side=side_exit,
amount=quantity,
params={"stopPrice": sl_price},
)
print(f"🛡️ SL terpasang di: ${sl_price:,.2f}")
return True
except Exception as e:
print(f"❌ Gagal eksekusi: {e}")
return False
if __name__ == "__main__":
print("Tes koneksi ke Binance Futures testnet...")
try:
exch = inisialisasi_exchange()
exch.load_markets()
balance = exch.fetch_balance()
usdt_free = balance["USDT"]["free"]
print(f"💰 Saldo demo: ${usdt_free:,.2f} USDT")
print("✅ Koneksi sukses. Mencoba order dummy...")
eksekusi_order(
symbol_input="BTC/USDT",
side="buy",
entry_price=90000,
tp_price=95000,
sl_price=85000,
modal_usd=200,
)
except Exception as e:
print(f"❌ Error: {e}")