HOME




Trading crypto in command line using APIs

What is an API? How does it work? How to buy and sell crypto?





We need APIs


As explained in the previous posts, an arbitrage strategy has absolutely no chances of success if not 100% automatized. Trading with your mouse through web interfaces would take way too long and arbitrage opportunities would be over even before you had the time to seize it.


Fortunately it is possible to exchange information with crypto exchanges through their API (Application Programming Interface). Basically it allows to buy and sell crypto in command line, and thus to create a bot that do it automatically when a price difference is detected.

Code resource


Let’s suppose I want to know what is my balance on my Bitstamp account. It is possible to do so using the R programming language using the code below. Note that you first need to:

# Some library are necessary to make these function work
library(dplyr)
library(digest)       # For the hmac function that allows to create the signature
library(RCurl)        # Pour récupérer les données en appelant des URLS
library(jsonlite)     # To go from JSON to data frame
  
# to access the Bitstamp API (enter your codes)
key_bitstamp="yyxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
secret_bitstamp="vfxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
customer_id_bitstamp="zxxxxxx"
  
# A function that returns my Bitstamp balance
get_my_balance_bitstamp=function(){
    nonce=as.character(as.numeric(Sys.time()) * 1000000)
    signature = toupper( hmac(key = secret_bitstamp, object = paste0( nonce, customer_id_bitstamp, key_bitstamp), algo = "sha256"))
    post_data <- paste0("key=", key_bitstamp, "&signature=", signature, "&nonce=", nonce)
    curl <- getCurlHandle()
    query_result_json <- rawToChar(getURLContent(curl = curl, url = "https://www.bitstamp.net/api/v2/balance/", binary = TRUE, postfields = post_data))
    result=fromJSON(query_result_json,  flatten=TRUE) %>% data.frame()
    result=result[ , c("bch_available", "btc_available", "eth_available", "eur_available", "ltc_available", "xrp_available")]
    return(result)
  }
  
#Use the function
get_my_balance_bitstamp()


I’ve written a set of function allowing to get balance, buy crypto and sell crypto for Bitstamp, Cex, and Kraken. You can find these function here. Here is an example of utilization:

# Enter your bitstamp codes
key_bitstamp="yyxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
secret_bitstamp="vfxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
customer_id_bitstamp="zxxxxxx"

# Source the script with bitstamp functions:
source("https://raw.githubusercontent.com/holtzy/Cryp-To/master/FUNCTIONS/Private_API_Functions_Bitstamp.R")

# Buy Etherum
from_euro_to_crypto_bitstamp(0.05, "ETHEUR" )

Next step


Now that we have an arbitrage algorithm and a set of function allowing to buy and sell, let’s try with real data!


Let’s play for real



 

A work by Yan Holtz for data-to-viz.com