feat: 💄 refactor fund_statistic

main
jackluson 3 years ago
parent 04f554db0d
commit 82fa598668

@ -0,0 +1,54 @@
'''
Desc: 基金统计
File: /statistic.py
Project: fund_info
File Created: Thursday, 13th May 2021 11:04:55 am
Author: luxuemin2108@gmail.com
-----
Copyright (c) 2021 Camel Lu
'''
import time
import datetime
import os
import sys
sys.path.append('../')
sys.path.append(os.getcwd() + '/src')
from utils.index import get_season_index
from sql_model.fund_query import FundQuery
class FundStatistic:
def __init__(self):
# 统计上一个季度
last_quarter_time = time.localtime(time.time() - 3 * 30 * 24 * 3600)
time.strftime("%m-%d", last_quarter_time)
year = time.strftime("%Y", last_quarter_time)
date = time.strftime("%m-%d", last_quarter_time)
index = get_season_index(date)
quarter_index = year + '-Q' + str(index)
self.quarter_index = quarter_index
def statistic_stock_fund_count(self, *, query_index=None, filter_count=100):
each_query = FundQuery()
query_index = query_index if query_index else self.quarter_index
results = each_query.select_top_10_stock(query_index)
# pprint(results)
code_dict = dict()
for result in results:
# print(result)
for index in range(1, len(result), 2):
code = result[index]
name = result[index + 1]
key = str(code) + '-' + str(name)
if(key in code_dict and code != None):
code_dict[key] = code_dict[key] + 1
else:
code_dict[key] = 1
filer_dict = dict()
for key, value in code_dict.items(): # for (key,value) in girl_dict.items() 这样加上括号也可以
if value > filter_count and key != None:
filer_dict[key] = value
# print(key + ":" + str(value))
list = sorted(filer_dict.items(), key=lambda x: x[1], reverse=True)
return list

@ -13,35 +13,13 @@ import pymysql
from pprint import pprint
from db.connect import connect
from sql_model.fund_query import FundQuery
from fund_info.statistic import FundStatistic
cursor = connect().cursor()
# cursor = connect().cursor()
if __name__ == '__main__':
each_query = FundQuery()
query_index = '2021-Q1'
results = each_query.select_top_10_stock(query_index)
# pprint(results)
code_dict = dict()
for result in results:
# print(result)
for index in range(1, len(result), 2):
code = result[index]
name = result[index + 1]
key = str(code) + '-' + str(name)
if(key in code_dict and code != None):
code_dict[key] = code_dict[key] + 1
else:
code_dict[key] = 1
filer_dict = dict()
for key, value in code_dict.items(): # for (key,value) in girl_dict.items() 这样加上括号也可以
if value > 100 and key != None:
filer_dict[key] = value
# print(key + ":" + str(value))
list = sorted(filer_dict.items(), key=lambda x: x[1], reverse=True)
print(len(list))
pprint(list)
# pprint(dir(code_dict))
# filer_dict = dict((name, getattr(code_dict, name))
# for name in dir(code_dict) if not (name == None or getattr(code_dict, name) > int(1)))
# pprint(filer_dict)
each_statistic = FundStatistic()
stock_top_list = each_statistic.statistic_stock_fund_count(
filter_count=88)
pprint(stock_top_list)
print(len(stock_top_list))

@ -99,8 +99,6 @@ class FundQuery:
str(index), str(index)) + ","
# print(stock_sql_join[0:-1])
stock_sql_join = stock_sql_join[0:-1]
print(
"🚀 ~ file: fund_query.py ~ line 102 ~ stock_sql_join", stock_sql_join)
sql_query_season = "SELECT t.fund_code," + stock_sql_join + \
" FROM fund_morning_stock_info as t WHERE t.quarter_index = %s AND t.stock_position_total > 20;" # 大于20%股票持仓基金
if query_index == None:

Loading…
Cancel
Save