def load_data(self, dataset, transpose=False): #dataset = 'Shareholding' data = Load.FinData(dataset=dataset, select=self.stock_id, date=self.date) if len(data) == 0: return data if transpose: data = Load.transpose(data) data['stock_id'] = data['stock_id'].astype(str) data['date'] = data['date'].astype(str) return data
def create_trade_sign(self, stock_price: pd.DataFrame) -> pd.DataFrame: stock_price = stock_price.sort_values("date") stock_price.index = range(len(stock_price)) InstitutionalInvestorsBuySell = Load.FinData( dataset="InstitutionalInvestorsBuySell", select=self.stock_id, date=self.start_date, end_date=self.end_date, ) InstitutionalInvestorsBuySell = InstitutionalInvestorsBuySell.groupby( ["date", "stock_id"], as_index=False).agg({ "buy": np.sum, "sell": np.sum }) InstitutionalInvestorsBuySell["diff"] = ( InstitutionalInvestorsBuySell["buy"] - InstitutionalInvestorsBuySell["sell"]) stock_price = pd.merge( stock_price, InstitutionalInvestorsBuySell[["stock_id", "date", "diff"]], on=["stock_id", "date"], how="left", ).fillna(0) stock_price["signal_info"] = self.detect_Abnormal_Peak( y=stock_price["diff"].values, lag=10, threshold=3, influence=0.35, )["signals"] stock_price["signal"] = 0 stock_price.loc[stock_price["signal_info"] == -1, "signal"] = 1 stock_price.loc[stock_price["signal_info"] == 1, "signal"] = -1 return stock_price
def load_taiwan_stock_margin_purchase_short_sale(self): self.TaiwanStockMarginPurchaseShortSale = Load.FinData( dataset="TaiwanStockMarginPurchaseShortSale", select=self.stock_id, date=self.start_date, end_date=self.end_date, ) self.TaiwanStockMarginPurchaseShortSale[[ "ShortSaleTodayBalance", "MarginPurchaseTodayBalance" ]] = self.TaiwanStockMarginPurchaseShortSale[[ "ShortSaleTodayBalance", "MarginPurchaseTodayBalance" ]].astype(int) self.TaiwanStockMarginPurchaseShortSale[ "ShortSaleMarginPurchaseTodayRatio"] = ( self. TaiwanStockMarginPurchaseShortSale["ShortSaleTodayBalance"] / self.TaiwanStockMarginPurchaseShortSale[ "MarginPurchaseTodayBalance"])
def init_stock_info(self): stock_info = Load.FinData(dataset='{}StockInfo'.format(self.country)) if 'industry_category' in stock_info.columns: _bool = [ False if x in ['ETF', '不動產投資信託證券'] else True for x in stock_info['industry_category'] ] stock_info = stock_info[_bool] stock_info.index = range(len(stock_info)) stock_info['country'] = self.country colname = ['stock_id', 'stock_name', 'country'] stockinfo = stock_info[colname] _bool = [ True if x in self.stock_id else False for x in stockinfo['stock_id'] ] self.stockinfo = stockinfo[_bool]
def load_data(self, dataset, transpose=False): #dataset = 'Shareholding' form_data = { 'dataset': dataset, 'stock_id': self.stock_id, 'date': self.date } res = requests.post(self.url, verify=True, data=form_data) temp = res.json() data = pd.DataFrame(temp['data']) if len(data) == 0: return data if transpose: data = Load.transpose(data) data['stock_id'] = data['stock_id'].astype(str) data['date'] = data['date'].astype(str) return data
def init(self, base_data): base_data = base_data.sort_values("date") base_data.index = range(len(base_data)) stock_id = base_data["stock_id"].unique() start_date = base_data["date"].min() end_date = base_data["date"].max() InstitutionalInvestorsBuySell = Load.FinData( dataset="InstitutionalInvestorsBuySell", select=stock_id, date=start_date, end_date=end_date, ) InstitutionalInvestorsBuySell = InstitutionalInvestorsBuySell.groupby( ["date", "stock_id"], as_index=False).agg({ "buy": np.sum, "sell": np.sum }) InstitutionalInvestorsBuySell["diff"] = ( InstitutionalInvestorsBuySell["buy"] - InstitutionalInvestorsBuySell["sell"]) base_data = pd.merge( base_data, InstitutionalInvestorsBuySell[["stock_id", "date", "diff"]], on=["stock_id", "date"], how="left", ).fillna(0) base_data["signal_info"] = self.detect_Abnormal_Peak( y=base_data["diff"].values, lag=10, threshold=3, influence=0.35, )["signals"] base_data["signal"] = 0 base_data.loc[base_data["signal_info"] == -1, "signal"] = 1 base_data.loc[base_data["signal_info"] == 1, "signal"] = -1 return base_data
def load_institutional_investors_buy_sell(self): self.InstitutionalInvestorsBuySell = Load.FinData( dataset="InstitutionalInvestorsBuySell", select=self.stock_id, date=self.start_date, end_date=self.end_date, ) self.InstitutionalInvestorsBuySell[[ "sell", "buy" ]] = (self.InstitutionalInvestorsBuySell[["sell", "buy" ]].fillna(0).astype(int)) self.InstitutionalInvestorsBuySell = ( self.InstitutionalInvestorsBuySell.groupby(["date", "stock_id"], as_index=False).agg({ "buy": np.sum, "sell": np.sum })) self.InstitutionalInvestorsBuySell["diff"] = ( self.InstitutionalInvestorsBuySell["buy"] - self.InstitutionalInvestorsBuySell["sell"])
from FinMind.Data import Load import datetime date = str(datetime.datetime.now().date() - datetime.timedelta(30)) date2 = str(datetime.datetime.now().date() - datetime.timedelta(200)) date3 = str(datetime.datetime.now().date() - datetime.timedelta(400)) _index = 0 # --------------------------------------------------------------- print("load TaiwanStockInfo") TaiwanStockInfo = Load.FinData(dataset="TaiwanStockInfo") print(TaiwanStockInfo.head()) print("load TotalMarginPurchaseShortSale") TotalMarginPurchaseShortSale = Load.FinData( dataset="TotalMarginPurchaseShortSale", date=date) print(TotalMarginPurchaseShortSale[:5]) print("load TaiwanStockNews") TaiwanStockNews = Load.FinData(dataset="TaiwanStockNews", date=date, select="2317") print(TaiwanStockNews[:5]) print("load TaiwanStockPrice {} ".format(TaiwanStockInfo.loc[_index, "stock_id"])) TaiwanStockPrice = Load.FinData(dataset="TaiwanStockPrice", select="2330", date=date2) print(TaiwanStockPrice[:5]) print("load StockDividendResult {} ".format(TaiwanStockInfo.loc[_index,
from FinMind.Data import Load import datetime date = str(datetime.datetime.now().date() - datetime.timedelta(30)) date2 = str(datetime.datetime.now().date() - datetime.timedelta(200)) date3 = str(datetime.datetime.now().date() - datetime.timedelta(400)) #--------------------------------------------------------------- print('load TaiwanStockInfo') TaiwanStockInfo = Load.FinData(dataset='TaiwanStockInfo') print(TaiwanStockInfo[:5]) _index = 477 print('load TaiwanStockPrice {} '.format(TaiwanStockInfo.loc[_index, 'stock_id'])) TaiwanStockPrice = Load.FinData(dataset='TaiwanStockPrice', select=['2330', '2317'], date=date2) print(TaiwanStockPrice[:5]) print('load 財報 FinancialStatements {} '.format( TaiwanStockInfo.loc[_index, 'stock_id'])) TaiwanStockFinancialStatements = Load.FinData( dataset='FinancialStatements', select=list(TaiwanStockInfo.loc[1530:1555, 'stock_id']), date=date3) print(TaiwanStockFinancialStatements[:5]) # transpose data = Load.transpose(TaiwanStockFinancialStatements) print('load 台股配息 TaiwanStockStockDividend {} '.format( TaiwanStockInfo.loc[_index, 'stock_id']))
c2.test_split_join() c2.test_str_long() c2.test_align() c2.test_replace() c3.introduction() ''' c8.introduction() c8.test_write() print(c.stock_data_folder_path) # code = 2867 # holly.test_load_historical_data(code, 2018, 10, 2019, 4) # holly.test_load_historical_data_gradually(2031, c.first_year, c.second_year) # holly.test_load_historical_data_gradually(2867, 2000, 2018) # holly.test_load_historical_data_gradually(2867, 2011, 2018) ''' for item in c.securities_20MA: holly.test_load_historical_data_gradually(item, c.first_year, c.second_year) ''' from FinMind.Data import Load TaiwanStockInfo = Load.FinData(dataset='TaiwanStockInfo') data = Load.FinData(dataset='TaiwanStockPrice', select=['2425'], date='2017-10-10') # data = Load.FinData(dataset = 'TaiwanStockStockDividend',select = ['2425'],date = '2017-10-10') print(data)
if isinstance(date, str): date = datetime.datetime.strptime(date, '%Y-%m-%d %H:%M:%S') elif isinstance(date, datetime.date): date = datetime.datetime.combine(date, datetime.datetime.min.time()) date = date + datetime.timedelta(minutes=1) second = date - datetime.datetime(1970, 1, 1, 0, 0, 0) second = (second.days * 24 * 60 * 60 + second.seconds) * 1000 return second chart = Highstock() data = Load.FinData(dataset='TaiwanStockPrice', select='2330', date='2018-10-10') print(' change type of dataframe to highcharts ') data['date'] = date2millisecond(data['date']) list_data = [] for i in range(len(data)): tem = [ int(data.loc[i, 'date']), float(data.loc[i, 'max']), float(data.loc[i, 'min']) ] list_data.append(tem) chart.add_data_set(list_data, 'arearange', 'Temperatures')
from FinMind.Data import Load import numpy as np from keras.layers.core import Dense, Dropout from keras.layers import LSTM , GRU from keras.models import Sequential from sklearn.preprocessing import MinMaxScaler import matplotlib.pyplot as plt import math from keras.optimizers import Adam print('input data') data = Load.FinData( dataset = 'TaiwanStockPrice', select = '2330.TW', date = '2010-01-01') #colname = ['date', 'open', 'high', 'low', 'close', 'volume'] #data = data[colname] print('select close price') stock_price = data['Close'].values.astype('float32') stock_price = stock_price.reshape(len(stock_price), 1) print(' 畫圖 ') plt.plot(stock_price) plt.show() print(' 歸一化 ') scaler = MinMaxScaler(feature_range=(0, 1)) stock_price = scaler.fit_transform(stock_price) print(' 取 80% data 當作 training data, 20% data 當作 testing data 做模型驗證 ') train_size = int(len(stock_price) * 0.8)
res = requests.post(url, verify=True, data=form_data) temp = res.json() data = pd.DataFrame(temp['data']) data.head() '''----------------FinancialStatements----------------''' form_data = { 'dataset': 'FinancialStatements', 'stock_id': '2317', 'date': '2019-01-01' } res = requests.post(url, verify=True, data=form_data) temp = res.json() data = pd.DataFrame(temp['data']) data = Load.transpose(data) data.head() '''----------------TaiwanCashFlowsStatement----------------''' form_data = { 'dataset': 'TaiwanCashFlowsStatement', 'stock_id': '2330', 'date': '2019-06-01' } res = requests.post(url, verify=True, data=form_data) temp = res.json() data = pd.DataFrame(temp['data']) data.head() '''----------------TaiwanStockStockDividend----------------''' form_data = { 'dataset': 'TaiwanStockStockDividend',
from FinMind.Data import Load import datetime date = str(datetime.datetime.now().date() - datetime.timedelta(30)) date2 = str(datetime.datetime.now().date() - datetime.timedelta(200)) date3 = str(datetime.datetime.now().date() - datetime.timedelta(400)) #--------------------------------------------------------------- print('load TaiwanStockInfo') TaiwanStockInfo = Load.FinData(dataset='TaiwanStockInfo') print(TaiwanStockInfo[:5]) _index = 9010 print('load TotalMarginPurchaseShortSale') TotalMarginPurchaseShortSale = Load.FinData( dataset='TotalMarginPurchaseShortSale', date=date) print(TotalMarginPurchaseShortSale[:5]) print('load TaiwanStockNews') TaiwanStockNews = Load.FinData(dataset='TaiwanStockNews', date=date, select='2317') print(TaiwanStockNews[:5]) print('load TaiwanStockPrice {} '.format(TaiwanStockInfo.loc[_index, 'stock_id'])) TaiwanStockPrice = Load.FinData(dataset='TaiwanStockPrice', select='2330', date=date2) print(TaiwanStockPrice[:5]) print('load StockDividendResult {} '.format(TaiwanStockInfo.loc[_index,
from FinMind.Data import Load import datetime date = str( datetime.datetime.now().date() - datetime.timedelta(30) ) date2 = str( datetime.datetime.now().date() - datetime.timedelta(200) ) date3 = str( datetime.datetime.now().date() - datetime.timedelta(400) ) #--------------------------------------------------------------- print('load TaiwanStockInfo') TaiwanStockInfo = Load.FinData(dataset = 'TaiwanStockInfo') print( TaiwanStockInfo[:5] ) print('load TaiwanStockPrice {} '.format(TaiwanStockInfo.loc[2,'stock_id'])) TaiwanStockPrice = Load.FinData( dataset = 'TaiwanStockPrice', select = TaiwanStockInfo.loc[2,'stock_id'], date = date) print( TaiwanStockPrice[:5] ) print('load 財報 FinancialStatements {} '.format(TaiwanStockInfo.loc[200,'stock_id'])) TaiwanStockFinancialStatements = Load.FinData( dataset = 'FinancialStatements', select = TaiwanStockInfo.loc[200,'stock_id'], date = date3) print( TaiwanStockFinancialStatements[:5] ) print('load 台股配息 TaiwanStockStockDividend {} '.format(TaiwanStockInfo.loc[200,'stock_id'])) TaiwanStockStockDividend = Load.FinData( dataset = 'TaiwanStockStockDividend', select = TaiwanStockInfo.loc[200,'stock_id'],
import math import datetime from tabulate import tabulate from wave_info.deal_with_wave import * from wave_logic.wave_logic_function import * from matplotlib import pyplot as plt from FinMind.Data import Load import numpy as np import json import pandas as pd TaiwanStockInfo = Load.FinData(dataset = 'TaiwanStockInfo') rs=test1(8088) print(tabulate(rs[0], headers='keys', tablefmt='psql')) rs[0].to_csv("earn.csv") print(tabulate(rs[1], headers='keys', tablefmt='psql')) rs[1].to_csv("lose.csv")
def init(self, base_data): base_data = base_data.sort_values("date") stock_id = base_data["stock_id"].unique() start_date = base_data["date"].min() end_date = base_data["date"].max() TaiwanStockMarginPurchaseShortSale = Load.FinData( dataset="TaiwanStockMarginPurchaseShortSale", select=stock_id, date=start_date, end_date=end_date, ) TaiwanStockMarginPurchaseShortSale[[ "ShortSaleTodayBalance", "MarginPurchaseTodayBalance" ]] = TaiwanStockMarginPurchaseShortSale[[ "ShortSaleTodayBalance", "MarginPurchaseTodayBalance" ]].astype(int) TaiwanStockMarginPurchaseShortSale[ "ShortSaleMarginPurchaseTodayRatio"] = ( TaiwanStockMarginPurchaseShortSale["ShortSaleTodayBalance"] / TaiwanStockMarginPurchaseShortSale[ "MarginPurchaseTodayBalance"]) InstitutionalInvestorsBuySell = Load.FinData( dataset="InstitutionalInvestorsBuySell", select=stock_id, date=start_date, end_date=end_date, ) InstitutionalInvestorsBuySell[[ "sell", "buy" ]] = (InstitutionalInvestorsBuySell[["sell", "buy"]].fillna(0).astype(int)) InstitutionalInvestorsBuySell = InstitutionalInvestorsBuySell.groupby( ["date", "stock_id"], as_index=False).agg({ "buy": np.sum, "sell": np.sum }) InstitutionalInvestorsBuySell["diff"] = ( InstitutionalInvestorsBuySell["buy"] - InstitutionalInvestorsBuySell["sell"]) base_data = pd.merge( base_data, InstitutionalInvestorsBuySell[["stock_id", "date", "diff"]], on=["stock_id", "date"], how="left", ).fillna(0) base_data = pd.merge( base_data, TaiwanStockMarginPurchaseShortSale[[ "stock_id", "date", "ShortSaleMarginPurchaseTodayRatio" ]], on=["stock_id", "date"], how="left", ).fillna(0) base_data.index = range(len(base_data)) base_data["signal"] = 0 sell_mask = (base_data["ShortSaleMarginPurchaseTodayRatio"] >= self.ShortSaleMarginPurchaseTodayRatioThreshold) & ( base_data["diff"] > 0) base_data.loc[sell_mask, "signal"] = -1 buy_mask = (base_data["ShortSaleMarginPurchaseTodayRatio"] < self.ShortSaleMarginPurchaseTodayRatioThreshold) & ( base_data["diff"] < 0) base_data.loc[buy_mask, "signal"] = 1 return base_data
def test1(stock_id, some_days_ago=700, rolling_num=20, nfactor=1, stop_loss_factor=10): try: #資料準備區 data = Load.FinData( dataset='TaiwanStockPrice', select=[str(stock_id)], date=str(datetime.date.today() - datetime.timedelta(days=some_days_ago))) #資料取得 critical_point_data = get_critical_point(data, rolling_num) #簡化波型(粗糙) simple_wave = get_simple_wave( critical_point_data) #處理簡化過的波型 把兩個下跌的波整併成一個波 wave_info = get_wave_info(simple_wave, critical_point_data) #計算使用波浪理論需要的參數 lose_list = [] earn_list = [] #邏輯運算區 for i in wave_info[wave_info['type1_buy']].iterrows(): #必要資訊 start_index = i[1]['next_index'] #起始日期的INDEX list_obj = i[1] #上面那個表(essential_data)的一列 now_index = start_index #開始迴圈時當前日期的index #邏輯資訊 start_point = list_obj['close'] #第一波的最高點 當超過最高點時就啟動了 wave_control_condition = list_obj[ 'last_mid_point'] #第一波的中間點 當低於這個點波型破壞 first_wave_gap = abs(start_point - wave_control_condition) * 2 purchase = False purchase_price = 0 stop_loss_factor = stop_loss_factor #跌超過20%就停損 nfactor = nfactor #漲超過第一波的n倍就停利 #logic compute while (now_index <= (len(data) - 1)): now_price = data['close'][now_index] now_index += 1 if (purchase): stoploss = stoploss_or_not(purchase_price, now_price, stop_loss_factor, wave_control_condition) if stoploss: purchase = False lose_list.append([ -now_price + purchase_price, now_price, start_index, now_index, first_wave_gap ]) break book_profit = book_profit_or_not(now_price, purchase_price, first_wave_gap, nfactor) if book_profit: purchase = False earn_list.append([ now_price - purchase_price, now_price, start_index, now_index, first_wave_gap ]) break elif (not purchase): buy = buy_or_not(now_price, wave_control_condition, start_point) if buy: purchase = True purchase_price = now_price else: pass earn_list = pd.DataFrame(earn_list, columns=['賺了', '當前價格', '買入日', '賣出日', '波一漲幅']) earn_list['號碼'] = stock_id lose_list = pd.DataFrame(lose_list, columns=['賠了', '當前價格', '買入日', '賣出日', '波一漲幅']) lose_list['號碼'] = stock_id return [earn_list, lose_list] except KeyError: print('沒抓到資料')
from FinMind.Data import Load import numpy as np #import pandas as pd from keras.layers.core import Dense, Dropout from keras.layers import LSTM, GRU from keras.models import Sequential from sklearn.preprocessing import MinMaxScaler import matplotlib.pyplot as plt import math from keras.optimizers import Adam TaiwanStockInfo = Load.FinData(dataset='TaiwanStockInfo') print('input data') data = Load.FinData(dataset='TaiwanStockPrice', select='2330', date='2010-01-01') #colname = ['date', 'open', 'high', 'low', 'close', 'volume'] #data = data[colname] print('select close price') date = [str(d) for d in data['date']] stock_price = data['close'].values.astype('float32') stock_price = stock_price.reshape(len(stock_price), 1) print(' 畫圖 ') plt.plot(stock_price) plt.show() print(' 取 80% data 當作 training data, 20% data 當作 testing data 做模型驗證 ') train_size = int(len(stock_price) * 0.8)