· 8 years ago · Jan 25, 2018, 08:12 PM
1from binance.client import Client
2from datetime import datetime
3import time
4import numpy
5import pandas
6from talib import abstract
7from stockstats import StockDataFrame
8import math
9
10class Trade:
11 def __init__(self, publicKey, secretKey, interval, symbol):
12 self.startingTime = int(time.time() - 60) * 1000
13 self.interval = interval
14 self.symbol = symbol
15 self.client = Client(publicKey, secretKey)
16 self.initializeHistoricalKlines()
17
18 def getCurrentKlines(self):
19 return self.client.get_klines(symbol=self.symbol, interval=self.interval, startTime=self.startingTime)
20
21 def initializeHistoricalKlines(self):
22 self.historicalKlines = self.client.get_klines(symbol=self.symbol, interval=self.interval, endTime=self.startingTime)
23
24 def getCurrentDataFrame(self):
25 currentKlines = self.getCurrentKlines()
26 allKlines = numpy.concatenate((self.historicalKlines, currentKlines), axis=0)
27
28 allKlines = numpy.delete(allKlines, [6,7,8,9,10,11], axis=1)
29 dataframe = pandas.DataFrame(allKlines)
30 dataframe.transpose()
31 dataframe.columns = ['timestamp', 'open', 'high', 'low', 'close', 'volume']
32 dataframe['datetime'] = dataframe.timestamp.apply(
33 lambda x: pandas.to_datetime(datetime.fromtimestamp(float(x) / 1000).strftime('%c'))
34 )
35 dataframe.set_index('datetime', inplace=True, drop=True)
36 dataframe.drop('timestamp', axis=1, inplace=True)
37
38 return dataframe.astype('float64')
39
40 def analyze_macd(self, hot_thresh, cold_thresh, all_data=False):
41 dataframe = self.getCurrentDataFrame()
42 macd_values = abstract.MACD(dataframe).iloc[:, 2]
43 macd_result_data = []
44
45 for i in range (0, macd_values.size):
46 if math.isnan(macd_values[i]):
47 continue
48
49 is_hot = False
50 if hot_thresh is not None:
51 is_hot = macd_values[i] > hot_thresh
52
53 is_cold = False
54 if cold_thresh is not None:
55 is_cold = macd_values[i] < cold_thresh
56
57 data_point_result = {
58 'date': macd_values.keys()[i],
59 'values': macd_values[i],
60 'is_cold': is_cold,
61 'is_hot': is_hot
62 }
63
64 macd_result_data.append(data_point_result)
65
66 if all_data:
67 return macd_result_data
68 else:
69 if macd_result_data:
70 return macd_result_data[-1]
71 else:
72 return macd_result_data
73
74 def run(self, hot_thresh, cold_thresh, buy):
75 lastMacd = 0
76 while(True):
77 macd = self.analyze_macd(hot_thresh=hot_thresh, cold_thresh=cold_thresh)
78 difference = math.fabs(macd['values'] - lastMacd)
79 if macd['is_hot'] and buy and difference > 1:
80 print(datetime.now(), end=' ')
81 print("Buy: ", end='')
82 print(self.client.get_orderbook_ticker(symbol=self.symbol)['askPrice'])
83 buy = False
84 lastMacd = macd['values']
85 elif macd['is_cold'] and buy == False and difference > 1:
86 print(datetime.now(), end=' ')
87 print("Sell: ", end='')
88 print(self.client.get_orderbook_ticker(symbol=self.symbol)['bidPrice'])
89 buy = True
90 lastMacd = macd['values']
91 time.sleep(1)
92
93
94trade = Trade(publicKey = '',
95 secretKey = '',
96 interval = "1m",
97 symbol = "BTCUSDT")
98print(trade.run(hot_thresh = 0, cold_thresh = 0, buy=True))