-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
198 lines (160 loc) · 6.05 KB
/
main.py
File metadata and controls
198 lines (160 loc) · 6.05 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
import requests
import time
from datetime import datetime
# Config
TELEGRAM_TOKEN = "Your_Bot_Token_Here"
CHAT_ID = "1024188205"
# Paper trading state
balance_usd = 10000 # modal simulasi $10,000
balance_btc = 0
trade_history = []
prices = []
def send_telegram(pesan):
try:
url = f"https://api.telegram.org/bot{TELEGRAM_TOKEN}/sendMessage"
params = {"chat_id": CHAT_ID, "text": pesan, "parse_mode": "Markdown"}
requests.post(url, params=params, timeout=10)
except:
pass
def get_btc_price():
try:
url = "https://api.coingecko.com/api/v3/simple/price"
params = {"ids": "bitcoin", "vs_currencies": "usd"}
headers = {"User-Agent": "Mozilla/5.0"}
r = requests.get(url, params=params, headers=headers, timeout=15)
return float(r.json()['bitcoin']['usd'])
except:
return None
def calculate_sma(prices_list, period):
"""Simple Moving Average"""
if len(prices_list) < period:
return None
return sum(prices_list[-period:]) / period
def calculate_rsi(prices_list, period=14):
"""Relative Strength Index"""
if len(prices_list) < period + 1:
return None
gains = []
losses = []
for i in range(1, period + 1):
diff = prices_list[-i] - prices_list[-i-1]
if diff > 0:
gains.append(diff)
losses.append(0)
else:
gains.append(0)
losses.append(abs(diff))
avg_gain = sum(gains) / period
avg_loss = sum(losses) / period
if avg_loss == 0:
return 100
rs = avg_gain / avg_loss
return 100 - (100 / (1 + rs))
def buy_btc(price, reason):
global balance_usd, balance_btc
if balance_usd < 100:
return
amount_usd = balance_usd * 0.5 # beli 50% dari balance
amount_btc = amount_usd / price
balance_usd -= amount_usd
balance_btc += amount_btc
trade = {
"type": "BUY",
"price": price,
"btc": amount_btc,
"usd": amount_usd,
"time": datetime.now().strftime('%H:%M:%S'),
"reason": reason
}
trade_history.append(trade)
pesan = (
f"🟢 *PAPER TRADE — BUY*\n\n"
f"💰 Price : `${price:,.2f}`\n"
f"₿ BTC : `{amount_btc:.6f}`\n"
f"💵 USD : `${amount_usd:,.2f}`\n"
f"📊 Reason : {reason}\n"
f"🕐 Time : `{trade['time']}`\n\n"
f"💼 Balance: `${balance_usd:,.2f}` + `{balance_btc:.6f} BTC`"
)
send_telegram(pesan)
print(f"\n🟢 BUY at ${price:,.2f} — {reason}")
def sell_btc(price, reason):
global balance_usd, balance_btc
if balance_btc < 0.0001:
return
amount_btc = balance_btc * 0.5 # jual 50% BTC
amount_usd = amount_btc * price
balance_btc -= amount_btc
balance_usd += amount_usd
trade = {
"type": "SELL",
"price": price,
"btc": amount_btc,
"usd": amount_usd,
"time": datetime.now().strftime('%H:%M:%S'),
"reason": reason
}
trade_history.append(trade)
total_value = balance_usd + (balance_btc * price)
pnl = total_value - 10000
pesan = (
f"🔴 *PAPER TRADE — SELL*\n\n"
f"💰 Price : `${price:,.2f}`\n"
f"₿ BTC : `{amount_btc:.6f}`\n"
f"💵 USD : `${amount_usd:,.2f}`\n"
f"📊 Reason : {reason}\n"
f"🕐 Time : `{trade['time']}`\n\n"
f"💼 Balance: `${balance_usd:,.2f}` + `{balance_btc:.6f} BTC`\n"
f"📈 P&L : `${pnl:+,.2f}`"
)
send_telegram(pesan)
print(f"\n🔴 SELL at ${price:,.2f} — {reason}")
def run_bot():
global prices
print("🤖 PAPER TRADING BOT STARTED")
print(f"💵 Initial Balance: $10,000")
print(f"📊 Strategy: SMA Crossover + RSI")
print(f"⏱️ Interval: 30 seconds\n")
send_telegram(
"🤖 *PAPER TRADING BOT STARTED*\n\n"
"💵 Balance: `$10,000`\n"
"📊 Strategy: SMA + RSI\n"
"🔄 Auto-trading simulation started!"
)
while True:
price = get_btc_price()
if not price:
print("Gagal ambil harga, skip...")
time.sleep(30)
continue
prices.append(price)
# Hitung indicators
sma_short = calculate_sma(prices, 5) # SMA 5 periode
sma_long = calculate_sma(prices, 10) # SMA 10 periode
rsi = calculate_rsi(prices, 10)
total_value = balance_usd + (balance_btc * price)
pnl = total_value - 10000
print(f"\n{'='*45}")
print(f"⏰ {datetime.now().strftime('%H:%M:%S')}")
print(f"₿ BTC Price : ${price:,.2f}")
print(f"📊 SMA5 : ${sma_short:,.2f}" if sma_short else "📊 SMA5 : Collecting...")
print(f"📊 SMA10 : ${sma_long:,.2f}" if sma_long else "📊 SMA10 : Collecting...")
print(f"📈 RSI : {rsi:.1f}" if rsi else "📈 RSI : Collecting...")
print(f"💼 Balance : ${balance_usd:,.2f} + {balance_btc:.6f} BTC")
print(f"💰 Total : ${total_value:,.2f} (P&L: ${pnl:+,.2f})")
# Trading signals
if sma_short and sma_long and rsi:
# BUY signal: SMA short crosses above SMA long + RSI < 40
if sma_short > sma_long and rsi < 40 and balance_usd > 100:
buy_btc(price, f"SMA crossover + RSI={rsi:.1f}")
# SELL signal: SMA short crosses below SMA long + RSI > 60
elif sma_short < sma_long and rsi > 60 and balance_btc > 0.0001:
sell_btc(price, f"SMA crossover + RSI={rsi:.1f}")
else:
print("⏳ HOLD — No signal")
else:
data_needed = max(0, 11 - len(prices))
print(f"⏳ Collecting data... ({data_needed} more needed)")
time.sleep(30)
# Run bot
run_bot()