Using Technical Analysis Indicators to Send Buy-or-Sell Trading Signals to a Chatroom – DZone AI | xxxUsing Technical Analysis Indicators to Send Buy-or-Sell Trading Signals to a Chatroom – DZone AI – xxx
菜单

Using Technical Analysis Indicators to Send Buy-or-Sell Trading Signals to a Chatroom – DZone AI

四月 3, 2020 - MorningStar

Over a million developers have joined DZone.

  • Using Technical Analysis Indicators to Send Buy-or-Sell Trading Signals to a Chatroom - DZone AI

    {{node.title}}

    {{node.type}} · {{ node.urlSource.name }} by

    Download {{node.downloads}}

  • {{totalResults}} search results

{{announcement.body}}

{{announcement.title}}

Let’s be friends:

1024)” dc-slot=”ads.sl1.slot(articles[0], 0)” tags=”ads.sl1.tags(articles[0], 0)” size=”ads.sl1.size(articles[0], 0)” style=”border:0;”>
1 && !articles[0].partner.isSponsoringArticle && (width > 1024)” dc-slot=”ads.sb2.slot(articles[0], 0)” tags=”ads.sb2.tags(articles[0], 0)” size=”ads.sb2.size(articles[0], 0)”>

Using Technical Analysis Indicators to Send Buy-or-Sell Trading Signals to a Chatroom

DZone ‘s Guide to

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.

Using Technical Analysis Indicators to Send Buy-or-Sell Trading Signals to a Chatroom - DZone AI by

CORE ·

May. 12, 20 · AI Zone ·

Free Resource

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
Java

 

x
 

1

def SMA(close,sPeriod,lPeriod):

2

    shortSMA = ta.SMA(close,sPeriod)

3

    longSMA = ta.SMA(close,lPeriod)

4

    smaSell = ((shortSMA <= longSMA) & (shortSMA.shift(1) >= longSMA.shift(1)))

5

    smaBuy = ((shortSMA >= longSMA) & (shortSMA.shift(1) <= longSMA.shift(1)))

6

    return smaSell,smaBuy,shortSMA,longSMA

The resultant smaSell and smaBuy Series will contain the date/time and a flag to indicate a signal e.g. for a daily interval

Java

 

xxxxxxxxxx
1

 

1

Date

2

2018-02-15    False

3

2018-02-16    False

4

    shortSMA = ta.SMA(close,sPeriod)

0

5

    shortSMA = ta.SMA(close,sPeriod)

1

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.

Java

 

    shortSMA = ta.SMA(close,sPeriod)

2

1

 

1

    shortSMA = ta.SMA(close,sPeriod)

3

2

    shortSMA = ta.SMA(close,sPeriod)

4

3

    shortSMA = ta.SMA(close,sPeriod)

5

4

    shortSMA = ta.SMA(close,sPeriod)

6

5

    shortSMA = ta.SMA(close,sPeriod)

7

As per my SMA function, my RSI function also returns a Series containing date/time and a flag to indicate buy/sell signals

Stochastics

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.

Java

 

    shortSMA = ta.SMA(close,sPeriod)

8

1

 

1

    shortSMA = ta.SMA(close,sPeriod)

9

2

    longSMA = ta.SMA(close,lPeriod)

0

3

    longSMA = ta.SMA(close,lPeriod)

1

4

    longSMA = ta.SMA(close,lPeriod)

2

5

    longSMA = ta.SMA(close,lPeriod)

3

6

    longSMA = ta.SMA(close,lPeriod)

4

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.

Java

 

    longSMA = ta.SMA(close,lPeriod)

5

1

18

 

1

    longSMA = ta.SMA(close,lPeriod)

6

2

    longSMA = ta.SMA(close,lPeriod)

7

3

    longSMA = ta.SMA(close,lPeriod)

8

4

    longSMA = ta.SMA(close,lPeriod)

9

5

    smaSell = ((shortSMA <= longSMA) & (shortSMA.shift(1) >= longSMA.shift(1)))

0

6

    smaSell = ((shortSMA <= longSMA) & (shortSMA.shift(1) >= longSMA.shift(1)))

1

7

    smaSell = ((shortSMA <= longSMA) & (shortSMA.shift(1) >= longSMA.shift(1)))

2

8

    smaSell = ((shortSMA <= longSMA) & (shortSMA.shift(1) >= longSMA.shift(1)))

3

9

    smaSell = ((shortSMA <= longSMA) & (shortSMA.shift(1) >= longSMA.shift(1)))

4

10

    smaSell = ((shortSMA <= longSMA) & (shortSMA.shift(1) >= longSMA.shift(1)))

5

11

    smaSell = ((shortSMA <= longSMA) & (shortSMA.shift(1) >= longSMA.shift(1)))

6

12

    smaSell = ((shortSMA <= longSMA) & (shortSMA.shift(1) >= longSMA.shift(1)))

7

13

    smaSell = ((shortSMA <= longSMA) & (shortSMA.shift(1) >= longSMA.shift(1)))

8

14

    smaSell = ((shortSMA <= longSMA) & (shortSMA.shift(1) >= longSMA.shift(1)))

9

15

    smaBuy = ((shortSMA >= longSMA) & (shortSMA.shift(1) <= longSMA.shift(1)))

0

16

    smaBuy = ((shortSMA >= longSMA) & (shortSMA.shift(1) <= longSMA.shift(1)))

1

17

    smaBuy = ((shortSMA >= longSMA) & (shortSMA.shift(1) <= longSMA.shift(1)))

2

18

    smaBuy = ((shortSMA >= longSMA) & (shortSMA.shift(1) <= longSMA.shift(1)))

3

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.

Java

 

    smaBuy = ((shortSMA >= longSMA) & (shortSMA.shift(1) <= longSMA.shift(1)))

4

1

30

 

1

    smaBuy = ((shortSMA >= longSMA) & (shortSMA.shift(1) <= longSMA.shift(1)))

5

2

    smaBuy = ((shortSMA >= longSMA) & (shortSMA.shift(1) <= longSMA.shift(1)))

6

3

    smaBuy = ((shortSMA >= longSMA) & (shortSMA.shift(1) <= longSMA.shift(1)))

7

4

    smaBuy = ((shortSMA >= longSMA) & (shortSMA.shift(1) <= longSMA.shift(1)))

8

5

    smaBuy = ((shortSMA >= longSMA) & (shortSMA.shift(1) <= longSMA.shift(1)))

9

6

    return smaSell,smaBuy,shortSMA,longSMA

0

7

    return smaSell,smaBuy,shortSMA,longSMA

1

8

    return smaSell,smaBuy,shortSMA,longSMA

2

9

    return smaSell,smaBuy,shortSMA,longSMA

3

10

    return smaSell,smaBuy,shortSMA,longSMA

4

11

    return smaSell,smaBuy,shortSMA,longSMA

5

12

    return smaSell,smaBuy,shortSMA,longSMA

6

13

    return smaSell,smaBuy,shortSMA,longSMA

7

14

    return smaSell,smaBuy,shortSMA,longSMA

8

15

    return smaSell,smaBuy,shortSMA,longSMA

9

16

xxxxxxxxxx

0

17

xxxxxxxxxx

1

18

xxxxxxxxxx

2

19

xxxxxxxxxx

3

20

xxxxxxxxxx

4

21

xxxxxxxxxx

5

22

xxxxxxxxxx

6

23

xxxxxxxxxx

7

24

xxxxxxxxxx

8

25

xxxxxxxxxx

9

26

Date

0

27

Date

1

28

Date

2

29

Date

3

30

Date

4

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…

Java

 

Date

5

1

12

 

1

Date

6

2

Date

7

3

Date

8

4

Date

9

5

2018-02-15    False

0

6

2018-02-15    False

1

7

2018-02-15    False

2

8

2018-02-15    False

3

9

2018-02-15    False

4

10

2018-02-15    False

5

11

2018-02-15    False

6

12

2018-02-15    False

7

Plotting Functions

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?

Java

 

2018-02-15    False

8

1

56

 

1

2018-02-15    False

9

2

2018-02-16    False

0

3

2018-02-16    False

1

4

2018-02-16    False

2

5

2018-02-16    False

3

6

2018-02-16    False

4

7

2018-02-16    False

5

8

2018-02-16    False

6

9

2018-02-16    False

7

10

2018-02-16    False

8

11

2018-02-16    False

9

12

    shortSMA = ta.SMA(close,sPeriod)

00

13

    shortSMA = ta.SMA(close,sPeriod)

01

14

    shortSMA = ta.SMA(close,sPeriod)

02

15

    shortSMA = ta.SMA(close,sPeriod)

03

16

    shortSMA = ta.SMA(close,sPeriod)

04

17

    shortSMA = ta.SMA(close,sPeriod)

05

18

    shortSMA = ta.SMA(close,sPeriod)

06

19

    shortSMA = ta.SMA(close,sPeriod)

07

20

    shortSMA = ta.SMA(close,sPeriod)

08

21

    shortSMA = ta.SMA(close,sPeriod)

09

22

    shortSMA = ta.SMA(close,sPeriod)

10

23

    shortSMA = ta.SMA(close,sPeriod)

11

24

    shortSMA = ta.SMA(close,sPeriod)

12

25

    shortSMA = ta.SMA(close,sPeriod)

13

26

    shortSMA = ta.SMA(close,sPeriod)

14

27

    shortSMA = ta.SMA(close,sPeriod)

15

28

    shortSMA = ta.SMA(close,sPeriod)

16

29

    shortSMA = ta.SMA(close,sPeriod)

17

30

    shortSMA = ta.SMA(close,sPeriod)

18

31

    shortSMA = ta.SMA(close,sPeriod)

19

32

    shortSMA = ta.SMA(close,sPeriod)

20

33

    shortSMA = ta.SMA(close,sPeriod)

21

34

    shortSMA = ta.SMA(close,sPeriod)

22

35

    shortSMA = ta.SMA(close,sPeriod)

23

36

    shortSMA = ta.SMA(close,sPeriod)

24

37

    shortSMA = ta.SMA(close,sPeriod)

25

38

    shortSMA = ta.SMA(close,sPeriod)

26

39

    shortSMA = ta.SMA(close,sPeriod)

27

40

    shortSMA = ta.SMA(close,sPeriod)

28

41

    shortSMA = ta.SMA(close,sPeriod)

29

42

    shortSMA = ta.SMA(close,sPeriod)

30

43

    shortSMA = ta.SMA(close,sPeriod)

31

44

    shortSMA = ta.SMA(close,sPeriod)

32

45

    shortSMA = ta.SMA(close,sPeriod)

33

46

    shortSMA = ta.SMA(close,sPeriod)

34

47

    shortSMA = ta.SMA(close,sPeriod)

35

48

    shortSMA = ta.SMA(close,sPeriod)

36

49

    shortSMA = ta.SMA(close,sPeriod)

37

50

    shortSMA = ta.SMA(close,sPeriod)

38

51

    shortSMA = ta.SMA(close,sPeriod)

39

52

    shortSMA = ta.SMA(close,sPeriod)

40

53

    shortSMA = ta.SMA(close,sPeriod)

41

54

    shortSMA = ta.SMA(close,sPeriod)

42

55

    shortSMA = ta.SMA(close,sPeriod)

43

56

    shortSMA = ta.SMA(close,sPeriod)

44

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).

Java

 

    shortSMA = ta.SMA(close,sPeriod)

45

1

 

1

    shortSMA = ta.SMA(close,sPeriod)

46

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.

Java

 

    shortSMA = ta.SMA(close,sPeriod)

47

1

12

 

1

    shortSMA = ta.SMA(close,sPeriod)

48

2

    shortSMA = ta.SMA(close,sPeriod)

49

3

    shortSMA = ta.SMA(close,sPeriod)

50

4

    shortSMA = ta.SMA(close,sPeriod)

51

5

    shortSMA = ta.SMA(close,sPeriod)

52

6

    shortSMA = ta.SMA(close,sPeriod)

53

7

    shortSMA = ta.SMA(close,sPeriod)

54

8

    shortSMA = ta.SMA(close,sPeriod)

55

9

    shortSMA = ta.SMA(close,sPeriod)

56

10

    shortSMA = ta.SMA(close,sPeriod)

57

11

    shortSMA = ta.SMA(close,sPeriod)

58

12

    shortSMA = ta.SMA(close,sPeriod)

59

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.

Java

 

    shortSMA = ta.SMA(close,sPeriod)

60

1

 

1

    shortSMA = ta.SMA(close,sPeriod)

61

2

    shortSMA = ta.SMA(close,sPeriod)

62

3

    shortSMA = ta.SMA(close,sPeriod)

63

4

    shortSMA = ta.SMA(close,sPeriod)

64

5

    shortSMA = ta.SMA(close,sPeriod)

65

6

    shortSMA = ta.SMA(close,sPeriod)

66

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.

Java

 

    shortSMA = ta.SMA(close,sPeriod)

67

1

 

1

    shortSMA = ta.SMA(close,sPeriod)

68

2

    shortSMA = ta.SMA(close,sPeriod)

69

3

    shortSMA = ta.SMA(close,sPeriod)

70

4

    shortSMA = ta.SMA(close,sPeriod)

71

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

Java

 

    shortSMA = ta.SMA(close,sPeriod)

72

1

 

1

    shortSMA = ta.SMA(close,sPeriod)

73

2

    shortSMA = ta.SMA(close,sPeriod)

74

3

    shortSMA = ta.SMA(close,sPeriod)

75

4

    shortSMA = ta.SMA(close,sPeriod)

76

5

    shortSMA = ta.SMA(close,sPeriod)

77

6

    shortSMA = ta.SMA(close,sPeriod)

78

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.

Java

 

    shortSMA = ta.SMA(close,sPeriod)

79

1

 

1

    shortSMA = ta.SMA(close,sPeriod)

80

2

    shortSMA = ta.SMA(close,sPeriod)

81

3

    shortSMA = ta.SMA(close,sPeriod)

82

4

    shortSMA = ta.SMA(close,sPeriod)

83

5

    shortSMA = ta.SMA(close,sPeriod)

84

6

    shortSMA = ta.SMA(close,sPeriod)

85

7

    shortSMA = ta.SMA(close,sPeriod)

86

8

    shortSMA = ta.SMA(close,sPeriod)

87

9

    shortSMA = ta.SMA(close,sPeriod)

88

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).

Java

 

    shortSMA = ta.SMA(close,sPeriod)

89

1

25

 

1

    shortSMA = ta.SMA(close,sPeriod)

90

2

    shortSMA = ta.SMA(close,sPeriod)

91

3

    shortSMA = ta.SMA(close,sPeriod)

92

4

    shortSMA = ta.SMA(close,sPeriod)

93

5

    shortSMA = ta.SMA(close,sPeriod)

94

6

    shortSMA = ta.SMA(close,sPeriod)

95

7

    shortSMA = ta.SMA(close,sPeriod)

96

8

    shortSMA = ta.SMA(close,sPeriod)

97

9

    shortSMA = ta.SMA(close,sPeriod)

98

10

    shortSMA = ta.SMA(close,sPeriod)

99

11

    longSMA = ta.SMA(close,lPeriod)

00

12

    longSMA = ta.SMA(close,lPeriod)

01

13

    longSMA = ta.SMA(close,lPeriod)

02

14

    longSMA = ta.SMA(close,lPeriod)

03

15

    longSMA = ta.SMA(close,lPeriod)

04

16

    longSMA = ta.SMA(close,lPeriod)

05

17

    longSMA = ta.SMA(close,lPeriod)

06

18

    longSMA = ta.SMA(close,lPeriod)

07

19

    longSMA = ta.SMA(close,lPeriod)

08

20

    longSMA = ta.SMA(close,lPeriod)

09

21

    longSMA = ta.SMA(close,lPeriod)

10

22

    longSMA = ta.SMA(close,lPeriod)

11

23

    longSMA = ta.SMA(close,lPeriod)

12

24

    longSMA = ta.SMA(close,lPeriod)

13

25

    longSMA = ta.SMA(close,lPeriod)

14

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.

Java

 

    longSMA = ta.SMA(close,lPeriod)

15

1

46

 

1

    longSMA = ta.SMA(close,lPeriod)

16

2

    longSMA = ta.SMA(close,lPeriod)

17

3

    longSMA = ta.SMA(close,lPeriod)

18

4

    longSMA = ta.SMA(close,lPeriod)

19

5

    longSMA = ta.SMA(close,lPeriod)

20

6

    longSMA = ta.SMA(close,lPeriod)

21

7

    longSMA = ta.SMA(close,lPeriod)

22

8

    longSMA = ta.SMA(close,lPeriod)

23

9

    longSMA = ta.SMA(close,lPeriod)

24

10

    longSMA = ta.SMA(close,lPeriod)

25

11

    longSMA = ta.SMA(close,lPeriod)

26

12

    longSMA = ta.SMA(close,lPeriod)

27

13

    longSMA = ta.SMA(close,lPeriod)

28

14

    longSMA = ta.SMA(close,lPeriod)

29

15

    longSMA = ta.SMA(close,lPeriod)

30

16

    longSMA = ta.SMA(close,lPeriod)

31

17

    longSMA = ta.SMA(close,lPeriod)

32

18

    longSMA = ta.SMA(close,lPeriod)

33

19

    longSMA = ta.SMA(close,lPeriod)

34

20

    longSMA = ta.SMA(close,lPeriod)

35

21

    longSMA = ta.SMA(close,lPeriod)

36

22

    longSMA = ta.SMA(close,lPeriod)

37

23

    longSMA = ta.SMA(close,lPeriod)

38

24

    longSMA = ta.SMA(close,lPeriod)

39

25

    longSMA = ta.SMA(close,lPeriod)

40

26

    longSMA = ta.SMA(close,lPeriod)

41

27

    longSMA = ta.SMA(close,lPeriod)

42

28

    longSMA = ta.SMA(close,lPeriod)

43

29

    longSMA = ta.SMA(close,lPeriod)

44

30

    longSMA = ta.SMA(close,lPeriod)

45

31

    longSMA = ta.SMA(close,lPeriod)

46

32

    longSMA = ta.SMA(close,lPeriod)

47

33

    longSMA = ta.SMA(close,lPeriod)

48

34

    longSMA = ta.SMA(close,lPeriod)

49

35

    longSMA = ta.SMA(close,lPeriod)

50

36

    longSMA = ta.SMA(close,lPeriod)

51

37

    longSMA = ta.SMA(close,lPeriod)

52

38

    longSMA = ta.SMA(close,lPeriod)

53

39

    longSMA = ta.SMA(close,lPeriod)

54

40

    longSMA = ta.SMA(close,lPeriod)

55

41

    longSMA = ta.SMA(close,lPeriod)

56

42

    longSMA = ta.SMA(close,lPeriod)

57

43

    longSMA = ta.SMA(close,lPeriod)

58

44

    longSMA = ta.SMA(close,lPeriod)

59

45

    longSMA = ta.SMA(close,lPeriod)

60

46

    longSMA = ta.SMA(close,lPeriod)

61

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 outputDF dataframe – showing the first few trading signals for BAE Systems (BAES.L)
  • the tail of the outputDF dataframe which represents the final few signals for 3I Group (III.L).

Simple Moving Average chart for 3I Group PLC — III.L

Using Technical Analysis Indicators to Send Buy-or-Sell Trading Signals to a Chatroom - DZone AI

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

Using Technical Analysis Indicators to Send Buy-or-Sell Trading Signals to a Chatroom - DZone AI

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

Using Technical Analysis Indicators to Send Buy-or-Sell Trading Signals to a Chatroom - DZone AI

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)

Using Technical Analysis Indicators to Send Buy-or-Sell Trading Signals to a Chatroom - DZone AI
Head of my output summary table for BAES.L

The tail of the Summary table — showing the most recent trading signals for 3I Group (III.L)

0

 Advanced issue found

 

Using Technical Analysis Indicators to Send Buy-or-Sell Trading Signals to a Chatroom - DZone AI

Note how the final few signals for 3I group compare with the crossover points on the charts

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.

Java

 

    longSMA = ta.SMA(close,lPeriod)

62

1

14

 

1

    longSMA = ta.SMA(close,lPeriod)

63

2

    longSMA = ta.SMA(close,lPeriod)

64

3

    longSMA = ta.SMA(close,lPeriod)

65

4

    longSMA = ta.SMA(close,lPeriod)

66

5

    longSMA = ta.SMA(close,lPeriod)

67

6

    longSMA = ta.SMA(close,lPeriod)

68

7

    longSMA = ta.SMA(close,lPeriod)

69

8

    longSMA = ta.SMA(close,lPeriod)

70

9

    longSMA = ta.SMA(close,lPeriod)

71

10

    longSMA = ta.SMA(close,lPeriod)

72

11

    longSMA = ta.SMA(close,lPeriod)

73

12

    longSMA = ta.SMA(close,lPeriod)

74

13

    longSMA = ta.SMA(close,lPeriod)

75

14

    longSMA = ta.SMA(close,lPeriod)

76

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 start_date and 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.

Closing Summary

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:

0

 Advanced issue found

 

0

 Advanced issue found

 

0

 Advanced issue found

 

Using Technical Analysis Indicators to Send Buy-or-Sell Trading Signals to a Chatroom - DZone AI

Example Buy and Sell Signal in the Chatroom
Note: For the purpose of the above screen capture, I did a run with the interval set to ‘ minute’ — in an attempt to generate signals sooner rather than later.

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.

Downlaods

Source for this workflow on GitHub
Source for Messenger ChatBot example on GitHub

Like This Article? Read More From DZone

Topics:
artificial intelligence ,chatrooms ,matplotlib ,python ,stock market trading tips ,tutorial

Opinions expressed by DZone contributors are their own.

AI Partner Resources

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.linkDescription }}

{{ parent.urlSource.name }}

by

CORE

· {{ parent.articleDate | date:’MMM. dd, yyyy’ }} {{ parent.linkDate | date:’MMM. dd, yyyy’ }}


Notice: Undefined variable: canUpdate in /var/www/html/wordpress/wp-content/plugins/wp-autopost-pro/wp-autopost-function.php on line 51