def calculateDayLineUtils(stock_id, limitNumber): data_ma_tuple = connect.query_list( "SELECT close, date FROM stock.stock_master where stock_id=" + stock_id + " order by date desc limit " + limitNumber) obj_ma = connect.transTupleToObject(data_ma_tuple, ['close', 'date']) close_data = obj_ma['close'] df_ma = pd.DataFrame(obj_ma) for days in [5, 10, 20, 60, 120, 240]: i = 0 days_value = [] date_value = [] for i in range(0, len(close_data) - days, +1): cur_ma = close_data[i:i + days] if cur_ma.count(None) > 0: break days_value.append(statistics.mean(cur_ma)) date_value.append(obj_ma['date'][i]) days_df = pd.DataFrame({ str(days) + '_days': days_value, 'date': date_value }) df_ma = df_ma.merge(days_df, how='left', left_on='date', right_on='date') # add stock_id column df_ma['stock_id'] = stock_id return df_ma
def calculateDayLine(): stock_list_tuple = connect.query_list( "SELECT stock_id FROM stock.stock_list") stock_list = connect.transTupleToList(stock_list_tuple, 0) for stock_id in stock_list: df_ma = calculateDayLineUtils(stock_id, 10000) ''' data_ma_tuple = connect.query_list( "SELECT close, date FROM stock.stock_master where stock_id=" + stock_id + " order by date desc limit 10000") obj_ma = connect.transTupleToObject(data_ma_tuple, ['close', 'date']) close_data = obj_ma['close'] df_ma = pd.DataFrame(obj_ma) for days in [5, 10, 20, 60, 120, 240]: i = 0 days_value = [] date_value = [] for i in range(0, len(close_data) - days, +1): cur_ma = close_data[i:i + days] if cur_ma.count(None) > 0: break days_value.append(statistics.mean(cur_ma)) date_value.append(obj_ma['date'][i]) days_df = pd.DataFrame({str(days)+'_days': days_value, 'date': date_value}) df_ma = df_ma.merge(days_df, how='left', left_on = 'date', right_on='date') df_ma['stock_id'] = stock_id ''' engine = connect.createEngine() df_ma[['stock_id', 'date', '5_days', '10_days', '20_days', '60_days', '120_days', '240_days']]\ .to_sql(name='stock_days', con=engine, if_exists='append', index=False) return "SUCCESSFUL"
def main(): stockid_tuple = connect.query_list("select stock_id from stock_list") stock_list = list() for stockid in stockid_tuple: stock_list.append(str(stockid[0])) return render_template('index.html', stock_list=stock_list)
def getTodayStock(): # get datetime for get stock now = datetime.datetime.now() now = now.strftime("%Y%m%d") today = now.date() stock_list_tuple = connect.query_list( "SELECT stock_id FROM stock.stock_list") stock_list = connect.transTupleToList(stock_list_tuple, 0) for stock_id in stock_list: res = requests.get( 'http://www.twse.com.tw/exchangeReport/STOCK_DAY?response=json&date=' + str(now) + '&stockNo=' + stock_id) # http://www.twse.com.tw/exchangeReport/STOCK_DAY?response=json&date=20181025&stockNo=3665 json_data = json.loads(res.text) monthInfoArray = json_data['data'] monthInfoArray.reverse() # get today stock info monthInfoArray = [monthInfoArray[0]] data_stock = saveStockInfo(monthInfoArray, stock_id) stock_info.calculateStockDayLine(stock_id, today) return data_stock
def getMA(stock_id, days): data_ma_tuple = connect.query_list( "SELECT close FROM stock.stock_master where stock_id=" + stock_id + " and close is not null order by date desc limit " + days) data_ma = connect.transTupleToList(data_ma_tuple, 0) return statistics.mean(data_ma)