首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >异步twitter调用在异步队列- https://github.com/DeviaVir/zenbot中的函数中。

异步twitter调用在异步队列- https://github.com/DeviaVir/zenbot中的函数中。
EN

Stack Overflow用户
提问于 2018-08-30 01:34:38
回答 1查看 76关注 0票数 0

系统信息

  • 编写了自定义代码(而不是使用zenbot香草):我添加了一个策略和库文件。该策略使用lib文件从Twitter中提取并计算其情绪。
  • 操作系统平台和发行版(例如LinuxUbuntu16.04):Ubuntu16.04
  • Zenbot版本(提交参考,或版本):v4.1.0
  • Zenbot分公司:不稳定
  • NodeJS版本:v8.11.2
  • Python版本(使用python脚本时):Python2.7.12,3.5.2
  • 精确命令复制(包括一切):./zenbot.sh交易--纸张--策略sentiment_strat --为期2天--周期=1000万
  • 我对conf-sample.js做了什么修改吗?:没有使用-**Extra库- twitter npm - v1.7.1,vader--**Extra -v1.1.3

描述这个问题

问题是,一旦twitter拉出,Zenbot将继续打印相同的BTC值。它似乎没有退出策略中的onPeriod()函数。支持退出Twitter是一项功能请求,因为这将允许用户执行情感分析或执行其他类型的分析来进行交易。我已经接近让它发挥作用了,但我仍然无法解释为什么会发生这种情况。我假设这与我使用异步函数的事实有关--我必须这样做,因为如果我不等待返回值,感情就会根据一个未定义的值来计算。

在调用main()之后,我在onPeriod()函数中放置了一个onPeriod(),但是正如您从errorlog.txt中看到的那样,它没有到达这个点--即使main()已经完成,并且已经执行了是否持有、购买或出售的决定,它仍然被困在main()中--参见第20、21、22行(第23行是相同BTC价格的重复输出,直到我退出该过程)。

在运行调试器并看到它在async_hooks.js和net.js中不断循环之后,我发现onPeriod()函数是在async.queue()中调用的。在另一个异步调用中进行异步调用是否有问题?或者是别的什么东西?

源代码/错误日志

文件位置: zenbot/extensions/strategy/sentiment_strat/strategy.js

代码语言:javascript
复制
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
   }
}

文件位置: zenbot/lib/getTweets.js

代码语言:javascript
复制
 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))
       });
    });
}

文件位置:zenbot/lib/CompuateSentiment.js

代码语言:javascript
复制
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

     } 
 }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-09-16 15:22:37

事实证明,这是一个问题,不包括一个特定的旗帜。必须包括--最小期限作为标志。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52088085

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档