-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
bot.py
executable file
·142 lines (120 loc) · 3.9 KB
/
bot.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
import os
import websocket as wb
from pprint import pprint
import json
# import talib
# import numpy as np
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()
# 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=ORDER_TYPE_MARKET, symbol=TRADE_SYMBOL):
# order_type = "MARKET" if side == "buy" else "LIMIT"
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):
# ws.send("{'event':'addChannel','channel':'ethusdt@kline_1m'}")
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)
# 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"]
pprint(symbol)
closed = candle["c"]
open = candle["o"]
high = candle["h"]
low = candle["l"]
volume = candle["v"]
interval = candle["i"]
pprint(f"closed: {closed}")
pprint(f"open: {open}")
pprint(f"high: {high}")
pprint(f"low: {low}")
pprint(f"volume: {volume}")
pprint(f"interval: {interval}")
pprint(f"is_candle_closed: {is_candle_closed}")
closed_prices.append(float(closed))
# create price entries
# print(TRADE_SYMBOL)
crypto = CryptoPrice(
crypto_name=symbol,
open_price=open,
close_price=closed,
high_price=high,
low_price=low,
volume=volume,
time=datetime.utcnow(),
)
# print(crypto.time, crypto.crypto_name, crypto.close_price, crypto.open_price, crypto.volume,
# crypto.high_price, crypto.low_price)
session.add(crypto)
session.commit()
session.close()
"""
if len(closed_prices) > RSI_PERIOD:
# closed_prices.pop(0)
all_rsi = talib.RSI(np.array(closed_prices), RSI_PERIOD)
pprint(f"all_rsi: {all_rsi}")
last_rsi = all_rsi[-1]
if last_rsi > RSI_OVERBOUGHT:
global in_position
if in_position:
print("Overbought, sell!")
success = order(SIDE_SELL, TRADE_SIZE, ORDER_TYPE_MARKET, TRADE_SYMBOL)
if success:
in_position = False
else:
print("overbought, but we dont have position")
elif last_rsi < RSI_OVERSOLD:
if in_position:
print("oversold but already in position")
else:
print("buy!")
success = order(SIDE_BUY, TRADE_SIZE, ORDER_TYPE_MARKET, TRADE_SYMBOL)
if success:
in_position = True
"""
ws = wb.WebSocketApp(BINANCE_SOCKET, on_open=on_open, on_close=on_close, on_error=on_error, on_message=on_message)
# ws1 = wb.WebSocketApp(B_S, on_open=on_open, on_close=on_close, on_error=on_error, on_message=on_message)
ws.run_forever()
# ws1.run_forever()