I created a little Bitcoin prediction app in Rstudio

in coding •  6 years ago 

The idea was fairly simple to create a basic program to check to see how good one is at predicting the price of Bitcoin in 5 seconds. This lead to creating this small program.

The R programming language is more about using statistics than building real programs but it can still be done. This is an example of that.

The code:

First call the libraries you'll need - and install them if needed.

library(httr)
library(jsonlite)
library(tcltk)

Then you'll need to create a API pull. I used cryptocompare's website for my pull.

url.coin <- 'https://min-api.cryptocompare.com/'
ticker <- 'BTC'
convert <- 'data/price?fsym='
base.currency <- 'USD'
middle <- '&tsyms='

Then form the data into your API request

request <- content(GET(url = paste0(url.coin,convert,ticker,middle,base.currency)))
x <- request$USD

prompt user input and ask for a prediction:

user.input <- tk_select.list(choices = c("Up", "Down"), preselect = NULL, multiple = F, title = c("Will Bitcoin's price go Up or Down in 5 seconds? The current price of Bitcoin is", x))

When the program is ran. The user.input line will produce a small little message box and an option to choose if the price will go up or down. Here's a picture of it.

Bitcoin predict 1.png

Then the program sleeps for 5 seconds:

Sys.sleep(5)

It gets the current price and stores it as a value.

request2 <- content(GET(url = paste0(url.coin,convert,ticker,middle,base.currency)))
y <- request2$USD

Then compares the price stored in X against the price stored in Y.

if(y > x & user.input == "Up") {
answer<-"Your prediction was correct!"
} else { if (y < x & user.input == "Up")
answer<-"Your prediction was wrong!"
else { if (y > x & user.input == "Down")
answer<-"Your prediction was wrong!"
else { if (y < x & user.input == "Down")
answer<-"Your prediction was correct!"
else { if (user.input == "")
answer <- "You failed to enter if the Bitcoin price will go Up or Down"}
} } }

Displays if you're right or wrong:

tk_messageBox(type = "ok", message = print(c(answer, "The current price of Bitcoin is",y, "The old price is", x)))

Display for correct:

Bitcoin predict correct.png

Display for wrong:

Bitcoin predict wrong.png

Display for if you failed to choose a prediction:

Bitcoin predict nothing.png

I thought this was a fun little project and figure I'd share it with the community. I've been exploring Rshiny for building web apps and I might create a more robust prediction program on Rshiny later. But who knows. Full code is at the bottom. Enjoy!

If you liked this content feel free to reblog, follow, and comment.

Anyways, hope you enjoyed!

Who is klabboy?

I’m different than some steemians. I have never been be paid to create content (baring likes from fellow steemians), I have never paid for upvotes, and all my content is 100% created by me. Nothing fake, no agenda, just my thoughts, and your comments.

Further, I’m a stutterer, crypto-enthusiast, economics student, data nerd, and outdoors lover. I post mostly on economics, data, personal fun/stories, and crypto. If you have an interest in those things and the occasional random topic, feel free to like, follow, and reblog my content. Hope to hear from you in the comments! Have a wonderful day!

Full code:

library(XML)
library(httr)
library(tcltk)
url.coin <- 'https://min-api.cryptocompare.com/'
ticker <- 'BTC'
convert <- 'data/price?fsym='
base.currency <- 'USD'
middle <- '&tsyms='
request <- content(GET(url = paste0(url.coin,convert,ticker,middle,base.currency)))
x <- request$USD
user.input <- tk_select.list(choices = c("Up", "Down"), preselect = NULL, multiple = F, title = c("Will Bitcoin's price go Up or Down in 5 seconds? The current price of Bitcoin is", x))
Sys.sleep(5)
request2 <- content(GET(url = paste0(url.coin,convert,ticker,middle,base.currency)))
y <- request2$USD
if(y > x & user.input == "Up") {
answer<-"Your prediction was correct!"
} else { if (y < x & user.input == "Up")
answer<-"Your prediction was wrong!"
else { if (y > x & user.input == "Down")
answer<-"Your prediction was wrong!"
else { if (y < x & user.input == "Down")
answer<-"Your prediction was correct!"
else { if (user.input == "")
answer <- "You failed to enter if the Bitcoin price will go Up or Down"}
} } }
tk_messageBox(type = "ok", message = print(c(answer, "The current price of Bitcoin is",y, "The old price is", x)))

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!