When TEJ API meets Line Notify

Sending the important data information to your phone through Line Notify

After building our own database through TEJ API and understanding how to use Window Task Scheduler to update the database regularly a few weeks ago. Today we are going to teach you how to make it more convenient. That is through the Line Notify to automatically import the data we want into our Line!

Highlights of this article

  • Line Notify Intro
  • Line Notify Apply
  • Line Notify + TEJ API

Links related to this article

Line Notify Intro

Line Notify is also an API. Like TEJ API that allows users to interact with the TEJ database with Python, Line Notify allows Python to interact with Line. Therefore, the application can be widely used, from the web crawler to find the price of the thing you want to the financial data when the conditions are met. It just purely depends on what kind of data or information that the users need.

Line Notify Apply

We can apply the API KEY through Line Notify Official WebsiteAfter logging in, you can see a button called Login Service and click it. You will see the following fields and fill in the information and you will see similar to the following picture. Among these fields, the service URL and Callback URL are both http://127.0.0.1, and the remaining fields are your own information.

After filling all the fields and sending out the application, a verification letter will be sent to your email. Then you will see the webpage in the picture below:

Now Line has given us a Client ID. Then you can find a button called “Personal Page” through the red box on the upper right, then find the “Issuance Token” at the bottom of the page. After clicking the button, Then you will see the webpage in the picture below:

This webpage will ask users about the chat room where users want to receive notifications. In addition to yourself, you need to use groups if you want to send information to multiple people!

After confirming the chat room you want to send, click release, you can get our Token (similar to API KEY), as shown in the figure:

Now let’s coding~~

Line Notify + TEJ API

Import Packages

Let’s first import the packages which will be used in our code.

import tejapi 
import pandas as pd
import numpy as np
import datetime
import matplotlib.pyplot as plt
tejapi.ApiConfig.api_key = "your key"
tejapi.ApiConfig.ignoretz = True

Getting and organizing the data through the TEJ API

Then get the data we want from the TEJ API. Here we use 2330 and 2303 as examples. You can replace the symbols you want!

ticks = ['2330', '2303', '1101']
DailyPrice= tejapi.get('TWN/EWPRCD',coid=ticks,
                        opts={'columns':['coid', 'mdate', 'open_d',  'high_d','low_d', 'close_d']},
                        mdate={'gte':'2020-05-01','lte':'2021-05-25'}, paginate=True, )
DailyPrice= DailyPrice.set_index('mdate')

After collecting the data, we do some simple calculations, such as the 5-day moving average, monthly moving average, and the rate of daily return. Because TEJ will update the latest data at 2:30 pm after the closing of each day, don’t worry about you will use the wrong data after this time!

MovingAvg_5D = {}
MovingAvg_20D = {}
DailyRt = {}
for ticker in ticks:
    MovingAvg_5D[ticker] = DailyPrice[DailyPrice['coid']==ticker]['close_d'].rolling(5).mean()
    MovingAvg_20D[ticker] = DailyPrice[DailyPrice['coid']==ticker]['close_d'].rolling(20).mean()
    DailyRt[ticker] = DailyPrice[DailyPrice['coid']==ticker]['close_d'].pct_change()*100

Connecting to the Line Notify

Next is to connect to our Line Notify. We first set the Token we got above and write a function for Line Notify. The internal parameters of the function do not need to be changed. The main purpose of the function is to send the information we want to our mobile phones through Line Notify.

token = "your token"
def LineNotify(params, token):
    
    headers = {
        "Authorization": "Bearer " + token,
        "Content-Type": "application/x-www-form-urlencoded"
        }
    r = requests.post("https://notify-api.line.me/api/notify", headers=headers, params=params)
    print(r.status_code)

The next step is to find the data we want from the indicators and set the conditions. Here we use if the five-day moving average or monthly moving average goes up as the signal. In addition to returning the current moving average price, we reply with the latest close price and the daily rate of return together.

import requests
params = []
for ticker in ticks:
    
    if MovingAvg_20D[ticker][-1] > MovingAvg_20D[ticker][-2] and MovingAvg_5D[ticker][-1] > MovingAvg_5D[ticker][-2]:

        params.append('\n' + ticker + ':\n' 
                      + "五日均線往上為: " + str(round(MovingAvg_5D[ticker][-1],2)) + '\n' 
                      + "月均線往上為: " + str(round(MovingAvg_20D[ticker][-1],2)) + '\n' 
                      + "日報酬率為: " + str(round(DailyRt[ticker][-1],2)) + '%' + '\n' 
                      + "當前價格: " + str(round(DailyPrice[DailyPrice['coid']==ticker]['close_d'][-1])) + '\n')
    
    elif MovingAvg_20D[ticker][-1] > MovingAvg_20D[ticker][-2]:

        params.append('\n' + ticker + ':\n' 
                      + "月均線往上為: " + str(round(MovingAvg_20D[ticker][-1],2)) + '\n' 
                      + "日報酬率為: " + str(round(DailyRt[ticker][-1],2)) + '%' + '\n' 
                      + "當前價格: " + str(round(DailyPrice[DailyPrice['coid']==ticker]['close_d'][-1])) + '\n')
    
    elif MovingAvg_5D[ticker][-1] > MovingAvg_5D[ticker][-2]:

        params.append('\n' + ticker + ':\n' 
                      + "五日均線往上為: " + str(round(MovingAvg_5D[ticker][-1],2)) + '\n'  
                      + "日報酬率為: " + str(round(DailyRt[ticker][-1],2)) + '%' + '\n' 
                      + "當前價格: " + str(round(DailyPrice[DailyPrice['coid']==ticker]['close_d'][-1])) + '\n')  
    
params = {'message': params}
LineNotify(params, token)

After everything is completed, we can see that our Line Notify will send the data which meets the condition we set earlier to our Line!!

Conclusion

Today’s content is mainly extended from Building your own database through TEJ API. With Line Notify, we can directly send the information we want to our mobile phones every day. Today, we only use the moving average and daily return for demonstration. You can also try your own indicators, such as technical indicators, moving average breakthroughs, or just want to know the latest closing price every day, etc. In this way, it saves our time to check everything one by one!

Finally, if you like this topic, please click 👏 below, giving us more support and encouragement. Additionally, if you have any questions or suggestions, please leave a message or email us, we will try our best to reply to you.👍👍

Links related to this article again!

Back
Procesing