-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathIndex.py
283 lines (220 loc) · 12.4 KB
/
Index.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import os
import asyncio
import math
import logging
from binance import AsyncClient, BinanceSocketManager
from dotenv import load_dotenv
from binance.enums import *
from datetime import datetime
# Rounds the order size down based on the maximum precision level allowed by the lot filter size
async def roundOrderSizeDown(tradeData, quantity):
step_size = [float(_['stepSize']) for _ in tradeData['symbolInfo']['filters'] if _['filterType'] == 'LOT_SIZE'][0]
step_size = '%.8f' % step_size
step_size = step_size.rstrip('0')
decimals = len(step_size.split('.')[1])
return math.floor(quantity * 10 ** decimals) / 10 ** decimals
# Rounds the order price precision down based on the maximum precision level allowed by the PRICE_FILTER tickSize
async def roundOrderPriceDown(tradeData, price):
step_size = [float(_['tickSize']) for _ in tradeData['symbolInfo']['filters'] if _['filterType'] == 'PRICE_FILTER'][0]
step_size = '%.8f' % step_size
step_size = step_size.rstrip('0')
decimals = len(step_size.split('.')[1])
return math.floor(price * 10 ** decimals) / 10 ** decimals
# Sums the commission fees paid in the order fills
async def getTotalFees(tradeData, order):
totalCommission = 0.0
for fill in order['fills']:
if fill['commissionAsset'] == 'USD':
totalCommission += float(fill['commission'])
elif fill['commissionAsset'] == 'BNB':
symbol = await tradeData['client'].get_ticker(symbol='BNBUSD')
totalCommission += float(symbol['lastPrice']) * float(fill['commission'])
else:
logging.error('WE HAVE A PROBLEM: commision Asset is not BNB or USD. Please add the logic to fix this. Asset: ' + fill['commissionAsset'])
quit()
return totalCommission
# Sums the fill prices paid
def getAverageFillPrice(order):
totalPrice = 0.0
totalWeight = 0.0
for fill in order['fills']:
weight = float(fill['qty'])
price = float(fill['price'])
totalPrice += price * weight
totalWeight += weight
return (totalPrice / totalWeight)
async def updatePriceFromSocket(tradeData):
socketPriceUpdate = await tradeData['webSocket'].recv()
while 'p' not in socketPriceUpdate:
socketPriceUpdate = await tradeData['webSocket'].recv()
return float(socketPriceUpdate['p'])
# Sells the existing position based on the trade data by placing a limit order and waiting for it to be filled
async def sellPosition(tradeData):
priceToSell = await roundOrderPriceDown(tradeData, (tradeData['currentPrice'] - (tradeData['currentPrice'] * tradeData['orderPriceDelta'])))
orderSize = await roundOrderSizeDown(tradeData, tradeData['baseBalance'])
logging.warning('Placing sell order, orderSize: ' + str(orderSize) + ' priceToSell: ' + str(priceToSell))
order = await tradeData['client'].create_order(
symbol=tradeData['tradeSymbol'],
side=SIDE_SELL,
type=ORDER_TYPE_LIMIT,
timeInForce=TIME_IN_FORCE_FOK,
quantity=orderSize,
price=priceToSell)
logging.warning('ORDER:')
logging.warning(order)
index = 0
orderComplete = False
while tradeData['positionExists'] == True and index < 300:
index += 1
await asyncio.sleep(1)
if order['status'] == ORDER_STATUS_FILLED:
logging.warning('Order state is filled.')
tradeData['positionExists'] = False
fees = await getTotalFees(tradeData, order)
averagePricePaid = getAverageFillPrice(order)
profit = ((float(order['executedQty']) * averagePricePaid) - tradeData['positionAcquiredCost']) - fees
logging.warning('Sell order fees: ' + str(fees) + ' quantitiy: ' + str(order['executedQty']) + ' price: ' + str(order['price']))
now = datetime.now()
dt_string = now.strftime('%d/%m/%Y %H:%M:%S')
fo = open('orders.txt', 'a')
fo.write(dt_string + ': Sell:' + tradeData['tradeSymbol'] + ' Price: ' + str(order['price']) + ' Quantity: ' + str(order['executedQty']) + ' Profit: ' + str(profit) + '\n')
fo.close()
orderComplete = True
break
if order['status'] == ORDER_STATUS_EXPIRED:
logging.warning('Order state is expired, failed to fill the order.')
orderComplete = True
break
if orderComplete == False:
await tradeData['client'].cancel_order(
symbol=tradeData['tradeSymbol'],
orderId=order['orderId'])
# Buys into a new position by placing a limit order and waiting for it to be filled
async def buyPosition(tradeData):
amountToSpend = tradeData['quoteTradeBalance'] - (tradeData['quoteTradeBalance'] * (float(tradeData['accountInfo']['takerCommission']) * .0001)) # Subtract fees
priceToBuy = await roundOrderPriceDown(tradeData, (tradeData['currentPrice'] + (tradeData['currentPrice'] * tradeData['orderPriceDelta'])))
orderSize = amountToSpend / priceToBuy
orderSize = await roundOrderSizeDown(tradeData, orderSize)
logging.warning('Placing buy order, orderSize: ' + str(orderSize) + ' priceToBuy: ' + str(priceToBuy))
order = await tradeData['client'].create_order(
symbol=tradeData['tradeSymbol'],
side=SIDE_BUY,
type=ORDER_TYPE_LIMIT,
timeInForce=TIME_IN_FORCE_FOK,
quantity=orderSize,
price=priceToBuy)
logging.warning('ORDER:')
logging.warning(order)
index = 0
orderComplete = False
while tradeData['positionExists'] == False and index < 120:
index += 1
await asyncio.sleep(1)
if order['status'] == ORDER_STATUS_FILLED:
logging.warning('Order state is filled.')
tradeData['positionExists'] = True
tradeData['positionAcquiredPrice'] = getAverageFillPrice(order)
tradeData['baseBalance'] = float(order['executedQty'])
fees = await getTotalFees(tradeData, order)
tradeData['positionAcquiredCost'] = (tradeData['baseBalance'] * tradeData['positionAcquiredPrice']) + fees
logging.warning('positionAcquiredPrice: ' + str(tradeData['positionAcquiredPrice']) + ' baseBalance: ' + str(tradeData['baseBalance']) + ' fees: ' + str(fees) + ' positionAcquiredCost: ' + str(tradeData['positionAcquiredCost']))
now = datetime.now()
dt_string = now.strftime('%d/%m/%Y %H:%M:%S')
fo = open('orders.txt', 'a')
fo.write(dt_string + ': Buy:' + tradeData['tradeSymbol'] + ' Price: ' + str(order['price']) + ' Quantity: ' + str(order['executedQty']) + ' Cost: ' + str(tradeData['positionAcquiredCost']) + '\n')
fo.close()
orderComplete = True
break
if order['status'] == ORDER_STATUS_EXPIRED:
logging.warning('Order state is expired, failed to fill the order.')
orderComplete = True
break
if orderComplete == False:
await tradeData['client'].cancel_order(
symbol=tradeData['tradeSymbol'],
orderId=order['orderId'])
# Loops on price updates until the price drops by the specified delta and is a profitable trade then triggers the sell
async def losePosition(tradeData):
while tradeData['positionExists'] == True:
tradeData['currentPrice'] = await updatePriceFromSocket(tradeData)
if tradeData['lastPeakPrice'] < tradeData['currentPrice']:
tradeData['lastPeakPrice'] = tradeData['currentPrice']
tradeData['lastValleyPrice'] = tradeData['currentPrice']
elif tradeData['lastValleyPrice'] > tradeData['currentPrice']:
tradeData['lastValleyPrice'] = tradeData['currentPrice']
target = tradeData['lastPeakPrice'] - (tradeData['lastPeakPrice'] * tradeData['sellPositionDelta'])
sellPrice = tradeData['currentPrice'] - (tradeData['currentPrice'] * tradeData['orderPriceDelta'])
receivedValue = (sellPrice * tradeData['baseBalance']) - ((sellPrice * tradeData['baseBalance']) * ((float(tradeData['accountInfo']['takerCommission']) * .0001))) # Should Taker or Maker fees be used in this calculation?
logging.info('New Valley Price: ' + str(tradeData['lastValleyPrice']))
logging.info('Must be less than or equal to target to trigger a sell, target: ' + str(target) + ' and ' + 'the received value: ' + str(receivedValue) + ' must be greater than the ' + 'positionAcquiredCost: ' + str(tradeData['positionAcquiredCost']))
if (tradeData['lastValleyPrice'] <= target) and (receivedValue > tradeData['positionAcquiredCost']):
logging.warning('Entering sell position.')
await sellPosition(tradeData)
# Loops on price updates until the price increases by the specified delta then triggers the buy
async def gainPosition(tradeData):
while tradeData['positionExists'] == False:
tradeData['currentPrice'] = await updatePriceFromSocket(tradeData)
if tradeData['lastPeakPrice'] < tradeData['currentPrice']:
tradeData['lastPeakPrice'] = tradeData['currentPrice'] #new peak price hit
target = tradeData['lastValleyPrice'] + (tradeData['lastValleyPrice'] * tradeData['buyPositionDelta'])
logging.info('New Peak Price: ' + str(tradeData['lastPeakPrice']))
logging.info('Must be greater than or equal to target to trigger a purchase, target: ' + str(target))
if tradeData['lastPeakPrice'] >= target:
logging.warning('Entering buy position.')
await buyPosition(tradeData)
elif tradeData['lastValleyPrice'] > tradeData['currentPrice']:
tradeData['lastPeakPrice'] = tradeData['currentPrice']
tradeData['lastValleyPrice'] = tradeData['currentPrice']
# Begins an infinite trading loop that alternates between gainPosition and losePosition based on if the position exists
async def beginTrading(tradeData):
tradeData['currentPrice'] = await updatePriceFromSocket(tradeData)
tradeData['lastPeakPrice'] = tradeData['currentPrice']
tradeData['lastValleyPrice'] = tradeData['currentPrice']
while True:
if tradeData['positionExists'] == False:
logging.warning('Entering gain position function.')
await gainPosition(tradeData)
else:
logging.warning('Entering lose position function.')
await losePosition(tradeData)
# Configures the logging, gets input from the user, configures the client, gets symbol and account info, initializes the socket manager
async def main():
# Configure the logging system
logging.basicConfig(filename ='botLog.log', level = logging.WARNING)
tradeSymbol = input('Enter the symbol you\'d like to trade (ex: BTCUSD): ')
sellPositionDelta = float(input('Enter the sell position delta (ex: .02): '))
buyPositionDelta = float(input('Enter the buy position delta (ex: .015): '))
client = await AsyncClient.create()
load_dotenv()
client = AsyncClient(os.getenv('API_KEY'), os.getenv('API_SECRET'), tld='us')
symbolInfo = await client.get_symbol_info(tradeSymbol)
accountInfo = await client.get_account()
quoteTradeBalance = float(input('Enter the amount of {0} to trade with (BTC ex: 0.00054, should be less then the max for rounding errors, and greater then the minimum order amount): '.format(symbolInfo['quoteAsset'])))
bsm = BinanceSocketManager(client)
async with bsm.trade_socket(tradeSymbol) as ts:
tradeData = {
'positionExists': False,
'lastPeakPrice': None,
'lastValleyPrice': None,
'tradeSymbol': tradeSymbol,
'sellPositionDelta': sellPositionDelta,
'buyPositionDelta': buyPositionDelta,
'orderPriceDelta': .002,
'client': client,
'symbolInfo': symbolInfo,
'accountInfo': accountInfo,
'quoteTradeBalance': quoteTradeBalance,
'currentPrice': None,
'webSocket': ts,
'positionAcquiredCost': None,
'positionAcquiredPrice': None,
'baseBalance': None
}
logging.warning('Initial Trade Data: ')
logging.warning(tradeData)
print('Beginning trading, see log file for more information. All orders placed by bot will be recorded in orders.txt')
await beginTrading(tradeData)
# Starts the program asynchronously
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(main())