Using Technical Analysis Indicators to Send Buy-or-Sell Trading Signals to a Chatroom
Using Technical Analysis Indicators to Send Buy-or-Sell Trading Signals to a Chatroom
In this article, see how you can use technical analysis indicators to send buy-or-sell trading signals to a chatroom on an ongoing basis.
Join the DZone community and get the full member experience.Join For Free
In this article, I will look at how I can use technical analysis indicators to send buy-or-sell trading signals to a chatroom on an ongoing basis — removing the need to keep eyeballing Technical Analysis charts constantly.
According to Investopedia ‘Technical Analysis is a trading discipline employed to evaluate investments and identify trading opportunities by analyzing statistical trends gathered from trading activity, such as price movement and volume’. It further states ‘technical analysts focus on patterns of price movements, trading signals … to evaluate a security’s strength or weakness’.
According to the various articles etc that I read, three of the more commonly used TA indicators are
- Simple Moving Averages
- Relative Strength Index
- Stochastic Oscillator
Investors and traders who use Technical Analysis as part of their trading strategy may refer to TA charts regularly to help determine their investment choices. So, it would be great if instead of constantly having to interrogate the charts, they could just receive a message when a Buy or Sell condition arises.
In this workflow, I am going to experiment with how I can use these indicators to generate Buy or Sell trading signals. There are several articles on the internet that attempt the same thing — often focusing on a single indicator — and most of these run the analysis on historical data, present the historical results, and stop there.
However, I want to take this a step further by continuing to run the analysis on an ongoing basis at a configured interval — e.g. every minute, hour, day (I did look at using realtime tick data as well but it was pointed out that this does not make much sense from a technical analysis perspective — as the ticks do not occur at regular interval).
Furthermore, when a trading signal is generated I will use a chat BOT to post the signal details into a chat room notifying the users — saving the effort of frequently interrogating the charts.
I have access to the Refinitiv Eikon desktop application so I will be using its Data API to access the data I require - however, it should be relatively straightforward to use your own source of equivalent data and associated API- as a substitute for the Eikon stuff.
I will also use symbology conversion functions to convert from ISINs to RIC (Reuters Instrument Codes) for requesting the various data - as the above API generally requires RIC codes for most of the data functions.
I also need to send messages when a trading signal is generated - so I will use the Refinitiv Messenger BOT API to post a message to an Eikon Messenger Chatroom. No doubt this could easily be replaced with some other form of messaging such as WhatsApp, email, SMS, etc.
Before we go any further I should mention that I am relatively new to the Eikon Data API and to Python itself — so you may come across some ‘not very Pythonic’ ways of doing things. If you find some instances which border on sacrilege (in the Python world) please let me know and I will try and amend the offending code (I am working on the
lower_case_with_underscores naming convention - but as a long time Java/C++ developer, not there yet!).
TA-Lib: Technical Analysis Library
When I started on this, I was using various Python scripts/snippets I found online for calculating my indicators and then wondering just how accurate they may truly be. After spending (wasting?) some considerable time testing and fretting about the veracity of these snippets, a colleague mentioned that there already existed a Python wrapper — ta-lib — for the well known Technical Analysis Library — TA-Lib. Of course — this being Python there would have to be a library for it (remember — I am a Python noob)!
Import our libraries
I think I should mention the versions of some of the key libraries I have installed — just in case you have any issues:
- eikon - 1.1.3a0
- pandas - 1.0.0
- numpy - 1.18.1
- talib - 0.4.17
- matplotlib - 3.1.3
I have used an alpha version of the
eikon library because I wanted to test it & also because I wanted to eliminate the
pandas.np deprecation warnings . If you are an Eikon user and have the official v1.1.2 (at the time of writing) installed it should still work fine and you could just
import warnings library and use
warnings.filterwarnings("ignore") to suppress them.
If you decide to build the TA-Lib binaries (rather than download the prebuilt ones) pay attention to the instructions on moving the folder — otherwise, you may be left scratching your head (like I did) as to why it won't build properly!
To post messages to the RM Chatroom I am using the existing Messenger BOT API example MessengerChatBot.Python from GitHub — full details are provided on the site.
Key Code Snippets
As there is a considerable amount of code involved, I will be omitting much of the code here and mostly showing only key code snippets — please refer to the GitHub repository for the full source code.
So, let me crack on with the code — in the following order:
- various helper functions for the Technical Analysis, timing & chart plotting
- the main control section
- finishing with the ongoing analysis loop.
Simple Moving Averages
This function uses the TA-Lib SMA function to calculate the Simple Moving Average using the Close price for two periods — which you will note later are 14 for the short period and 200 for the long period. As you will see later, the period interval itself can vary e.g minute, daily, monthly, hourly — so for example, calculate SMA for 14 days and 200 days.
With the calculated SMAs, it then uses the following logic to generate Buy and Sell signals:
- If the short period SMA crosses up through the long period SMA then this is a buy signal
- If the short period SMA crosses down through the long period SMA then this is a sell signal
The resultant smaSell and smaBuy Series will contain the date/time and a flag to indicate a signal e.g. for a daily interval
Relative Strength Index
RSI calculation is usually done for a 14 day period — so once again I feed in the Close price for the instrument to the TA-Lib RSI function. The common methodology is to set high and low thresholds of the RSI at 70 and 30. The idea is that if the lower threshold is crossed, the asset is becoming oversold and we should buy. Conversely, if the upper threshold is crossed then the asset is becoming overbought and we should sell.
As per my SMA function, my RSI function also returns a Series containing date/time and a flag to indicate buy/sell signals
The TA-Lib Stoch function returns two lines slowk and slowd which can then be used to generate the buy/sell indicators. A crossover signal occurs when the two lines cross in the overbought region (commonly above 80) or oversold region (commonly below 20). When a slowk line crosses below the slowd line in the overbought region it is considered a sell indicator. Conversely, when an increasing slowk line crosses above the slowd line in the oversold region it is considered a buy indicator.
Sending a Trade Signal to the Chat Room
I need a way of letting users know that a Trade signal has been generated by the Technical Analysis. So, I am going to use the Messenger BOT API to send messages to other Refinitiv Messenger users. I am re-purposing the existing MessengerChatBot.Python example from GitHub.
As mentioned previously, it should be fairly straightforward to replace this with your choice of messaging API - essentially we are just posting a message with a name of the Instrument and whether a Buy or Sell signal has been generated.
FYI: the acronym ‘RIC’ stands for Reuters Instrument Code — so, for example, the code ‘VOD.L’ represents Vodafone from LSE.
Run the Technical Analysis
Initially, I will do a historical TA run, after which I will use this function to run the above 3 TA methodologies on the data I get as part of the ongoing Technical Analysis.
I am going to repeat some of this code later in the main historical TA run loop — purely for ease of reading.
If the timestamp of the final TA signal, matches the timestamp of the most recent data point — then we have one or more new trade signal(s) — so inform the Chatroom users via the Chat BOT.
Timing Helper Functions
I also need a few helper functions to calculate some time values based on the selected interval — the main one is…
Whilst not essential to the workflow, I wanted to plot a few charts to provide a visual representation of the various TA indicators — so we can try and visually tie-up instances where a price rises or drops in line with a TA trade signal — so for example when the short SMA crosses up through the long SMA, do we see an upward trend in the price after that point in time?
So, that’s the helper functions out of the way — let’s move on the main control section
Connecting to the Eikon Application
To connect to my running instance of Eikon I need to provide my Application Key (which can be generated within Eikon using the AppKey Generator app).
Some Initialisation Code
I need to calculate the start and end date for my price query — based on the chosen periodicity/interval, as well as specify the periods for moving averages.
Also, as I will be requesting the price of each instrument individually, I create a container to hold all the price data for the full basket of instruments.
Finally, I set some display properties for the Pandas dataframe.
TA Analysis Summary Output
Once the initial historical TA has been run, I want to present a summary table of the signal over that period.
For this, I am going to use a Dataframe to output the results in a readable format.
I am also creating some blank columns which I will use for padding the dataframe later.
Convert my ISIN symbols to RICs
Whilst Eikon can accept various Symbology types for certain interactions, I need to specify RICs (Reuters Instrument Code) for much of the data I intend to access.
Therefore, I could just start with a bunch of RICs — but not everyone works with RICs — so here is an example of symbology conversion at work.
Snapshot Some Summary Data Values
Once I have run the TA I want to present a summary table reflecting the price changes for each instrument over various periods such as a week, month, year etc — along with the TA results.
So, for this, I am using the
get_data method to obtain various Percent price change values as well the name of the corporate entity and the most recent Closing price
In the above call, I am requesting each instrument’s per cent Price change for Week, Month and Year to Date — and the 6m and 1yr period as well.
Putting It All Together for Our Initial ‘Historical’ Analysis Run
I can now go ahead and request my historical data and perform the Technical Analysis.
As well as interval data at the minute, hour, daily etc Eikon product can also provide tick data — i.e. individual trades as well as Bid//Ask changes. However, there are some differences in the data set for tick data compared to the other intervals:
- Historical tick data will be indexed on the date+time of every tick — and therefore the index can vary across instruments
- Intervalised data will be indexed according to the interval e.g. every minute, hour etc and therefore use the same index values across multiple instruments
- I can get high, low prices for interval data (which is needed for the Stochastic TA) but not for tick data
As you can see, since the tick data is not interval based, it does not make sense from a TA point of view. For interval data, I can specify multiple RICs in the
get_timeseries call and get back a single dataframe with the prices for all the RICs. However, to make the code easier to read, I will loop through the RICs and request each one individually rather than slightly increased subsequent complication involved if I have to use a single dataframe containing all the data.
I have split the following section of code into smaller chunks for ease of reading and annotations.
For each RIC code in our list, the first thing I do is use the
get_timeseries function to request a subset of fields at the specified Interval for the previously calculated time period.
Next, I calculate values for Price Up, Down and no change movements for the analysis period. Then I call the various TA functions to calculate the indicators and generate the signals (& plot charts if enabled).
Finally, I use the data and signals etc to generate a dataframe displaying a Summary Table of the various stats + any signals generated for our configured TA time period and interval.
Once that is done, I simply display the head and tail of the Summary Table.
Before we get to the table, let us have a quick look at the charts I plotted and compare them to the subset of TA signals in the table displayed further down.
Here I have included
- the SMA, RSI and Stochastic charts for the final RIC — III.L
- the head of the
outputDFdataframe - showing the first few trading signals for BAE Systems (BAES.L)
- the tail of the
outputDFdataframe which represents the final few signals for 3I Group (III.L).
Simple Moving Average chart for 3I Group PLC — III.L
Towards the end of the Simple Moving Average chart, you can see that the SMA14 crosses down through the SMA200 line which is a sell indicator — and if you refer to the tail end of the Summary table below you will see an SMA Sell indicator for the 5th of March 2020.
Relative Strength Index chart for 3I Group PLC — III.L
Here I am plotting the Close price in the upper chart and the RSI value in the lower chart. Just at the tail end, you can see that the RSI value crosses below the lower threshold twice around the end of Feb / start of March — which corresponds to the RSI Buy indicator on 2020–03–05 in the Summary table below
Stochastics chart for 3I Group PLC — III.L
Once again I am plotting the Close price in the upper chart and the Stochastics slowk and slowd lines in the lower chart. You can see that the slowk line crosses above the slowd line in the oversold region (below 20) on three occasions — i.e. buy indicators reflected in the summary table below with Stochastic Buy entries for 2020–02–28, 2020–03–03 and 2020–03–13.
Finally, just before the end of the chart, we see the slowk and slowd lines cross in the overbought region (above 80). This is reflected in the table below as a Stochastic Sell entry at 2020–03–27.
Historical Summary Table
After the initial Technical Analysis run using the historical data, I output a historical Summary table with some basic stats as well as the history of the Trade signals for the configured Interval and Time Period.
For the basic stats, I display the Percent change for various periods such as Week to Day, Month to Day, 6 months — to provide an indication of just how the particular stock has been trading over those periods. I also display the number of intervals where the price has gone up, down or no change as potentially useful reference points.
The rightmost columns of the table contain the Buy and Sell signals occurrences for the 3 TA methodologies — one row for each date/time that triggered a signal — you may occasionally see more than one signal in a single row. For example, whilst I was testing both RSI and Stoch indicated Buy signals on 11th October 2018 for the 3i Group (III.L) — concerning this, you may find the Reuters 3 yr chart for III interesting…
Head of the Summary table — showing the first few trading signals for BAE Systems (BAES.L)
The tail of the Summary table — showing the most recent trading signals for 3I Group (III.L)
Ongoing Technical Analysis
Now that I have the historical analysis out of the way, I will move onto the ongoing ‘real-time’ analysis. In other words, the code that I can keep running to perform the TA on an ongoing basis at the configured interval.
I put the script to sleep for our configured interval, and when it wakes I request the latest data points for each RIC.
The simplest approach would be to just make the above
get_timeseries calls with a revised
end_date parameters. However, this would be quite wasteful of resources and so I will request just the latest data point for my configured interval.
To do this, I make the call with
count=1 (rather than Start/End times) to get just the latest data point. I then drop the earliest data point from our historical data points and append the latest one.
I can then invoke the Technical Analysis functions with the most recent data included.
I could now leave this script running with my preferred interval and it will send messages to the chatroom, each time the Technical Analysis yields a Buy or Sell signal, like the ones shown below:
For a trader who uses Simple Moving Average, Relative Strength Indices and/or Stochastic charts to help inform their trading decisions — this could mean they don’t need to actively sit there eyeballing charts all day long.
I hope you found this exercise useful and no doubt there are many possible improvements or refinements that could be made to both the code and the simple TA methodology I have used.
A few examples to consider are:
- In the Summary table extract above I can see that on 2020–03–05, SMA based TA is indicating a Sell whereas the RSI based TA is indicating a Buy — which suggests that further refinements are required
- An RSI related article I read, suggested that once the line crosses a threshold it may be better to wait till it crosses back in the opposite direction before generating a signal — so for example, if the crosses below %30, wait till it crosses back up above 30% before generating a Buy signal.
- In my summary output table, I show the ‘% change’ for a week, month, year etc — which could be relevant for my ‘daily’ interval-based TA — but not so much if say I used an ‘hourly’ interval. It was suggested that a better approach could be to show ‘% change’ values for say, Hour t, Hour t+1, t+5, t+10 etc according to my chosen interval.
- Another improvement could be to show up/down markers on the plot lines highlighting the crossover points.
As you can imagine, there is no shortage of material on Technical Analysis to be found on the internet.
NOTE: I do not endorse the above workflow as something that should be used for active trading — it is purely an educational example.
Opinions expressed by DZone contributors are their own.