def _resync_dividend_policy(stock_id): stock_data = read(str(stock_id)) if stock_data is None: return print(stock_data.df_performance.index) price_measurement_processor = PriceMeasurementProcessor(stock_id) df_prices = price_measurement_processor.get_data_frame() # print('index = ', df_prices.index) # print('type = ', type(df_prices.index)) dividend_policy_processor = DividendPolicyProcessor(stock_id) year_indexes = stock_data.df_performance.index.tolist() df_dividend_policy = dividend_policy_processor.get_data_frames( {'year': year_indexes[0]}, {'year': year_indexes[-1]}) prices = [] shares = [] for year in stock_data.df_performance.index.tolist(): prices.append(df_prices.loc[str(year), '平均股價'] if str(year) in df_prices.index else 0) shares.append(df_dividend_policy.loc[str(year), '股息'] if str(year) in df_dividend_policy.index else float(0)) print('prices = ', prices) print('result_share = ', shares) stock_data.df_performance = stock_data.df_performance.assign( 股息=shares).assign(平均股價=prices) print('result = ', stock_data.df_performance) store(stock_data)
def test_read_stock_data(self): from stock_data import read stock_data = read('1101') self.assertIsNotNone(stock_data) print(stock_data.stock_id) print(stock_data.df_statement) print(stock_data.df_performance) print(stock_data.df_profit_matrix)
def sync_data(stock_id): stock_data = read(str(stock_id)) df_statement = _sync_statement( stock_id, None if stock_data is None else stock_data.df_statement) df_performance = _sync_performance( stock_id, df_statement, None if stock_data is None else stock_data.df_performance) df_profit_matrix = _sync_profit_matrix( stock_id, None if stock_data is None else stock_data.df_profit_matrix) return StockData( stock_id, df_performance, df_statement, df_profit_matrix ) if df_statement is not None and df_performance is not None and df_profit_matrix is not None else None
def generate_predictions2(df_prices, stock_ids=[]): error_stock_ids = [] df_predictions = None for stock_id in stock_ids: str_stock_id = str(stock_id) # try: # stock_data = get_stock_data(str_stock_id) # except: # stock_data = None # # if stock_data is None: # continue try: stock_data = read(str_stock_id) s_stock = get_predict_evaluate(stock_data, float(df_prices.loc[str_stock_id])) except Exception as e: print("Unexpected error:", e) traceback.print_tb(e.__traceback__) print('Get error when get stock ', stock_id, ' stock_data = ', stock_data) error_stock_ids.append(stock_id) s_stock = None except: print("Unexpected error:", sys.exc_info()[0]) error_stock_ids.append(stock_id) s_stock = None if s_stock is None: continue if df_predictions is None: df_predictions = pd.DataFrame(columns=s_stock.index, data=[s_stock.values]) df_predictions['股號'] = [str_stock_id] df_predictions = df_predictions.set_index('股號') print("first record") print(df_predictions) print("index = ", df_predictions.index) else: print('get index = ', df_predictions.index) df_predictions.loc[str_stock_id] = s_stock.values print('result = ', df_predictions) print('err_id = ', error_stock_ids) output_path = gen_output_path('data', 'evaluations.xlsx') with pd.ExcelWriter(output_path) as writer: df_predictions.to_excel(writer, sheet_name='predictions')
def test_predict_evaluation(self): from stock_data import read stock_data = read('2330') self.assertIsNotNone(stock_data) df = None with open(gen_output_path('data', 'prices.xlsx'), 'rb') as file: df = pd.read_excel(file) file.close() s_2330 = get_predict_evaluate(stock_data, float(df.loc['2330', '收盤價'])).rename('2330') result = pd.concat([s_2330], axis=1) print('result 1', result.T) stock_data = read('6294') s_6294 = get_predict_evaluate(stock_data, float(df.loc['6294', '收盤價'])).rename('6294') self.assertIsNotNone(stock_data) result.loc[:, '6294'] = s_6294 print('result 2', result.T) result.loc[:, '6294'] = s_6294 print('result 3', result.T)
def get_stock_data(stock_id, sync_to_latest=False): from stock_data import read str_stock_id = str(stock_id) stock_data = read(str_stock_id) if stock_data is None or sync_to_latest: try: print('get stock_data for ', stock_id) # stock_data = get_evaluate_performance(str_stock_id, 2014) stock_data = sync_data(str_stock_id) from stock_data import store store(stock_data) except (IndexError, KeyError, AttributeError) as e: print("get exception: ", e, " for ", stock_id) traceback.print_tb(e.__traceback__) return None return stock_data
def test_get_matrix_value(self): from stock_data import read stock_data = read('1102') get_matrix_value(stock_data)