paint-brush
Giving a twitter bot ability to predict bitcoin value based on historical databy@ognjengt
4,170 reads
4,170 reads

Giving a twitter bot ability to predict bitcoin value based on historical data

by Ognjen GataloDecember 21st, 2017
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

For some time now I’ve shown interest in cryptocurrencies, and most of all predicting their rise and fall in the next few days. I’ve tried some algorithms, but at the end of the day, I think that no one can really with certitude claim their rise or fall. I’ve tried to focus only on one currency, and <a href="https://hackernoon.com/tagged/bitcoin" target="_blank">bitcoin</a> being the leader, I decided to go with it.

Company Mentioned

Mention Thumbnail
featured image - Giving a twitter bot ability to predict bitcoin value based on historical data
Ognjen Gatalo HackerNoon profile picture

For some time now I’ve shown interest in cryptocurrencies, and most of all predicting their rise and fall in the next few days. I’ve tried some algorithms, but at the end of the day, I think that no one can really with certitude claim their rise or fall. I’ve tried to focus only on one currency, and bitcoin being the leader, I decided to go with it.

Disclaimer: This article will concentrate to explain the algorithm used to predict the value.

I have seen that the fastest way to get the information related to bitcoin is on twitter, so I decided to create a bot, that would every 2 hours tweet the predictions of bitcoin value in the next N days. N being the number of days people request the most. So for example if 3 people requested that the bot predicts the value in the next 5 days, and 7 people requested that the bot predicts the value in the next 2 days, the bot would tweet the prediction for 2 days, because the more people requested that prediction.

I quickly created a node app and after some time of searching and trying to figure out the best way to query historical data, bitcoin charts and extract useful data that could be helpful for future rise and falls, I decided that the K nearest neighbors in combination with some other algorithms is the way to go.

This prediction algorithm is constructed out of next steps:

  1. Collect all of the user tweets (requests)
  2. Get the most requested day to predict for
  3. Get the current bitcoin value
  4. Find K (10) nearest dates in the past 2 months, on which the value of bitcoin was most similar to the current value
  5. For every date found (call this PAST_DATE), find the value of BTC after the next N days (call this N_DAYS_AFTER_PAST_DATE)
  6. For each date, calculate the difference between the values on N_DAYS_AFTER_PAST_DATE and PAST_DATE
  7. Sum all of those differences and divide it by K
  8. The result is how much did bitcoin grew in the given time span between all of PAST_DATES and N_DAYS_AFTER_PAST_DATES

If this confuses you, I will try to make it a bit clearer now.

First step: Collect all of the user tweets (requests)

Using the Twit module, I search the twitter api for tweets that contain “@coin_instinct Predict for <number> days” extract just the numbers from the tweets, and create the array of numbers.

Second step: Get most requested day

When a prediction is tweeted, the number of days in the future to predict is stored in a black list array. The black list array contains the number of days of the past 4 predictions, that helps solve redundancy issues, and tweeting the same prediction that has already been tweeted in the past 8 hours.

This function is pretty simple, it just gets the most requested number in number array. If that number already exists in blackListArr, then it returns the second most requested, and so on. If all of the requested numbers are in the blackListArr, then the bot predicts for n random days in the future.

Third step: Get the current bitcoin value

Using the blockchain.info API, we can get the current value of bitcoin, and store it into a variable

This function will run 2 minutes after the algorithm begins the work.

Fourth step: Finding the K nearest neighbors

Here I won’t include all of the functions, that are being called, such as querying the coindesk api, for PAST_DATES and N_DAYS_AFTER_PAST_DATE, I will just extrapolate the finding nearest neighbors based on similarities that we get. Whole project could be found on my github page, that I put at the bottom of this article.

As we calculated the differences between all of the bitcoin values in the past 2 months and current bitcoin value, we need to find the dates that have the difference values closest to 0. So we first call Math.Abs on whole similarities array and then, sort the array in ascending order.

From here we can easy get the top 10 dates on which the value of bitcoin was the closest to the value now.

Fifth step: Get final results array

Here we will get the array of objects, each containing start and end property. Start property will represent the value of bitcoin on the specific day in history, and End property will represent the value of bitcoin N days after that specific day. From this data, we can get the insight in value growth or loss.

The code is pretty straightforward, we go through all of the kNearest and get the data for specific dates. Store those results into an finalResults array and return it.

Sixth step: Calculating the prediction

All that is left is to calculate the prediction, and we do that with the next function call.

And that is it! We also need to create the cool text with all of the emojis in it, and tweet the prediction of course.

Conclusion

If you are still scratching your head over something that is fine, keep in mind that these are just raw functions, extracted from a full code that you can find on my github repo: https://github.com/ognjengt/coin-instinct-bot . Please do check it out, all of this will make much more sense.

Also, this is just my approach to somehow predict the bitcoin value in the next days, and I would like to hear opinions on this approach. The predictions don’t always match the correct value, but I have noticed that most of the time, the predictions are just 100, or 200 $ off. So you could say that isn’t that big of a miss, especially in this crypto world, where the charts are flying up and down like crazy.

Problem with this algorithm is that it just analyses the historical bitcoin data, and makes prediction out of that, so there isn’t any way to detect if the value is going to drop. I’m working on inserting a human factor, where I would scrape the websites for possible articles that are hinting to the bitcoin value crash, and add that to the equation.

And of course the thing this article is all about. To see the twitter bot live in action you can visit this link here: https://twitter.com/coin_instinct . Try it out and tweet to him! Request a prediction!

Thank you for reading this article, if you have any questions, feel free to post it in the comments or find me on twitter https://twitter.com/ognjengt