How to Use CoinGecko’s Free API to Calculate the ROI of a Long Position: Step-by-step

Grant Ferowich
Coinmonks
3 min readAug 25, 2023

--

Curious Weekly — writing inspired by curiosity.

Are you looking to buy the CoinGecko Annual API subscription here? Use referral code CGGRANT to get $500 off your Annual API subscription purchase.

Suppose you bought 1 Bitcoin on January 1, 2023 and you want to find out the return on investment (ROI) programmatically. This guide is for you.

CoinGecko has an awesome API. I have used CoinGecko’s API in an application for making financial calculations. Today, I will show you how you can also calculate the performance of a long position position.

CoinGecko’s API has been helpful for me

Let’s quickly review some basics. A long position consists of you buying some asset. If you are long, your position appreciates when the price of the asset increases. In this article, I will go over the calculation of the return on investment (ROI) of a long position.

Now, let’s move on to the programmatic approach. Let’s start with the input information. To determine the ROI of a long or short position we need the following information:

  • the quantity of the asset
  • the initial date on which you purchased the asset

Suppose you bought 1 Bitcoin on January 1, 2023 and you want to find out the ROI programmatically. With just the quantity of the asset and the initial date, we can use CoinGecko’s API and some math skills to output the ROI.

We can break down the process into smaller steps. First, we want to know the initial position value in dollars. Secondly, we want to know the current position value. Finally, we can calculate the ROI as a percentage by doing: (Current position value — initial position value) / initial position value. So, just to develop intuition and to provide an example, if I had one Bitcoin at a price of $10,000, the initial position value would be $10,000. Suppose the price increased to $20,000. The current position value would be $20,000. ($20,000 — $10,000) / $10,000 returns 1, which, in percentage point terms, is 100%.

Here’s what a script looks like for calculating the initial position value programmatically.

const axios = require('axios');
// inputs: api key string, asset quantity, and index date
// output: $ value of the position
const calculateInitialPositionInt = async (APISymbolStr, quantityInt ,indexDateStr) => {
let historicalPriceAPIStr = "https://api.coingecko.com/api/v3/coins/"+APISymbolStr+"/history?date="+indexDateStr+"&localization=false";
let historicalPriceInt = await axios.get(historicalPriceAPIStr).then((response) => response.data.market_data.current_price.usd);
let initialPositionValueInt = historicalPriceInt * quantityInt;
return initialPositionValueInt;
}

const callFunction = async () => {
const initialBitcoinPositionValueInt = await calculateInitialPositionInt("bitcoin", 1, "01-01-2023")
console.log('Intial Bitcoin position value: $', initialBitcoinPositionValueInt);
}
callFunction();

Now, let’s add in the function to calculate the present position value:

const calculatePresentPositionInt = async (APISymbolStr, quantityInt) => {
let presentPriceAPIStr = "https://api.coingecko.com/api/v3/simple/price?ids="+APISymbolStr+"&vs_currencies=usd";
let presentPriceInt = await axios.get(presentPriceAPIStr).then((response) => response.data[APISymbolStr].usd);
let presentPositionValueInt = presentPriceInt * quantityInt;
return presentPositionValueInt;
}

Lastly, let’s calculate the return on investment in percentage point terms.

const calculatePercentageROIInt = (initialInt, presentInt) => {
return ((presentInt - initialInt)/ initialInt) * 100
}

Now, let’s put it all together!

// file is called calculateRoi.js
const axios = require('axios');
// inputs: api key string, asset quantity, and index date
// output: $ value of the position
const calculateInitialPositionInt = async (APISymbolStr, quantityInt ,indexDateStr) => {
let historicalPriceAPIStr = "https://api.coingecko.com/api/v3/coins/"+APISymbolStr+"/history?date="+indexDateStr+"&localization=false";
let historicalPriceInt = await axios.get(historicalPriceAPIStr).then((response) => response.data.market_data.current_price.usd);
let initialPositionValueInt = parseInt(historicalPriceInt * quantityInt);
return initialPositionValueInt;
}

const calculatePresentPositionInt = async (APISymbolStr, quantityInt) => {
let presentPriceAPIStr = "https://api.coingecko.com/api/v3/simple/price?ids="+APISymbolStr+"&vs_currencies=usd";
let presentPriceInt = await axios.get(presentPriceAPIStr).then((response) => response.data[APISymbolStr].usd);
let presentPositionValueInt = presentPriceInt * quantityInt;
return presentPositionValueInt;
}

const calculatePercentageROIInt = (initialInt, presentInt) => {
return ((presentInt - initialInt)/ initialInt) * 100
}

const callFunctions = async () => {
const initialBitcoinPositionValueInt = await calculateInitialPositionInt("bitcoin", 1, "01-01-2023")
console.log("Intial Bitcoin position value: $", initialBitcoinPositionValueInt);
const presentBasketValueInt = await calculatePresentPositionInt("bitcoin", 1);
console.log("Present Bitcoin position value: $", presentBasketValueInt)
const percentageROIInt = calculatePercentageROIInt(initialBitcoinPositionValueInt, presentBasketValueInt);
console.log("ROI in percentage terms:" ,percentageROIInt)
}


callFunctions();

The script prints out the following output to the console:

Intial Bitcoin position value: $ 16540
Present Bitcoin position value: $ 25904
ROI in percentage terms: 56.614268440145096

Boom! Purchasing 1 Bitcoin on January 01, 2023 resulted in a 56.61% ROI as of August 26, 2023.

Also, read:

--

--

Grant Ferowich
Coinmonks

Writing code. "Everyone you meet is fighting a battle you know nothing about. Be kind." Curious Weekly - https://curiousweekly.com/.