Making Your Own Crypto-portfolio In Python

in #steemstem6 years ago

Yesterday, I revisited one of my old small Python projects. Some time ago I decided to build my own crypto-portfolio to be able to visualize my profits and losses. Therefore I would not need to calculate them manually or keep a record of it in some web page. Anyway, in this post, I will be sharing my code with you, and I will explain how you can modify that code in order to make it represent your own portfolio! You can find all the code in my Github repostory. First, let me show you how it looks:

cryptoPortfolio.PNG

I chose the variables symbol, name, quantity, initial price, current price, initial value, current value, % return on investment, return on investment, 24 hour change, and 7 day change because those are the ones I find most interesting. For the initial and current price, I refer to the price per unit of that currency. For initial and current value, I refer to the total amount of money that you have invested in X coin. In order to get to this point, you will have to edit some fields in the 'portfolio.py' file.

cryptoPortfolioMyPortfolio.PNG

As you can see from the image above, the variable 'myPortfolio' is a list of dictionaries with each dictionary representing a coin. The fields that dictionary holds are 'symbol', 'amount', and 'priceAtBuy'. For this portfolio to represent your own, you will have to edit these dictionaries, remove and/or add as many coins you are holding. If you bought one type of coin at different times, and therefore at different prices, you should have one dictionary per buy, so two dictionaries for that coin. As an example, see bitcoin-cash is twice in this dictionary. Once you have modified that dictionary to represent your own, you can run the program and your portfolio should be up and running!

In the portfolio, on the bottom right corner, you can see two buttons, 'Update' and 'Pie Chart'. The 'Update' button can only retrieve new data every five minutes. This is because I am retrieving the data from the coinmarketcap's API and they update it every five minutes. If you click on the 'Pie Chart' button, a pie chart will come up representing the portion of the portfolio that each coin/buy represents.

cryptoPortfolioPieChart.PNG

Now we move to a more interesting part of that little project. The purpose of holding different coins is to have a diversified portfolio. Nevertheless, if all the coins have a high correlation in their performance, the diversification is useless. Therefore, I decided to build a correlation matrix in order to see if my choice of coins for a diversified portfolio were the correct. To do this, for your own portfolio, you will have to modify some variables in two other files, 'marketLooking.py' and 'correlationMatrix.py'.

cryptoPortfolioMarketLooking.PNG

In the image above, you can see the variables you will have to modify in the 'marketLooking.py' file. My varibales are meant to represent the coins I am holding. Say you are also holding bitcoin (apart from adding it to the dictionary mentioned in 'portfolio.py'), then you would add a line that would look like this:

bitcoin = pd.Series((market.ticker('bitcoin'))[0])

What this file does is retrieve the market data from coinmarketcap every half an hour for a period of 12 hours. Each time it returns the data, it weeds it out to only keep the relevant data to the coins you are holding in a dataframe. Afterwards it exports the dataframe as a csv to a folder called 'Data'.

Finally, in the 'correlationMatrix.py' file you will have to change one line to represent the coins you are holding. In the image below you can see that line.

cryptoPortfolioCorrelationMatrixInfo.PNG

Say that on top of holding those four kinds of coins, you are also holding bitcoin. Then the line would look something like this:

dictOfCurrencies = {'bitcoin-cash': [], 'litecoin': [], 'siacoin': [], 'digibyte': [], 'bitcoin':[]}

Once you are done with those changes, you can run the 'correlationMatrix.py' file and you will get something like the following image.

cryptoPortfolioCorrelationMatrix.PNG

I hope this project will help you visualize your own profits and losses as well as the correlations among your coins. Take in mind that in order to have better correlation calculations, you will have to run 'marketLooking.py' several times in order to have more data to study. If you have any questions or doubts, let me know. I know some stuff might not be very clear, so the sooner I get doubts, the sooner I can edit this to make it even more clear for everyone.

Best,

@capatazche

Sort:  

I noticed that you use the steemstem tag. A small note: it is unlikely that you will get curated by steemstem if you frequently use bidbots

You're right, you had told me but I had forgotten. That is fine, more than expecting a vote, I just want to share with that community.

Thanks for the reminder, sensei.

Nice project and good blog.

Thanks, Steven. Hopefully it will be useful for more people.

Congratulations, your post received 9.52% up vote form @spydo courtesy of @capatazche! I hope, my gratitude will help you getting more visibility.
You can also earn by making delegation. Click here to delegate to @spydo and earn 95% daily reward payout! Follow this link to know more about delegation benefits.

You got a 3.23% upvote from @upmewhale courtesy of @capatazche!

Earn 100% earning payout by delegating SP to @upmewhale. Visit http://www.upmewhale.com for details!

You just planted 0.43 tree(s)!


Thanks to @capatazche

We have planted already 7446.81 trees
out of 1,000,000


Let's save and restore Abongphen Highland Forest
in Cameroonian village Kedjom-Keku!
Plant trees with @treeplanter and get paid for it!
My Steem Power = 22160.78
Thanks a lot!
@martin.mikes coordinator of @kedjom-keku
treeplantermessage_ok.png

Coin Marketplace

STEEM 0.27
TRX 0.12
JST 0.032
BTC 57369.97
ETH 2943.81
USDT 1.00
SBD 3.63