S Lazy-H
  • Home
  • About
  • Posts
  • Contact
  • Slide Rules
  • A Biker’s Tale

Cryptocurrency

R Programming
Stocks
cryptocurrency
Author

Sam Hutchins

Published

June 23, 2024

I was surfing old posts on this website, and ran across Checking stocks using R that I posted in 2020. It was about getting stock price history for various stocks. Recently, I have been reading a bit about cryptocurrency and thought it might be nice to have a R (R Core Team 2024) program to keep track of possible cryptocurrency acquisitions. And as usual, I remind readers I am not a R programmer, or even very knowledgeable about the intricacies of R programming. I just like to play with R.

After LOTS of investigation and reading, I tried a couple of paths available for acquiring cryptocurrency, such as binance, bitcoin, and kraken. There are others that I did not look into, as available choices can become overwhelming! As I wished to write a simple R program, I ultimately decided to investigate kraken, which also has a Android app. It also comes in two flavors, one for casual trading, and another for professional crypto trading.

One thing I’d like to note here is there are many websites that go into great detail when describing the cryptocurrency environment, I make no attempt to regurgitate that here.

Anyway, getting right into it, I decided to use a straight forward data.table() concept to keep track of things, and potentially have the data available for future tax purposes. This mostly consists of an easy way to keep cost basis data handy. One nice thing I discovered from the IRS website is there is currently no tax burden when transferring cryptocurrency from wallets as long as you control or own both wallets. This is as it should be, because most folks might prefer using what’s called a cold wallet1 as opposed to a hot wallet2 to store the digital assets.

There are many cold wallets available, such as ELLIPAL Titan, Ledger Nano S and Trezor, just to name a few. Which a person chooses depends on anticipated usage and price range, among other factors.

So, most folks may acquire ‘coin’ through their favorite exchange, but then immediately transfer the asset to their cold wallet. The cold wallet will contain the access code to manipulate their assets, but doesn’t actually contain the asset itself. This is recorded in the blockchain that records the transaction, which is available to all.

As everything is about Bitcoin, traded as BTC, I looked there first, but after seeing the growing inefficiency of their blockchain process which can take the equivalent of a small country’s energy consumption to generate, I looked elsewhere. Not to mention the fees for a Bitcoin transaction can be significant (staggering is an appropriate term)! So I looked into NANO, traded as XNO, which uses a totally different methodology, and processes a transaction in seconds instead of ten or more minutes. And, each transaction is its own blockchain.

So, the first step is create a basic structure that can be modified as the need arises. I did this using random data, but I pulled real price data from the Internet to make it more realistic, and tried to keep it aligned so as to make the initial table useful for manipulation. The below code is the initial structure generated in the program.

Code
Acquisition_Date <- c("4-26-24","4-27-24","4-27-24","5-1-24","5-11-24","6-6-24","6-8-24","6-11-24","6-17-24","6-21-24")
Coin <- c("Nano","Nano","Nano","Nano","Nano","Nano","Nano","Nano","Nano","Nano")
Source <- c("NanoDrop","Kraken","NanoDrop","Kraken","Kraken","NanoDrop","Kraken","Kraken","Kraken","Kraken")
Cost <- c(0,20,0,20,20,0,20,20,20,20)
Amount <- c("0.00010","16.29885","0.00010","17.71975","16.54779","0.00010","17.05528","17.54430","19.74773","21.63009")
Kraken_Fee <- c(0.00,1.28,0.00,1.28,1.28,0.00,0.39,0.39,0.39,0.39)
Basis <- c(0.00,18.72,0.00,18.72,18.72,0.00,19.61,19.61,19.61,19.61)
To_Wallet <- c(0.00010,16.19884,0.00010,17.66975,16.49779,0.00010,17.00528,17.49430,19.69773,21.58009)
On_Date <- c("4-26-2024","4-30-2024","4-27-24","5-5-24","5-15-24","6-6-24","6-11-2024","6-11-24","6-17-24","6-21-24")
nanofile <- data.frame(Acquisition_Date,Coin,Source,Cost,Amount,Kraken_Fee,Basis,To_Wallet,On_Date)
write.table(nanofile,file="nanofile.csv",row.names=F,sep=",")

There is a NANO Faucet where one can obtain some NANO, so I mixed in a few of those, just for variety, and to illustrate how to filter the data table later. The last two lines combine the created vectors into the nanofile table and writes the table to disk. This initial structure creation should only be done the first time the program is run, or if the data table doesn’t exist. Additional data would then be added to the created structure as required. Once the initial table is saved to disk, it can be read in the future as so.

nanofile <- read.csv("nanofile.csv", header=T)

So, the data.table we just created with the above data looks like this.

   Acquisition_Date Coin   Source Cost   Amount Kraken_Fee Basis To_Wallet   On_Date
1           4-26-24 Nano NanoDrop    0  0.00010       0.00  0.00   0.00010 4-26-2024
2           4-27-24 Nano   Kraken   20 16.29885       1.28 18.72  16.19884 4-30-2024
3           4-27-24 Nano NanoDrop    0  0.00010       0.00  0.00   0.00010   4-27-24
4            5-1-24 Nano   Kraken   20 17.71975       1.28 18.72  17.66975    5-5-24
5           5-11-24 Nano   Kraken   20 16.54779       1.28 18.72  16.49779   5-15-24
6            6-6-24 Nano NanoDrop    0  0.00010       0.00  0.00   0.00010    6-6-24
7            6-8-24 Nano   Kraken   20 17.05528       0.39 19.61  17.00528 6-11-2024
8           6-11-24 Nano   Kraken   20 17.54430       0.39 19.61  17.49430   6-11-24
9           6-17-24 Nano   Kraken   20 19.74773       0.39 19.61  19.69773   6-17-24
10          6-21-24 Nano   Kraken   20 21.63009       0.39 19.61  21.58009   6-21-24

The first column can be made prettier using the below line, the results being seen in a later table. Same applies to the last column, but later I remove it.

nanofile$Acquisition_Date <- mdy(nanofile$Acquisition_Date)

Now let’s say we needed the total NANO bought, but didn’t want the NanoDrop entries included. To filter the table, we could use the following code to skip the unwanted entries and derive a total. The table itself is not affected by this action.

writeLines(paste("Total Bought:", with(nanofile, sum(Amount[Source=='Kraken']))))

Which shows:

Total in Wallet: 126.14378

Consider the situation where we wish to calculate additional data using the above, such as how much the price per coin may be. To do this we can do the calculation, then add the new data to the data table, as so.

nanofile <- mutate(nanofile, Per_unit_price = nanofile$Basis / nanofile$Amount)

or,

nanofile <- mutate(nanofile, Per_unit_price = Basis / Amount)

This new column is added to the existing table, but only in the current R environment, although the new table could be written to disk if desired. As the data is calculated anew during each program execution, this is probably not a good idea.

Note the above code places the calculated column at the end of the data file. We can rearrange the column order and show only the columns we desire using the below line.

nanofile <- nanofile %>% select(Acquisition_Date,Coin,Source,Cost,Amount,Kraken_Fee,Basis,Per_unit_price)

giving the following table structure:

  Acquisition_Date Coin   Source Cost   Amount Kraken_Fee Basis Per_unit_price 
1        2024-04-26 Nano NanoDrop    0  0.00010       0.00  0.00     0.00000000
2        2024-04-27 Nano   Kraken   20 16.29885       1.28 18.72     1.14854729
3        2024-04-27 Nano NanoDrop    0  0.00010       0.00  0.00     0.00000000
4        2024-05-01 Nano   Kraken   20 17.71975       1.28 18.72     1.05644831
5        2024-05-11 Nano   Kraken   20 16.54779       1.28 18.72     1.13126889
6        2024-06-06 Nano NanoDrop    0  0.00010       0.00  0.00     0.00000000
7        2024-06-08 Nano   Kraken   20 17.05528       0.39 19.61     1.14979056 
8        2024-06-11 Nano   Kraken   20 17.54430       0.39 19.61     1.11774194
9        2024-06-17 Nano   Kraken   20 19.74773       0.39 19.61     0.99302553
10       2024-06-21 Nano   Kraken   20 21.63009       0.39 19.61     0.90660742

Lastly, if we wished a printable file we can use the following to write the desired information to disk.

cat("NANO Cryptocurrency Report\n\n", file="nanofile.txt") # output to file
capture.output(nanofile, file="nanofile.txt",append=T) # output table to file
cat(paste("\nTotal Bought:",with(nanofile, sum(Amount[Source=='Kraken']))),file="nanofile.txt",append=T)

The first line adds a heading, the second the data table, the last another calculation. Notice the append=T at the end of all additional lines after the first. Without this, the last line would be the only content in the file.

I had fun with this, and I may have another post in the future describing how to pull market data and display in several ways, for entertainment and evaluation purposes. Always keep in mind cryptocurrency trading must be done knowing it contains infinite volatility and any investment made may be completely lost. Use only funds you can survive without.

God Bless you and yours, and if you don’t know the Lord Jesus, time is getting very short! If you don’t believe in God, what if you’re wrong? One of Satan’s greatest triumphs is convincing people he doesn’t exist. But God has already won, and through His Son Jesus, we are offered redemption free of charge! It cannot be bought, sold, or traded, but only acquired by accepting Jesus as your Lord and Savior.

References

R Core Team. 2024. R: A Language and Environment for Statistical Computing. Vienna, Austria: R Foundation for Statistical Computing. https://www.R-project.org/.

Footnotes

  1. A cold wallet is circuitry contained in a device similar to a thumb drive that is never directly connected or accessible to the Internet.↩︎

  2. A hot wallet is usually a online wallet provided through various cryptocurrency exchanges.↩︎

© S Lazy-H 2019 - 2025