forked from youtube-jocoding/gpt-bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autotrade_v3.py
389 lines (334 loc) · 15.4 KB
/
autotrade_v3.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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
import os
from dotenv import load_dotenv
load_dotenv()
import pyupbit
import pandas as pd
import pandas_ta as ta
import json
from openai import OpenAI
import schedule
import time
import requests
from datetime import datetime
import sqlite3
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import base64
# Setup
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
upbit = pyupbit.Upbit(os.getenv("UPBIT_ACCESS_KEY"), os.getenv("UPBIT_SECRET_KEY"))
def initialize_db(db_path='trading_decisions.sqlite'):
with sqlite3.connect(db_path) as conn:
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS decisions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
timestamp DATETIME,
decision TEXT,
percentage REAL,
reason TEXT,
btc_balance REAL,
krw_balance REAL,
btc_avg_buy_price REAL,
btc_krw_price REAL
);
''')
conn.commit()
def save_decision_to_db(decision, current_status):
db_path = 'trading_decisions.sqlite'
with sqlite3.connect(db_path) as conn:
cursor = conn.cursor()
# Parsing current_status from JSON to Python dict
status_dict = json.loads(current_status)
current_price = pyupbit.get_orderbook(ticker="KRW-BTC")['orderbook_units'][0]["ask_price"]
# Preparing data for insertion
data_to_insert = (
decision.get('decision'),
decision.get('percentage', 100), # Defaulting to 100 if not provided
decision.get('reason', ''), # Defaulting to an empty string if not provided
status_dict.get('btc_balance'),
status_dict.get('krw_balance'),
status_dict.get('btc_avg_buy_price'),
current_price
)
# Inserting data into the database
cursor.execute('''
INSERT INTO decisions (timestamp, decision, percentage, reason, btc_balance, krw_balance, btc_avg_buy_price, btc_krw_price)
VALUES (datetime('now', 'localtime'), ?, ?, ?, ?, ?, ?, ?)
''', data_to_insert)
conn.commit()
def fetch_last_decisions(db_path='trading_decisions.sqlite', num_decisions=10):
with sqlite3.connect(db_path) as conn:
cursor = conn.cursor()
cursor.execute('''
SELECT timestamp, decision, percentage, reason, btc_balance, krw_balance, btc_avg_buy_price FROM decisions
ORDER BY timestamp DESC
LIMIT ?
''', (num_decisions,))
decisions = cursor.fetchall()
if decisions:
formatted_decisions = []
for decision in decisions:
# Converting timestamp to milliseconds since the Unix epoch
ts = datetime.strptime(decision[0], "%Y-%m-%d %H:%M:%S")
ts_millis = int(ts.timestamp() * 1000)
formatted_decision = {
"timestamp": ts_millis,
"decision": decision[1],
"percentage": decision[2],
"reason": decision[3],
"btc_balance": decision[4],
"krw_balance": decision[5],
"btc_avg_buy_price": decision[6]
}
formatted_decisions.append(str(formatted_decision))
return "\n".join(formatted_decisions)
else:
return "No decisions found."
def get_current_status():
orderbook = pyupbit.get_orderbook(ticker="KRW-BTC")
current_time = orderbook['timestamp']
btc_balance = 0
krw_balance = 0
btc_avg_buy_price = 0
balances = upbit.get_balances()
for b in balances:
if b['currency'] == "BTC":
btc_balance = b['balance']
btc_avg_buy_price = b['avg_buy_price']
if b['currency'] == "KRW":
krw_balance = b['balance']
current_status = {'current_time': current_time, 'orderbook': orderbook, 'btc_balance': btc_balance, 'krw_balance': krw_balance, 'btc_avg_buy_price': btc_avg_buy_price}
return json.dumps(current_status)
def fetch_and_prepare_data():
# Fetch data
df_daily = pyupbit.get_ohlcv("KRW-BTC", "day", count=30)
df_hourly = pyupbit.get_ohlcv("KRW-BTC", interval="minute60", count=24)
# Define a helper function to add indicators
def add_indicators(df):
# Moving Averages
df['SMA_10'] = ta.sma(df['close'], length=10)
df['EMA_10'] = ta.ema(df['close'], length=10)
# RSI
df['RSI_14'] = ta.rsi(df['close'], length=14)
# Stochastic Oscillator
stoch = ta.stoch(df['high'], df['low'], df['close'], k=14, d=3, smooth_k=3)
df = df.join(stoch)
# MACD
ema_fast = df['close'].ewm(span=12, adjust=False).mean()
ema_slow = df['close'].ewm(span=26, adjust=False).mean()
df['MACD'] = ema_fast - ema_slow
df['Signal_Line'] = df['MACD'].ewm(span=9, adjust=False).mean()
df['MACD_Histogram'] = df['MACD'] - df['Signal_Line']
# Bollinger Bands
df['Middle_Band'] = df['close'].rolling(window=20).mean()
# Calculate the standard deviation of closing prices over the last 20 days
std_dev = df['close'].rolling(window=20).std()
# Calculate the upper band (Middle Band + 2 * Standard Deviation)
df['Upper_Band'] = df['Middle_Band'] + (std_dev * 2)
# Calculate the lower band (Middle Band - 2 * Standard Deviation)
df['Lower_Band'] = df['Middle_Band'] - (std_dev * 2)
return df
# Add indicators to both dataframes
df_daily = add_indicators(df_daily)
df_hourly = add_indicators(df_hourly)
combined_df = pd.concat([df_daily, df_hourly], keys=['daily', 'hourly'])
combined_data = combined_df.to_json(orient='split')
return json.dumps(combined_data)
def get_news_data():
### Get news data from SERPAPI
url = "https://serpapi.com/search.json?engine=google_news&q=btc&api_key=" + os.getenv("SERPAPI_API_KEY")
result = "No news data available."
try:
response = requests.get(url)
news_results = response.json()['news_results']
simplified_news = []
for news_item in news_results:
# Check if this news item contains 'stories'
if 'stories' in news_item:
for story in news_item['stories']:
timestamp = int(datetime.strptime(story['date'], '%m/%d/%Y, %H:%M %p, %z %Z').timestamp() * 1000)
simplified_news.append((story['title'], story.get('source', {}).get('name', 'Unknown source'), timestamp))
else:
# Process news items that are not categorized under stories but check date first
if news_item.get('date'):
timestamp = int(datetime.strptime(news_item['date'], '%m/%d/%Y, %H:%M %p, %z %Z').timestamp() * 1000)
simplified_news.append((news_item['title'], news_item.get('source', {}).get('name', 'Unknown source'), timestamp))
else:
simplified_news.append((news_item['title'], news_item.get('source', {}).get('name', 'Unknown source'), 'No timestamp provided'))
result = str(simplified_news)
except Exception as e:
print(f"Error fetching news data: {e}")
return result
def fetch_fear_and_greed_index(limit=1, date_format=''):
"""
Fetches the latest Fear and Greed Index data.
Parameters:
- limit (int): Number of results to return. Default is 1.
- date_format (str): Date format ('us', 'cn', 'kr', 'world'). Default is '' (unixtime).
Returns:
- dict or str: The Fear and Greed Index data in the specified format.
"""
base_url = "https://api.alternative.me/fng/"
params = {
'limit': limit,
'format': 'json',
'date_format': date_format
}
response = requests.get(base_url, params=params)
myData = response.json()['data']
resStr = ""
for data in myData:
resStr += str(data)
return resStr
def get_current_base64_image():
screenshot_path = "screenshot.png"
try:
# Set up Chrome options for headless mode
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-dev-shm-usage")
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--window-size=1920x1080")
service = Service('/usr/local/bin/chromedriver') # Specify the path to the ChromeDriver executable
# Initialize the WebDriver with the specified options
driver = webdriver.Chrome(service=service, options=chrome_options)
# Navigate to the desired webpage
driver.get("https://upbit.com/full_chart?code=CRIX.UPBIT.KRW-BTC")
# Wait for the page to load completely
wait = WebDriverWait(driver, 10) # 10 seconds timeout
# Wait for the first menu item to be clickable and click it
first_menu_item = wait.until(EC.element_to_be_clickable((By.XPATH, "//*[@id='fullChartiq']/div/div/div[1]/div/div/cq-menu[1]")))
first_menu_item.click()
# Wait for the "1 Hour" option to be clickable and click it
one_hour_option = wait.until(EC.element_to_be_clickable((By.XPATH, "//cq-item[@stxtap=\"Layout.setPeriodicity(1,60,'minute')\"]")))
one_hour_option.click()
# Wait for the indicators menu item to be clickable and click it
indicators_menu_item = wait.until(EC.element_to_be_clickable((By.XPATH, "//*[@id='fullChartiq']/div/div/div[1]/div/div/cq-menu[3]")))
indicators_menu_item.click()
# Wait for the indicators container to be present
indicators_container = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "cq-scroll.ps-container")))
# Scroll the container to make the "MACD" indicator visible
driver.execute_script("arguments[0].scrollTop = arguments[0].scrollHeight / 2.5", indicators_container)
# Wait for the "MACD" indicator to be clickable and click it
macd_indicator = wait.until(EC.element_to_be_clickable((By.XPATH, "//cq-item[translate[@original='MACD']]")))
macd_indicator.click()
# Take a screenshot to verify the actions
driver.save_screenshot(screenshot_path)
except Exception as e:
print(f"Error making current image: {e}")
return ""
finally:
# Close the browser
driver.quit()
with open(screenshot_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode('utf-8')
def get_instructions(file_path):
try:
with open(file_path, "r", encoding="utf-8") as file:
instructions = file.read()
return instructions
except FileNotFoundError:
print("File not found.")
except Exception as e:
print("An error occurred while reading the file:", e)
def analyze_data_with_gpt4(news_data, data_json, last_decisions, fear_and_greed, current_status, current_base64_image):
instructions_path = "instructions_v3.md"
try:
instructions = get_instructions(instructions_path)
if not instructions:
print("No instructions found.")
return None
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": instructions},
{"role": "user", "content": news_data},
{"role": "user", "content": data_json},
{"role": "user", "content": last_decisions},
{"role": "user", "content": fear_and_greed},
{"role": "user", "content": current_status},
{"role": "user", "content": [{"type": "image_url","image_url": {"url": f"data:image/jpeg;base64,{current_base64_image}"}}]}
],
response_format={"type":"json_object"}
)
advice = response.choices[0].message.content
return advice
except Exception as e:
print(f"Error in analyzing data with GPT-4: {e}")
return None
def execute_buy(percentage):
print("Attempting to buy BTC with a percentage of KRW balance...")
try:
krw_balance = upbit.get_balance("KRW")
amount_to_invest = krw_balance * (percentage / 100)
if amount_to_invest > 5000: # Ensure the order is above the minimum threshold
result = upbit.buy_market_order("KRW-BTC", amount_to_invest * 0.9995) # Adjust for fees
print("Buy order successful:", result)
except Exception as e:
print(f"Failed to execute buy order: {e}")
def execute_sell(percentage):
print("Attempting to sell a percentage of BTC...")
try:
btc_balance = upbit.get_balance("BTC")
amount_to_sell = btc_balance * (percentage / 100)
current_price = pyupbit.get_orderbook(ticker="KRW-BTC")['orderbook_units'][0]["ask_price"]
if current_price * amount_to_sell > 5000: # Ensure the order is above the minimum threshold
result = upbit.sell_market_order("KRW-BTC", amount_to_sell)
print("Sell order successful:", result)
except Exception as e:
print(f"Failed to execute sell order: {e}")
def make_decision_and_execute():
print("Making decision and executing...")
try:
news_data = get_news_data()
data_json = fetch_and_prepare_data()
last_decisions = fetch_last_decisions()
fear_and_greed = fetch_fear_and_greed_index(limit=30)
current_status = get_current_status()
current_base64_image = get_current_base64_image()
except Exception as e:
print(f"Error: {e}")
else:
max_retries = 5
retry_delay_seconds = 5
decision = None
for attempt in range(max_retries):
try:
advice = analyze_data_with_gpt4(news_data, data_json, last_decisions, fear_and_greed, current_status, current_base64_image)
decision = json.loads(advice)
break
except Exception as e:
print(f"JSON parsing failed: {e}. Retrying in {retry_delay_seconds} seconds...")
time.sleep(retry_delay_seconds)
print(f"Attempt {attempt + 2} of {max_retries}")
if not decision:
print("Failed to make a decision after maximum retries.")
return
else:
try:
percentage = decision.get('percentage', 100)
if decision.get('decision') == "buy":
execute_buy(percentage)
elif decision.get('decision') == "sell":
execute_sell(percentage)
save_decision_to_db(decision, current_status)
except Exception as e:
print(f"Failed to execute the decision or save to DB: {e}")
if __name__ == "__main__":
initialize_db()
# testing
# schedule.every().minute.do(make_decision_and_execute)
# Schedule the task to run at 00:01
schedule.every().day.at("00:01").do(make_decision_and_execute)
# Schedule the task to run at 08:01
schedule.every().day.at("08:01").do(make_decision_and_execute)
# Schedule the task to run at 16:01
schedule.every().day.at("16:01").do(make_decision_and_execute)
while True:
schedule.run_pending()
time.sleep(1)