-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathTradingBot.py
199 lines (147 loc) · 5.85 KB
/
TradingBot.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 25 19:39:49 2018
@author: Khera
"""
import time
import binance
from binance.client import Client
from binance.enums import *
from numpy import *
import math
import datetime
import CoreFunctions as cf
#%%
api_key = 'API_KEY'
api_secret = 'API_SECRET'
client = Client(api_key, api_secret)
#%%
firstRun = True
makeTrade = False
state = 0
prevTime = 0
data = []
signals = []
currentBtc = cf.getCoinBalance(client, 'btc')
print(currentBtc)
currentBnb = cf.getCoinBalance(client, 'bnb')
print(currentBnb)
currentTRX = cf.getCoinBalance(client, 'TRX')
print(currentTRX)
hasToken = False
currentTokenBalance = 0
#to change market just use the find and replace command to replace TRX with any other symbol.
#For example replace TRX with LRC for the LRC-BTC market
market = "TRXBTC"
trade = "TRX"
sellToBuyTransition = False
buyPrice = 0
bestPrice = 0
sinceBest = 0
while(True):
#check time stamp if its different then add to list and change state
#IMPORTANT 498 is the latest full candle. if we use 499 ie the last candle
#we are using incomplete data which can cause false crossovers!
if state == 0:
candles = client.get_klines(symbol=market, interval=Client.KLINE_INTERVAL_5MINUTE)
if firstRun == True:
prevTime = datetime.datetime.fromtimestamp(candles[498][0]/ 1e3)
firstRun = False
makeTrade = True
for i in range(499):
data.append(candles[i])
else:
currTime = datetime.datetime.fromtimestamp(candles[498][0]/ 1e3)
if prevTime != currTime:
data.append(candles[498])
prevTime = currTime
makeTrade = True
else:
makeTrade = False
print(makeTrade)
# if timestamp is different then we attempt to trade
if makeTrade == True:
state = 1
makeTrade = False
#if its not then look for early selling opporunity to cash in profits
else:
if hasToken == True:
try:
prices = client.get_order_book(symbol=market)
price = prices['bids'][0][0]
if float(price) > float(bestPrice):
bestPrice = price
sinceBest = 0
else:
sinceBest = sinceBest + 1
if (float(price)/float(buyPrice))>1.001 and sinceBest >= 2:
print("Selling")
sellAmt = int(cf.getCoinBalance(client, trade))
cf.executeSell(client, market, sellAmt)
currentTokenBalance = 0
hasToken = False
sellToBuyTransition = False
buyPrice = 0
bestPrice = 0
sinceBest = 0
currentBtc = cf.getCoinBalance(client, 'btc')
else:
print("no early exit")
except Exception as e:
print(e)
time.sleep(10)
#make signals data used for the strategy
if state == 1:
signals = cf.makeTrainingData(data)
print(1)
state = 2
#buy BNB if we have less than required minimum - Uncomment in order to allow this to work!
if state == 2:
# currentBnb = cf.getCoinBalance(client, 'bnb')
#
# if currentBnb < 0.01:
# cf.executeBuy(client, 'BNBBTC', 0.1)
currentBtc = cf.getCoinBalance(client, 'btc')
state = 3
#make trade based on calculated signals
if state == 3:
current = signals[len(signals)-1]
if current[0] > current[1]:
print("Buy Signal")
if hasToken == False and sellToBuyTransition == True:
try:
print("Buying")
currentBtc = cf.getCoinBalance(client, 'btc')
prices = client.get_order_book(symbol=market)
price = prices['asks'][0][0]
buyPrice = price
buyAmt = int(currentBtc / float(price))
cf.executeBuy(client, market, buyAmt)
currentTokenBalance = buyAmt
hasToken = True
state = 0
time.sleep(10)
except Exception as e:
print(e)
else:
state = 0
time.sleep(10)
if current[0] < current[1]:
print("Sell Signal")
if sellToBuyTransition == False:
sellToBuyTransition = True
if hasToken == True:
try:
print("Selling")
sellAmt = int(cf.getCoinBalance(client, trade))
cf.executeSell(client, market, sellAmt)
currentTokenBalance = 0
hasToken = False
currentBtc = cf.getCoinBalance(client, 'btc')
state = 0
time.sleep(10)
except Exception as e:
print(e)
else:
state = 0
time.sleep(10)