-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbtce.lua
179 lines (153 loc) · 5.02 KB
/
btce.lua
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
local crypto = require 'crypto'
local tablex = require 'pl.tablex'
local apiquery = require 'tools.apiquery'
local urlencode_parm = require 'tools.util'.urlencode_parm
local nonce = require 'tools.util'.nonce
local apiv = "/api/3"
local url = "https://btc-e.com"
local function btce_authquery (self, cmd, parm)
parm = parm or {}
parm.method = cmd
parm.nonce = nonce ()
local post_data = urlencode_parm (parm)
self.headers.sign = crypto.hmac.digest ("sha512", post_data, self.secret)
local res = apiquery.postrequest (url, "/tapi", self.headers, post_data)
if res.error then
-- if it's just a bad nonce, update nonce and retry
local new_nonce = res.error:match "invalid nonce parameter; .+ should send:(%d+)"
if new_nonce then
nonce (new_nonce + 0)
return btce_authquery (self, cmd, parm)
end
end
return res.error and res or res['return']
end
local btce_publicquery = function (self, cmd, parm)
parm = parm or {}
parm = urlencode_parm (parm)
if #parm > 0 then
parm = "?" .. parm
end
local r, c = apiquery.getrequest (url .. apiv, cmd .. parm)
assert (c == 200 and type(r) == 'table')
if r.error then return nil, r.error end
return r
end
local get_marketsymbol
do
local symbols
local initsymbols, getsymbols
initsymbols = function (market1, market2)
local res = btce_publicquery (nil, "/info")
symbols = tablex.pairmap (function (_, v) return v,v end,
tablex.keys (res.pairs))
get_marketsymbol = getsymbols
return get_marketsymbol (market1, market2)
end
getsymbols = function (market1, market2)
market1, market2 = market1:lower (), market2:lower ()
local marketpair = symbols[market2 .. "_" .. market1] or
(market1 .. "_" .. market2)
return marketpair
end
get_marketsymbol = initsymbols
end
local btce_publicapi = {}
function btce_publicapi:markethistory (market1, market2)
local currencypair = get_marketsymbol(market1, market2)
local r, errmsg = btce_publicquery (self,
"/trades/" .. currencypair)
if not r then return r, errmsg end
return r[currencypair]
end
function btce_publicapi:orderbook (market1, market2)
local currencypair = get_marketsymbol(market1, market2)
local r, errmsg = btce_publicquery (self,
"/depth/".. currencypair)
if not r then return r, errmsg end
local unpack = unpack or table.unpack
r = r[currencypair]
r.bids.price = tablex.imap (function (v) return v[1] end, r.bids)
r.asks.price = tablex.imap (function (v) return v[1] end, r.asks)
r.bids.amount = tablex.imap (function (v) return v[2] end, r.bids)
r.asks.amount = tablex.imap (function (v) return v[2] end, r.asks)
tablex.clear (r.bids)
tablex.clear (r.asks)
return r
end
local btce_tradingapi = {}
function btce_tradingapi:balance ()
local r = self.authquery ("getInfo")
if r.error then return nil, r.error end
return r.funds
end
function btce_tradingapi:tradehistory (market1, market2, start_period, stop_period)
local parm =
{
pair = get_marketsymbol(market1, market2),
since = start_period,
['end'] = stop_period
}
local r = self.authquery ("TradeHistory", parm)
if r.error then return nil, r.error end
return r
end
function btce_tradingapi:buy (market1, market2, rate, quantity)
local parm =
{
pair = get_marketsymbol(market1, market2),
type = "buy",
rate = rate,
amount = quantity,
}
local r = self.authquery ("Trade", parm)
if r.error then return nil, r.error end
return r
end
function btce_tradingapi:sell (market1, market2, rate, quantity)
local parm =
{
pair = get_marketsymbol(market1, market2),
type = "sell",
rate = rate,
amount = quantity,
}
local r = self.authquery ("Trade", parm)
if r.error then return nil, r.error end
return r
end
function btce_tradingapi:cancelorder (ordernumber)
local r = self.authquery ("CancelOrder", {order_id = ordernumber})
if r.error then return nil, r.error end
return r
end
function btce_tradingapi:openorders (market1, market2)
local r = self.authquery ("ActiveOrders",
{pair = get_marketsymbol(market1, market2)})
if r.error then return nil, r.error end
return r
end
local make_authself = function (apikey, apisecret)
return
{
headers =
{
key = apikey, ["content-type"] = "application/x-www-form-urlencoded",
},
secret = apisecret,
}
end
local make_apifactory = function (apimethods)
return function (apikey, apisecret)
assert (type(apikey) == 'string' and type(apisecret) == 'string',
"Bad or missing api secret key pair.")
local self = make_authself (apikey, apisecret)
local new_api = tablex.update ({}, apimethods)
new_api.authquery = function (...)
return btce_authquery (self, ...)
end
return new_api
end
end
btce_publicapi.tradingapi = make_apifactory (btce_tradingapi)
return btce_publicapi