-1

I'm trying to access the Poloniex API using requests.

The returnBalances code works, but the returnTradeHistory code does not.

The returnTradeHistory is commented out in the example.

Data is returned for returnBalances but not for returnTradeHistory.

I know the whole APIKey and secret code is working because I am getting accurate returnBalances data.

So why is returnTradeHistory not working?

from time import time
import urllib.parse
import hashlib
import hmac
import requests
import json

APIKey=b"stuff goes in here"
secret=b"stuff goes in here"

url = "https://poloniex.com/tradingApi"

# this works and returns data
payload = {
    'command': 'returnBalances',
    'nonce': int(time() * 1000),
}

# this does not work and does not return data
#payload = {
#    'command': 'returnTradeHistory',
#    'currencyPair': 'BTC_MAID',
#    'nonce': int(time() * 1000),
#}

paybytes = urllib.parse.urlencode(payload).encode('utf8')
sign = hmac.new(secret, paybytes, hashlib.sha512).hexdigest()
headers = {
    'Content-Type': 'application/x-www-form-urlencoded',
    'Key': APIKey,
    'Sign': sign,
}
r = requests.post(url, data=paybytes, headers=headers)
fulldata=r.content
data = json.loads(fulldata)
print(data)
Makoto
  • 104,088
  • 27
  • 192
  • 230
  • That might have more to do with the API you're invoking than your code. Do you get back any specific response code that isn't a 2xx? – Makoto Jun 03 '18 at 14:40
  • For both I get . The "returnBalances" has content in "r" but "returnTradeHistory" has no content in "r". I strong suspect it's how I am passing the 'currencyPair' : 'BTC_MAIN' to the api. It is a bit frustrating all the examples I find on the poloniex api do not address passing params to the api. – Bob Clarke Jun 03 '18 at 16:16
  • I got a buy to work. Now the proud owner of 40 MAIDS. Certain commands like returnTradeHistory, return24Volume and returnTicket need to be treated differently. Don't know why. – Bob Clarke Jun 03 '18 at 17:34

1 Answers1

0

According to the official poloniex API documentation:

returnTradeHistory

Returns the past 200 trades for a given market, or up to 50,000 trades between a range specified in UNIX timestamps by the "start" and "end" GET parameters [...]

so it is required to specify the start and end parameter

e.g: https://poloniex.com/public?command=returnTradeHistory&currencyPair=BTC_NXT&start=1410158341&end=1410499372

A. STEFANI
  • 6,707
  • 1
  • 23
  • 48