Skip to content

Commit

Permalink
修复一些bug,调整了目录结构
Browse files Browse the repository at this point in the history
  • Loading branch information
woldy committed Jan 6, 2024
1 parent fbca819 commit e890341
Show file tree
Hide file tree
Showing 41 changed files with 1,821 additions and 457 deletions.
2 changes: 1 addition & 1 deletion database/finhack_structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,4 @@ CREATE TABLE `factors_mining` (
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

-- Dump completed on 2023-12-27 13:30:47
-- Dump completed on 2024-01-06 21:56:46
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion examples/demo-project/loader/testmodule_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def testaction(self):
Log.logger.debug("loading "+self.module_name)
Log.logger.debug("testarg1 is:"+str(self.args.testarg1))
Log.logger.debug("testarg2 is:"+str(self.args.testarg2))
obj=self.klass()
obj=self.klass
obj.args=self.args
obj.run()

Expand Down
12 changes: 10 additions & 2 deletions examples/demo-project/testmodule/default/default_testmodule.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import finhack.library.log as Log
from runtime.constant import *
import runtime.global_var as global_var

from finhack.market.astock.astock import AStock
import time
from finhack.factor.default.factorManager import factorManager
from finhack.factor.default.factorAnalyzer import factorAnalyzer
class DefaultTestmodule():
def __init__(self):
pass
Expand All @@ -18,4 +20,10 @@ def run(self):

def run2(self):
print(self.args)
print('run2')
print('run2')
stock_list=AStock.getStockCodeList(strict=False, db='tushare')
print(stock_list)


def run3(self):
factorAnalyzer.alphalens("pe_0")
2 changes: 1 addition & 1 deletion finhack/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.0.1.dev4'
__version__ = '0.0.1.dev5'
3 changes: 3 additions & 0 deletions finhack/collector/tushare/tushare_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ def run(self):
table=list(v.values())[0]
tsSHelper.setIndex(table,db)


def save():
pass


def getAStockBasic(self):
Expand Down
2 changes: 1 addition & 1 deletion finhack/core/loader/collector_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def run(self):
# print(self.module_path)
# print(self.user_module_path)
# print(self.klass)
collector=self.klass()
collector=self.klass
collector.run()

pass
Expand Down
2 changes: 1 addition & 1 deletion finhack/core/loader/factor_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def run(self):
# print(self.module_path)
# print(self.user_module_path)
# print(self.klass)
factor=self.klass()
factor=self.klass
factor.run()

pass
2 changes: 1 addition & 1 deletion finhack/core/loader/helper_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def run(self):
# print(self.module_path)
# print(self.user_module_path)
# print(self.klass)
helper=self.klass()
helper=self.klass
helper.run()

pass
Expand Down
2 changes: 1 addition & 1 deletion finhack/core/loader/trader_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def run(self):
# print(self.module_path)
# print(self.user_module_path)
# print(self.klass)
trader=self.klass()
trader=self.klass
trader.args=self.args
trader.run()

Expand Down
3 changes: 0 additions & 3 deletions finhack/factor/default/alphaEngine.py
Original file line number Diff line number Diff line change
Expand Up @@ -786,9 +786,6 @@ def calc(formula='',df=pd.DataFrame(),name="alpha",check=False,replace=False):
df=df.sort_index()





if diff_date>0 and diff_date<100:
dt=datetime.datetime.strptime(str(max_date),'%Y%m%d')
start_date=dt-datetime.timedelta(days=700)
Expand Down
9 changes: 8 additions & 1 deletion finhack/factor/default/default_factor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,12 @@
from finhack.market.astock.astock import AStock
from finhack.factor.default.taskRunner import taskRunner
class DefaultFactor:
def __init__(self):
pass

def run(self):
taskRunner.runTask()
pass
taskRunner.runTask()

def test(self):
print(self.args)
119 changes: 118 additions & 1 deletion finhack/factor/default/factorAnalyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,133 @@
import traceback
import numpy as np
import pandas as pd

import pandas as pd
import alphalens as al
from alphalens.utils import get_clean_factor_and_forward_returns
from alphalens.tears import create_full_tear_sheet
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
warnings.simplefilter(action='ignore', category=RuntimeWarning)
warnings.simplefilter(action='ignore', category=UserWarning)
from finhack.library.mydb import mydb
from finhack.factor.default.factorManager import factorManager
from finhack.market.astock.astock import AStock
from scipy.stats import zscore

class factorAnalyzer():


def all_corr():
pass



def alphalens(factor_name):


df_industry=AStock.getStockIndustry()


# df_all.index=df_all['date']
# price.index = pd.to_datetime(price.index)
# assets = df_all.set_index( [df_all.index,df_all['symbol']], drop=True,append=False, inplace=False)
df=factorManager.getFactors(factor_list=['close',factor_name])
# 假设 df 是您提供的 DataFrame,我们首先重置索引
df = df.reset_index().merge(df_industry, on='ts_code')
df['industry'] = df['industry'].fillna('其他')

df['trade_date'] = pd.to_datetime(df['trade_date'], format='%Y%m%d')

df[factor_name] = df.groupby(['trade_date', 'industry'])[factor_name].transform(zscore)

# 确保因子值没有 NaN,如果有 NaN,可以选择填充或者去除对应的行
df = df.dropna(subset=[factor_name,'industry'])

# 重置索引,准备进行 Alphalens 分析
df = df.set_index(['trade_date', 'ts_code'])

# 创建价格 DataFrame
prices = df['close'].unstack()

# 获取行业中性化后的因子数据
factor = df[factor_name]


unique_industries = df['industry'].unique()
# 创建 groupby_labels 字典,将每个行业标签映射到自己,确保没有遗漏
groupby_labels = {ind: ind for ind in unique_industries}


# 检查 groupby_labels 是否包含所有 unique_industries 中的行业
missing_labels = [ind for ind in unique_industries if ind not in groupby_labels]
if missing_labels:
print(f"Missing industry labels in groupby_labels: {missing_labels}")
# 您可以选择添加缺失的行业标签到 groupby_labels 中
for missing in missing_labels:
groupby_labels[missing] = '其他' # 或者将其映射到 '其他'

# 使用 Alphalens 进行因子分析
factor_data = al.utils.get_clean_factor_and_forward_returns(
factor=factor,
prices=prices,
periods=(1, 5, 10),
groupby=df['industry'], # 指定行业分组
groupby_labels=groupby_labels, # 指定行业标签
)


# 因子收益率分析
mean_return_by_qt, std_err_by_qt = al.performance.mean_return_by_quantile(factor_data)
#aal.plotting.plot_quantile_returns_bar(mean_return_by_qt)
#aplt.show()

# 因子信息比率
ic_by_day = al.performance.factor_information_coefficient(factor_data)
#al.plotting.plot_information_coefficient(ic_by_day)
#plt.show()

# 分位数平均收益率
quantile_returns = al.performance.mean_return_by_quantile(factor_data)[0].apply(al.utils.rate_of_return, axis=0, base_period='1D')
#al.plotting.plot_quantile_returns_violin(quantile_returns)
#plt.show()

# 分位数累积收益
#cumulative_returns_by_qt = al.performance.cumulative_returns_by_quantile(factor_data, period=1)
#al.plotting.plot_cumulative_returns_by_quantile(cumulative_returns_by_qt, period=1)
#plt.show()

# 分位数收益率的全面统计
#full_tear_sheet = al.tears.create_full_tear_sheet(factor_data, long_short=True, group_neutral=False, by_group=False)

# 因子自相关性分析
autocorrelation = al.performance.factor_rank_autocorrelation(factor_data)
#al.plotting.plot_autocorrelation(autocorrelation)
#plt.show()

# 因子收益率和分位数收益率的IC分析
mean_monthly_ic = al.performance.mean_information_coefficient(factor_data, by_time='M')
#al.plotting.plot_monthly_ic_heatmap(mean_monthly_ic)
#plt.show()

print("\nmean_return_by_qt")
print(mean_return_by_qt)
print("\nic_by_day")
print(ic_by_day)
print("\nquantile_returns")
print(quantile_returns)
print("\nautocorrelation")
print(autocorrelation)
print("\nmean_monthly_ic")
print(mean_monthly_ic)

# print('---')
# print(full_tear_sheet)

#al.plotting.plot_quantile_returns_bar(mean_return_by_qt)
pass



def analys(factor_name,df=pd.DataFrame(),days=[1,2,3,5,8,13,21],pool='all',start_date='20000101',end_date='20100101',formula="",relace=False,table='factors_analysis'):
try:

Expand Down
2 changes: 1 addition & 1 deletion finhack/factor/default/factorManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def getAlphaList(listname):

def getIndicatorsList():
return_fileds=[]
path = os.path.dirname(__file__)+"/indicators/"
path = INDICATORS_DIR
for subfile in os.listdir(path):
if not '__' in subfile:
indicators=subfile.split('.py')
Expand Down
27 changes: 22 additions & 5 deletions finhack/factor/default/indicatorCompute.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from functools import lru_cache
from importlib import import_module
from multiprocessing import cpu_count

import importlib
from runtime.constant import *
from finhack.library.mydb import mydb
from finhack.library.config import Config
Expand Down Expand Up @@ -66,7 +66,9 @@ def split_list_n_list(origin_list, n):
for code_list in code_lists:
with ProcessPoolExecutor(max_workers=n) as pool:
for ts_code in code_list:
mytask=pool.submit(indicatorCompute.computeListByStock,ts_code,list_name,'',factor_list,c_list)
#computeListByStock(ts_code,list_name='all',where='',factor_list=None,c_list=[],pure=True,check=True,df_price=pd.DataFrame(),db='tushare'):

mytask=pool.submit(indicatorCompute.computeListByStock,ts_code,list_name,'',factor_list,c_list,false,false)
tasklist.append(mytask)

wait(tasklist,return_when=ALL_COMPLETED)
Expand All @@ -79,7 +81,7 @@ def split_list_n_list(origin_list, n):

#计算单支股票的一坨因子
#pure=True时,只保留factor_list中的因子
def computeListByStock(ts_code,list_name='all',where='',factor_list=None,c_list=[],pure=True,check=False,df_price=pd.DataFrame(),db='tushare'):
def computeListByStock(ts_code,list_name='all',where='',factor_list=None,c_list=[],pure=True,check=True,df_price=pd.DataFrame(),db='tushare'):
try:
Log.logger.info('computeListByStock---'+ts_code)

Expand Down Expand Up @@ -344,7 +346,22 @@ def computeFactorByStock(ts_code,factor_name,df_price=pd.DataFrame(),where='',db
df_price=indicatorCompute.computeFactorByStock(ts_code,f,df_price,db)

factor=factor_name.split('_')
module = getattr(import_module('finhack.factor.default.indicators.'+indicators), indicators)
#module = getattr(import_module('finhack.factor.default.indicators.'+indicators), indicators)



# 定义文件路径
file_path = INDICATORS_DIR+indicators+".py"

# 获取文件名和类名
module_name = indicators
class_name = indicators

# 加载模块
spec = importlib.util.spec_from_file_location(module_name, file_path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)

func=getattr(module,func_name,lambda x,y:x)

shift="0"
Expand Down Expand Up @@ -424,7 +441,7 @@ def computeFactorByStock(ts_code,factor_name,df_price=pd.DataFrame(),where='',db
def getFactorInfo(factor_name):
factor=factor_name.split('_')
factor_filed=factor[0]
path = os.path.dirname(__file__)+"/indicators/"
path = INDICATORS_DIR
for subfile in os.listdir(path):
if not '__' in subfile:
indicators=subfile.split('.py')
Expand Down
2 changes: 1 addition & 1 deletion finhack/factor/default/preCheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def checkIndicatorsChange():
factor=factor_name.split('_')
factor_filed=factor[0]
return_fileds=[]
path = os.path.dirname(__file__)+"/indicators/"
path = INDICATORS_DIR
for subfile in os.listdir(path):
if find:
continue
Expand Down
Loading

0 comments on commit e890341

Please sign in to comment.