Core Tutorial: Creating a Crypto Twitter Sentiment Trading Signal


Hey everyone, Matthew from Streamr here. In the last few videos we’ve created an Ethereum price watcher and then we
created this trading signal which essentially ‘buys the dip’ if there is one
in the last 15 seconds. So in this tutorial we are going to add crypto
Twitter sentiment into this trading signal to make it a bit smarter. So let’s
go ahead and go into our streams and let’s create a new stream called ETH
sentiment. This is all we need to do we’re going to copy the stream ID
because we’ll need that later but I’ll just hit save and exit. And the
next thing we will do is let’s start up let’s create a node script. So we
can run NPM in it and we’ll run through these defaults – they don’t matter.
And we will want to install these two packages ‘StreamrClient’ and ‘Sentiment’. So this
is StreamrClient here. You can find it on NPM. And this is just a random
sentiment node.js based sentiment analyser you can use whatever you want
but this one it works for me. Great. So we will open our code editor now. And I’ll
create a new file. Scripty dot js I will this was the stream ID that I
copied from before I will just make note of that, that this is the sentiment
stream ID. And if we go back we can copy and paste the sentiment initialiser. Great. Now let’s just double check that it works as expected running node scripty dot js. So you will need NPM and Node. I’m running on the Mac environment. You can run it on Windows as well you just need
those prerequisites set up. So the sentiment library returns a comparative
value which looks like the best one for us. So we’ll keep that in mind as we
progress so the next thing we want to do is subscribe to the Twitter raw data feed from the Marketplace So let’s go ahead and find that.
So I’ve already searched for Twitter and I’m gonna run into this product here and
I’m after the Ethereum Tweets stream. I’m gonna view the live data. This looks
good I after this text. This is what we’ll run
the sentiment analyzer on. I am going to copy the stream ID this icon in the in
the top right. We will call this the Twitter stream ID. Great, okay. And now
we should consult the Streamr client documentation over here. We can go
straight into the repository and into the examples folder. We’re running in
node. And we want this node example ‘Subscribe’. This is really good
boilerplate that we can again copy and paste. And so, okay. So I need an API key
as well so that’s something we need to grab. So I’ll go back to the browser I’ve added this. And go to my profile menu to
API keys. From here I’m going to click on the hamburger menu and copy the key. And I’ll paste it in. Great. Okay so let’s modify what we copy and
pasted. So we don’t need this get or create stream. We already have the stream. All we need is this client subscribe actually. So let’s trim around. And we
need to replace this stream ID with Twitter stream ID. Remove this
console.log. And so what this will do is it will subscribe to our Twitter stream
you don’t need to resend actually and it will console.log the message so we know
that we want the text of the Tweet so we’ll add that text to make things a bit
nicer, go back to my terminal and I will run my trusty Scripty. Great. So now we
have the tweets come in. All that’s left to do is run the sentiment analyser over
it and then push it to a new stream. So let’s go ahead and copy this line. This
is what we want. The result equals sentiment dot analyze. Cats are not stupid. We are going to use message dot text instead and now we need to publish this to our
Sentiment stream ID. So this is you can consult the docs but this is quite
simple it’s just instead of client dot subscribe its
client dot publish this time. And it requires as the
first argument your ID and the second argument is the value but there’s one
gotcha in that, we can’t put the value directly in here it needs to be an
object. So let’s do that now. And what should we call this? We should probably
call this Sentiment. So now let’s run through this script we’ve created a
Streamr client. It’s subscribed to our Twitter stream
that we saw on the Marketplace and for every message that we receive, every
message that’s detected on this stream it’s going to run a sentiment analyzer
on this and it’s going to publish that result to this new stream. Now
there’s one thing I forgot. That’s to add comparative because in the sentiment
library, here we go, the returned object we want this comparative score. It seems
like the most sensible thing to use anyway. So I will save. I will cross my
fingers and run the script again! Okay so with any luck we can now go back to the
Core app and click on our sentiment stream and great! Okay so our sentiment
scores are coming through. That’s exactly what we wanted. So for each tweet we
have calculated a sentiment and we’ve pushed it to the stream. So let’s go
forward. We’re going to go back to our canvases to our Ethereum trading signal
canvas. I can stop it now because we need to make some additions to it. So the
first thing we’ll do is we will add our sentiment stream that we just created
and we’re going to need some additional modules so that we can make sense of
this. So we’ll need a GreaterThan and we will need and ‘And’ module. So we’ll plug
in the sentiment into the A input of GreaterThan and we’ll do it so that as
long as the sentiment is say positive so above zero – a very low bar – we’ll set this
to true. And we can also add a moving average because we want to not make, this is quite frivolous but we want to make sure that the trading signal is not
based on the very last tweet. We can say make it so that it’s based on the last
three tweets for example. So I just need to switch this order around actually
so the sentiment comes into our moving average the window length let’s say
three tweets in our sample size three tweets. Check the output to the a of the
GreaterThan input and keep B as zero. And now we should disconnect our
existing trading signal input. Disconnect all, right click ‘disconnect all’, and we’re
gonna put this And simple And module in between so our LessThan still comes in
to the And and our new GreaterThan sentiment analyzer will come into the
B input and this And module if A will output true, if A and B are true if
either are false, the output will be false. So this looks good to me
and now we can connect it all up. And before I hit start I will just add a
chart and a table. So these are good to have, just to kind of see how the data is
moving and also to use as a dashboard which I’ll show you in a moment.
And now because this output is a boolean we can’t connect it directly to a chart
but lucky for us there is a converter module called BooleanToNumber. We just
need to pass through this value. So now 0 is false and 1 is true so it can be
charted. So I’m gonna hit start and wait a few moments. So this will take some
time to get the minimum number of events necessary to go through the delay and go
through the moving average. I’ll come back to that. In Dashboards what we can
do is we can basically pick out our visualisations from our running
canvases so if I click our module or our Dashboard
browser we can find our canvas. It was called ETH Trading Signal and we can
choose our chart, and we can choose our table. So this is our trading signal and
this is the trading signal visualized in a chart so whenever the chart shows 1, it’s a buy signal. And when it’s 0 it will be a don’t buy I guess. And that pretty much
concludes our trading signal. So we’ve now I think created a pretty
interesting trading signal. This will just kind of run forever as long as the
canvas is running. Just to recap we have in this in the top half, this is buy the
dip sort of logic and the bottom half is monitoring the crypto Twitter sentiment
and if it’s positive over the last three tweets then we kind of pass on this
thumbs up to this And module which combines those two pieces of information and makes a decision whether or not to advise a trade. There you go
please do NOT trade this this bot! I’m sure it’ll lose you a lot of money but
you can kind of see where this is going. All these parameters can be
configured. You can make some really interesting scalping bots or monitoring
certain prices and yeah you can create new products on the Marketplace.
If you’re trading signals are good you can put a price on your data. You can extract from a Marketplace product and enhance it and repackage it into a new product because You can do a lot of things in the
Core app and I I really encourage experimentation. And please share
your canvases. Please share your results! And I wish you the best and good luck
and thanks for watching!

Leave a Reply

Your email address will not be published. Required fields are marked *