forked from yasinkuyu/binance-trader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trader.py
179 lines (120 loc) · 4.91 KB
/
trader.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
# -*- coding: UTF-8 -*-
# @yasinkuyu
from BinanceAPI import *
import config
client = BinanceAPI(config.api_key, config.api_secret)
def errexit(msg):
print("Error: " + msg)
exit(1)
def lastPrice(symbol):
ret = client.get_ticker(symbol)
if 'msg' in ret:
errexit(ret['msg'])
return float(ret["lastPrice"])
def orderbooks(symbol):
ret = client.get_orderbooks(symbol, 5)
if 'msg' in ret:
errexit(ret['msg'])
print 'Bids:'
for (price, volume, dummy) in ret['bids']:
print '%lf\t%lf' % (float(price), float(volume))
print 'Asks: '
for (price, volume, dummy) in ret['asks']:
print '%lf\t%lf' % (float(price), float(volume))
print ''
def balances():
balances = client.get_account()
for balance in balances['balances'] :
if int(balance["locked"]) > 0 or int(balance["free"]) > 0 :
print '%s: %s' % (balance['asset'], balance['free'])
def orders(symbol, limit):
orders = client.get_all_orders(symbol=symbol, limit=limit)
print orders
def tickers():
return client.get_all_tickers()
def server_time():
return client.get_server_time()
def openorders(symbol):
return client.get_open_orders(symbol)
def checkorder(symbol, orderId):
ret = client.query_order(symbol, orderId)
if 'msg' in ret:
errexit(ret['msg'])
return ret['status']
symbol = 'LINKBTC'
print "The crypto money symbol. Sample: %s" % symbol
name = raw_input()
if name != "":
symbol = name
profit = 1.3 #percentage of profit
orderId = None
targetPrice = 0
quantity = 0.1
testMode = True
increasing = 0.00000001
targetProfitPrice = None
print '%%%s started to profit for %s' % (profit, symbol)
if testMode:
print "Test mode active"
while True:
ret = client.get_ticker(symbol)
lastPrice = float(ret["lastPrice"])
ret = client.get_orderbooks(symbol, 5)
lastBid = float(ret['bids'][0][0])
lastAsk = float(ret['asks'][0][0])
buyPrice = lastBid + increasing
sellPrice = lastAsk - increasing
checkProfitPrice = buyPrice + (buyPrice * profit / 100)
earnTotal = sellPrice - buyPrice
targetPrice = sellPrice
if orderId is None:
#orderStatus = checkorder(symbol, orderId) --> illegal karakter hatası
print 'price:%.8f buyp:%.8f sellp:%.8f (bid:%.8f ask:%.8f) ' % (lastPrice, buyPrice, sellPrice, lastBid, lastAsk)
if lastAsk >= checkProfitPrice:
targetProfitPrice = checkProfitPrice
if not testMode:
ret = client.buy_limit(symbol, quantity, buyPrice)
if 'msg' in ret:
errexit(ret['msg'])
orderId = ret['orderId']
print "******************"
print 'Order Id: %d' % orderId
else:
orderId = "100000"
print "Percentage of %s profit. Order created from %.8f. Profit: %.8f BTC" % (profit, sellPrice, earnTotal)
print "#####################"
else:
targetProfitPrice = None
if orderId is not None:
if not testMode:
ret = client.cancel(symbol, orderId)
if 'msg' in ret:
errexit(ret['msg'])
print 'Order has been canceled.'
else:
print "Target sell price: %.8f " % targetProfitPrice
if lastAsk >= targetProfitPrice:
ret = client.get_open_orders(symbol)
if 'msg' in ret:
errexit(ret['msg'])
print "Orders"
for order in ret:
price = float(order['price'])
origQty = float(order['origQty'])
executedQty = float(order['executedQty'])
if order['orderId'] == orderId and testMode:
if not testMode:
print "Order: %d: %lf\t%lf\t%lf" % (order['orderId'], price, origQty, executedQty)
else:
print "Order: 0000"
targetProfitPrice = None
orderId = None
if not testMode:
ret = client.sell_limit(symbol, quantity, targetPrice)
print 'Sales were made at %s price.' % (targetPrice)
print '---------------------------------------------'
if 'msg' in ret:
errexit(ret['msg'])
print ret
else:
print "Order Id: %s. The test order is complete. Price %s" % (orderId, targetPrice)