Tutorial: Python Script To Get STEEM Price from Poloniex into Excel File

in #coding9 years ago (edited)

Hello everyone! In this tutorial, I am showing you how to retrieve prices from an cryptocurrency exchange such as Poloniex and export them into an Excel file. Poloniex provides an excellent API which enables us to retrieve data from them.

First of all, we need to import all the necessary libraries. At the very beginning of your code, write

import requests
import csv
import datetime

Next is the Public API method to retrieve the prices from Poloniex. Paste the URL on your browser and see what you get!

url = "https://poloniex.com/public?command=returnChartData&currencyPair=BTC_STEEM&start=1483228800&end=1496275200&period=86400"

Try changing the parameters - currency, start, end, and period, according to your needs. In this tutorial, we'll use BTC_STEEM for the currency, a period of 86400 seconds, which is 1 day, and running from January 1st 2017 to June 1st 2017. The dates are in UNIX timestamp. Use a UNIX timestamp converter to convert one.

Next, we'll retrieve the response from the URL and parse that data into a JSON tree so the code can read it.

url_response = requests.get(url)
url_parse = url_response.json()

Afterwards, we'll open a new Excel file and prepare it for writing data.

f = open("BTC_STEEM.csv","wb")
writer = csv.writer(f)

Now that the file is ready for writing, we'll input the column titles in the first row.

titles = ["Date", "High", "Low", "Open", "Close", "Volume"]
writer.writerow(titles)

Next, we will start putting all the data from the JSON tree we parsed earlier into their respective columns, row by row.

for i in url_parse:
price_data = []
date = datetime.datetime.fromtimestamp(int(i["date"])).strftime('%Y-%m-%d %H:%M:%S')
price_data.append(date)
price_data.append(i["high"])
price_data.append(i["low"])
price_data.append(i["open"])
price_data.append(i["close"])
price_data.append(i["volume"])
print price_data
writer.writerow(price_data)

After writing all the data, we need to close the Excel file.

f.close

Congratulations! You now have a Python code that retrieves STEEM prices from a cryptocurrency exchange. Now go to your terminal and run your code. Enjoy!

Sort:  

Congratulations @hazimiasyraf! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

You published your First Post
You made your First Vote
You got a First Vote

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

By upvoting this notification, you can help all Steemit users. Learn how here!

Congratulations @hazimiasyraf! You have received a personal award!

1 Year on Steemit
Click on the badge to view your Board of Honor.

Do not miss the last post from @steemitboard:
SteemitBoard World Cup Contest - Semi Finals - Day 1


Participate in the SteemitBoard World Cup Contest!
Collect World Cup badges and win free SBD
Support the Gold Sponsors of the contest: @good-karma and @lukestokes


Do you like SteemitBoard's project? Then Vote for its witness and get one more award!

Congratulations @hazimiasyraf! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Vote for @Steemitboard as a witness to get one more award and increased upvotes!

Coin Marketplace

STEEM 0.05
TRX 0.33
JST 0.082
BTC 62815.51
ETH 1636.46
USDT 1.00
SBD 0.41