Posts

Automatically buying HBD when it is "on sale".

avatar of @themarkymark
25
@themarkymark
·
·
0 views
·
3 min read

Someone asked if there was a bot to buy HBD when it is below $1 on Bittrex.

While I don't know of any bot that is able to do this, the process it pretty simple. I am going to use Python, but this can be done just as easily in Javascript or any other language.

import requests 
 
TICKER_ENDPOINT = 'https://api.bittrex.com/v3/markets/tickers' 
tickers = requests.get(TICKER_ENDPOINT) 
 
if not tickers.ok: 
	print("Something went wrong") 
 
for ticker in tickers.json(): 
    if ticker['symbol'] == 'HBD-BTC': 
        hbd_btc = float(ticker['lastTradeRate']) 
    if ticker['symbol'] == 'BTC-USD': 
        btc_usd = float(ticker['lastTradeRate']) 
 
hbd_usd = btc_usd*hbd_btc 
if hbd_usd < 0.98: 
    print("BONANZA!") 
    # bittrex.buy() 
else: 
    print(f"HBD: {hbd_usd}") 

This code is pretty simple, and will query Bittrex API looking for the current price of BTC and HBD.

There is a BTC-USD pairing, which means we can treat the BTC price as USD. Bittrex only has a HBD-BTC pairing, meaning we need to treat HBD as a BTC price.

First we need to import requests, this is the goto library in Python to fetch data on the web and communicate with APIs.

If you do a quick Google search for "Bittrex API" you will find this page.

Some quick looking around you will find what we are looking for is a list of their tickers.

It looks like this end point has what we are looking for. 'https://api.bittrex.com/v3/markets/tickers'

As you can see it is basically a list of dictionaries containing some basic market data on each of their pairs.

So let's download this data into a variable named tickers using tickers = requests.get(endpoint).

Let's do a quick guard clause to make sure we have a valid response.

if not tickers.ok: 
	print("Something went wrong") 

Request has a nice feature where you can check the response for valid status codes. You can also query tickers.status_code to get the exact status code or use ticker.raise_for_status() which will throw an exception on a bad status code. Requests has a few ways to handle this in more detail.

Now that we have a reasonable expection of ticker data, we need to iterate the results to find the ones we are looking for. This can be done in many ways, I opt'd for a more straight forward approach that is easy to read as a laymen.

for ticker in tickers.json(): 
    if ticker['symbol'] == 'HBD-BTC': 
        hbd_btc = float(ticker['lastTradeRate']) 
    if ticker['symbol'] == 'BTC-USD': 
        btc_usd = float(ticker['lastTradeRate']) 

This iterates (goes though every one) all the dictionaries that represents each pair in the list and assigns the result we are looking for into variables.

At this point hbd_btc represents the last trade price (in BTC) of HBD. btc_usd represnts the last trade price of BTC (in USD). We are looking for a USD price of HBD, so we can multply the two to get a USD version of HBD.

hbd_usd = btc_usd*hbd_btc

Simple enough, and now we have a hbd_usd pairing price, technically this doesn't exist but it gives us an accurate price of HBD in terms of USD if we traded both on Bittrex immediately.

At this point we just need a simple if statement to check if HBD is below our buy threshold.

if hbd_usd < 0.98: 
    print("BONANZA!") 
    # bittrex.buy() 
else: 
    print(f"HBD: {hbd_usd}") 

# bittrex.buy() is just a comment and would be where we would add code to authenticate and execute a buy order for HBD on Bittrex.

Using the last traded price may not be the best option, you may want to use bid or ask prices a more accurate representation of the current price. You could experiment with different options to see how it more accurately reflects your needs.

If the ask price of HBD is below, you can comfortable knowing the price is below $1 as people are trying to sell at this price. If the bid price is below $1, this could just be people trying to undercut the price and isn't as accurate representation of the "current price" of HBD.


Securely chat with me on Keybase

Why you should vote me as witness

Posted Using LeoFinance Beta