2 minute read

Tags: ,

最近又開始有點感受到 小低潮 XD

需求不明 :laughing:

阿自己也沒有很用心的認真去 分析客戶+業務到底要啥 XDDD

:neutral_face: :star2: :sparkles:

我自己的問題 :bowtie:

靠著 潛力無窮的正向循環 :stuck_out_tongue_closed_eyes:

玩一點可愛的 :poop:Project 來找回 Second Wind :heart:

好~~~ :notes:

問了老姊 有甚麼需求~~ 讓偶可以大顯神威~~~~ :tada:

美國股市(特定股票) 的 30/60/90 最高 :chart_with_upwards_trend: & 最低點 :chart_with_downwards_trend: 的 MA :black_nib:

哈哈哈哈哈 我先玩了 台股 然後就繼續下去搂~~~ YA!

GOGOGO 我的 SECOND WIND 起風起風起風~~~ :sunny::sunny::sunny::v::v::v:

丟到 line bot 上 自己都感覺很好玩 XDDD :heart_eyes:

生活就是這樣

有高有低有起有落

繼續當小瘦瘦八~~~~ :balloon::balloon::balloon:

import urllib3
import json
import pytz
from datetime import datetime, timedelta
import time


def process_stock_data(result):
    response_string = result.data.decode('utf-8')
    res_json = json.loads(response_string)
    return res_json


def get_pre_five_month(this_month):
    this_month_map = {
        1: {'pre_year': ['08', '09', '10', '11', '12'], 'this_year': []},
        2: {'pre_year': ['09', '10', '11', '12'], 'this_year': ['01']},
        3: {'pre_year': ['10', '11', '12'], 'this_year': ['01', '02']},
        4: {'pre_year': ['11', '12'], 'this_year': ['01', '02', '03']},
        5: {'pre_year': ['12'], 'this_year': ['01', '02', '03', '04']},
        6: {'pre_year': [], 'this_year': ['01', '02', '03', '04', '05']},
        7: {'pre_year': [], 'this_year': ['02', '03', '04', '05', '06']},
        8: {'pre_year': [], 'this_year': ['03', '04', '05', '06', '07']},
        9: {'pre_year': [], 'this_year': ['04', '05', '06', '07', '08']},
        10: {'pre_year': [], 'this_year': ['05', '06', '07', '08', '09']},
        11: {'pre_year': [], 'this_year': ['06', '07', '08', '09', '10']},
        12: {'pre_year': [], 'this_year': ['07', '08', '09', '10', '11']},
    }
    return this_month_map[this_month]


class StockFactory:
    def __init__(self):
        self.http = urllib3.PoolManager()
        self.final_list = []
        # init da

    def get_stock(self, date, stock_no):
        """
        5
        秒內不能存取超過3次
        超過會鎖IP
        鎖的時間不一定
        至少20分鐘起跳
        """
        a = self.http.request('GET',
                         'https://www.twse.com.tw/exchangeReport/STOCK_DAY?response=json&date={}&stockNo={}'.format(
                             date, stock_no))
        time.sleep(5)
        return a

    def process_date_list(self):
        # taiwan time
        c_time = datetime.now(pytz.utc) + timedelta(hours=8)
        this_year = c_time.year
        pre_year = int(c_time.year) - 1
        pre_five_month = get_pre_five_month(c_time.month)
        # pre year
        if len(pre_five_month['pre_year']) != 0:
            for m in pre_five_month['pre_year']:
                date_str = '{}{:02d}01'.format(pre_year, int(m))
                self.final_list.append(date_str)

        # this year
        if len(pre_five_month['this_year']) != 0:
            for m in pre_five_month['this_year']:
                date_str = '{}{:02d}01'.format(this_year, int(m))
                self.final_list.append(date_str)
        # this moment
        self.final_list.append('{}{:02d}{:02d}'.format(c_time.year, c_time.month, c_time.day))
        return self.final_list

    def get_30_60_90_data(self, date_list, stock_no):
        stock_data = []
        for date in date_list:
            r = self.get_stock(date, stock_no)
            r_json = process_stock_data(r)
            try:
                stock_data.extend(r_json['data'])
            except:
                return None

        last_90 = stock_data[len(stock_data) - 90:]
        last_60 = stock_data[len(stock_data) - 60:]
        last_30 = stock_data[len(stock_data) - 30:]
        """
           "日期",
           "成交股數",
           "成交金額",
           "開盤價",
           "最高價",
           "最低價",
           "收盤價",
           "漲跌價差",
           "成交筆數"
           """
        # indx 4: 最高價 , index 5: 最低價
        return {
            30: {'h': sum([float(d[4].replace(',', '')) for d in last_30]) / 30, 'l': sum([float(d[5].replace(',', '')) for d in last_30]) / 30},
            60: {'h': sum([float(d[4].replace(',', '')) for d in last_60]) / 60, 'l': sum([float(d[5].replace(',', '')) for d in last_60]) / 60},
            90: {'h': sum([float(d[4].replace(',', '')) for d in last_90]) / 90, 'l': sum([float(d[5].replace(',', '')) for d in last_90]) / 90}
        }