Ejemplo n.º 1
0
 def __init__(self, sid: str, ndays: int):
     Stock.__init__(self, sid)
     # Init data
     self.ndays = ndays
     self.realtime = realtime.get(self.sid)
     self.fetch_210()
     self.bias_ratio()
Ejemplo n.º 2
0
def _load_daily_stock_price(stock_id, sleep=1):  #-> Tuple
    stock = Stock(str(stock_id))
    date = stock.date[-1].strftime('%Y/%m/%d')
    low_price = stock.low[-1]
    high_price = stock.high[-1]
    price = stock.price[-1]
    latest_5_mv_avg_price = stock.moving_average(stock.price[-5:], days=5)[0]
    if sleep:
        time.sleep(sleep)
    return (date, price, low_price, high_price, latest_5_mv_avg_price)
Ejemplo n.º 3
0
def fetch(twid='2330', csv="", start_time=None, end_time=datetime.now()):
    if start_time == None:
        start_time = datetime.strptime(start, "2010-1-1")

    if csv == "":
        csv = twid + ".csv"

    stock = Stock(twid)
    stock.fetch_from(int(start_time.strftime("%Y")),
                     int(start_time.strftime("%m")))
    stock_to_googleCSV(stock, csv)
Ejemplo n.º 4
0
def echo(update, context):
    query = update.message.text
    ret = rtProvider.getStockInfo(query)
    result_str = ret['ID'] + " " + ret['Name'] + "的即時股價: " + str(
        ret['RealPrice'])
    context.bot.send_message(chat_id=update.effective_chat.id, text=result_str)
    ret['photo'].seek(0)
    context.bot.send_photo(chat_id=update.effective_chat.id,
                           photo=ret['photo'])
    stock = Stock(query)
    tendayMA_str = ("5日線:%.2f" % stock.moving_average(stock.price, 5)[-1])
    moMA_str = ("月線:%.2f" % stock.moving_average(stock.price, 20)[-1])
    context.bot.send_message(chat_id=update.effective_chat.id,
                             text=tendayMA_str + "\n" + moMA_str)
Ejemplo n.º 5
0
def stock_query(code, temp):
    stock = Stock(code)
    x = twstock.realtime.get(code)
    hour = datetime.now().hour + 8
    if hour < 14 and hour >= 9:
        change = round(
            float(x['realtime']['latest_trade_price']) -
            float(stock.price[-1]), 2)
    else:
        change = round(
            float(x['realtime']['latest_trade_price']) -
            float(stock.price[-2]), 2)

    if change < 0:
        color = '#2d8540'
    elif change > 0:
        color = '#F25702'
    else:
        color = '#111111'
    temp['body']['contents'][2]['contents'][0]['contents'].append(
        component(x['info']['code'], color))
    temp['body']['contents'][2]['contents'][1]['contents'].append(
        component(x['realtime']['latest_trade_price'], color))
    temp['body']['contents'][2]['contents'][2]['contents'].append(
        component(x['realtime']['open'], color))
    temp['body']['contents'][2]['contents'][3]['contents'].append(
        component(change, color))
    temp['body']['contents'][2]['contents'][4]['contents'].append(
        component(x['realtime']['high'], color))
    temp['body']['contents'][2]['contents'][5]['contents'].append(
        component(x['realtime']['low'], color))
    return temp
Ejemplo n.º 6
0
    def download(self): 
        self.result.setPlainText("Start from: " + str(self.StartTime.date().toPyDate())
         + " End to: "+  str(self.EndTime.date().toPyDate()) + '\r\n')
        # print(self.result.getPaintContext())
        stockNo = self.stockNoEdit.text()

        stockItem = None
        for key in self.stockInfo.keys(): 
            for item in self.stockInfo[key]:
                if (item.code == stockNo):
                    stockItem = item
                    print(stockItem)
                    self.dir = "./"+stockItem.group+"/"+stockItem.code+"_"+stockItem.name+ "/"

        if (stockItem == None):
            print("The stock is not exist!")
            return
            

        if re.match(r'^([\d]+)$', stockNo) != None:
            # try:
            proxyProvider = None
            proxy_req_num=3
            stock = Stock(stockNo,False) 
            Text =  self.ProxyList.toPlainText().split()
            # print(self.ProxyList.toPlainText().split())
            proxy_list = []

        #     '''judge if input is an valid ip'''
        for i,proxyip in enumerate(Text):
            regx_ip = re.match(r'^\d+\.\d+\.\d+\.\d+:\d+$',proxyip)
            if( regx_ip  != None and [0<=int(x)<256 for x in re.split('[\.]',re.split(':', regx_ip.group(0))[0]) ].count(True)==4 ):
                proxy_list.append(proxyip)

        proxy_req_num = (len(proxy_list))

        ''' Get data '''
        if (proxy_req_num !=0):
            ''' with proxy'''
            proxyProvider = proxy.RoundRobinProxiesProvider(proxy_list)
            if (proxy_req_num > 3):
                proxy_req_num = 3
            
            
            self.pool.apply_async(async_task, args=(stock,2020,8,proxyProvider,proxy_req_num),callback=self.async_finsh) 
            # 
            # tasks = asyncio.run( stock.async_fetch_from(2020, 3,proxyProvider,proxy_req_num))
        else:
            ''' without proxy'''
            
            i = 0
            while 1:
                date = self.StartTime.date().addMonths(i)
                year = date.year()
                month = date.month()
                self.pool.apply_async(sync_task, args=(stock,year,month,True,),callback=self.finish) 
                if (self.EndTime.date().year() ==  year and self.EndTime.date().month() == month) :
                    break
                i += 1
Ejemplo n.º 7
0
    def complate_data(self, sid):
        ret = False
        last_date = self.date[-1]
        stock = Stock(sid, initial_fetch=False)

        fetch_from_date = mdates.num2date(last_date) + timedelta(-10)
        stock.fetch_from(fetch_from_date.year, fetch_from_date.month)

        col_num = len(self.raw[0])  # csv 檔的 column 數
        append_str = ""
        for index in range(0, col_num - 5):
            append_str = append_str + ",0"

        for index in range(-len(stock.date), 0):
            d = stock.date[index]
            o = stock.open[index]
            h = stock.high[index]
            l = stock.low[index]
            c = stock.close[index]
            if (mdates.date2num(d) > last_date):
                str = "{0},{1},{2},{3},{4}".format(d.strftime('%Y/%m/%d'), o,
                                                   h, l, c)
                print("資料回補:" + str)
                f = open(self.filename, 'a+')
                f.write(str + append_str + "\n")
                f.close()
                ret = True

        #return ret  # no yahoo
        #yahoo 資料確認
#        d,o,h,l,c = self.complete_data_yahoo(sid)
#        #print(d,o,h,l,c)
#        try:
#            if(mdates.date2num(d) > mdates.date2num(stock.date[-1]) and mdates.date2num(d) > last_date ):
#                str = "{0},{1},{2},{3},{4}".format(d.strftime('%Y/%m/%d'),o,h,l,c )
#                print("資料回補 yahoo:" + str)
#                f = open(self.filename, 'a+')
#                f.write(str+append_str+"\n")
#                f.close()
#                ret = True
#        except:
#            print("Error 資料回補 yahoo:" + str)
#            pass
# 有更新資料時,回傳True
        return ret
Ejemplo n.º 8
0
def dump_updated_sid(dir_path, codes):
    """
    stock = Stock('2330')
    ma_p = stock.moving_average(stock.price, 5)       # 計算五日均價
    ma_c = stock.moving_average(stock.capacity, 5)    # 計算五日均量
    ma_p_cont = stock.continuous(ma_p)                # 計算五日均價持續天數
        ma_br = stock.ma_bias_ratio(5, 10)                # 計算五日、十日乖離值
    """

    for sid in codes:
        print('code {}'.format(sid))
        stock = Stock(sid)
        stock.fetch_from(2018, 8)
        # Data(date=datetime.datetime(2017, 5, 2, 0, 0), capacity=45851963, turnover=9053856108, open=198.5, high=199.0, low=195.5, close=196.5, change=2.0, transaction=15718)

        sid2date2data = {}
        for ins in stock.data:
            sys.stdout.write('.')
            date = ins.date.strftime('%Y%m%d')
            open_p = ins.open
            close_p = ins.close
            high = ins.high
            low = ins.low
            change = ins.change
            capacity = ins.capacity
            transaction = ins.transaction
            turnover = ins.turnover
            if sid not in sid2date2data:
                sid2date2data[sid] = {}
            data = (high, low, open_p, close_p, change, capacity, transaction,
                    turnover)
            sid2date2data[sid][date] = data
        for sid in sid2date2data:
            file_path = os.path.join(dir_path, '{}.csv'.format(sid))
            with open(file_path, 'w') as w:
                for date in sorted(sid2date2data[sid].keys()):
                    high, low, open_p, close_p, change, capacity, transaction, turnover = sid2date2data[
                        sid][date]
                    w.write(','.join(
                        map(str, [
                            sid, date, open_p, close_p, high, low, change,
                            capacity, transaction, turnover
                        ])) + '\n')
        print('')
Ejemplo n.º 9
0
def loadHistory(Code, startYear, startMonth, FinalYear, FinalMonth):
    stock = Stock(str(Code))
    monthList = list()
    for year in range(startYear, FinalYear):
        for month in range(1, 13):
            stock.fetch(year, month)

            for index in range(len(stock.data)):
                newItem = list(stock.data[index])
                newDate = newItem.pop(0)
                newItem.insert(0, newDate.strftime("%Y-%m-%d"))
                monthList.append(newItem)
            time.sleep(3)
        print("========================================================")
        print("=                 Load  ", year, "  finish                 =")
        print("========================================================")

    i = 1 if FinalYear > startYear else startMonth
    for month in range(i, FinalMonth + 1):
        stock.fetch(FinalYear, month)
        for index in range(len(stock.data)):
            newItem = list(stock.data[index])
            newDate = newItem.pop(0)
            newItem.insert(0, newDate.strftime("%Y-%m-%d"))
            monthList.append(newItem)
        time.sleep(3)
    print("========================================================")
    print("=                 Load  ", str(Code), "  finish                 =")
    print("========================================================")

    return monthList
Ejemplo n.º 10
0
def test_load_historical_data_months(code, year, begin_month, end_month):
    """
    Goal
    ----------
    Download historical data and basic unit is one month.

    Parameters
    ----------
    code : int or str
    year : int
    begin_month : int
    end_month : int

    TODO
    ----------
    1. Thinl trade suspension case
    2.
    """
    data_list = []
    if begin_month > end_month:
        return data_list

    stock = Stock(str(code))
    print("\n[" + str(code) + "]")
    for month in range(begin_month, end_month+1):
        time.sleep(5)
        stock.fetch(year, month)
        print(str(year) + '/' + str(month) + '   \tfinish')

        for index in range(len(stock.data)):
            new_data = list(stock.data[index])
            new_data[0] = stock.data[index][0].strftime("%Y-%m-%d %H:%M:%S")
            data_list.append(new_data)
        #if (month) --- check is empty?
        # 也有可能是停牌
        # 回傳錯誤,並說明原因
    return data_list
Ejemplo n.º 11
0
def main():
    
    stock = Stock('2330')                             # 擷取
    ma_p = stock.moving_average(stock.price, 5)       # 計算五日均價
    strma_p=str(ma_p)
    ma_c = stock.moving_average(stock.capacity, 5)    # 計算五日均量
    strma_c=str(ma_c)
    ma_p_cont = stock.continuous(ma_p)                # 計算五日均價持續天數
    strma_p_cont=str(ma_p_cont)
    ma_br = stock.ma_bias_ratio(5, 10)                # 計算五日、十日乖離值
    strma_br=str(ma_br)
    
    print ("計算五日均價", strma_p)
    print ("計算五日均量", strma_c)
    print ("計算五日均價持續天數", strma_p_cont)
    print ("計算五日、十日乖離值", strma_br)
Ejemplo n.º 12
0
def get_data(ticker):
    stock = Stock(ticker)
    index = Stock('3056')
    data = stock.fetch_from(n_yrs_ago.year, n_yrs_ago.month)
    data_index = index.fetch_from(n_yrs_ago.year, n_yrs_ago.month)
    s_close = []
    tpe_index = []
    for i in range(len(data)):
        s_close.append(data[i][6])
        tpe_index.append(data_index[i][6] * 500)
    df_close = pd.DataFrame(s_close)
    df_index = pd.DataFrame(tpe_index)
    df_close_index = pd.DataFrame(list(zip(s_close, tpe_index)))
    conv_to_csv(df_close, df_index, df_close_index, ticker)
    frame = [df_close, df_index]
    return frame
Ejemplo n.º 13
0
import DateAgoTest
import pandas as pd
import matplotlib.pyplot as plt

from talib import abstract
from twstock import Stock

stockID = '2330'
stock = Stock(stockID)

total = []

DList = DateAgoTest('2019-07', 7)
stockData = stock.fetch_from(2019, 7)

head = ["date", "capacity", "turnover", "open", 
"high", "low", "close", "change", "transaction"]

for value in stockData:

  day = str(value[0]).split()
  
  if day[0] in DList:
  
    total.append([day[0], value[1], value[2], value[3], 
            value[4], value[5], value[6], value[7], value[8])

df = pd.DataFrame(total, columns = head)
df.set_index('date', inplace = True)
df['close'].plot(figsize=(16, 8))
Ejemplo n.º 14
0
# import pandas as pd
# data = pd.read_csv("./stock_list.csv")
# stockNumberList =  data['id']
# print(stockNumberList[3])

# exit()

from StockFunction import getStockList

stockList = getStockList()

exit()

from twstock import Stock

stock = Stock('2330')  # 擷取台積電股價
ma_p = stock.moving_average(stock.price, 5)  # 計算五日均價
ma_c = stock.moving_average(stock.capacity, 1)  # 計算五日均量
ma_p_cont = stock.continuous(ma_p)  # 計算五日均價持續天數
ma_br = stock.ma_bias_ratio(5, 10)  # 計算五日、十日乖離值

# print(stock.data[-1])
print(ma_c)

exit()

from testHello import sample_func
sample_func()

exit()
import pandas as pd

from datetime import datetime
import twstock
from twstock import Stock
import time
from tqdm import tqdm

stock = Stock("2330")
time.sleep(2)
# 延遲兩秒

stock.fetch_from(2020, 5)

date = stock.date  #日期
high_price = stock.high  #盤中最高價
low_price = stock.low  # 盤中最低價
close_price = stock.close  #收盤價
open_price = stock.open  # 開盤價
capacity = stock.capacity  # 總成總成交股數
turnover = stock.turnover  # 總成交金額
change = stock.change  # 漲跌價差
transaction = stock.transaction  # 成交筆數

day_number = len(date)

# 建立 和天數相符的 [0] list
day_list = []
day_list = [0] * day_number
#把取出的time 轉乘int 再存入day_list
for i in tqdm(range(day_number)):
Ejemplo n.º 16
0
def callback():
    # get X-Line-Signature header value
    signature = request.headers['X-Line-Signature']

    # get request body as text
    body = request.get_data(as_text=True)
    app.logger.info("Request body: " + body + "1234")

    # handle webhook body
    try:
        handler.handle(body, signature)
    except InvalidSignatureError:
        abort(400)
    try:
        events = parser.parse(body, signature)
    except InvalidSignatureError:
        print('InvalidSignatureError')
        abort(400)

    # if event is MessageEvent and message is TextMessage, then echo text
    for event in events:
        if not isinstance(event, MessageEvent):
            continue
        if not isinstance(event.message, TextMessage):
            continue

        text = event.message.text
        #userId = event['source']['userId']
        if (text.lower() == 'me'):
            content = str(event.source.user_id)

            line_bot_api.reply_message(event.reply_token,
                                       TextSendMessage(text=content))
        elif (text.lower() == 'profile'):
            profile = line_bot_api.get_profile(event.source.user_id)
            my_status_message = profile.status_message
            if not my_status_message:
                my_status_message = '-'
            line_bot_api.reply_message(event.reply_token, [
                TextSendMessage(text='Display name: ' + profile.display_name),
                TextSendMessage(text='picture url: ' + profile.picture_url),
                TextSendMessage(text='status_message: ' + my_status_message),
            ])

        elif (text.startswith('$')):
            text = text[1:]
            content = ''

            stock_rt = twstock.realtime.get(text)
            my_datetime = datetime.fromtimestamp(stock_rt['timestamp'] +
                                                 8 * 60 * 60)
            my_time = my_datetime.strftime('%H:%M:%S')
            stock = Stock(text)
            bfp = BestFourPoint(stock)
            signal = bfp.best_four_point()

            a = str(signal)
            b = signal[0]
            i4 = ImageSendMessage(
                original_content_url="https://i.imgur.com/wUVUYYJ.jpg",
                preview_image_url="https://i.imgur.com/wUVUYYJ.jpg")
            i5 = ImageSendMessage(
                original_content_url="https://i.imgur.com/mE50Pes.jpg",
                preview_image_url="https://i.imgur.com/mE50Pes.jpg")

            if b == True:
                line_bot_api.reply_message(event.reply_token, i4)
            if b == False:
                line_bot_api.reply_message(event.reply_token, i5)

        elif (text.startswith('#')):
            text = text[1:]
            content = ''

            stock_rt = twstock.realtime.get(text)
            my_datetime = datetime.fromtimestamp(stock_rt['timestamp'] +
                                                 8 * 60 * 60)
            my_time = my_datetime.strftime('%H:%M:%S')
            stock = Stock(text)
            bfp = BestFourPoint(stock)
            signal = bfp.best_four_point()
            a = str(signal)

            content += '%s (%s) %s\n' % (stock_rt['info']['name'],
                                         stock_rt['info']['code'], my_time)
            content += '現價: %s / 開盤: %s\n' % (
                stock_rt['realtime']['latest_trade_price'],
                stock_rt['realtime']['open'])
            content += '最高: %s / 最低: %s\n' % (stock_rt['realtime']['high'],
                                              stock_rt['realtime']['low'])
            content += '量: %s\n' % (
                stock_rt['realtime']['accumulate_trade_volume'])
            content += '買賣訊號:%s\n' % (a)
            stock = twstock.Stock(text)  #twstock.Stock('2330')
            content += '------------------------\n'
            content += '最近五日價格: \n'
            price5 = stock.price[-5:][::-1]
            date5 = stock.date[-5:][::-1]
            for i in range(len(price5)):
                #content += '[%s] %s\n' %(date5[i].strftime("%Y-%m-%d %H:%M:%S"), price5[i])
                content += '[%s] %s\n' % (date5[i].strftime("%Y-%m-%d"),
                                          price5[i])

            line_bot_api.reply_message(event.reply_token,
                                       TextSendMessage(text=content))

        elif (text.startswith('/')):
            text = text[1:]
            fn = '%s.png' % (text)
            stock = twstock.Stock(text)
            my_data = {
                'close': stock.close,
                'date': stock.date,
                'open': stock.open
            }
            df1 = pd.DataFrame.from_dict(my_data)

            df1.plot(x='date', y='close')
            plt.title('[%s]' % (stock.sid))
            plt.savefig(fn)
            plt.close()

            # -- upload
            # imgur with account: [email protected]

            client = ImgurClient(client_id, client_secret)
            print("Uploading image... ")
            image = client.upload_from_path(fn, anon=True)
            print("Done")

            url = image['link']
            image_message = ImageSendMessage(original_content_url=url,
                                             preview_image_url=url)

            line_bot_api.reply_message(event.reply_token, image_message)

    return 'OK'
Ejemplo n.º 17
0
                sql = "INSERT INTO news (`code`,`date`,`title`,`url`,`logTime`) VALUES (%s,%s,%s,%s,%s)"
                val = (code,date,title,url,datetime.datetime.now())
                cursor.execute(sql, val)
                conn.commit()
                if count == MAX_COUNT: 
                    break
                else:
                    count = count + 1
                print (date,title,url)
            except:
                if fail_count == MAX_FAIL_COUNT:
                    break
                else:
                    fail_count = fail_count + 1                
                    print ('insert failed')
        time.sleep(3) 
'''
if __name__ == '__main__':
    cursor = conn.cursor()
    code_list = []
    sql = "SELECT distinct(code) FROM own"
    cursor.execute(sql)
    for row in cursor:
        code = row[0]
        stock = Stock(code)
        week_ma = stock.moving_average(stock.price, 5)
        month_ma = stock.moving_average(stock.price, 20)
        season_ma = stock.moving_average(stock.price, 60)
        print(code, week_ma[-1], month_ma[-1], season_ma[-1])
        time.sleep(10)
Ejemplo n.º 18
0
if localtime.tm_mon < int(mon_span):
    yrs = localtime.tm_year - 1
    mon = 12 - (int(mon_span) - localtime.tm_mon)
else:
    yrs = localtime.tm_year
    mon = localtime.tm_mon

print(yrs, mon)

stock_num = input("Enter Stock: ")

for i in range(1, 6):
    time.sleep(1)
    print("Wait.... ", 5 - i, "second")

stock = Stock(stock_num)
print("First Stage complete ")

data = pd.DataFrame(stock.fetch_from(yrs, mon))
data.set_index('date', inplace=True)
data.index = data.index.format(formatter=lambda x: x.strftime('%Y-%m-%d'))
print("Fetching complete")

fig = plt.figure(figsize=(24, 16))
#用add_axes創建副圖框
ax = fig.add_axes([0.05, 0.25, 0.95, 0.75])
ax2 = fig.add_axes([0.05, 0.05, 0.95, 0.25])
ax.set_title(stock_num + "KChart", fontsize=12)
ax2.set_xticks(range(0, len(data.index), 10))
ax2.set_xticklabels(data.index[::10], rotation=0)
#使用mpl_finance套件
Ejemplo n.º 19
0
def close_price(stock_num):
    stock = Stock(stock_num)
    return stock.price
Ejemplo n.º 20
0
def capacity(stock_num):
    stock = Stock(stock_num)
    return math.floor(stock.capacity[-1] / 1000)
Ejemplo n.º 21
0
#!/usr/bin/env python3

import pandas as pd
import matplotlib.pyplot as plt

from talib import abstract
from twstock import Stock

stock = Stock('2330')

df = pd.DataFrame(stock.fetch_from(2019, 7))

df.set_index('date', inplace=True)

df['close'].plot(figsize=(16, 8))

plt.show()