Example #1
0
def getStockList(force_update=False):
    if force_update:
        twstock.__update_codes()

    stocks = []
    for code in twstock.codes:
        stock = twstock.codes[code]
        if stock.type == '股票':
            stocks.append(stock)

    LOGGER.info(f'Get {len(stocks)} stocks in list')

    return stocks
Example #2
0
def genStockList(output_file=None, force_update=False):
    if force_update:
        twstock.__update_codes()

    stocks = []
    for code in twstock.codes:
        stock = twstock.codes[code]
        if stock.type == '股票':
            # stocks.append(f'{code},{stock.name}')
            # stocks.append((code, stock.name))
            stocks.append(code)

    LOGGER.info(f'Get {len(stocks)} stocks in list')
    return sorted(stocks)
Example #3
0
def runUpdate():
    print("Update all stocks start!")
    df = pd.DataFrame()
    #end_date = datetime.today() - timedelta(days=1)#設定資料起訖日期
    ts.__update_codes()
    yf.pdr_override()
    for key, value in ts.codes.items():
        if value.market == "上市" and len(value.code) >= 4:
            if len(value.code) >= 5 and get_stock_history.check_ETF_stock(
                    value.code) == False:
                continue
        #    if int(value.code) < 9000:
        #        continue
        # try:
        #     deleteStockDayTable(str(value.code+".TW"))
        # except:
        #     print("SQL No Table:" + str(value.code+".TW"))

        #SQL沒資料抓取一整包
        # yf.pdr_override()
            start_date = datetime(2005, 1, 1)
            end_date = datetime.today()  #設定資料起訖日期
            df = data.get_data_yahoo([value.code + info.local_type.Taiwan],
                                     start_date,
                                     end_date,
                                     index_col=0)
            if df.empty:
                print("yahoo no data:" +
                      str(value.code + info.local_type.Taiwan))
                continue
            df.to_sql(name=value.code + info.local_type.Taiwan,
                      con=MySql_server.engine,
                      if_exists='replace')
            get_stock_history.load_memery[value.code +
                                          info.local_type.Taiwan] = df
            print("Update stocks " + value.code + info.local_type.Taiwan +
                  " OK!")

    #dataframe = pd.read_sql(sql = "2330.TW",con=MySql_server.engine,index_col='Date')
    #print(dataframe)
    #存更新日期
    get_stock_info.Update_date = str(datetime.today())[0:10]
    get_stock_info.Save_Update_date()
    print("Update all stocks end!")
import string
import time

import twstock

twstock.__update_codes()


def is_stock(stock_code: string):
    return stock_code in twstock.codes


class Stock_API:
    def __init__(self, stock_code: string):
        self.stock_code = stock_code

    def get_stock(self):
        time.sleep(2)  # 防止 IP 遭到證交所 Ban 掉
        stock_data = twstock.realtime.get(self.stock_code)
        return Stock_Data(stock_data)

    def get_history_price(self, year: int, month: int):
        time.sleep(2)  # 防止 IP 遭到證交所 Ban 掉
        stock_data = twstock.Stock(self.stock_code)
        return stock_data.fetch_from(year, month)


class Stock_Data:
    def __init__(self, stock_data: dict):
        self.stock_data = stock_data
Example #5
0
from datetime import datetime, timedelta
from numpy import true_divide
import pandas as pd
import get_stock_history
import get_stock_info
import tools
import twstock as ts  #抓取台灣股票資料套件

ts.__update_codes()


class data_user_stock():  #手持股票資訊
    def __init__(self, stock_info, amount, price):
        self.stock_info = stock_info
        self.amount = amount
        self.price = price

    def add_amount(self, amount, price):
        self.price = ((self.price * self.amount) +
                      (price * amount)) / (self.amount + amount)
        self.amount = self.amount + amount

    def minus_amount(self, amount):
        if self.amount > amount:
            self.amount = self.amount - amount
            return True
        elif self.amount < amount:
            print(str(self.stock_info.number) + '數量不足!')
        self.amount = 0
        return False