In this article, I’d like to describe two BitBay in Python scripts that may help Crypto trading on BitBay. Bitbay seems to be one of the largest cryptocurrency exchanges in Europe. Also, they provide easy access to exchange public data through REST API. Let’s check the highest, lowest, and most recent transactions on BitBay!
Intro
For a couple of last days, the web is talking about the rise of Bitcoin value. It may be worth to check whether investing in cryptocurrencies is deserving consideration again. As many of you may notice, the most recognizable cryptocurrency is Bitcoin. Because of that, it seems to set trends for other altcoins. Before placing cash into any investment, every investor should be 100% sure if the risk should be taken. To not waste time for data gathering, I’ve created two scrips to get and store data regarding the most recent transactions and the highest and lowest of them. Base versions are considering only one pair of crypto-fiat “BTC-PLN”, but you can check on your own any of the available pairs as well.
Please remember, below solutions are only informative. You are playing at your own risk!
Data are gatherd with use of BitBay REST API. Original documentation may be found here.
Approach description
I’m not an expert in any trading. The first thing which comes to my mind when checking what is going on in the market is to check how people are reacting. Another thing is to check if the current moments are similar or far from the highest and lowest. To do that, it would be good to get a list of the latest, the highest, and the lowest transactions. Voila! BitBay gives you the possibility to do so by placing API calls. As a response, REST API is replying with output data in the form of JSON messages.
In simple words, both BitBay in Python codes follow 3 steps structure using 3 helper libraries: requests, json, csv. Each of them is simply describing their usage, but let me elaborate a bit more when exactly they are used. 2 variables are set before anything is done: market_code and limit. The user can modify them on his own, but modification should follow some strict rules. market_code should represent available crypto-fiat pair, and the limit should be between 1 and 300 maximum.
Library “requests” is used during REST API calls. As you see in the attached below code, the communication part is done in getTransactions function. This is quite a common method of reaching REST API in Python, and it’s also fairly simple. When it comes to the “json” library – we use it once the response is retrieved. Respond is a string containing a JSON message. In order to look for values we’re interested in, we need to parse this message (i.e., read as a kind of dictionary “key-value” pairs). The parsed message is stored in the “parsed_response” variable. In the end, for further analysis, we want to save results. The best approach is to save outputs in a CSV file, in which each row will represent one transaction. That’s why the script is opening the CSV file and writes matched key-value pairs for each line to it.
The most recent transactions
Below you can find the full script with detailed comments to get the most current transactions for particular crypto-fiat pairs:
# Import libraries
import requests
import json
import csv
# Identifier for market code
market_code = "BTC-PLN"
# Identifier for quantity limit
limit = "100"
# Function to get data from REST API based on market code identifier
# market_code - identifies for which pair data should be retrieved
# query_params - list of parameters used for API call in concatenated string
def getTransactions(market_code, query_params):
# URL for API to get recent transactions list
url = "https://api.bitbay.net/rest/trading/transactions/"+market_code+"?"+query_params
# API headers
headers = {'content-type': 'application/json'}
# Return plain JSON message text
return requests.request("GET", url, headers=headers).text
# Execute getTransaction function (with provided parameters) and store its output in response variable
response = getTransactions(market_code, "limit="+limit)
# Parse JSON message to navigate easily
parsed_response = json.loads(response)
# Open CSV file names "last_transactions.csv"
with open(r'last_transactions.csv', 'a', newline='') as csvfile:
# Define field/column names accordingly with JSON message content
fieldnames = ['id', 'time', 'amount', 'rate', 'type']
# Create object to write CSV based on dictionary mapping
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
# For every entry in JSON message identified in "items" section
for entry in parsed_response["items"]:
# Write entry to CSV file as separate row
writer.writerow({'id': entry["id"], 'time': entry["t"], 'amount': entry["a"], 'rate': entry["r"], 'type': entry["ty"]})
# Print entry to standard output
print(entry)
The highest and lowest transactions
Below you can find the full script to get the highest and lowest transactions for particular crypto-fiat pairs:
# Import libraries
import requests
import json
import csv
# Identifier for market code
market_code = "BTC-PLN"
# Function to get data from REST API based on market code identifier
# market_code - identifies for which pair data should be retrieved
def getTransactions(market_code):
# URL for API to get orderbook lists
url = "https://api.bitbay.net/rest/trading/orderbook/"+market_code
# API headers
headers = {'content-type': 'application/json'}
# Return plain JSON message text
return requests.request("GET", url, headers=headers).text
# Execute getTransaction function and store its output in response variable
response = getTransactions(market_code)
# Parse JSON message to navigate easily
parsed_response = json.loads(response)
# Open CSV file names "orderbook.csv"
with open(r'orderbook.csv', 'a', newline='') as csvfile:
# Define field/column names accordingly with JSON message content
fieldnames = ['type', 'ratio', 'current_amount', 'start_amount', 'past_amount', 'count_orders']
# Create object to write CSV based on dictionary mapping
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
# Write header with prowided field/column names
writer.writeheader()
# For every entry in JSON message identified in "sell" section
for entry in parsed_response["sell"]:
# Write entry to CSV file as separate row (static type SELL)
writer.writerow({'type': "SELL", 'ratio': entry["ra"], 'current_amount': entry["ca"], 'start_amount': entry["sa"], 'past_amount': entry["pa"], 'count_orders': entry["co"]})
# Print entry to standard output
print(entry)
# For every entry in JSON message identified in "buy" section
for entry in parsed_response["buy"]:
# Write entry to CSV file as separate row (static type BUY)
writer.writerow({'type': "BUY", 'ratio': entry["ra"], 'current_amount': entry["ca"], 'start_amount': entry["sa"], 'past_amount': entry["pa"], 'count_orders': entry["co"]})
# Print entry to standard output
print(entry)
Please note that both mentioned BitBay in Python scripts are available through my GitHub repository.
I highly encurage you to check my other articles in Applications section.
Thanks!
No comment yet, add your voice below!