JP’s Calorie Calculator

I quickly put together a Calorie Calculator. The calculators that Google suggested weren’t really that good, so why not make my own?

calorie calcFirst it calculates the Basal Metabolic Rate (BMR). It is a measurement of the number of calories needed to perform your body’s most basic (basal) functions, like breathing, circulation and cell production. It depends on gender, age, height and weight. There is a standard formula for this, so I only had to copy it.

Since I only made this for my own use, the inputs are in metric units. However, it is interesting to learn my height in inches and weights in pounds, so I made it show these automatically.

The second part asks for how many minutes you spend on each of ten activity levels.  The scale starts with simple tasks like watching TV and driving, while medium levels are walking and bicycling and the scale ends with hard exercise. I didn’t see such a scale in other calculators, and to me it makes more sense to group activities this way.

The final result is your basal metabolism plus energy burned on activities.

I also wanted to compare different units, so it outputs kJ and kWh along with kcal. The equivalent amount of body fat and assumed wattage are included as well. This all fairly easy physics, and interesting for comparison.

The job took me a few hours. I did it on my spare time to practice HTML5, JavaScript and CSS.

The Counterparty SQLite Database

Counterparty is perhaps the world’s only database not controlled by anyone. It’s decentralized. It just exists. To create a copy of this database you run a specific script, and it’s in everyone’s interest to stick to the same implementation. This is the nature of social consensus. To make a record to the database you must send a specially encoded Bitcoin dummy transaction. The rules are such that a financial ecosystem evolves from it.

Hard to grasp? I bet!

One feature of Counterparty is that you can issue an asset which can be thought of as a stock. Each asset has a unique name (or ticker). You can only register a name that no one has claimed before. There is a second-hand market for names, and some people have rushed to pick up good names in hope of selling them later.

I’ve played around with this database a bit in SQLite Studio.

An SQL query (1) shows that the rate of registrations was low to begin with, and that there have been a few booms of new name registrations.

Total XCP Assets

Registering a name is not free. The protocol has a native currency, XCP, and each registration costs half an XCP. At the peak of registrations, one XCP was less than a dollar. There is also a free numerical asset class, but it is not included in this graph. The concept of Block Height is simply a unit of time. 20,000 blocks is a bit less than 5 months.

To those who wonder who receives the XCP fee when nobody controls the database … the answer is no one. The XCP is “burned” or destroyed. There are 2.6 million XCP and this amount decreases as fees are burned.

Another SQL search (2) counted the number of assets by first letter.

assets by first letter

Not too interesting, I suppose. Just a technical note – regular named assets start with letters B-Z and numerical assets consist of the letter A followed by a number.

Finally I grouped asset names by length (3).

assets by length

The rule is that a regular asset must be 4-12 characters. In the very beginning they could be up to 14 characters. Numerical assets begin with A followed by a really large number. Total length must be 18-21 characters. The exact requirement is a number between 95,428,956,661,682,177 and 18,446,744,073,709,551,615. Welcome to the world of crypto!

These graphs aren’t that exciting but they illustrate the powers of Counterparty and SQLite. Below are the actual searches I made:


block_index AS block,
COUNT(*) AS total
FROM assets
WHERE substr(asset_name,1,1) NOT LIKE 'A'
GROUP BY block/100

The last statement grouped them by 100’s of blocks. The actual block displayed was the last block with a registration, e.g. 281244 for the 281200’s. In LibreOffice Calc I simply took the floor of this number divided by 100 and the multiplied by 100 to get the round 100’s I wanted. Finally I made a column with accumulated registrations for the graph.


substr(asset_name,1,1) AS first_letter,
COUNT(*) AS total
FROM assets
GROUP BY first_letter



length(asset_name) AS asset_length,
COUNT(*) AS total
FROM assets
GROUP BY asset_length


Extract Data from Websites and Import to Excel

morningstar downloadI recently built a downloader for Grotte Investments. It gathers data from Morningstar and Yahoo Finance. It loops through thousands of stocks from around the globe, extracts the specified data and outputs a CSV file. It can then be easily imported to Excel.

When making a downloader lots of things can and will go wrong. Connection problems, server problems, missing data, wrongly formatted data, unknown encoding – all potentially add complexity. Typically you need to run a downloader a few times just to sort out all the bugs.

Having said that, mastering the art of building downloaders is great fun. For this downloader the challenge was actually to understand the logic behind Morningstar’s website. The data was not included in the source code. The data feed was gathered by a JavaScript within the source. The next challenge was to link Morningstar’s and Yahoo’s ticker symbols. It’s not too difficult but it just took some time. Apart from this, the coding wasn’t that difficult.

If it were my first time doing this, it would maybe take 100 hours to code. Now it only took me around 10 hours to get everything right. The next time I aim for 5!

My Award Winning Bitcoin Wallet

Yup, my project won the 1st price in the Devparty competition February 2016. It was an open competition where anyone could submit their project for the Counterparty Bitcoin platform. Counterparty adds financial instruments to the existing Bitcoin system.

My project was initially a toolbox, called CounterTools, for recovering passwords, timestamping files, sending in bulk, and a few other features I missed in existing software. During the project I realized it would be feasible to make a full blown Bitcoin wallet.

countertoolsIn this blog I will describe the coding experience, not details of the actual project. If you are curious about the project, go try the CounterTools DEMO online. If you want to go one step further and make your own wallet, you must download it from GitHub.

This was my first real JavaScript project. My HTML and CSS skills were also put to a test. The interesting part, that I soon realized,  is that almost for any JavaScript that I may have, someone else have asked the same question before, and Stack Overflow has the answer. There are libraries for just about anything. Even Bitcoin has a great Bitcore library and for Counterparty I copied a lot from the Counterparty Chrome Browser Wallet. This made building  JavaScript feel more like building Lego than actual programming …

I did not use a developer environment. All coding was written in Notepad++. It’s better than Microsoft’s Notepad but it does not auto-complete, nor warn about bugs. All it does is color the syntax. It’s a bit helpful. For debugging I used Firefox’s developer tools. It worked okay, and I didn’t want to waste time during the competition to  learn a new IDE. After it was all over I did some research, and found Brackets to be a good, free alternative.

I think HTML and JavaScript was the best choice for this project. No installation is needed. The code is open to review, which is important for a project like this. Most importantly, it runs in any browser on any system. It does not matter if you use Apple, Linux or Microsoft, desktop or smartphone – as long as you have a modern browser, it works.

A secondary goal of this project was to make it developer friendly. I want others to build on what I have built. If you are interested in creating something Counterparty related, please drop a comment.


A Big Integer Brain Teaser

The financial platform Counterparty* has a rather eccentric stock ticker format. It starts with the character ‘A’ followed by a very, very large number between 95 quadrillion and 18 quintillion!

How can you verify if a string is a valid stock ticker?

Logically, the solution should be very simple. The string must start with ‘A’ and if the remaining string is an integer in the specified range, then the ticker is valid.

However, this does not work unless the integer format is unsigned 64 bit. In fact, the exact upper limit is 18,446,744,073,709,551,615 or 264 − 1, which is the max value for this integer format.

So far, so good. Now what if I tell you that you must write the algorithm in JavaScript and it does not support numbers of this magnitude.

One solution is to import a library, like BigInteger. It’s doable but it adds some complexity (it adds both developer time and download time) since an additional file must be referenced and loaded.

The solution I came up with circumvents this problem by splitting the number in two. Each piece is small enough to be handled by JavaScript’s native number format.

To easier understand the logic, let’s pretend the numbers were much smaller. The ticker could have a value between 678 to 2345 but the max integer were 99. Then the way to approach this problem would be:

  • Count digits, make integer x1 from first two digits, make integer x2 from remaining digits
  • If # digits = 3 AND x1 < 67 then return NOT VALID
  • If # digits = 3 AND x1 = 67 AND x2 < 8 then return NOT VALID
  • If # digits = 4 AND x1 > 23 then return  NOT VALID
  • If # digits = 4 AND x1 = 23 AND x2 > 45 then return  NOT VALID
  • Else return VALID

Some prior tests are assumed, and the below code shows the exact function that solves the initial problem.

function isValidNumericAsset(asset) {
	//'A' followed by a really large number
	//Min = 26^12+1 =    95,428,956,661,682,177
	//Max = 2^64-1 = 18,446,744,073,709,551,615
	if (asset.length>21) return false;
	if (asset.length<18) return false;
	if (asset[0] != 'A') return false;
	if (asset.substring(0,2) == 'A0') return false;
	if (asset.substring(1).match(/[^0-9]/) != null) return false;
	if (asset.length==18 && asset.substring(1,9)<95428956) return false;
	if (asset.length==18 && asset.substring(1,9)==95428956  && asset.substring(9)<661682177) return false; 
	if (asset.length==21 && asset.substring(1,10)>184467440) return false;
	if (asset.length==21 && asset.substring(1,10)==184467440  && asset.substring(10)>73709551615) return false;
    return true;
  • Counterparty is an online cryptographic token platform. You can issue tokens which, among many things, can represent shares in a company. The tokens you issue must have a unique name, just like a stock ticker uniquely identifies a company. The naming convention is that the name must either begin with ‘A’ followed by a big number or [B-Z] followed by 3-11 letters. Since the name must be unique, there is a fee for issuing an alphabetic names to prevent squatting. A numerical name is free to issue and the numbers are made incomprehensibly large so that squatting won’t be worthwhile.



Switching from VB to C#

Learning a new programming language is not that hard. The real challenge is converting your old code into a new language, as well as getting used to a new programming environment.

Ever since I learned how to make Windows applications in Visual Basic. NET in 2005, it has been my primary language. I learned all the basics at a university course and soon I coded small apps for stock trading. These programs analyzed the market in real time, alarmed me when events were triggered and downloaded daily price data. For every new program that I made, I copied code from previous programs. Therefore it was natural to stick with VB.NET.

morningstar download

With Visual Studio it is easy to make a Windows application.

VB.NET is by the way a very good language, and I don’t know any better programming environment than Microsoft’s Visual Studio. The notion that Visual Basic is slow or too simple may have been true with VB6 fifteen years ago, but the modern VB.NET is just as good as any other language today.

There’s only one big drawdown. Few programmers use VB. And when I program I increasingly rely on Google and Stack Overflow. The more common a languages is, the more support it has. This is the reason I want to change my primary language.

My decision has come to C#.

C# (pronounced cee sharp) is another Microsoft language. Its syntax is similar to C, but it compiles to the same machine code as VB. Since Visual Studio has identical interface for VB and C#, I do not have to spend time getting used to a new environment. Whenever I make a new program, I will use C# unless I’m in rush. Whatever code I need to recycle I will need to convert from VB. This will slow me down a little bit, so I will still use VB.NET whenever time is a priority. Later, as I get more and more code in C#, it will increasingly be the language of choice. Eventually I will be more efficient in C# than I’m in VB.NET today, since it’s easier to find online resources for C#.

Why C# and not Java?

Java is the most used language. The reasons I still didn’t choose Java are

  • I prefer to make Windows form applications and Visual Studio does not support Java.
  • Nor did I find an environment (IDE) for Java that’s better than VS.
  • If I ever want to switch from C# to Java, the leap shouldn’t be that big. The syntax is almost identical.

Here’s a list of the languages most in demand as of 2016:


Yet, despite selecting C# as my primary language, I think it is good to know many languages and be familiar with several IDE’s. Among all the languages listed above I have some experience with all of them except IOS and Ruby. Java will always carry a special position. The summer of 2009 I took a course in Java at the Harvard Summer School. It was an excellent experience where I learned to write code directly in a text editor and even on paper. That’s a great way learning since you cannot rely on auto-complete. However, it’s not recommended for real development. Unless there’s a better IDE than Visual Studio for Java, I rather use C# and VB.