-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathartanbulma.py
87 lines (68 loc) · 2.41 KB
/
artanbulma.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
import websocket
import json
import pprint
import rel
from datetime import datetime
import botfunction as func
from botclass import BinanceExchangeInfo as symbolClass
rel.safe_read()
ORAN = 3.0
# websocket.enableTrace(True)
def on_close(ws, close_status_code, close_msg):
print(f"closed connection:{close_msg}")
def on_error(ws, error):
print(f"closed: {error}")
def on_open(ws):
print("opened connection")
def on_message(ws, message):
global ORAN
response = json.loads(message)
# print(json_data)
data = response['data']
# pprint.pprint(listTicker)
symbol = data["s"]
candle = data["k"]
isClosedCandle = candle["x"]
if isClosedCandle:
closePrice = float(candle["c"])
openPrice = float(candle["o"])
if closePrice > openPrice:
oran = (closePrice - openPrice) * 100 / openPrice
if oran > ORAN:
line = f"saat:{datetime.now()} symbol:{symbol} open price: {openPrice} close price:{closePrice} oran: {oran}"
# print(line)
with open("artancoin.txt", "a") as fp:
fp.write(line)
fp.write("\n")
def main():
# Connect database
db = func.connectDB()
dbCursor = db.cursor()
# Read binance section from config.ini
binanceConfig = func.readConfig(filename="config.ini", section="binance")
streamName = "@kline_3m"
symbol = symbolClass()
symbol.dbCursor = dbCursor
symbolRows = symbol.readAllSymbol()
symbolCount = len(symbolRows)
if (symbolRows is not None) and (symbolCount > 0):
streams = "stream?streams="
ix = 0
for symbolRow in symbolRows:
coin = json.loads(symbolRow[0])
coinName = coin["symbol"].lower()
streams = streams + f"{coinName}{streamName}"
ix = ix + 1
if ix < symbolCount:
streams = streams + "/"
baseUrl = binanceConfig["websocket_base"]
socketUrl = f"{baseUrl}/{streams}"
ws = websocket.WebSocketApp(socketUrl,
on_open=on_open,
on_message=on_message,
on_error=on_error,
on_close=on_close)
ws.run_forever(dispatcher=rel) # Set dispatcher to automatic reconnection
rel.signal(2, rel.abort)
rel.dispatch()
main()