Skip to content
This repository has been archived by the owner on Feb 15, 2022. It is now read-only.

Connect and pull Tweets from Twitter API during run time. #1689

Open
sachag678 opened this issue Aug 23, 2018 · 0 comments
Open

Connect and pull Tweets from Twitter API during run time. #1689

sachag678 opened this issue Aug 23, 2018 · 0 comments

Comments

@sachag678
Copy link

sachag678 commented Aug 23, 2018

System information

  • Have I written custom code (as opposed to using zenbot vanilla): I have added a strategy and lib files. The strategy uses the lib files to make a pull from the Twitter API and calculate the sentiment of it.
  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Ubuntu 16.04
  • Zenbot version (commit ref, or version): v4.1.0
  • Zenbot branch:unstable
  • NodeJS version:v8.11.2
  • Python version (when using a python script): python 2.7.12, 3.5.2
  • Exact command to reproduce (include everything):
    ./zenbot.sh trade --paper --strategy sentiment_strat --days 2 --period=10m
  • Did I make any changes to conf-sample.js?: No
    -**Extra libraries used - twitter npm - v1.7.1, vader-sentiment -v1.1.3

Describe the problem

The issue is that once the twitter API pull is made the Zenbot will keep printing the same BTC value. It doesn't seem to exit past the onPeriod() function in the strategy. Having the support of pulling from Twitter is a feature request because this would allow users to perform sentiment analysis or perform other types of analysis to make trades. I am close to making it work but I'm stuck on why this is happening. I'm assuming it has something to do with the fact that I'm using an async function - I have to do this because if I don't wait for the return value, the sentiment will be calculated on an undefined value.

I have placed a console.log() in the onPeriod() function after I call main() but as you see from the errorlog.txt, it doesn't get to that point - it is stuck inside main() even though main() has completed and the determination of whether to hold, buy or sell has been performed - See lines 20, 21, 22 (Line 23 is the repeating output of the same BTC price until I exited the process.)
errorlog.txt

Source code / Error logs

file location: zenbot/extensions/strategy/sentiment_strat/strategy.js

let sentiment = require('../../../lib/calculateSentiment'),
twit = require('../../../lib/getTweets.js'),
n = require('numbro'),
z = require('zero-fill'),
getTweetsBoolean = true

module.exports = {
name: 'sentiment_strat',
description: 'Strategy based on sentiment.',

getOptions: function () {
    this.option('period', 'period length, same as --period_length', String, '10m')
},

calculate: function (s) {

    if (s.in_preroll) {
        return
    }




},

onPeriod: function (s, cb) {
    if (s.in_preroll) {
        cb()
        return
    }

    async function main(getTweetsBoolean) {
        number_of_tweets = 100
        pulled_tweets = await twit('BTC', number_of_tweets)
        console.log('pulled_tweets')
        sentiment(s, 'sentiment', pulled_tweets)

        console.log('Sentiment: ' + s.period.sentiment)

        if (s.period.sentiment > 0.1) {
            console.log('buy')
            s.signal = 'buy'
        } else if (s.period.sentiment < -0.1) {
            console.log('sell')
            s.signal = 'sell'
        } else {
            console.log('hold')
        }
        return

    }

    main()
    console.log('onPeriod After main()')
    cb()
},

onReport: function (s) {
    var cols = []
    return cols
   }
}

file location: zenbot/lib/getTweets.js

var Twitter = require('twitter')

module.exports = function getTweets(keyphrase, number_of_tweets) {

function parseTweets(tweets) {
var parsed_tweets = []
list_of_unparsed_tweets = tweets['statuses']
for (let i = 0; i < list_of_unparsed_tweets.length; i++) {
	num_followers = list_of_unparsed_tweets[i]['user']['followers_count']
	user = list_of_unparsed_tweets[i]['user']['screen_name']
    parsed_tweets.push({"tweet":filter(list_of_unparsed_tweets[i]                                                                          
  ['full_text']),"num_followers":num_followers, "user": user})
}
  return parsed_tweets
}

function filter(tweet){
return tweet.replace(/["']/g,'').replace(/[()*.,:;]/g,'').replace(/\W\s/g,'')
}

//create twitter client
var client = new Twitter({
    consumer_key: '',
    consumer_secret: '',
    access_token_key: '',
    access_token_secret: ''
});

//perform the get request based on the given keyphrase
return new Promise(function(resolve, reject){
client.get('search/tweets', {
    q: keyphrase,
	count: number_of_tweets,
	tweet_mode: 'extended',
	lang : 'en'
}, function (error, tweets, response) {
    if(error){
		console.log(error)
		return
	}
	resolve(parseTweets(tweets))
       });
    });
}

file location: zenbot/lib/calculateSentiment.js

 var vader = require('vader-sentiment')

module.exports = function calculateSentiment(s, key, texts) {
if (s.lookback == null || s.period == null) {
 s.period['key'] = ''
} else {
 let totalFollowers = 0
 for (let j = 0; j < texts.length; j++) {
   totalFollowers += texts[j]['num_followers']
 }
 let totalSentiment = 0
 for (let i = 0; i < texts.length; i++) {
   let weighted_normalized_sentiment = vader.SentimentIntensityAnalyzer.polarity_scores(texts[i] 
 ['tweet'])['compound']*texts[i]['num_followers']/totalFollowers
   totalSentiment += weighted_normalized_sentiment 
 }
 s.period[key] = totalSentiment / texts.length

     } 
 }
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants