-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
bot_2.py
executable file
·132 lines (111 loc) · 3.24 KB
/
bot_2.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
import os
import websocket as wb
from pprint import pprint
import json
import redis
import os
from binance.client import Client
from binance.enums import *
from dotenv import load_dotenv
from datetime import datetime
from database_insert import create_table
from base_sql import Session
from price_data_sql import CryptoPrice
load_dotenv()
# connect to Redis
r = redis.Redis(host="localhost", port=6379, db=0)
# this functions creates the table if it does not exist
create_table()
# create a session
session = Session()
BINANCE_SOCKET = "wss://stream.binance.com:9443/stream?streams=ethusdt@kline_3m/btcusdt@kline_3m"
B_S = "wss://stream.binance.com:9443/stream?streams=btcusdt@aggTrade/btcusdt@depth"
RSI_PERIOD = 14
RSI_OVERBOUGHT = 70
RSI_OVERSOLD = 30
TRADE_SYMBOL = "ETHUSD"
TRADE_SIZE = 0.05
closed_prices = []
in_position = False
API_KEY = os.environ.get("API_KEY")
API_SECRET = os.environ.get("API_SECRET")
client = Client(API_KEY, API_SECRET, tld="us")
def order(side, size, order_type=Client.ORDER_TYPE_MARKET, symbol=TRADE_SYMBOL):
try:
order = client.create_order(
symbol=symbol,
side=side,
type=order_type,
quantity=size,
)
print(order)
return True
except Exception as e:
print(e)
return False
def on_open(ws):
print("connection opened")
def on_close(ws):
print("closed connection")
def on_error(ws, error):
print(error)
def on_message(ws, message):
message = json.loads(message)
session = Session()
# pprint(message)
candle = message["data"]["k"]
pprint(candle)
trade_symbol = candle["s"]
is_candle_closed = candle["x"]
global closed_prices
# if is_candle_closed:
symbol = candle["s"]
closed = candle["c"]
open = candle["o"]
high = candle["h"]
low = candle["l"]
volume = candle["v"]
pprint(f"closed: {closed}")
pprint(f"open: {open}")
pprint(f"high: {high}")
pprint(f"low: {low}")
pprint(f"volume: {volume}")
closed_prices.append(float(closed))
# create price entries
print(trade_symbol)
crypto = CryptoPrice(
crypto_name=symbol,
open_price=float(open),
close_price=float(closed),
high_price=float(high),
low_price=float(low),
volume=float(volume),
time=datetime.utcnow(),
)
print(
f"Time: {crypto.time}, Name: {crypto.crypto_name}, Close Price: {crypto.close_price}, Open Price: {crypto.open_price}, Volume: {crypto.volume}"
)
with session.open() as session:
try:
session.add(crypto)
session.commit()
except Exception as e:
print(e)
session.rollback()
session.close()
# add message to Redis queue
message_data = {
"symbol": symbol,
"open_price": open,
"close_price": closed,
"high_price": high,
"low_price": low,
"volume": volume,
"time": str(crypto.time),
}
r.lpush("crypto", json.dumps(message_data))
print(closed_prices)
# create a WebSocketApp instance
ws = wb.WebSocketApp(BINANCE_SOCKET, on_open=on_open, on_close=on_close, on_error=on_error, on_message=on_message)
# run the WebSocket connection in a separate thread
ws.run_forever()