# Best way to calculate profit/loss with historical data

## Problem

I need to build a profit/loss graph for a mobile app. The api from which I can get the historical price data only allows me to retrieve 1 item at a time. If the user has 500 items that would create an api failure since I can only make 50 req./min.

What is the best way to calculate this? Let's say the user buys:

1. Bitcoin on 2018
2. Ethereum on 2020

Solution 1:

• On the very first time the user refreshes the data, save when and it was saved and up to which historical date.
• Do as many requests as coin the user have (problem here)
• Calculate offline the profit loss based on the dates.
• If the user comes back 5 days later, only pull those 5 days from the historical api and merge it with the previous ones.

The problem here is that I'm making too many api requests. Imagine the user has 1000+ coins.

Also calculating the profit/loss by hour might be a problem.

Solution 2:

Create a server side job and every day get the historical data, but this might be a little overboard since there are also multiple currencies, I was hoping to find an easier/less expensive solution.

## Tech using

For the infrastructure I'm using AWS -> DynamoDB with DataStore + AppSync

What is the best way to accomplish this given many users and many coins per user, taking into consideration I can make 1 api request per coin and up to 50 request per min.?

Thanks

• Could you please clarify: when you say 500 coins, do you mean 500 hundred coins of a few cryptocurrencies (e.g. bitcoin, ethereum, theter), or are you referring to 500 different currencies? Can you confirm that the limitation of the API is 1 crypto currency (and all coins within the same currency have equal value on the same day)? Mar 13, 2022 at 9:55
• @Christophe It’s the first one you mentioned. 500 coins (bitcoin, etherium, theter…) yes, the api is from CoinGecko, historical data limits 1 coin per request, probably because it would be too big if you include more. So the request for 1 can include the historical price in hours or days up to x days or the max amount of days since the coin started. If you want I can provide the url of the api, it’s public. I didn’t understand your last question , for example if I select the currency usd all coins will have the same days and currency. Mar 13, 2022 at 16:03