[Algorithm Trading #4] Poloniex API

in #trading7 years ago

Hi,

01_title.png

Today, I'm going to write about how to call poloniex API.

02_poloniex.jpg

Poloniex is a cryptocurrency exchange and they support API calls.
The calls include ticker, historical data, submitting order and checking balance.
You could find all api calls in here (right now at this point, the site not working)
https://www.poloniex.com/support/api/

I'm going to use MATLAB.
First we need a wrapper to call api.
It is found in bitcointalk : https://bitcointalk.org/index.php?topic=1817541.0

You need to download and add the functions to your MATLAB path.
url2.m, DataHash.m, HMAC.m
https://www.mathworks.com/matlabcentral/fileexchange/35693-urlread2
http://www.mathworks.com/matlabcentral/fileexchange/31272-datahash
http://www.mathworks.com/matlabcentral/fileexchange/46182-hmac-hash-message-authentication-code-function

Also, you need to save the functions written in the bitcointalk link such as

function response=POLO_Call(command,params)
% Donate BTC: 3NQ3RpAcXjNRgFXCAXuAf6icjuG31ZSfeT if like it
% appKey and appSecret-----------------------------
appKey='';
appSecret='';
%nonce
nonce=;%create a nonce of your style
%SHA512 encoding---------------------------------------------------------
addr='https://poloniex.com/tradingApi';
parameters=['command=',command,'&nonce=',nonce params];
requestString=HMAC(appSecret,parameters,'SHA-512');%hmac-shaxxx depends on exchange
%header=struct('name',name,'value',value);

header1=struct('name','Content-Type','value','application/x-www-form-urlencoded');
header2=struct('name','Key','value',appKey);
header3=struct('name','Sign','value',requestString);
headers=[header1, header2, header3];
response = urlread2(addr,'POST',parameters,headers);

In the variable appKey and appSecret, you should find and type your own key in the poloniex API.

In the nouce variable, I recommend you to do following

nonce = strrep(num2str(now*1000000-7.34E11), '.', '');
nonce = nonce(1:10);

You just completed the key function POLO_Call(command,params).

Let's see balance of our account using the function

function amt=POLO_Bal(currency)
%eg:POLO_Bal('BTC')
command='returnBalances';
params='';
tmp=POLO_Call(command,params);
amt=POLO_J2M(tmp,currency);

If we call POLO_Bal('BTC'), then it returns the balance of bitcoin in your account.
The subfunction, returns all the balance in your account with tmp variable

command='returnBalances';
params='';
tmp=POLO_Call(command,params);

The tmp variable is a JSON string which is not suitable in MATLAB.
03_tmp.PNG

Using JSON string parser, let's parse the string. http://www.mathworks.com/matlabcentral/fileexchange/25713

04_parsed.PNG

You could see the balance of all coin.
That's it for today, have a wonderful day.

Sort:  

Can we do this similar things in mathematica?

Can you offer any insights to the POLO_J2M function which is missing here?