Why do I get HTTP 403 errors for many commands?
Using pry I find
[10] pry(#<RAPFLAG::Poloniex>)> currencies = JSON.parse(::Poloniex.currencies.body) => {"1CR"=>{"id"=>1, "name"=>"1CRedit", "txFee"=>"0.01000000", "minConf"=>3, "depositAddress"=>nil, "disabled"=>0, "delisted"=>1, "frozen"=>0}, "ABY"=>{"id"=>2, "name"=>"ArtByte", "txFee"=>"0.01000000", "minConf"=>8, "depositAddress"=>nil, "disabled"=>0, "delisted"=>1, "frozen"=>0}, <...> } [11] pry(#<RAPFLAG::Poloniex>)> currencies['BITUSD'] => {"id"=>272, "name"=>"BitUSD", "txFee"=>"0.15000000", "minConf"=>50, "depositAddress"=>"poloniexwallet", "disabled"=>0, "delisted"=>1, "frozen"=>0}
Currencies is part of the public API which uses a GET. This works. But using a Trading API call which uses POST, results in an error, even when the configuration looks okay. Eg.
[16] ::Poloniex.configuration => #<Poloniex::Configuration:0x0055a22cad9d18 @key="our api key", @secret="our api secrect"> [17] pry(#<RAPFLAG::Poloniex>)> ::Poloniex.complete_balances RestClient::Forbidden: 403 Forbidden from /home/niklaus/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rest-client-2.0.1/lib/restclient/abstract_response.rb:223:in `exception_with_response'
Setting a pry break statement at line 130 of poloniex/lib/poloniex.rb I can see
[1] pry(Poloniex)> params => {:command=>"returnBalances", :nonce=>14912882925726680} [2] pry(Poloniex)> resource[ 'tradingApi' ] => #<RestClient::Resource:0x005618a01da808 @block=nil, @options={}, @url="https://www.poloniex.com/tradingApi"> [3] pry(Poloniex)> configuration.key => "our api key" [4] pry(Poloniex)> create_sign( params ) => "8dc9f05c806b6959cd5b35a81f72a2b02de5d24876a222319930e296d493e79340c528b6578aca62452cade02386ea7550d6ee0e5453a2390600d0ec5b4a9c0f" [6] pry(Poloniex)> res = resource[ 'tradingApi' ].post params, { Key: configuration.key , Sign: create_sign( params ) } RestClient::Forbidden: 403 Forbidden
Working on the issue, that we should create in the summary an entry for each day. Create the spec test for it. The balance in the crypto currency will be the same for each intermediate day, but the balance in USD is update for each day, as the exchange rate is changing constantly. Pushed commit Create balance for each day in summary
The https://github.com/psyipm/poloniex-websockets has examples, but it is not correct, e.g. gemspec name and directory/gem names do not match.
After looking at the gem implementation I see that it has nice interface to watch in real time the tickets and order_books and trollbox (the public API), but nothing for the POST API.
Pushed commit Poloniex only supports functions available via public API as we have already some (skipped) spec tests for poloniex and one working example.
After that Zeno removed the IP limit for the API calls, things start to work. Here some pry examples
# to fetch all withdrawls [10] pry(#<RAPFLAG::Poloniex>)> withdrawls = JSON.parse(::Poloniex.deposits_withdrawls(0, Time.now.to_i).body); [11] pry(#<RAPFLAG::Poloniex>)> withdrawls.values.last.first => {"withdrawalNumber"=>1166558, "currency"=>"BTC", "address"=>"xxx", "amount"=>"0.19392645", "timestamp"=>1460985451, "status"=>"COMPLETE: 65f37739d71efa52e8c34da049830a7f1f78165cb3a29dc04972c1460f464699", "ipAddress"=>"212.35.14.109"} [21] pry(#<RAPFLAG::Poloniex>)> currency_pair = "USDT_#{@currency}" => "USDT_BTC" [22] pry(#<RAPFLAG::Poloniex>)> trade_history = JSON.parse(::Poloniex.trade_history(currency_pair).body) => [] [27] pry(#<RAPFLAG::Poloniex>)> open_orders = JSON.parse(::Poloniex.open_orders('all').body); [28] pry(#<RAPFLAG::Poloniex>)> open_orders.size => 107 [29] pry(#<RAPFLAG::Poloniex>)> open_orders.first => ["BTC_AMP", []] [30] pry(#<RAPFLAG::Poloniex>)> trade_history = JSON.parse(::Poloniex.open_orders('all').body); [31] pry(#<RAPFLAG::Poloniex>)> trade_history.first => ["BTC_AMP", []] [32] pry(#<RAPFLAG::Poloniex>)> trade_history.size => 107 [33] pry(#<RAPFLAG::Poloniex>)> trade_history.values.first => [] [34] pry(#<RAPFLAG::Poloniex>)> trade_history.values.first.find_all{|x| x.size > 0} => [] [35] pry(#<RAPFLAG::Poloniex>)> available_account_balances = JSON.parse(::Poloniex.available_account_balances.body); [36] pry(#<RAPFLAG::Poloniex>)> available_account_balances.size => 2 [37] pry(#<RAPFLAG::Poloniex>)> available_account_balances.first => ["exchange", {"GNT"=>"0.80626065"}] [39] pry(#<RAPFLAG::Poloniex>)> available_account_balances => {"exchange"=>{"GNT"=>"0.80626065"}, "lending"=>{"BTC"=>"0.22034755"}}
Looks like Poloniex is not always. Got this time 422 errors #<RestClient::UnprocessableEntity: 422 Unprocessable Entity>
[24] pry(#<RAPFLAG::Poloniex>)> t2 = JSON.parse(::Poloniex.trade_history('BTC_GNT').body) {:currencyPair=>"BTC_GNT", :command=>"returnTradeHistory", :nonce=>14913098207949966} => [] 36] pry(#<RAPFLAG::Poloniex>)> complete_balances['GNT'] => {"available"=>"22493.80626065", "onOrders"=>"0.00000000", "btcValue"=>"1.48931491"} [37] pry(#<RAPFLAG::Poloniex>)> complete_balances = JSON.parse(::Poloniex.complete_balances.body); {:command=>"returnCompleteBalances", :nonce=>14913103603719396} [38] pry(#<RAPFLAG::Poloniex>)> complete_balances.size => 284 [16] pry(#<RAPFLAG::Poloniex>)> addrs = JSON.parse(::Poloniex.deposit_addresses.body) {:command=>"returnDepositAddresses", :nonce=>14913120866558044} => {"BTC"=>"12jQat5WU97uPb8Ky5nrKAeJJAJPzN3e7g", "ETH"=>"0xf05d0aea22ff3267775a7279e0126d051f71e56c"}
Trying to get exchange rates via a new function in the poloniex gem
def self.get_daily_exchange_rate( currency_pair, date_in_unix_seconds ) res = get 'returnChartData', currencyPair: currency_pair, period: 86400, start: (date_in_unix_seconds.to_i - 86400), :end => date_in_unix_seconds.to_i info = JSON.parse(res.body) info ? info.first['close'] : 'invalid' end
Here I have the problem, that requesting a quote from 45 days or older returns 0. Verified it, as JSON.parse(get 'returnChartData', currencyPair: currency_pair, period: 86400, start: 0, :end => Time.now.to_i).size returns 46. Okay. Looks as if the GTN is not older, eg.
[19] pry(Poloniex)> JSON.parse(get 'returnChartData', currencyPair: 'BTC_XMR', period: 86400, start: 0, :end => Time.now.to_i).size => 1052 [20] pry(Poloniex)> JSON.parse(get 'returnChartData', currencyPair: 'BTC_GNT', period: 86400, start: 0, :end => Time.now.to_i).size => 46
After adding start/end time parameter in the poloniex gem, I was able to return the trade_history, as seen here
1] pry(#<RAPFLAG::Poloniex>)> trade_history.size => 5 [2] pry(#<RAPFLAG::Poloniex>)> trade_history.first => ["BTC_GNT", [{"globalTradeID"=>93016359, "tradeID"=>"310895", "date"=>"2017-03-25 09:24:34", "rate"=>"0.00004439", "amount"=>"20527.59765713", "total"=>"0.91122006", "fee"=>"0.00150000", "orderNumber"=>"2424561197", "type"=>"buy", "category"=>"exchange"}, {"globalTradeID"=>93015571, "tradeID"=>"310884", "date"=>"2017-03-25 09:22:21", "rate"=>"0.00004439", "amount"=>"2000.00000000", "total"=>"0.08878000", "fee"=>"0.00150000", "orderNumber"=>"2424561197", "type"=>"buy", "category"=>"exchange"}]]
Tradable_balances look interesting, too
[9] pry(#<RAPFLAG::Poloniex>)> tradable_balances = JSON.parse(::Poloniex.tradable_balances.body) {:command=>"returnTradableBalances", :nonce=>14913156112451182} => {"BTC_BTS"=>{"BTC"=>"0.00000000", "BTS"=>"0.00000000"}, "BTC_CLAM"=>{"BTC"=>"0.00000000", "CLAM"=>"0.00000000"}, <..> [10] pry(#<RAPFLAG::Poloniex>)> tradable_balances.size => 11
More info is given via active_loans (introduce in poloniex gem)
[2] pry(#<RAPFLAG::Poloniex>)> active_loans.size => 2 [3] pry(#<RAPFLAG::Poloniex>)> active_loans.keys => ["provided", "used"] [8] pry(#<RAPFLAG::Poloniex>)> active_loans.keys => ["provided", "used"] [9] pry(#<RAPFLAG::Poloniex>)> active_loans['provided'].first => {"id"=>161111543, "currency"=>"BTC", "rate"=>"0.00148500", "amount"=>"0.10076213", "duration"=>60, "autoRenew"=>0, "date"=>"2017-03-23 09:21:02", "fees"=>"0.00182698"} [11] pry(#<RAPFLAG::Poloniex>)> active_loans['provided'].size => 23 [12] pry(#<RAPFLAG::Poloniex>)> active_loans['used'].size => 0
Committed my changes to poloniex, pushed the commit Added some missing methods. Default value for start/end to fetch all values and created a pull request https://github.com/Lowest0ne/poloniex/pull/6
Pushed my commit rapflag -p produces some csv files