create account

Altcoin Portfolio The Pythonic Way - Have FUN & LEARN a New Skill! by bitinformant

View this thread on: hive.blogpeakd.comecency.com
· @bitinformant · (edited)
Altcoin Portfolio The Pythonic Way - Have FUN & LEARN a New Skill!
I thought I would share a simple method of querying CoinMarketCap for altcoin prices via their simple API using Python.

<center>![](https://steemitimages.com/DQmZjctvwgA5gvdyXiuqLqsP8d7okc84kxkkMWgp7nprJDN/image.png)</center>

If you have never used Python before then this is for you! Python is a powerful programming language which you all should take the time to learn the basics. Once you understand the basics, you can do so many things!

Learning Python is a great way to advance your knowledge using computers and to become more  sophisticated. This simple altcoin portfolio introduction will help you start your own projects.

<h1> Lets get everything setup</h1>
Firstly, lets get the environment all setup on your computer. These steps should be about the same if you are using Windows or Mac. 
1) Head over to Anaconda Python and install it. <a href="https://www.continuum.io/downloads#windows">Windows</a> or <a href="https://www.continuum.io/downloads#macos">Mac</a>. Make sure to choose Python 3.x and complete the install.
2) Anaconda has the Jupyter Notebook built in. The Jupyter notebook provides a simple and clean interface for writing and testing out Python code. The notebook makes it easy to share with others as well.
3) If you are using Windows then click on "start" and type "cmd". Click the cmd application. If you are using Mac then search for "terminal" and click the application. When either the cmd or terminal windows is open, type "jupyter notebook" and press enter to to launch it.

<center>![](https://steemitimages.com/DQmRrMebZR1tebx8yq9VZ74NidekUEvSS941pt8XT5k5F2B/image.png)</center>

<center>![](https://steemitimages.com/DQmW4Kx83dV8HPWKZBBHyWpRvZmUUXhApzpaipLn7BbSUBf/image.png)</center>

The notebook should automatically launch itself in your default browser (chrome for me). If it doesn't launch, then you will have to manually copy the link provided and paste into your browser. The link is shown after you press enter, see above picture for highlighted link.

You should now have the Jupyter notebook up and running! You should be looking at a browser window like below.

<center>![](https://steemitimages.com/DQmYLhcAiRCiuoE5Npd5p6h8Dqczyzweg3tPtwd3KZoYLGZ/image.png)</center>

<h1> Start a new project and lets go!</h1>
To start a new project, simply click "new" in the upper right hand corner and make sure to click "python 3".

Now we have a new Jupyter project, lets start coding Python!

We will be referencing <a href="https://coinmarketcap.com/api/">Coinmarketcaps API</a> for altcoin ticker prices. 
Specifically we will be calling <a href="https://api.coinmarketcap.com/v1/ticker/">https://api.coinmarketcap.com/v1/ticker/</a> for our script to parse.

In Python we need to import some basic libraries and call the API and get the response.

>import requests
import json
url = 'https://api.coinmarketcap.com/v1/ticker/'
r = requests.get(url)
data = json.loads(r.text)

It should look like this. Press shift + enter to execute the current cell.

![](https://steemitimages.com/DQmYmKypeZrAurxpYminm1hDQypkqcYLygyhNxzBhKsPaDc/image.png)

 In the next cell we will let Python know what tickers we are interested in looking at as well as how many coins owned. I will be using made up numbers for this and is not my portfolio :)

>ids = ['ethereum', 'gambit', 'decred', 'digibyte', 'nimiq', 'iota', 'mobilego', 'neo', 'monero', 'edgeless']
quantities = [20, 400, 101, 100000, 300, 766, 300, 1000, 44, 50]

![](https://steemitimages.com/DQmR1GPAyipLXH5eGHHMpSTY3wcLDfnZNvsjLiZtSEPvepo/image.png)

Finally in the third cell we will parse the data and calculate prices then print them to the screen.

>i = 0
balance = 0
print("SYM \t Price \t\t Amount \t Total BTC")
for d in data:
    sym = d['symbol']
    name = d['id']
    if name in ids:
        price = float(d['price_btc'])
        amount = quantities[ids.index(d['id'])]
        total = float(d['price_btc'])*amount
        print("%s \t %0.8f \t %i \t\t %0.2f" % (sym, price, amount, total))
        i += 1
        balance += total
print("\nBTC Total: %0.3f" % balance)

![](https://steemitimages.com/DQmXs5yZ77SbaDqsF5m6TgApxHAZMKzRo821DQgzG43NFJP/image.png)

<h1> The final result! </h1>

The final project is shown below. You can see the Jupyter notebook printed out the prices and totals for each coins. It also printed the total balance in BTC!

![](https://steemitimages.com/DQmPboKcu7CHRthZeqVgxYTXRYfkK437pGSQPcNGbUdtkzv/image.png)Please take your time to look over each line of this code and familiarize yourself on what each one does. Learning this simple script will make your Python skill powerful!

<h1> Personal challenge </h1>
Can you add a fancy pie chart to this script to add a graphical view? Hint, look at matplotlib.

<hr>
<center>Have thoughts on the matter? Please say something below!</center>
<center>If you like the content you see, please follow for more like it.</center>
<hr>
<center><h1>&#128640;&#128640;&#128640; Thanks for Reading &#128640;&#128640;&#128640;</h1></center>
<table style="width:100%">
  <tr>
    <td><center><h1>
            <p>Follow, Resteem and VOTE UP</p>
            <p>@BitInformant</p>
           <p><a href="https://twitter.com/Bit_Informant">Twitter</a></p>
           <p><a href="https://www.youtube.com/channel/UCPFZTPGXT2Zlz-EIVHOaIqw">YouTube</a></p>
            <p>Crypto & Tech Lovers</p></h1>
            </center>
    </td>
    <td><img src="https://steemitimages.com/DQmQ4TyqUeVLvbjCYpcdW8RVf5y2jkaMCTJRh8EpzoDnaCC/image.png" width="30px"></td> 
  </tr>
</table>
👍  , ,
properties (23)
authorbitinformant
permlinkaltcoin-portfolio-the-pythonic-way-have-fun-and-learn-a-new-skill
categorybitcoin
json_metadata{"tags":["bitcoin","crypto","python"],"users":["bitinformant"],"image":["https://steemitimages.com/DQmZjctvwgA5gvdyXiuqLqsP8d7okc84kxkkMWgp7nprJDN/image.png","https://steemitimages.com/DQmRrMebZR1tebx8yq9VZ74NidekUEvSS941pt8XT5k5F2B/image.png","https://steemitimages.com/DQmW4Kx83dV8HPWKZBBHyWpRvZmUUXhApzpaipLn7BbSUBf/image.png","https://steemitimages.com/DQmYLhcAiRCiuoE5Npd5p6h8Dqczyzweg3tPtwd3KZoYLGZ/image.png","https://steemitimages.com/DQmYmKypeZrAurxpYminm1hDQypkqcYLygyhNxzBhKsPaDc/image.png","https://steemitimages.com/DQmR1GPAyipLXH5eGHHMpSTY3wcLDfnZNvsjLiZtSEPvepo/image.png","https://steemitimages.com/DQmXs5yZ77SbaDqsF5m6TgApxHAZMKzRo821DQgzG43NFJP/image.png","https://steemitimages.com/DQmPboKcu7CHRthZeqVgxYTXRYfkK437pGSQPcNGbUdtkzv/image.png","https://steemitimages.com/DQmQ4TyqUeVLvbjCYpcdW8RVf5y2jkaMCTJRh8EpzoDnaCC/image.png"],"links":["https://www.continuum.io/downloads#windows","https://www.continuum.io/downloads#macos","https://coinmarketcap.com/api/","https://api.coinmarketcap.com/v1/ticker/","https://twitter.com/Bit_Informant","https://www.youtube.com/channel/UCPFZTPGXT2Zlz-EIVHOaIqw"],"app":"steemit/0.1","format":"markdown"}
created2017-07-29 15:28:36
last_update2017-07-29 16:05:27
depth0
children6
last_payout2017-08-05 15:28:36
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length5,534
author_reputation46,840,471,722
root_title"Altcoin Portfolio The Pythonic Way - Have FUN & LEARN a New Skill!"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id10,127,904
net_rshares1,746,354,018
author_curate_reward""
vote details (3)
@bitinformant ·
$0.81
Its been recommended by a friend the Pandas might work well for this project.
https://pandas.pydata.org/pandas-docs/stable/index.html
👍  
properties (23)
authorbitinformant
permlinkre-bitinformant-altcoin-portfolio-the-pythonic-way-have-fun-and-learn-a-new-skill-20170801t140546899z
categorybitcoin
json_metadata{"tags":["bitcoin"],"links":["https://pandas.pydata.org/pandas-docs/stable/index.html"],"app":"steemit/0.1"}
created2017-08-01 14:05:45
last_update2017-08-01 14:05:45
depth1
children0
last_payout2017-08-08 14:05:45
cashout_time1969-12-31 23:59:59
total_payout_value0.609 HBD
curator_payout_value0.203 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length133
author_reputation46,840,471,722
root_title"Altcoin Portfolio The Pythonic Way - Have FUN & LEARN a New Skill!"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id10,427,443
net_rshares198,493,187,760
author_curate_reward""
vote details (1)
@veleje ·
I suggest to also add python tag
👍  
properties (23)
authorveleje
permlinkre-bitinformant-altcoin-portfolio-the-pythonic-way-have-fun-and-learn-a-new-skill-20170729t154808015z
categorybitcoin
json_metadata{"tags":["bitcoin"],"app":"steemit/0.1"}
created2017-07-29 15:48:09
last_update2017-07-29 15:48:09
depth1
children1
last_payout2017-08-05 15:48:09
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length32
author_reputation490,220,361,761
root_title"Altcoin Portfolio The Pythonic Way - Have FUN & LEARN a New Skill!"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id10,129,581
net_rshares2,950,687,641
author_curate_reward""
vote details (1)
@bitinformant ·
Just did! Thank you sir.
properties (22)
authorbitinformant
permlinkre-veleje-re-bitinformant-altcoin-portfolio-the-pythonic-way-have-fun-and-learn-a-new-skill-20170729t160536214z
categorybitcoin
json_metadata{"tags":["bitcoin"],"app":"steemit/0.1"}
created2017-07-29 16:05:42
last_update2017-07-29 16:05:42
depth2
children0
last_payout2017-08-05 16:05:42
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length24
author_reputation46,840,471,722
root_title"Altcoin Portfolio The Pythonic Way - Have FUN & LEARN a New Skill!"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id10,130,949
net_rshares0
@veleje · (edited)
Thank you (from Python beginner) for your example and I hope more will be coming :-) This one was very useful for me. 

I was just wondering how to get certain data from coinmarketcap. I knew I can loop through the dictionaries inside that json and print out data like this: (example is for STEEM)

print(data[19]['symbol'], data[19]['price_usd'])

But I was wondering, how to make sure to always get data for say STEEM, because when STEEM  changes rank and ends on say 18th place, upper line of code will return data for different crypto currency. 

Well, you just showed me, how to elegantly solve this by creating list of id's I want data for and then get the info with:

if name in ids:

Cool, solves the problem currencies changing their 'rank', their position. 

Thanks, very appreciated!
properties (22)
authorveleje
permlinkre-bitinformant-altcoin-portfolio-the-pythonic-way-have-fun-and-learn-a-new-skill-20170729t191751641z
categorybitcoin
json_metadata{"tags":["bitcoin"],"app":"steemit/0.1"}
created2017-07-29 19:17:51
last_update2017-07-29 20:31:03
depth1
children2
last_payout2017-08-05 19:17:51
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length794
author_reputation490,220,361,761
root_title"Altcoin Portfolio The Pythonic Way - Have FUN & LEARN a New Skill!"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id10,146,004
net_rshares0
@bitinformant ·
Glad the example was helpful to you. Full Steem ahead :)

Did you figure out how to add the pie chart?
properties (22)
authorbitinformant
permlinkre-veleje-re-bitinformant-altcoin-portfolio-the-pythonic-way-have-fun-and-learn-a-new-skill-20170730t133818050z
categorybitcoin
json_metadata{"tags":["bitcoin"],"app":"steemit/0.1"}
created2017-07-30 13:38:18
last_update2017-07-30 13:38:18
depth2
children1
last_payout2017-08-06 13:38:18
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length102
author_reputation46,840,471,722
root_title"Altcoin Portfolio The Pythonic Way - Have FUN & LEARN a New Skill!"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id10,219,886
net_rshares0
@veleje ·
Sorry, haven't touched computer since. I'm not ready for matplotlib yet anyway, maybe in a year, :-) 
I'm old & slow learner with not enough free time...
properties (22)
authorveleje
permlinkre-bitinformant-re-veleje-re-bitinformant-altcoin-portfolio-the-pythonic-way-have-fun-and-learn-a-new-skill-20170731t021637969z
categorybitcoin
json_metadata{"tags":["bitcoin"],"app":"steemit/0.1"}
created2017-07-31 02:16:39
last_update2017-07-31 02:16:39
depth3
children0
last_payout2017-08-07 02:16:39
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length153
author_reputation490,220,361,761
root_title"Altcoin Portfolio The Pythonic Way - Have FUN & LEARN a New Skill!"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id10,274,897
net_rshares0