Day 36 - Intermediate+ Stock Trading News Alert Project

本章由 Dot 分享,筆記連結網址於此。由 Sky 取得同意後,整理於下方,方便大家參考與討論。

時間:2021年5月23日 20:30~21:00
與會人員:Dot, Wayne, 玉米, Shadow, Yeh, Sky
分享:Dot

學習目標

Stock Trading News Alert Project

上課筆記

#316 介紹今日目標

#317 選擇難度

#318 程式講解 - 1

#319 程式講解 - 2

#320 程式講解 - 3

程式碼

import requests

STOCK = "TSLA"
COMPANY_NAME = "Tesla Inc"

## STEP 1: Use https://www.alphavantage.co
# When STOCK price increase/decreases by 5% between yesterday and the day before yesterday then print("Get News").
send = False
STOCK_Endpoint = "https://www.alphavantage.co/query"
STOCK_API_KEY = "MJUJXEEZ10UIIATX"
stock_params = {
    "function": "TIME_SERIES_DAILY",
    "symbol": STOCK,
    "apikey": STOCK_API_KEY,
}
response = requests.get(STOCK_Endpoint, params=stock_params)
data = response.json()["Time Series (Daily)"]
first_two_data_list = [value for (key, value) in data.items()][:2]
data1_close_price = float(first_two_data_list[0]["4. close"])
data2_close_price = float(first_two_data_list[1]["4. close"])
if abs(data1_close_price - data2_close_price) > 5:
    print(f"{data1_close_price} {data2_close_price} - Get News")
    send = True

## STEP 2: Use https://newsapi.org
# Instead of printing ("Get News"), actually get the first 3 news pieces for the COMPANY_NAME.

# GET https://newsapi.org/v2/everything?q=Apple&from=2021-05-23&sortBy=popularity&apiKey=API_KEY

if send:
    NEWS_Endpoint = "https://newsapi.org/v2/everything"
    NEWS_API_KEY = "6727c5a9a9e847b6be7f90b7a55ddcdd"
    news_params = {
        "q": COMPANY_NAME,
        "from": [key for (key, value) in data.items()][:1][0],
        "sortBy": "popularity",
        "apiKey": NEWS_API_KEY,
    }

    response2 = requests.get(NEWS_Endpoint, params=news_params)
    top_three_articles = response2.json()["articles"][:3]
    print(top_three_articles)

## STEP 3: Use https://www.twilio.com
# Send a seperate message with the percentage change and each article's title and description to your phone number. 

    chat_id = "756199869"
    token = "1822455768:AAE5FnVMmqITXiASMPG31BCtaTpgqmMGyd4"
    TG_Endpoint = "https://api.telegram.org/bot" + token + "/"
    tg_params = {
        "method": "sendMessage",
        "chat_id": chat_id,
        "text": "",
    }
    for article in top_three_articles:
        tg_params["text"] = f"{STOCK}: {data1_close_price - data2_close_price}\nHeadline: {article['title']}\nBrief: {article['description']}"
        requests.post(TG_Endpoint, params=tg_params)

#Optional: Format the SMS message like this: 
"""
TSLA: 🔺2%
Headline: Were Hedge Funds Right About Piling Into Tesla Inc. (TSLA)?. 
Brief: We at Insider Monkey have gone over 821 13F filings that hedge funds and prominent investors are required to file by the SEC The 13F filings show the funds' and investors' portfolio positions as of March 31st, near the height of the coronavirus market crash.
or
"TSLA: 🔻5%
Headline: Were Hedge Funds Right About Piling Into Tesla Inc. (TSLA)?. 
Brief: We at Insider Monkey have gone over 821 13F filings that hedge funds and prominent investors are required to file by the SEC The 13F filings show the funds' and investors' portfolio positions as of March 31st, near the height of the coronavirus market crash.
"""