1. Day 36 Goals: what you will make by the end of the day

  2. Choose Your Destiny!

  3. Solution & Walkthrough for Step 1 - Check for Stock Price Movements

  4. Solution & Walkthrough for Step 2 - Get the News Articles

  5. Solution & Walkthrough for Step 3 - Send the SMS Messages

316. Day 36 Goals: what you will make by the end of the day

  1. stock new monitoring project
  2. stock price + news Alerts create a Bloomberg terminal function

317. Choose Your Destiny!

  1. Normal, Hard, or Extra Hard and choose your level → go by normal to learn first

Untitled

318. Solution & Walkthrough for Step 1 - Check for Stock Price Movements

  1. get stock API Key and take daily data
import requests #用API request.get

#常數設定 / 取得stock API/ News API endpoint 
STOCK_NAME = "TSLA"
COMPANY_NAME = "Tesla Inc"

STOCK_ENDPOINT = "<https://www.alphavantage.co/query>"
NEWS_ENDPOINT = "<https://newsapi.org/v2/everything>"

Stock_API_Key = done
News_API_Key = done

## STEP 1: Use <https://www.alphavantage.co/documentation/#daily>
# When stock price increase/decreases by 5% between yesterday and the day before yesterday then print("Get News").

#TODO 1. - Get yesterday's closing stock price. Hint: You can perform list comprehensions on Python dictionaries. e.g. [new_value for (key, value) in dictionary.items()]

# 先設定要取得公司的相關參數/fuctions 要抓daily 資料/公司名/api/ (最主要是參數stock API documentation)
stock_params = {
    "function": "TIME_SERIES_DAILY",
    "symbol": STOCK_NAME,
    "apikey": Stock_API_Key,

}

#用request.get 取得API 端點資料 (STOCK_ENDPOINT參數用網址常數先寫好如上,params 用字典結構把用到的參數寫入)
response = requests.get(STOCK_ENDPOINT, params=stock_params)
print(response.json())

#設定下一階_取得daily 資料 (注意要跟json 資料結構命名方式一致)
data = response.json()["Time Series (Daily)"]

data_list = [ value for (key, value) in data.items()]  #直接使用dict 抓資料很困難,因此改用list方式解決--> list comprehension /
yesterday_data = data_list[0] # 抓list 第一列
yesterday_closing_price = yesterday_data["4. close"] # 抓昨天的收盤價price
print(yesterday_closing_price) # print 收盤價
#這部分最難的是如何想到用list comprehension -> D26 study again

#TODO 2. - Get the day before yesterday's closing stock price

day_before_yesterday_data = data_list[1] #前天資料_資料序列是倒序所以[1]是指前一天
day_before_yesterday_closing_price = day_before_yesterday_data["4. close"] # 取得前一天的第四項 close(要照網站命名方式寫)
print(day_before_yesterday_closing_price)

#TODO 3. - Find the positive difference between 1 and 2. e.g. 40 - 20 = -20, but the positive difference is 20. Hint: <https://www.w3schools.com/python/ref_func_abs.asp>
difference = abs(float(yesterday_closing_price) - float(day_before_yesterday_closing_price))
print(difference) #取得差異值

#TODO 4. - Work out the percentage difference in price between closing price yesterday and closing price the day before yesterday.

difference_percentage = float(difference) / float(yesterday_closing_price) *100
print(difference_percentage) #取得百分比

#TODO 5. - If TODO4 percentage is greater than 5 then print("Get News").

if difference_percentage > 5:
    print("get news")