Creating Your Own Spreadsheets and Charts Using Poloniex API – Part 4

If you have reached this page without going through the previous instructions, please Click Here to start at Part 1.

Reminder: Please remember to copy and paste your script code from the link at the bottom of this page.  If you do not you may get an ‘illegal character’ message when you try to save your script.

At this point we have received the data from Poloniex, and created a large field called parcedData.  It contains all of the data in one huge field and needs to be broken down into individual records (rows) and fields (cells) to be populated in the spreadsheet.  I am going to simply give you the command to do this, but I can’t leave you without understanding what is being done.  You will need to know in case you are planning to use the other APIs.

Here is the ‘asks’ command we will use in our script.  This will take just the two fields passed in the ‘asks’ section of the JSON file, and define the first field as ‘askRate’ and the second field as ‘askAmount.’  These headers are put in the first two fields of the ‘asks’ array with the push command.

*

// Define the ‘asks’ array, and populate it from the parced data pulled
var asks = [];
asks.push([‘askRate’, ‘askAmount’]);

for(var key in parcedData.asks)
{
asks.push(parcedData.asks[key]);
}

*

Take a close look at the code above and make sure you understand it.  The section “parcedData.asks” is actually saying “only look at the data in the asks key within the field parcedData.”  The for command says to loop, and the field key simply accumulates… 0, 1, 2, 3, etc.  So, you end up looping through each record in the asks data, pushing each out to an array we have called asks.

Next we will put the data into the spreadsheet.  In the first line we tell it where to put the columns and how many to put out there.  The portion in the next lines of code saying (1, 1, asks.length, 2) is defined as:

  1. Start Row (1)
  2. Start Column (1 – Column A)
  3. Number of Rows (asks.length – however many rows were in the ‘asks’ section)
  4. Number of Columns (2 – A & B)

You do not have to know the value of asks.length.  The script already knows how many rows there are.  The second line says Set Values, or more simply stated: ‘write it’ to the range we defined (A & B).

*

// Put the ‘ask’ data into the spreadsheet columns A & B
askRange = APIPullSheet.getRange(1, 1, asks.length, 2);
askRange.setValues(asks);

*

At this point you can finally run your script and see the ‘asks’ data populate in the spreadsheet columns A & B, just like you told it to.  To run your script, save it first, then press the ‘play’ arrow.

part4image1

 

Let’s go ahead and do the same things for the ‘bids.’  Give it a try yourself by copying the ‘asks’ code above, pasting it next in your Script, and modifying it to work for ‘bids.’  We want to write to columns C & D, so you will need to set the Start Column to 3 in your bidRange.  If you run your code and don’t get the result you are looking for, compare it to the code below and see where the difference is.

*

// Get the ‘bids’ data into the spreadsheet columns C & D
var bids = [];
bids.push([‘bidRate’, ‘bidAmount’]);

for(var key in parcedData.bids)
{
bids.push(parcedData.bids[key]);
}

bidRange = APIPullSheet.getRange(1, 3, bids.length, 2);
bidRange.setValues(bids);

*

It is probably a good idea to ‘Make a copy’ (under the File menu) here.

The reason I wanted to devote an entire lesson to this one piece is because different JSON files sent from Poloniex are formatted differently.  For example, if you ask for ‘ALL’ currency pairs, like in the command below, the returned JSON file will be formatted differently.  The first field in each section will define the currency pair (example “BTC_1CR”).  After this, all of the ‘asks’ for BTC_1CR will be listed, followed by all of the ‘bids.’  Then the JSON file will specify the next currency pair (“BTC_AMP”), and its data, etc, etc, etc.

https://poloniex.com/public?command=returnOrderBook&currencyPair=ALL

 

There are many different APIs at Poloniex, and each is different.  The code to handle pulling each one and putting it into a table is complicated and would take a thorough understanding of JavaScript.  In order for you to figure it out, you will have to dive deeper into coding than I am going to go into here.

I strongly recommend you learn the JavaScript needed yourself to pull other APIs in.  If you do not, you will not be able to pull JSON API information from other sites, or even the other available Poloniex APIs, when you need it.  There are many sites offering free online classes for JavaScript, such as https://www.codecademy.com.

If you just do not have the time or patience, I have created a Poloniex Public API Cheat Sheet and will provide it to anyone who sends a tip to the Bitcoin address below.  This cheat sheet covers all Public APIs provided by Poloniex.  Be sure to look on the Poloniex API page at the information you will be able to pull before sending any BTC.

part4image2

Send me a message using the Contact Form with your Email you want me to send the Cheat Sheet to, and the time you sent the tip.  I will send the document your way as soon as I see your request.

 

Click Here to see what my Script looks like at this point.

Click Here to continue to Part Five.

 

 

Please recognize a lot of effort went into figuring this process out and documenting it to save you time, with the goal of giving you information you can use to make more BTC in the future.  If this is useful information for you, I would appreciate any tips you are able to send to the following

BTC address : 1EPyFDbRtfxMvpAhCyXBH2xwMuJ77Wwb41

ETH Address: 0x2F4203691f48a95D96A261f3453f39fdf490c386

Advertisements