diff --git a/transferIndustrySatistics_python.py b/transferIndustrySatistics_python.py index 21d3227..44a6798 100644 --- a/transferIndustrySatistics_python.py +++ b/transferIndustrySatistics_python.py @@ -2,42 +2,42 @@ import pymysql -#数据库A的连接信息;NAS数据库 -db_a_config = { - 'host': '192.168.0.222', - 'user': 'root', - 'password': '1qazse42W3', - 'db': 'ry', - 'charset': 'utf8mb4', -} - -# 数据库B的连接信息;NAS数据库 -db_b_config = { - 'host': '192.168.0.222', - 'user': 'root', - 'password': '1qazse42W3', - 'db': 'mojin', - 'charset': 'utf8mb4', -} - -# # 数据库A的连接信息;腾讯云数据库 +# #数据库A的连接信息;NAS数据库 # db_a_config = { -# 'host': '124.223.98.178', +# 'host': '192.168.0.222', # 'user': 'root', # 'password': '1qazse42W3', # 'db': 'ry', # 'charset': 'utf8mb4', # } -# # 数据库B的连接信息;腾讯云数据库 +# # 数据库B的连接信息;NAS数据库 # db_b_config = { -# 'host': '124.223.98.178', +# 'host': '192.168.0.222', # 'user': 'root', # 'password': '1qazse42W3', # 'db': 'mojin', # 'charset': 'utf8mb4', # } +# 数据库A的连接信息;腾讯云数据库 +db_a_config = { + 'host': '124.223.98.178', + 'user': 'root', + 'password': '1qazse42W3', + 'db': 'ry', + 'charset': 'utf8mb4', +} + +# 数据库B的连接信息;腾讯云数据库 +db_b_config = { + 'host': '124.223.98.178', + 'user': 'root', + 'password': '1qazse42W3', + 'db': 'mojin', + 'charset': 'utf8mb4', +} + try: # 连接数据库A db_a_conn = pymysql.connect(**db_a_config) @@ -68,10 +68,14 @@ try: sql_trade_period = "trading" db_a_cursor.execute(sql_trade_period_qurey,sql_trade_period) trading_period_dates = db_a_cursor.fetchall() + #前一个交易日 + prevTradeDate = '' periodIndex = 0 for trading_date in trading_period_dates: if periodIndex == 0: period_trading_dateMap['1',trading_date] + elif periodIndex == 1: + prevTradeDate = trading_date elif periodIndex == 4: period_trading_dateMap['5',trading_date] elif periodIndex == 5: @@ -93,12 +97,15 @@ try: periodIndex += 1 print('start transfer statistics ',trading_date , ' data.') - #查询日交易数据 + #查询日交易数据 以成交额进行排序 sql_stock_query = 'select code,name,trade_day, open,close,high,low,differrange,volume,amount,limitupnum,limitdownnum,suspendnum,risenum,fallnum,flatnum,componentnum,mv,pettm,pettm_mid,liqmv,rcnthigh,rcntlow from stock_index where trade_day = %s order by volume DESC' db_a_cursor.execute(sql_stock_query,trading_date) datas = db_a_cursor.fetchall() sortIndex = 1 - #遍历数据,并组装数据,转存到目标库中 + + industries_tradeDaymap = {} + totalTunrover = 0.00 + #遍历交易日数据 for stock_index_data in datas: code,name,trade_day, open,close,high,low,differrange,volumn,amount,limitupnum,limitdownnum,suspendnum,risenum,fallnum,flatnum,componentnum,mv,pettm,pettm_mid,liqmv,rcnthigh,rcntlow = stock_index_data business_date = trading_date @@ -107,6 +114,13 @@ try: turnover = amount turnover_sort = sortIndex sortIndex +=1 + rise_number = risenum + flat_number = flatnum + # industries_tradeDaymap[code] = {business_date,security_code,turnover_sort,rise_number,flat_number} + industries_tradeDaymap[code] = {'business_date':business_date,'security_code':security_code,'turnover_sort':turnover_sort,'rise_number':rise_number,'flat_number':flat_number} + # 总成交额 + totalTunrover += turnover + turnover_change = '' #todo turnover_ratio = '' #todo turnover_ratio_change = '' #todo @@ -135,11 +149,9 @@ try: turnover30_ratio_change = '' #todo turnover30_sort = '' #todo turnover30_sort_change = ''#todo - rise_number = risenum rise_number_change = ''#todo losses_number = fallnum losses_number_change = '' #todo - flat_number = flatnum flat_number_change = '' #todo rise5_number = '' #todo rise5_number_change = ''#todo @@ -166,8 +178,30 @@ try: flat30_number = '' #todo flat30_number_change = '' #todo # create_time = - sql_insert = f"INSERT INTO dong_cai_secondary_industry (business_date,sort,security_code,security_name,security_type,number_ingredients,opening_price,closing_price,volume,turnover,total_capitalization,free_capitalization,change_percentage,highest_price,lowest_price,rise_number,losses_number,flat_number,rise_stop_number,losses_stop_number,stop_number,recently_high,recently_low,pe_ttm,pe_center_ttm) VALUES ( %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s,%s, %s, %s, %s)" - db_b_cursor.execute(sql_insert, (business_date,sort,security_code,security_name,security_type,number_ingredients,opening_price,closing_price,volume,turnover,total_capitalization,free_capitalization,change_percentage,highest_price,lowest_price,rise_number,losses_number,flat_number,rise_stop_number,losses_stop_number,stop_number,recently_high,recently_low,pe_ttm,pe_center_ttm)) + # sql_insert = f"INSERT INTO dong_cai_secondary_industry (business_date,sort,security_code,security_name,security_type,number_ingredients,opening_price,closing_price,volume,turnover,total_capitalization,free_capitalization,change_percentage,highest_price,lowest_price,rise_number,losses_number,flat_number,rise_stop_number,losses_stop_number,stop_number,recently_high,recently_low,pe_ttm,pe_center_ttm) VALUES ( %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s,%s, %s, %s, %s)" + # db_b_cursor.execute(sql_insert, (business_date,sort,security_code,security_name,security_type,number_ingredients,opening_price,closing_price,volume,turnover,total_capitalization,free_capitalization,change_percentage,highest_price,lowest_price,rise_number,losses_number,flat_number,rise_stop_number,losses_stop_number,stop_number,recently_high,recently_low,pe_ttm,pe_center_ttm)) + + #遍历前一日交易数据,及排名数据,获取变化值 + sql_stock_query = 'select code,name,trade_day, open,close,high,low,differrange,volume,amount,limitupnum,limitdownnum,suspendnum,risenum,fallnum,flatnum,componentnum,mv,pettm,pettm_mid,liqmv,rcnthigh,rcntlow from stock_index where trade_day = %s order by volume DESC' + db_a_cursor.execute(sql_stock_query,prevTradeDate) + + industries_prevTradeDaymap = {} + prevTotalTunrover = 0.00 + for stock_index_data in datas: + code,name,trade_day, open,close,high,low,differrange,volumn,amount,limitupnum,limitdownnum,suspendnum,risenum,fallnum,flatnum,componentnum,mv,pettm,pettm_mid,liqmv,rcnthigh,rcntlow = stock_index_data + business_date = trading_date + security_code = code + security_name = name + turnover = amount + turnover_sort = sortIndex + sortIndex +=1 + rise_number = risenum + flat_number = flatnum + # industries_tradeDaymap[code] = {business_date,security_code,turnover_sort,rise_number,flat_number} + industries_prevTradeDaymap[code] = {'business_date':business_date,'security_code':security_code,'turnover_sort':turnover_sort,'rise_number':rise_number,'flat_number':flat_number} + # 总成交额 + totalTunrover += turnover + #提交数据到数据库B中 db_b_conn.commit() print('successed transfer ',trading_date , ' data.' )