Skip to content

Commit

Permalink
不知道更新了些啥,反正提交一下吧
Browse files Browse the repository at this point in the history
  • Loading branch information
woldy committed Jan 31, 2024
1 parent 7195304 commit ffcd43f
Show file tree
Hide file tree
Showing 12 changed files with 386 additions and 31 deletions.
183 changes: 182 additions & 1 deletion database/finhack_structure.sql
Original file line number Diff line number Diff line change
@@ -1 +1,182 @@
mysqldump: Got error: 1045: Access denied for user 'root'@'localhost' (using password: NO) when trying to connect
-- MySQL dump 10.13 Distrib 8.0.30, for Linux (x86_64)
--
-- Host: localhost Database: finhack
-- ------------------------------------------------------
-- Server version 8.0.30

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!50503 SET NAMES utf8mb4 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

--
-- Table structure for table `auto_train`
--

DROP TABLE IF EXISTS `auto_train`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `auto_train` (
`id` int NOT NULL AUTO_INCREMENT,
`start_date` varchar(10) DEFAULT NULL,
`valid_date` varchar(10) DEFAULT NULL,
`end_date` varchar(10) DEFAULT NULL,
`features` text CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci,
`label` varchar(255) DEFAULT NULL,
`shift` int DEFAULT NULL,
`param` text CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci,
`hash` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
`created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`loss` varchar(255) DEFAULT NULL,
`algorithm` varchar(255) DEFAULT NULL,
`filter` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT '',
`score` double(10,10) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=12675 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Table structure for table `backtest`
--

DROP TABLE IF EXISTS `backtest`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `backtest` (
`id` int NOT NULL AUTO_INCREMENT,
`instance_id` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
`features_list` mediumtext CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci,
`train` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
`model` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
`strategy` varchar(255) DEFAULT NULL,
`start_date` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
`end_date` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
`init_cash` double(100,5) DEFAULT NULL,
`args` mediumtext CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci,
`history` mediumtext CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci,
`returns` mediumtext CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci,
`logs` mediumtext CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci,
`total_value` double(100,5) DEFAULT NULL,
`alpha` double(100,5) DEFAULT NULL,
`beta` double(100,5) DEFAULT NULL,
`annual_return` double(100,5) DEFAULT NULL,
`cagr` double(100,5) DEFAULT NULL,
`annual_volatility` double(100,5) DEFAULT NULL,
`info_ratio` double(100,5) DEFAULT NULL,
`downside_risk` double(100,5) DEFAULT NULL,
`R2` double(100,5) DEFAULT NULL,
`sharpe` double(100,5) DEFAULT NULL,
`sortino` double(100,5) DEFAULT NULL,
`calmar` double(100,5) DEFAULT NULL,
`omega` double(100,5) DEFAULT NULL,
`max_down` double(100,5) DEFAULT NULL,
`SQN` double(100,5) DEFAULT NULL,
`created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`filter` varchar(255) DEFAULT '',
`win` double(100,5) DEFAULT NULL,
`server` varchar(255) DEFAULT NULL,
`trade_num` int DEFAULT NULL,
`runtime` varchar(255) DEFAULT NULL,
`starttime` varchar(100) DEFAULT NULL,
`endtime` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
`benchReturns` mediumtext,
`roto` double(100,5) DEFAULT NULL,
`simulate` int DEFAULT '0',
`benchmark` varchar(255) DEFAULT NULL,
`strategy_code` text,
PRIMARY KEY (`id`),
UNIQUE KEY `instence_id` (`instance_id`)
) ENGINE=InnoDB AUTO_INCREMENT=388626 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Table structure for table `factors_analysis`
--

DROP TABLE IF EXISTS `factors_analysis`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `factors_analysis` (
`id` int NOT NULL AUTO_INCREMENT,
`factor_name` varchar(255) DEFAULT NULL,
`days` varchar(255) DEFAULT NULL,
`pool` varchar(255) DEFAULT NULL,
`start_date` varchar(10) DEFAULT NULL,
`end_date` varchar(10) DEFAULT NULL,
`formula` text CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci,
`IC` float(10,5) DEFAULT NULL,
`IR` float(10,5) DEFAULT NULL,
`IRR` float(10,5) DEFAULT NULL,
`score` float(10,5) DEFAULT NULL,
`max_up_corr` float(10,7) DEFAULT NULL,
`hash` varchar(255) DEFAULT NULL,
`updated_at` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2388 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Table structure for table `factors_list`
--

DROP TABLE IF EXISTS `factors_list`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `factors_list` (
`id` int NOT NULL AUTO_INCREMENT,
`factor_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
`indicators` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
`func_name` varchar(255) DEFAULT NULL,
`code` text CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci,
`return_fileds` text CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci,
`md5` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
`check_type` int DEFAULT '0',
`status` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT 'activate',
`created_at` datetime DEFAULT CURRENT_TIMESTAMP,
`updated_at` datetime DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2406 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Table structure for table `factors_mining`
--

DROP TABLE IF EXISTS `factors_mining`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `factors_mining` (
`id` int NOT NULL AUTO_INCREMENT,
`factor_name` varchar(255) DEFAULT NULL,
`days` varchar(255) DEFAULT NULL,
`pool` varchar(255) DEFAULT NULL,
`start_date` varchar(10) DEFAULT NULL,
`end_date` varchar(10) DEFAULT NULL,
`formula` text CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci,
`IC` float(20,5) DEFAULT NULL,
`IR` float(20,5) DEFAULT NULL,
`IRR` float(20,5) DEFAULT NULL,
`score` float(10,5) DEFAULT NULL,
`hash` varchar(255) DEFAULT NULL,
`updated_at` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `hash` (`hash`(32)) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=31156 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

-- Dump completed on 2024-01-31 13:01:19
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.dev8'
__version__ = '0.0.1.dev9'
17 changes: 11 additions & 6 deletions finhack/core/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@


class Core:
def __init__(self,project_path=''):
def __init__(self,project_path='',args=True):
self.project_path=project_path
self.usage="""
Expand All @@ -26,13 +26,18 @@ def __init__(self,project_path=''):
finhack trainer auto --vendor=lightgbm #自动进行lightgbm训练
finhack -h
-------------------------------"""
self.generate_args()
self.check_project()
if args:
self.generate_args()
self.check_project()
self.check_env()
self.refresh_runtime()
self.append_args()
self.generate_global()
self.init_logger()
if args:
self.append_args()
self.generate_global()
self.init_logger()
else:
sys.path.append(self.project_path+'/data/cache/')


def init_logger(self):
from runtime.constant import LOGS_DIR
Expand Down
1 change: 1 addition & 0 deletions finhack/core/loader/base_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def background(self,args):

def run(self):
klass=self.klass
klass.args=self.args
klass.run()


Expand Down
1 change: 0 additions & 1 deletion finhack/factor/default/alphaEngine.py
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,6 @@ def calc(formula='',df=pd.DataFrame(),name="alpha",check=False,replace=False):
df=factorManager.getFactors(factor_list=col_list,cache=True)
else:
df=df.sort_index()


if diff_date>0 and diff_date<100:
dt=datetime.datetime.strptime(str(max_date),'%Y%m%d')
Expand Down
10 changes: 9 additions & 1 deletion finhack/factor/default/default_factor.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,12 @@ def show(self):

def analys(self):
factor_name=self.args.factor
factorAnalyzer.alphalens(factor_name)
factorAnalyzer.alphalens(factor_name)

def alpha(self):
formula=self.args.formula
print(formula)
df_alpha=alphaEngine.calc(formula=formula,name="alpha",check=True,replace=False)
print(df_alpha)

factorAnalyzer.alphalens(factor_name='alpha',df=df_alpha)
45 changes: 27 additions & 18 deletions finhack/factor/default/factorAnalyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,19 @@ def all_corr():



def alphalens(factor_name):

def alphalens(factor_name='alpha',df=pd.DataFrame(),notebook=False):

df_industry=AStock.getStockIndustry()

if df.empty:
df=factorManager.getFactors(factor_list=['close',factor_name])
else:
df_close=factorManager.getFactors(factor_list=['close'])

df_close[factor_name]=df
df=df_close

# 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('其他')
Expand Down Expand Up @@ -94,7 +97,7 @@ def alphalens(factor_name):
#plt.show()

# 分位数累积收益
#cumulative_returns_by_qt = al.performance.cumulative_returns_by_quantile(factor_data, period=1)
#Wcumulative_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()

Expand All @@ -111,19 +114,25 @@ def alphalens(factor_name):
#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)

if notebook:
full_tear_sheet = al.tears.create_full_tear_sheet(factor_data, long_short=True, group_neutral=False, by_group=False)
print(full_tear_sheet)
else:

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ testarg2=this is also a test key

[factor]
factor=
formula=


[trainer]
Expand All @@ -29,7 +30,7 @@ max_f=128

[backtest]
cash=10000,20000,50000,100000,200000,500000,1000000
p=3
p=2

[trader]
strategy=DemoStrategy
Expand Down
Loading

0 comments on commit ffcd43f

Please sign in to comment.