-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathauto.py
99 lines (85 loc) · 3.01 KB
/
auto.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
from btceapi import common
from btceapi import trade
from btceapi import public
import sys
import collections
import threading
import time
#Value that determins how significant a change must be to make a trade
#If value goes up or down this much in USD, a sell or buy will be attempted
trade_threshold = 0.001
verbose = 2 #0 = only report trades or attempted trades, 1 = inform of current price 2 = relay all data collected
#set nonce to current time
nonce = (int(time.time()))
#how many seconds to wait before refreshing price
wait = 5
#note this api key will be deleted and at no point will have trade or withdrawal rights, trying to use it will be useless
api_key = "8E9LX6K1-JSN6HS6G-HRH2XM57-X5ULY2FV-QTQHN6XN"
api_secret = "c4556cc5329a6fd74790ca37a48212eddaeec6c34c0b67f0ac9ea59e7c9e9d6d"
api = trade.TradeAPI(api_key, api_secret, nonce)
#set what to exchange (i.e. ltc_usd for LTC to USD or btc_ltc for BTC to LTC)
pair = "ltc_usd"
#set these to your pair, (i.e. "btc" for first and "usd" for the second for btc_usd)
curr1 = "balance_ltc"
curr2 = "balance_usd"
#gets the last trade price from btc-e
def get_last(pair):
tickerW = common.makeJSONRequest("/api/2/%s/ticker" % pair)
ticker = tickerW.get(u'ticker')
last_price = ticker.get(u'last')
return last_price
last = get_last(pair)
#initializes 10 element list of prices with first last as value for each element
price_list = [last] * 10
#sets current price by averaging last ten results of get_last
def average_price(v = 2):
average_last = sum(price_list)/ float(len(price_list))
#price_list = collections.deque([])
#price_list.appendleft(last)
price_list.append(last)
price_list.pop(10)
if verbose > 0 and v == 1:
print "last price checked was", average_last
if verbose > 1 and v == 1:
print "price list is", price_list
return average_last
earliest = average_price()
early = earliest
nonce = time.time()
#get balance information, assign balance of first pair to
def get_balance():
account_info = vars(api.getInfo())
bal1 = account_info[curr1]
bal2 = account_info[curr2]
get_balance()
def make_trade(trade):
if trade == "buy":
print "buying 1",
def check_if_changed(threshold, early, late = average_price()):
print early
print late
print late + threshold
print late - threshold
late = average_price()
if early >= late + threshold:
early = average_price()
make_trade("buy")
if verbose > 1:
print "Price threshold updated to", early
if early <= late - threshold:
early = average_price()
make_trade("buy")
print "Price threshold updated to", early
#refreshes every <wait> seconds
def refresh_price():
average_price(1)
threading.Timer(wait, refresh_price).start()
last = get_last(pair)
time.sleep(wait)
price_list.insert(0, last)
price_list.pop()
nonce = (int(time.time()))
check_if_changed(trade_threshold, earliest)
if verbose > 1:
print "Last price retrieved was", last
refresh_price()