-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmain.py
91 lines (77 loc) · 3.73 KB
/
main.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
# coding: utf-8
import pandas as pd
import talib
import numpy as np #computing multidimensionla arrays
import datetime
import urllib3
import time
import settings
#################################### Logging #################################################
import logging
logging.basicConfig(filename='null',format='%(message)s', level=logging.INFO)
logger = logging.getLogger(__name__)
#create a file handler
handler = logging.FileHandler('logfile.log')
handler.setLevel(logging.INFO)
#create a logging format
title = logging.Formatter('%(message)s')
handler.setFormatter(title)
# add the handlers to the logger
logger.addHandler(handler)
##############################################################################################
# Initialize Client and connect to Binance
from binance.client import Client
api_key = settings.api_key
api_secret = settings.api_secret
client = Client(api_key, api_secret)
# StochasticRSI Function
def Stoch(close,high,low, smoothk, smoothd, n):
lowestlow = pd.Series.rolling(low,window=n,center=False).min()
highesthigh = pd.Series.rolling(high, window=n, center=False).max()
K = pd.Series.rolling(100*((close-lowestlow)/(highesthigh-lowestlow)), window=smoothk).mean()
D = pd.Series.rolling(K, window=smoothd).mean()
return K, D
#################################### Logging #################################################
logger.info("Date Close RSI %K %D")
info = logging.Formatter('%(asctime)s %(message)s ','%Y-%m-%d %H:%M:%S')
handler.setFormatter(info)
logger.addHandler(handler)
##############################################################################################
# Main program
while True:
# ping client to avoid timeout
client = Client(api_key, api_secret)
# Get Binance Data into dataframe
candles = client.get_klines(symbol='BTCUSDT', interval=Client.KLINE_INTERVAL_1MINUTE)
df = pd.DataFrame(candles)
df.columns=['timestart','open','high','low','close','?','timeend','?','?','?','?','?']
df.timestart = [datetime.datetime.fromtimestamp(i/1000) for i in df.timestart.values]
df.timeend = [datetime.datetime.fromtimestamp(i/1000) for i in df.timeend.values]
# Compute RSI after fixing data
float_data = [float(x) for x in df.close.values]
np_float_data = np.array(float_data)
rsi = talib.RSI(np_float_data, 14)
df['rsi'] = rsi
# Compute StochRSI using RSI values in Stochastic function
mystochrsi = Stoch(df.rsi, df.rsi, df.rsi, 3, 3, 14)
df['MyStochrsiK'],df['MyStochrsiD'] = mystochrsi
#################################### End of Main #############################################
# WARNING: If Logging is removed uncomment the next line.
# time.sleep(1) # Sleep for 1 second. So IP is not rate limited. Can be faster. Up to 1200 requests per minute.
#################################### Logging #################################################
newestcandlestart = df.timestart.astype(str).iloc[-1] #gets last time
newestcandleend = df.timeend.astype(str).iloc[-1] #gets current time?
newestcandleclose = df.close.iloc[-1] #gets last close
newestcandleRSI = df.rsi.astype(str).iloc[-1] #gets last rsi
newestcandleK = df.MyStochrsiK.astype(str).iloc[-1] #gets last rsi
newestcandleD = df.MyStochrsiD.astype(str).iloc[-1] #gets last rsi
#Sleeps every 29 seconds and wakes up to post to logger.
t = datetime.datetime.utcnow()
sleeptime = (t.second)
if sleeptime == 0 or sleeptime ==30:
logger.info(newestcandleclose + " "
+ newestcandleRSI + " "
+ newestcandleK + " "
+ newestcandleD )
time.sleep(28)
##############################################################################################