Writing a web widget to monitor the value of your crypto portfolio requires very simple PHP and HTML skills.
The purpose of the article is super simple, but I think there are probably a lot of people out there who would join a cryptocurrency investment, have a website somewhere, and want to set up a private (or hell, maybe public) page somewhere that shows the value of your crypto portfolio in real time. However, you may not know how to write your own code.
Using the free Coinmarketcap API is very easy. When you add some styles to it, you can make your portfolio widgets in no limit to how much extra math and calculation you can add.
For me it was basically just making a little Bootstrap table and setting it up to output my own pathetic “portfolio”, and on the page I own it, it appears as follows:
It’s so easy now! Whenever I wonder what my portfolio is worth, I think about how ridiculous I used to be, tired of updating spreadsheets with currency values.
All you need is a working site, for example, any normal server running WordPress. In the sample code below, I’ve already pulled out the bootstrap stuff, so it should just render a plain old HTML table. You might want to add custom classes or other cool things (for example, set negative percentages to red and positive numbers to green).
First, you need to tell the code how much of each currency you have. I use an array to save it, called $myCoins in the code below. Hopefully you can see how to customize yourself using your own currency symbols and place balances in those locations. Please note that whenever you buy more cryptocoins and change content, you need to update the balance in the $myCoins section of the script.
Anyway, that’s the basic code, and I’ll add more comments below. : -)
<? $myCoins = array('BTC' => array('balance' => 0.0093), 'ETH' => array('balance' => 0.235724420), 'XRB' => array ('balance' => 2.524402070), 'MIOTA' => array ('balance' => 33.000000000), 'XRP' => array ('balance' => 49.000000000), 'XLM' => array ('balance' => 105.894000000), 'TRX' => array ('balance' => 599.400000000); // ok now hit the api... $coinbasePublicAPI = 'https://api.coinmarketcap.com/v1/ticker/'; $coinData = file_get_contents($coinbasePublicAPI); $coinData = json_decode($coinData, true); echo '<table>'; echo '<tr>'; echo '<td>NAME</td>'; echo '<td>SYMBOL</td>'; echo '<td>PRICE</td>'; echo '<td>HOLDINGS</td>'; echo '<td>VALUE</td>'; echo '<td>1hr</td>'; echo '<td>24hr</td>'; echo '<td>7day</td>'; echo '</tr>'; $numCoinbaseCoins = sizeof ($coinData); $portfolioValue = 0; for ( $xx=0; $xx<$numCoinbaseCoins; $xx++) { // this part compares your coins to the data... $thisCoinSymbol = $coinData[$xx]['symbol']; // if you have it, this var is true... $coinHeld = array_key_exists($thisCoinSymbol, $myCoins); // comment the next line out & you will see ALL of the coins // returned (not just the ones you own): if ( ! $coinHeld ) { continue; } echo '<tr>'; // name: echo '<td>' . $coinData[$xx]['name'] .'</td>'; // symbol: echo '<td>' . $thisCoinSymbol .'</td>'; // price: $thisCoinPrice = $coinData[$xx]['price_usd']; echo '<td>$' . number_format($thisCoinPrice,2) .'</td>'; // holdings: echo '<td>'; if ($coinHeld) { $myBalance_units = $myCoins[$thisCoinSymbol]['balance']; echo number_format($myBalance_units,10); } echo '</td>'; // track running total value of coins: if ($coinHeld) { $myBalance_USD = $myBalance_units * $thisCoinPrice; $portfolioValue += $myBalance_USD; } // value: echo '<td>$'. number_format($myBalance_USD,2) .'</td>'; // 1h market change: echo '<td>' . $coinData[$xx]['percent_change_1h'] .'%</td>'; // 24h market change: echo '<td>' . $coinData[$xx]['percent_change_24h'] .'%</td>'; // 7d market change: echo '<td>' . $coinData[$xx]['percent_change_7d'] .'%</td>'; echo '</tr>'; } echo '<tr>'; echo '<td colspan="4"><strong>TOTAL</strong></td>'; echo '<td colspan="4"><strong>$' . number_format($portfolioValue,2) . '</strong></td>'; echo '</tr>'; echo '</table>'; ? >Copy the code
… That’s all you need. Just customize the initial $myCoins array, which should render your table. Chances are, your portfolio is more impressive than mine, because I’m new to all of this and I’m still learning about crypto investing.
notes
The above script hits the Coinmarketca.com API. The API method and other instructions are as follows: coinmarketcap.com/api/
They require you to call the API no more than 10 times per minute, so maybe don’t put it on a 24/7 traffic-crazy site.
The above routine only calls the main API once, so it only enters the first 100 tokens. If you are investing a token in the list, at you need to customize the above script to iterative multiple calls the API, this can be achieved by is added to the end of the URL to complete the “start” parameters, such as: api.coinmarketcap.com/v1/ticker/?… You need to set up the API hit loop and build a larger data set from the results, then parse it all onto the screen.
OTOH, I think, in order to get them all (I think they have about 1,500 tokens) you would need to call their API more than 10 times, so it’s not a good resource for doing any large development or project. Obviously, they’re going to come out with a paid API for something like this.
In addition to adding styles, etc., you may need to build your token list to contain more information. For example, instead of the simple array I showed you, maybe yours looks like:
$myCoins = array('BTC' => array('balance' => 0.0093, 'wallet' => 'Coinbase', 'notes' => 'whatever', 'buy - in - price' = > '8005.22'), 'the ETH' = > array (' the balance '= > 0.235724420,' wallet '= >' Trezor ', 'notes' = >' whatever '. 'buy - in - price' = > '555.88'), 'XRB' = > array (' the balance '= > 2.524402070,' wallet '= >' Binance ', 'notes' = >' whatever '. 'buy - in - price' = > '1.25'), 'MIOTA' = > array (' the balance '= > 33.000000000,' wallet '= >' GDAX ', 'notes' = >' whatever '. 'buy - in - price' = > '0.25'), 'XRP' = > array (' the balance '= > 49.000000000,' wallet '= >' Kucoin ', 'notes' = >' whatever '. 'buy-in-price' => '1.25'), 'XLM' => array ('balance' => 105.894000000, 'wallet' => 'Paper wallet', 'notes' = >' whatever 'and' buy - in - price '= >' 2.50 '), 'TRX' = > array (' the balance '= > 599.400000000,' wallet '= >' Bittrex ', 'notes' = >' whatever 'and' buy - in - price '= >' 0.054 '));Copy the code
… Then your widget or report might be even more exciting. I actually like to use a small database application to keep track of balances without having to update the code whenever the balance changes. But, to me, it’s as easy and fast as anything else… Of course, my balance is pretty pathetic. But, the idea is that you’re not just storing token balances; You can also store additional information and use it to calculate and/or display results in your widgets or financial reports, or whatever you’re building.
Easy, I know… But it’s a bit of fun and hopefully helpful for some people who want to extract Coinmarketcap.com data into their site. : -)
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Share some interactive online programming tutorials related to blockchain such as Ethereum, EOS and Bitcoin:
- Java Ethereum development tutorial, mainly for Java and Android programmers for blockchain Ethereum development web3J details.
- Python Ethereum is a detailed explanation of blockchain ethereum development by Python engineers using Web3.py.
- PHP Ethereum, mainly introduces the use of PHP for smart contract development interaction, account creation, transactions, transfers, token development, filters and transactions and other content.
- Ethereum introductory course, mainly introduces smart contract and DAPP application development, suitable for entry.
- Ethereum development advanced tutorial, mainly introduces the use of Node.js, mongodb, blockchain, IPFS to achieve decentralized e-commerce DApp combat, suitable for advanced.
- C# ethereum, mainly explains how to use C# based development. Net ethereum applications, including account management, status and transactions, smart contract development and interaction, filters and transactions, etc.
- This course will help you get a quick start on the development of EOS blockchain decentralized applications. It covers core knowledge points such as EOS tool chain, account and wallet, issuing tokens, development and deployment of smart contracts, and interaction between codes and smart contracts. Finally, the development of a notepad DApp will be completed using all knowledge points comprehensively.
- Java development tutorial COINS, this course for beginners, content covers the core concepts of COINS, such as block mechanism, key chain store, decentralized consensus and script, trading and UTXO etc, also explained how to integrate the currency support functions in Java code, such as creating address wallet, tectonic naked trading, management, Is a rare bitcoin development course for Java engineers.
- PHP currency development tutorial, this course for beginners, content covers the core concepts of COINS, such as block mechanism, key chain store, decentralized consensus and script, trading and UTXO etc, also explained how to integrated the currency support functions in PHP code, such as creating address wallet, tectonic naked trading, management, This is a rare bitcoin development course for Php engineers.
- Tendermint blockchain development in detail, this course is suitable for engineers who want to use Tendermint blockchain development, the course content includes the core concepts of tendermint application development model, such as ABCI interface, Merkle tree, multi-version state library, etc., also includes the rich practical code such as token issuance. It is the best choice for go language engineers to quickly start blockchain development.
Huizhi net original translation, reprint please indicate the source. Write a web widget to monitor your crypto