To anyone who asks I always say that a great feature of ProfitView is its flexibility. It’s an algorithmic trading platform, but you can do anything you want with it. You can build anything you want. You can automate anything you want. You can use it for anything you want.
In order to demonstrate this flexibility, I decided to build a bot that trades on news. It had to be simple and easy to understand but compelling. It had to be possible to build it into a sophisticated trading strategy.
In this blog, I’ll walk you through my thinking in creating this bot. I’ll show you how I built it and give you the code. You can use it as a template for your own, more serious, news bot.
The system I’ll show you is just a starting point, but it reads the news in real time and generates trading signals that can make money — all in less than 25 lines of code.
Thinking Through the Problem
Before settling on a particular implementation, I wanted to explore my options for both news sources and analysis. The journey was as important as the result, and it’s worth sharing how I arrived at a simple working solution.
I wanted a bot that would predict the direction and strength of feeling a typical trader would have about a particular coin as they were about to make a trade. Surely the news they had been reading would have a strong effect on them. I needed a quantitaive measure of this feeling. I wanted it to be simple, so that it could be deployed quickly. It needed to run quickly, so that it could be used in real time. I also wanted it to be free (or very cheap).
Evaluating News Sources
For a trading bot based on news analysis, the first challenge is finding a reliable and timely source of news. Here’s what I explored:
1. Scraping Google News
My first thought was to use Google News. Its algorithm matches my objectives: Google keeps track of what people are looking at and pushes the most relevant forward.
However, scraping it directly came with challenges:
- Dynamic Content: Google News articles are dynamically loaded using JavaScript, requiring tools like Selenium or Playwright. Using them would be complex and slow.
- Rate Limits: Frequent scraping risks triggering CAPTCHAs or being blocked.
- Overhead: Managing headless browsers introduces complexity.
2. News APIs
Next, I explored structured news APIs, such as:
- NewsAPI.org: Offers keyword-based queries but has strict limits on free usage and paid plans are expensive.
- GDELT API: Free and comprehensive, but in my attempt to use it I found it was not returning any results. Possibly overloaded with requests?
3. RSS Feeds
I recalled the existance of RSS feeds. Then I remembered that Google News itself had an RSS feed. This should give me a consistent, free - and fully legitimate - source of news. I found a simple Python library - feedparser - to parse the RSS feed. This seemed like a great solution - at least for now.
Determining Sentiment
With the news source in place, the next step was to find a quantitative measure of trade direction and strength. What I was looking for is termed “sentiment”. My objective was to run a trading bot - of course using our product ProfitView. The “Position” bot-type seemed the most appropriate. It takes a number between -1.0 and 1.0 and uses that to determine the size and direction of trades. -1.0 is a strong sell, 0.0 is neutral and 1.0 is a strong buy.
My first thought was to use an LLM - and I have a subscription to OpenAI. Its “mini” models looked right since they are designed for this kind of task: quick repetitive analysis of short text. But I thought I should also look at dedicated sentiment analysis systems.
I experimented with the VADER Sentiment Analysis and TextBlob libraries. I was initially impressed. Vader was very quick and its default output was a score between -1.0 and 1.0. TextBlob was also quick. But their mechanisms score without context. Looking at some of the results, I saw language that would reasonably be interpreted as negative, but in the context for Bitcoin it was actually positive.
For example, looking at these headlines:
Headline | Vader | TextBlob | GPT |
---|---|---|---|
Microsoft Shareholders To Vote On Bitcoin Investment Proposal | 0.0 | 0.0 | 0.7 |
Behind Bitcoin's Rally Is a Simple Fact: Supplies Are Limited | -0.23 | -0.16 | 1.0 |
ETFs holding bitcoin are now the crypto's largest holders, surpassing creator Satoshi Nakamoto | 0.0 | 0.0 | 0.7 |
El Salvador to scale back bitcoin dreams to seal $1.3bn IMF deal | 0.402 | 0.0 | -0.7 |
The Microsoft vote is clearly an opportunity for Bitcoin. However, Vader and TextBlob both interpret it as neutral. The limitation of supply is a positive for Bitcoin - but Vader and TextBlob interpret this as negative. Similarly, big new holdings by ETFs are a positive and El Salvador scaling back for an IMF deal is clearly negative. In those cases, Vader and TextBlob can’t see the context and get it wrong.
There are a difficulties using OpenAI however. It is not free. Using the latest mini model cost fractions of a cent per million tokens, this is not bad. But it is also relatively slow. My compromise was to collate the headlines into a single string and send it to the model. This is a little less accurate, but it’s fast and cheap.
Deploying the Bot
We use ProfitView to deploy and run the bot. You can do this yourself by signing up here:
This makes it easy to deploy and manage. Here’s the code - also available on GitHub:
from profitview import Link, logger, cron
from openai import OpenAI
from dotenv import load_dotenv
import feedparser
import os
load_dotenv()
class Signals(Link):
OPENAI_CLIENT = OpenAI(api_key=os.getenv('OPENAI_API_KEY'))
@cron.run(on='1m')
def check_news(self):
self.signal('bitmex', 'XBTUSD', size=self.predict('Bitcoin'))
def predict(self, coin):
feed = feedparser.parse(
f"https://news.google.com/rss/search?q={coin}&tbs=qdr:h")
google_feed_query = f"""
Assess these news headlines as they pertain to
cryptocurrency {coin}, taking into account their publication
date and time for relevance. Provide a single floating point
number between -1.0 and 1.0, with -1.0 signifying extreme
negativity, 0.0 neutrality and 1.0 extreme positivity.
Provide only the number and no other text.
"""
google_feed_query += "".join([
f"""Published: {entry.published}. Headline: {entry.title}
""" for entry in feed.entries])
response = self.OPENAI_CLIENT.chat.completions.create(
model = "gpt-4o-mini",
response_format={ "type": "text"},
messages=[{"role": "system",
"content": "You are a cryptocurrency trading expert."},
{"role": "user", "content": google_feed_query}])
prediction = float(response.choices[0].message.content)
logger.info(f"{prediction=}")
return prediction
Because of the use of additional libraries, it is necessary to sign up to a ProfitView Active Trader account. With that you can ssh
into your container and pip install
those libraries (feedparser
and openai
). I also use dotenv
to load the OpenAI API key from the environment.
This bot is actually available now on BitMEX - you can use it from this link. This may require you to be whitelisted. Email support@bitmex.com if you want to try it.
Trading with the Bot
Immediately on deployment, the bot was fetching news, analyzing sentiment, and executing trades. How did it do? In normal situations, it followed the news effectively, and tended to maintain positive PnL. However when there was big news, such as with the surprise interest in IMF loans in El Salvador, it was too slow to pick up the change in mood. For this reason it is important to deploy it with a stop-loss (and likely a take-profit).
On deployment to BitMEX, the stop-loss can be configured in the bot configuration.
While this version is a starting point, it demonstrated the power of automating news-based trading signals.
What I Learned
- Start Simple: Automating trading doesn’t require complex infrastructure. Sometimes, a minimal solution can work well.
- Iterate Fast: By testing different news sources and sentiment models, I quickly identified what worked and discarded what didn’t.
- AI Strengths: OpenAI’s GPT models excelled at understanding nuanced sentiment, making them ideal for this use case.
What next?
To improve this bot, I plan to:
- Backtest: by reviewing the historical Bitcoin price vs news sentiment at the time, it is possible to validate the approach and test variations.
- Calibrate: it should be possible to train the model to be more accurate based on historical data.
- More news sources: rather than just Google News, I will add more news sources and more specific crypto-financial ones.
- Better sentiment analysis: LLMs work well, however there is some diversity in the ways they can be applied. I will experiment with more sophisticated sentiment analysis.
- Integrate with other trading bots: using news based signals in conjunction with other trading bots should improve the results.
Get the Code
You can explore the full project, including my Jupyter notebook experiments, in the GitHub repository:
GitHub Repository: ProfitView News Droid
Conclusion
This project is a testament to how accessible AI and automation have become for traders. With less than 25 lines of code, we turned real-time news into actionable trading signals. While this is just the beginning, it opens the door to more advanced strategies and insights.
Let’s shape the future of trading together.