系统信息
描述这个问题
问题是,一旦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
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
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
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
}
}发布于 2018-09-16 15:22:37
事实证明,这是一个问题,不包括一个特定的旗帜。必须包括--最小期限作为标志。
https://stackoverflow.com/questions/52088085
复制相似问题