seanspotatobusiness
Posts: 185
Joined: Tue May 22, 2012 11:19 pm

Bittrex API and Python

Tue May 07, 2019 8:19 am

I'm trying to use the Bittrex API with the Python code available here: https://github.com/ericsomdahl/python-bittrex to get my order history so I can send myself a notification whenever an order is filled. I get the following error from the following code. Can anyone advise what's wrong? Thanks.

Python script:

Code: Select all

import requests
import time
import datetime
from bittrex.bittrex import *

def log(line):
    print str(datetime.datetime.now().strftime("%m%d %H:%M ")) + line

oldData = None

my_bittrex = Bittrex("apikey", "secretkey, api_version="API_V1_1")

while True:
            with my_bittrex.get_order_history() as response:
                newData = response.json()
            if oldData is not None and newData != oldData:
                log('New trade detected on BT/Sean')
            else: log('No new trades detected')
            oldData = newData    
            time.sleep(300)

Error:

Code: Select all

$ python /home/pi/Mining/BittrexNotificationScript_Sean.py
Traceback (most recent call last):
  File "/home/pi/Mining/BittrexNotificationScript_Sean.py", line 27, in <module>
    with my_bittrex.get_order_history() as response:
  File "/home/pi/.local/lib/python2.7/site-packages/bittrex/bittrex.py", line 536, in get_order_history
    }, protection=PROTECTION_PRV)
  File "/home/pi/.local/lib/python2.7/site-packages/bittrex/bittrex.py", line 137, in _api_query
    raise Exception('method call not available under API version {}'.format(self.api_version))
Exception: method
The same error occurs when my_bittrex = Bittrex("apikey", "secretkey, api_version="API_V2_0") or my_bittrex = Bittrex("apikey", "secretkey, api_version="API_V1_1")
Raspberry Pi 3 B and B+
PRETTY_NAME="Raspbian GNU/Linux 9 (stretch)"
NAME="Raspbian GNU/Linux"
VERSION_ID="9"
VERSION="9 (stretch)"

User avatar
MrYsLab
Posts: 314
Joined: Mon Dec 15, 2014 7:14 pm
Location: Noo Joysey, USA

Re: Bittrex API and Python

Wed May 08, 2019 6:02 pm

You have a typo in your code :

Code: Select all

my_bittrex = Bittrex("apikey", "secretkey, api_version="API_V1_1")
should be:

Code: Select all

my_bittrex = Bittrex("apikey", "secretkey", api_version="API_V1_1")
Not a big deal, but just thought I would point that out.

Now on to your question. When your code executes:

Code: Select all

with my_bittrex.get_order_history() as response:
debugging get_order_history:

Code: Select all

    def get_order_history(self, market=None):
        """
        Used to retrieve order trade history of account

        Endpoint:
        1.1 /account/getorderhistory
        2.0 /key/orders/getorderhistory or /key/market/GetOrderHistory

        :param market: optional a string literal for the market (ie. BTC-LTC).
            If omitted, will return for all markets
        :type market: str
        :return: order history in JSON
        :rtype : dict
        """
        if market:
            return self._api_query(path_dict={
                API_V1_1: '/account/getorderhistory',
                API_V2_0: '/key/market/GetOrderHistory'
            }, options={'market': market, 'marketname': market}, protection=PROTECTION_PRV)
        else:
            return self._api_query(path_dict={
                API_V1_1: '/account/getorderhistory',
                API_V2_0: '/key/orders/getorderhistory'
            }, protection=PROTECTION_PRV)
The method is calling _api_query, and looking at _api_query:

Code: Select all

    def _api_query(self, protection=None, path_dict=None, options=None):
        """
        Queries Bittrex

        :param request_url: fully-formed URL to request
        :type options: dict
        :return: JSON response from Bittrex
        :rtype : dict
        """

        if not options:
            options = {}

        if self.api_version not in path_dict:
            raise Exception('method call not available under API version {}'.format(self.api_version))
Here are the variables coming in:

path_dict:
<type 'dict'>: {'v1.1': '/account/getorderhistory', 'v2.0': '/key/orders/getorderhistory'}

and self.api_version:

'API_V1_1'

Clearly self.api_version is not in the returned dictionary.

I think you need to contact the author of python-bittrex for help.

seanspotatobusiness
Posts: 185
Joined: Tue May 22, 2012 11:19 pm

Re: Bittrex API and Python

Thu May 09, 2019 1:57 am

Thanks very much. I used "API_V2_0" because that was the example given but if I change it to "v1.1" then I get a different error, suggesting that this was the problem. What does it think I'm trying to give an attribute to? I'm trying to store the response of an API request (so I can compare it with the response five minutes later).

Code: Select all

$ python /home/pi/Mining/BittrexNotificationScript_Sean.py
Traceback (most recent call last):
  File "/home/pi/Mining/BittrexNotificationScript_Sean.py", line 27, in <module>
    with my_bittrex.get_order_history() as response:
AttributeError: __exit__
Raspberry Pi 3 B and B+
PRETTY_NAME="Raspbian GNU/Linux 9 (stretch)"
NAME="Raspbian GNU/Linux"
VERSION_ID="9"
VERSION="9 (stretch)"

Return to “Python”