📜 ⬆️ ⬇️

We write Twitter-bot, which predicts the course Bitcoin

The programmer Oggen Gatalo, the author of the article whose translation we are publishing today, has been interested in cryptocurrency for some time. Especially it takes the forecasting course for the next few days. He says that he tried some algorithms, but in the end he had the feeling that none of the approaches would allow us to make short-term forecasts with confidence. Then he, focusing on the market leader Bitcoin, decided to work on his own method of predicting courses. The proposed algorithm, as well as its implementation in the form of a twitter bot, of course, with some changes, are also suitable for working with other digital currencies.

image

General overview of the algorithm and its implementation


Twitter seems to me a convenient platform for the implementation of projects in the field of cryptocurrency, similar to mine. Here you can, in particular, organize convenient interaction between users and programs for which your own accounts are open. That is why I decided to make my development in the form of a bot for Twitter. In particular, my bot should, every 2 hours, tweet a bitcoin cost forecast for the next N days. N is the period in days, the forecast for which interests most of all the people who have requested relevant information from the bot. For example, if 3 people made a request for a 5-day forecast, and 7 people are interested in the forecast for 2 days, the bot tweets the forecast for 2 days.

The bot is implemented on Node.js, I developed it pretty quickly. It took some time to search for sources of historical and current price data and to select ways to work with them. The main question was the algorithm. In the end, I decided that they would use the K-nearest-neighbor method in combination with some more things.
')
The considered method of price forecasting includes the following sequence of actions:

  1. Collect user requests for the next forecast.
  2. Finding the duration of the forecast, which is most often applied for.
  3. Getting the current value of bitcoin.
  4. Finding K(10) nearest dates in the last 2 months, in which the price of Bitcoin was most similar to its current price.
  5. For the price of each of the dates found ( PAST_DATE ), finding the BTC price N days after it ( N_DAYS_AFTER_PAST_DATE ).
  6. For each date, the calculation of the difference between the price values N_DAYS_AFTER_PAST_DATE and PAST_DATE .
  7. Adding all the differences found and dividing what happened by K
  8. Getting the result in the video of the average rate change bitcoin between all groups of values PAST_DATE and N_DAYS_AFTER_PAST_DATE . This result is used when forming a tweet.

Consider the basic steps of this process in more detail.

Step 1. Collect user requests


In this step, using the twit module and the Twitter API, you search for tweets that contain the following construct: @coin_instinct Predict for <number> days . Then, from the found tweets, the numbers symbolizing the number of days for which users want to get a forecast are extracted, and an array of numbers is created.

Step 2. Finding the duration of the forecast that interests the most users


When the bot, once every 2 hours, tweets the forecast, the number of days that was used in this forecast is stored in the black list, represented by an array. This array contains the duration of the predictions for the last 4 tweets. This approach allows you to avoid the frequent appearance of similar tweets and the issuance of the same predictions that have already been tweeted in the last 8 hours.

 async function findMostFrequent(array, blackListArr) {   if(array.length == 0)       return null;   var modeMap = {};   var maxEl = array[0], maxCount = 1;   //     blackList    maxEl  ,    .         ,  generateRandom   var containsValidNumbers = false;   for(var i = 0; i < array.length; i++) {     if(!blackListArr.includes(array[i])) {       maxEl = array[i];       containsValidNumbers = true;       break;     }   }   if(!containsValidNumbers) return await generateRandom(blackListArr);   for(var i = 0; i < array.length; i++)   {       var el = array[i];       if(blackListArr.includes(el)) continue;       if(modeMap[el] == null)           modeMap[el] = 1;       else           modeMap[el]++;        if(modeMap[el] > maxCount)       {           maxEl = el;           maxCount = modeMap[el];       }   }   await addToBlackList(maxEl);   return maxEl;  } 

The function presented here is very simple. She selects the most frequently requested duration of the predictions from an array of numbers. If the number found is already in blackListArr , the function returns the second most requested forecast, and so on. If all the requested forecast durations are already on the black list, then the bot issues a prediction for a randomly selected period.

Step 3. Getting the current value of Bitcoin


You can find out the current value of Bitcoin using the API blockchain.info. The resulting value is stored in a variable.

 function refreshBitcoinPrices() { const request = async () => {   var results = await fetch("https://blockchain.info/ticker");   results = await results.json();   bitcoinData.results = results;   if(bitcoinData.results) {console.log('Blockchain API works!');}   else console.log('Blockchain API is down at the moment.');   //      this.todayDate = new Date();   this.todayDate = this.todayDate.toISOString().split('T')[0];   console.log(this.todayDate);   console.log('New prices fetched.');   console.log('Most recent bitcoin price: '+bitcoinData.results.USD.last);   console.log('Time: '+new Date().getHours()+':'+new Date().getMinutes()); }   request();   //       setInterval(() => {     request();   },COIN_FETCH_TIMEOUT); // 1000*60*118 } 

This function starts 2 minutes after the start of the algorithm.

Step 4. Find K closest neighbors


Here I do not provide a description of all the functions used in this step, such as querying the Coindesk API to load the data required for the PAST_DATE and N_DAYS_AFTER_PAST_DATE . The search for nearest neighbors is shown here, based on how similar they are to the current price value. The complete project code can be found in my GitHub repository .

 async function getNearestNeighbours(similarities) { //      k(10),     0 var absSimilarities = []; similarities.forEach( (similarity) => {   absSimilarities.push({     date: similarity.date,     similarityScore: Math.abs(similarity.similarityScore)   }) }) absSimilarities = absSimilarities.sort(function(a,b) {   return (a.similarityScore > b.similarityScore) ? 1 : ((b.similarityScore > a.similarityScore) ? -1 : 0); }); var kNearest = []; for(var i = 0; i < K; i++) {   kNearest.push(absSimilarities[i].date); } return kNearest; } 

Since we calculated the difference between all bitcoin prices in the last 2 months and its current value, we need to find the dates in which these values ​​are closest to 0. Therefore, we first call Math.abs for all similatityScore properties of objects that are in array, and then sort the array in descending order, based on these properties.

At this stage, you can find the first 10 dates in which the Bitcoin price was closest to its current price.

Step 5. Formation of an array of results


At this step, we will get an array of objects, each of which contains the properties start and end . The start property represents the Bitcoin price for some of the previous dates, the end property is used to store the price N days after that day. Based on these data, we can form a forecast, make a conclusion about whether the price will rise or fall.

 async function getFinalResults(kNearest,nDays) { var finalResults = []; var finalResult = {}; await forEach(kNearest, async(date) => {   var dateTime = new Date(date);   var pastDate = dateTime.toISOString().split('T')[0];   var futureDate = new Date(date);   futureDate.setDate(futureDate.getDate() + nDays);   futureDate = futureDate.toISOString().split('T')[0];     var valueForThatDay = this.coinDeskApiResults.bpi[pastDate];   var valueForFutureDay = this.coinDeskApiResults.bpi[futureDate];   finalResult = {     start: valueForThatDay,     end: valueForFutureDay   }   finalResults.push(finalResult); }) return finalResults; } 

Here we go through all the elements from kNearest and get data for specific dates, after which we save the results in the finalResults array and return it.

Step 6. Prediction


Now it remains only to form a forecast. This is done using the following function.

 /** *    *  ,     * @param {*Array} data  ,      start  end * @param {*Float} currentBitcoinValue    */ async function calculatePrediction(data,currentBitcoinValue) { var finalPredictionData = {   raw: 0,   percentage: 0,   positive: '',   finalValue: 0 } var sum = 0; await forEach(data, async (value) => {   sum += value.end - value.start; }) sum = sum / K; finalPredictionData.raw = sum; finalPredictionData.finalValue = currentBitcoinValue + sum; finalPredictionData.positive = sum > 0 ? 'true' : 'false'; finalPredictionData.percentage = ((finalPredictionData.finalValue - currentBitcoinValue) / currentBitcoinValue) * 100; return finalPredictionData; } 

That's all, the forecast has been formed, it remains only to tweet it, having issued it accordingly.

Results


If you want to get acquainted with my bot more closely - I suggest to look into the above repository , which stores the full code of the project. In addition, I would like to note that this material describes my approach to predicting the Bitcoin price for the next few days, it is still in development, so if you have something to say about the proposed algorithm, let me know . The forecasts that the bot makes are not always accurate, but I noticed that in most cases the predicted value differs from the real only by $ 100-200. So, apparently, we can say that the bot usually makes mistakes not so much, especially considering how crazy the rates of cryptocurrency behave.

The main problem of the described algorithm is that it analyzes only historical data on Bitcoin and makes predictions based on them. There is no prediction mechanism here, for example, sharp price drops. I am working to take into account in the forecast, let's say, the "human factor". It is planned to implement this by collecting, for example, articles from websites, by analyzing which you can find a hint of the possibility of sudden changes in the course and add this data to the equation.

By the way, all this time I was talking about the bot, but I never presented it to you. A special account has been opened for him, coin_instinct . You can tvitnut him a request for a forecast.

Dear readers! Do you develop bots for twitter or predict cryptocurrency rates? If yes, please share your experience.

Source: https://habr.com/ru/post/345590/


All Articles