Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
A
alpha-mind
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Dr.李
alpha-mind
Commits
fbb26530
Unverified
Commit
fbb26530
authored
Apr 09, 2018
by
iLampard
Committed by
GitHub
Apr 09, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #5 from alpha-miner/master
merge update
parents
02e028fd
a6f2d473
Changes
15
Hide whitespace changes
Inline
Side-by-side
Showing
15 changed files
with
335 additions
and
873 deletions
+335
-873
README.md
README.md
+10
-0
crosssetctions.py
alphamind/analysis/crosssetctions.py
+5
-2
optimizers.pyx
alphamind/cython/optimizers.pyx
+59
-21
models.py
alphamind/data/dbmodel/models.py
+0
-634
universe.py
alphamind/data/engines/universe.py
+10
-10
processing.py
alphamind/data/processing.py
+3
-0
pfopt
alphamind/pfopt
+1
-1
linearbuilder.py
alphamind/portfolio/linearbuilder.py
+4
-3
test_linearbuild.py
alphamind/tests/portfolio/test_linearbuild.py
+19
-0
build_linux_dependencies.sh
build_linux_dependencies.sh
+3
-0
build_windows_dependencies.bat
build_windows_dependencies.bat
+1
-0
Example 3 - Multi Weight Gap Comparison.ipynb
notebooks/Example 3 - Multi Weight Gap Comparison.ipynb
+6
-25
Example 7 - Portfolio Optimizer Performance.ipynb
notebooks/Example 7 - Portfolio Optimizer Performance.ipynb
+203
-175
setup.py
setup.py
+10
-1
xgboost
xgboost
+1
-1
No files found.
README.md
View file @
fbb26530
...
@@ -20,6 +20,16 @@
...
@@ -20,6 +20,16 @@
**Alpha - Mind**
是基于
**Python**
开发的股票多因子研究框架。
**Alpha - Mind**
是基于
**Python**
开发的股票多因子研究框架。
## 依赖
该项目主要有两个主要的github外部依赖:
*
[
portfolio - optimizer
](
https://github.com/alpha-miner/portfolio-optimizer
)
:该项目是相同作者编写的用于资产组合配置的优化器工具包;
*
[
xgboost
](
https://github.com/dmlc/xgboost
)
: 该项目是alpha - mind中一些机器模型的基础库。
这两个库都已经使用git子模块的方式打包到alpha-mind代码库中。
## 功能
## 功能
alpha - mind 提供了多因子研究中常用的工具链,包括:
alpha - mind 提供了多因子研究中常用的工具链,包括:
...
...
alphamind/analysis/crosssetctions.py
View file @
fbb26530
...
@@ -33,7 +33,7 @@ def cs_impl(ref_date,
...
@@ -33,7 +33,7 @@ def cs_impl(ref_date,
total_risk_exp
=
total_data
[
constraint_risk
]
total_risk_exp
=
total_data
[
constraint_risk
]
er
=
total_data
[[
factor_name
]]
.
values
.
astype
(
float
)
er
=
total_data
[[
factor_name
]]
.
values
.
astype
(
float
)
er
=
factor_processing
(
er
,
[
winsorize_normal
,
standardize
],
total_risk_exp
.
values
,
[
winsorize_normal
,
standardize
])
.
flatten
()
er
=
factor_processing
(
er
,
[
winsorize_normal
,
standardize
],
total_risk_exp
.
values
,
[
standardize
])
.
flatten
()
industry
=
total_data
.
industry_name
.
values
industry
=
total_data
.
industry_name
.
values
codes
=
total_data
.
code
.
tolist
()
codes
=
total_data
.
code
.
tolist
()
...
@@ -46,7 +46,10 @@ def cs_impl(ref_date,
...
@@ -46,7 +46,10 @@ def cs_impl(ref_date,
total_risk_exp
=
target_pos
[
constraint_risk
]
total_risk_exp
=
target_pos
[
constraint_risk
]
activate_weight
=
target_pos
[
'weight'
]
.
values
activate_weight
=
target_pos
[
'weight'
]
.
values
excess_return
=
np
.
exp
(
target_pos
[[
'dx'
]]
.
values
)
-
1.
excess_return
=
np
.
exp
(
target_pos
[[
'dx'
]]
.
values
)
-
1.
excess_return
=
factor_processing
(
excess_return
,
[
winsorize_normal
,
standardize
],
total_risk_exp
.
values
,
[
winsorize_normal
,
standardize
])
.
flatten
()
excess_return
=
factor_processing
(
excess_return
,
[
winsorize_normal
,
standardize
],
total_risk_exp
.
values
,
[
winsorize_normal
,
standardize
])
.
flatten
()
port_ret
=
np
.
log
(
activate_weight
@
excess_return
+
1.
)
port_ret
=
np
.
log
(
activate_weight
@
excess_return
+
1.
)
ic
=
np
.
corrcoef
(
excess_return
,
activate_weight
)[
0
,
1
]
ic
=
np
.
corrcoef
(
excess_return
,
activate_weight
)[
0
,
1
]
x
=
sm
.
add_constant
(
activate_weight
)
x
=
sm
.
add_constant
(
activate_weight
)
...
...
alphamind/cython/optimizers.pyx
View file @
fbb26530
...
@@ -7,13 +7,14 @@ Created on 2017-7-20
...
@@ -7,13 +7,14 @@ Created on 2017-7-20
"""
"""
cimport numpy as cnp
cimport numpy as cnp
from libcpp.string cimport string
from libcpp.vector cimport vector
from libcpp.vector cimport vector
import numpy as np
import numpy as np
cdef extern from "lpoptimizer.hpp" namespace "pfopt":
cdef extern from "lpoptimizer.hpp" namespace "pfopt":
cdef cppclass LpOptimizer:
cdef cppclass LpOptimizer:
LpOptimizer(int, int, double*, double*, double*, double*) except +
LpOptimizer(int, int, double*, double*, double*, double*
, string
) except +
vector[double] xValue()
vector[double] xValue()
double feval()
double feval()
int status()
int status()
...
@@ -26,12 +27,15 @@ cdef class LPOptimizer:
...
@@ -26,12 +27,15 @@ cdef class LPOptimizer:
cdef int m
cdef int m
def __cinit__(self,
def __cinit__(self,
cnp.ndarray[double, ndim=2] cons_matrix,
cnp.ndarray[double, ndim=2] cons_matrix,
double[:] lbound,
double[:] lbound,
double[:] ubound,
double[:] ubound,
double[:] objective):
double[:] objective,
str method='simplex'):
self.n = lbound.shape[0]
self.n = lbound.shape[0]
self.m = cons_matrix.shape[0]
self.m = cons_matrix.shape[0]
py_bytes = method.encode('ascii')
cdef string c_str = py_bytes
cdef double[:] cons = cons_matrix.flatten(order='C');
cdef double[:] cons = cons_matrix.flatten(order='C');
self.cobj = new LpOptimizer(self.n,
self.cobj = new LpOptimizer(self.n,
...
@@ -39,7 +43,8 @@ cdef class LPOptimizer:
...
@@ -39,7 +43,8 @@ cdef class LPOptimizer:
&cons[0],
&cons[0],
&lbound[0],
&lbound[0],
&ubound[0],
&ubound[0],
&objective[0])
&objective[0],
c_str)
def __dealloc__(self):
def __dealloc__(self):
del self.cobj
del self.cobj
...
@@ -151,9 +156,25 @@ cdef extern from "mvoptimizer.hpp" namespace "pfopt":
...
@@ -151,9 +156,25 @@ cdef extern from "mvoptimizer.hpp" namespace "pfopt":
int status()
int status()
cdef extern from "qpalglib.hpp" namespace "pfopt":
cdef cppclass QPAlglib:
QPAlglib(int,
double*,
double*,
double*,
double*,
double) except +
vector[double] xValue()
int status()
cdef class QPOptimizer:
cdef class QPOptimizer:
cdef MVOptimizer* cobj
cdef MVOptimizer* cobj
cdef QPAlglib* cobj2
cdef cnp.ndarray er
cdef cnp.ndarray cov
cdef double risk_aversion
cdef int n
cdef int n
cdef int m
cdef int m
...
@@ -169,12 +190,15 @@ cdef class QPOptimizer:
...
@@ -169,12 +190,15 @@ cdef class QPOptimizer:
self.n = lbound.shape[0]
self.n = lbound.shape[0]
self.m = 0
self.m = 0
self.er = np.array(expected_return)
self.cov = np.array(cov_matrix)
self.risk_aversion = risk_aversion
cdef double[:] cov = cov_matrix.flatten(order='C')
cdef double[:] cov = cov_matrix.flatten(order='C')
cdef double[:] cons
cdef double[:] cons
if cons_matrix is not None:
if cons_matrix is not None:
self.m = cons_matrix.shape[0]
self.m = cons_matrix.shape[0]
cons = cons_matrix.flatten(order='C')
;
cons = cons_matrix.flatten(order='C')
self.cobj = new MVOptimizer(self.n,
self.cobj = new MVOptimizer(self.n,
&expected_return[0],
&expected_return[0],
...
@@ -187,25 +211,39 @@ cdef class QPOptimizer:
...
@@ -187,25 +211,39 @@ cdef class QPOptimizer:
&cubound[0],
&cubound[0],
risk_aversion)
risk_aversion)
else:
else:
self.cobj = new MVOptimizer(self.n,
self.cobj2 = new QPAlglib(self.n,
&expected_return[0],
&expected_return[0],
&cov[0],
&cov[0],
&lbound[0],
&lbound[0],
&ubound[0],
&ubound[0],
0,
risk_aversion)
NULL,
NULL,
NULL,
risk_aversion)
def __dealloc__(self):
def __dealloc__(self):
del self.cobj
if self.cobj:
del self.cobj
else:
del self.cobj2
def feval(self):
def feval(self):
return self.cobj.feval()
if self.cobj:
return self.cobj.feval()
else:
x = np.array(self.cobj2.xValue())
return 0.5 * self.risk_aversion * x @ self.cov @ x - self.er @ x
def x_value(self):
def x_value(self):
return np.array(self.cobj.xValue())
if self.cobj:
return np.array(self.cobj.xValue())
else:
return np.array(self.cobj2.xValue())
def status(self):
def status(self):
return self.cobj.status()
if self.cobj:
\ No newline at end of file
return self.cobj.status()
else:
status = self.cobj2.status()
if 1 <= status <= 4:
return 0
else:
return status
alphamind/data/dbmodel/models.py
View file @
fbb26530
...
@@ -12,614 +12,6 @@ Base = declarative_base()
...
@@ -12,614 +12,6 @@ Base = declarative_base()
metadata
=
Base
.
metadata
metadata
=
Base
.
metadata
class
FullFactor
(
Base
):
__tablename__
=
'full_factor'
__table_args__
=
(
Index
(
'full_factor_trade_date_code_uindex'
,
'trade_date'
,
'code'
,
unique
=
True
),
)
trade_date
=
Column
(
DateTime
,
primary_key
=
True
,
nullable
=
False
)
code
=
Column
(
Integer
,
primary_key
=
True
,
nullable
=
False
)
secShortName
=
Column
(
String
(
10
))
exchangeCD
=
Column
(
String
(
4
))
preClosePrice
=
Column
(
Float
(
53
))
actPreClosePrice
=
Column
(
Float
(
53
))
openPrice
=
Column
(
Float
(
53
))
highestPrice
=
Column
(
Float
(
53
))
lowestPrice
=
Column
(
Float
(
53
))
closePrice
=
Column
(
Float
(
53
))
turnoverVol
=
Column
(
BigInteger
)
turnoverValue
=
Column
(
Float
(
53
))
dealAmount
=
Column
(
BigInteger
)
turnoverRate
=
Column
(
Float
(
53
))
accumAdjFactor
=
Column
(
Float
(
53
))
negMarketValue
=
Column
(
Float
(
53
))
marketValue
=
Column
(
Float
(
53
))
chgPct
=
Column
(
Float
(
53
))
isOpen
=
Column
(
Integer
)
vwap
=
Column
(
Float
(
53
))
AccountsPayablesTDays
=
Column
(
Float
(
53
))
AccountsPayablesTRate
=
Column
(
Float
(
53
))
AdminiExpenseRate
=
Column
(
Float
(
53
))
ARTDays
=
Column
(
Float
(
53
))
ARTRate
=
Column
(
Float
(
53
))
ASSI
=
Column
(
Float
(
53
))
BLEV
=
Column
(
Float
(
53
))
BondsPayableToAsset
=
Column
(
Float
(
53
))
CashRateOfSales
=
Column
(
Float
(
53
))
CashToCurrentLiability
=
Column
(
Float
(
53
))
CMRA
=
Column
(
Float
(
53
))
CTOP
=
Column
(
Float
(
53
))
CTP5
=
Column
(
Float
(
53
))
CurrentAssetsRatio
=
Column
(
Float
(
53
))
CurrentAssetsTRate
=
Column
(
Float
(
53
))
CurrentRatio
=
Column
(
Float
(
53
))
DAVOL10
=
Column
(
Float
(
53
))
DAVOL20
=
Column
(
Float
(
53
))
DAVOL5
=
Column
(
Float
(
53
))
DDNBT
=
Column
(
Float
(
53
))
DDNCR
=
Column
(
Float
(
53
))
DDNSR
=
Column
(
Float
(
53
))
DebtEquityRatio
=
Column
(
Float
(
53
))
DebtsAssetRatio
=
Column
(
Float
(
53
))
DHILO
=
Column
(
Float
(
53
))
DilutedEPS
=
Column
(
Float
(
53
))
DVRAT
=
Column
(
Float
(
53
))
EBITToTOR
=
Column
(
Float
(
53
))
EGRO
=
Column
(
Float
(
53
))
EMA10
=
Column
(
Float
(
53
))
EMA120
=
Column
(
Float
(
53
))
EMA20
=
Column
(
Float
(
53
))
EMA5
=
Column
(
Float
(
53
))
EMA60
=
Column
(
Float
(
53
))
EPS
=
Column
(
Float
(
53
))
EquityFixedAssetRatio
=
Column
(
Float
(
53
))
EquityToAsset
=
Column
(
Float
(
53
))
EquityTRate
=
Column
(
Float
(
53
))
ETOP
=
Column
(
Float
(
53
))
ETP5
=
Column
(
Float
(
53
))
FinancialExpenseRate
=
Column
(
Float
(
53
))
FinancingCashGrowRate
=
Column
(
Float
(
53
))
FixAssetRatio
=
Column
(
Float
(
53
))
FixedAssetsTRate
=
Column
(
Float
(
53
))
GrossIncomeRatio
=
Column
(
Float
(
53
))
HBETA
=
Column
(
Float
(
53
))
HSIGMA
=
Column
(
Float
(
53
))
IntangibleAssetRatio
=
Column
(
Float
(
53
))
InventoryTDays
=
Column
(
Float
(
53
))
InventoryTRate
=
Column
(
Float
(
53
))
InvestCashGrowRate
=
Column
(
Float
(
53
))
LCAP
=
Column
(
Float
(
53
))
LFLO
=
Column
(
Float
(
53
))
LongDebtToAsset
=
Column
(
Float
(
53
))
LongDebtToWorkingCapital
=
Column
(
Float
(
53
))
LongTermDebtToAsset
=
Column
(
Float
(
53
))
MA10
=
Column
(
Float
(
53
))
MA120
=
Column
(
Float
(
53
))
MA20
=
Column
(
Float
(
53
))
MA5
=
Column
(
Float
(
53
))
MA60
=
Column
(
Float
(
53
))
MAWVAD
=
Column
(
Float
(
53
))
MFI
=
Column
(
Float
(
53
))
MLEV
=
Column
(
Float
(
53
))
NetAssetGrowRate
=
Column
(
Float
(
53
))
NetProfitGrowRate
=
Column
(
Float
(
53
))
NetProfitRatio
=
Column
(
Float
(
53
))
NOCFToOperatingNI
=
Column
(
Float
(
53
))
NonCurrentAssetsRatio
=
Column
(
Float
(
53
))
NPParentCompanyGrowRate
=
Column
(
Float
(
53
))
NPToTOR
=
Column
(
Float
(
53
))
OperatingExpenseRate
=
Column
(
Float
(
53
))
OperatingProfitGrowRate
=
Column
(
Float
(
53
))
OperatingProfitRatio
=
Column
(
Float
(
53
))
OperatingProfitToTOR
=
Column
(
Float
(
53
))
OperatingRevenueGrowRate
=
Column
(
Float
(
53
))
OperCashGrowRate
=
Column
(
Float
(
53
))
OperCashInToCurrentLiability
=
Column
(
Float
(
53
))
PB
=
Column
(
Float
(
53
))
PCF
=
Column
(
Float
(
53
))
PE
=
Column
(
Float
(
53
))
PS
=
Column
(
Float
(
53
))
PSY
=
Column
(
Float
(
53
))
QuickRatio
=
Column
(
Float
(
53
))
REVS10
=
Column
(
Float
(
53
))
REVS20
=
Column
(
Float
(
53
))
REVS5
=
Column
(
Float
(
53
))
ROA
=
Column
(
Float
(
53
))
ROA5
=
Column
(
Float
(
53
))
ROE
=
Column
(
Float
(
53
))
ROE5
=
Column
(
Float
(
53
))
RSI
=
Column
(
Float
(
53
))
RSTR12
=
Column
(
Float
(
53
))
RSTR24
=
Column
(
Float
(
53
))
SalesCostRatio
=
Column
(
Float
(
53
))
SaleServiceCashToOR
=
Column
(
Float
(
53
))
SUE
=
Column
(
Float
(
53
))
TaxRatio
=
Column
(
Float
(
53
))
TOBT
=
Column
(
Float
(
53
))
TotalAssetGrowRate
=
Column
(
Float
(
53
))
TotalAssetsTRate
=
Column
(
Float
(
53
))
TotalProfitCostRatio
=
Column
(
Float
(
53
))
TotalProfitGrowRate
=
Column
(
Float
(
53
))
VOL10
=
Column
(
Float
(
53
))
VOL120
=
Column
(
Float
(
53
))
VOL20
=
Column
(
Float
(
53
))
VOL240
=
Column
(
Float
(
53
))
VOL5
=
Column
(
Float
(
53
))
VOL60
=
Column
(
Float
(
53
))
WVAD
=
Column
(
Float
(
53
))
REC
=
Column
(
Float
(
53
))
DAREC
=
Column
(
Float
(
53
))
GREC
=
Column
(
Float
(
53
))
FY12P
=
Column
(
Float
(
53
))
DAREV
=
Column
(
Float
(
53
))
GREV
=
Column
(
Float
(
53
))
SFY12P
=
Column
(
Float
(
53
))
DASREV
=
Column
(
Float
(
53
))
GSREV
=
Column
(
Float
(
53
))
FEARNG
=
Column
(
Float
(
53
))
FSALESG
=
Column
(
Float
(
53
))
TA2EV
=
Column
(
Float
(
53
))
CFO2EV
=
Column
(
Float
(
53
))
ACCA
=
Column
(
Float
(
53
))
DEGM
=
Column
(
Float
(
53
))
SUOI
=
Column
(
Float
(
53
))
EARNMOM
=
Column
(
Float
(
53
))
FiftyTwoWeekHigh
=
Column
(
Float
(
53
))
Volatility
=
Column
(
Float
(
53
))
Skewness
=
Column
(
Float
(
53
))
ILLIQUIDITY
=
Column
(
Float
(
53
))
BackwardADJ
=
Column
(
Float
(
53
))
MACD
=
Column
(
Float
(
53
))
ADTM
=
Column
(
Float
(
53
))
ATR14
=
Column
(
Float
(
53
))
ATR6
=
Column
(
Float
(
53
))
BIAS10
=
Column
(
Float
(
53
))
BIAS20
=
Column
(
Float
(
53
))
BIAS5
=
Column
(
Float
(
53
))
BIAS60
=
Column
(
Float
(
53
))
BollDown
=
Column
(
Float
(
53
))
BollUp
=
Column
(
Float
(
53
))
CCI10
=
Column
(
Float
(
53
))
CCI20
=
Column
(
Float
(
53
))
CCI5
=
Column
(
Float
(
53
))
CCI88
=
Column
(
Float
(
53
))
KDJ_K
=
Column
(
Float
(
53
))
KDJ_D
=
Column
(
Float
(
53
))
KDJ_J
=
Column
(
Float
(
53
))
ROC6
=
Column
(
Float
(
53
))
ROC20
=
Column
(
Float
(
53
))
SBM
=
Column
(
Float
(
53
))
STM
=
Column
(
Float
(
53
))
UpRVI
=
Column
(
Float
(
53
))
DownRVI
=
Column
(
Float
(
53
))
RVI
=
Column
(
Float
(
53
))
SRMI
=
Column
(
Float
(
53
))
ChandeSD
=
Column
(
Float
(
53
))
ChandeSU
=
Column
(
Float
(
53
))
CMO
=
Column
(
Float
(
53
))
DBCD
=
Column
(
Float
(
53
))
ARC
=
Column
(
Float
(
53
))
OBV
=
Column
(
Float
(
53
))
OBV6
=
Column
(
Float
(
53
))
OBV20
=
Column
(
Float
(
53
))
TVMA20
=
Column
(
Float
(
53
))
TVMA6
=
Column
(
Float
(
53
))
TVSTD20
=
Column
(
Float
(
53
))
TVSTD6
=
Column
(
Float
(
53
))
VDEA
=
Column
(
Float
(
53
))
VDIFF
=
Column
(
Float
(
53
))
VEMA10
=
Column
(
Float
(
53
))
VEMA12
=
Column
(
Float
(
53
))
VEMA26
=
Column
(
Float
(
53
))
VEMA5
=
Column
(
Float
(
53
))
VMACD
=
Column
(
Float
(
53
))
VOSC
=
Column
(
Float
(
53
))
VR
=
Column
(
Float
(
53
))
VROC12
=
Column
(
Float
(
53
))
VROC6
=
Column
(
Float
(
53
))
VSTD10
=
Column
(
Float
(
53
))
VSTD20
=
Column
(
Float
(
53
))
KlingerOscillator
=
Column
(
Float
(
53
))
MoneyFlow20
=
Column
(
Float
(
53
))
AD
=
Column
(
Float
(
53
))
AD20
=
Column
(
Float
(
53
))
AD6
=
Column
(
Float
(
53
))
CoppockCurve
=
Column
(
Float
(
53
))
ASI
=
Column
(
Float
(
53
))
ChaikinOscillator
=
Column
(
Float
(
53
))
ChaikinVolatility
=
Column
(
Float
(
53
))
EMV14
=
Column
(
Float
(
53
))
EMV6
=
Column
(
Float
(
53
))
plusDI
=
Column
(
Float
(
53
))
minusDI
=
Column
(
Float
(
53
))
ADX
=
Column
(
Float
(
53
))
ADXR
=
Column
(
Float
(
53
))
Aroon
=
Column
(
Float
(
53
))
AroonDown
=
Column
(
Float
(
53
))
AroonUp
=
Column
(
Float
(
53
))
DEA
=
Column
(
Float
(
53
))
DIFF
=
Column
(
Float
(
53
))
DDI
=
Column
(
Float
(
53
))
DIZ
=
Column
(
Float
(
53
))
DIF
=
Column
(
Float
(
53
))
MTM
=
Column
(
Float
(
53
))
MTMMA
=
Column
(
Float
(
53
))
PVT
=
Column
(
Float
(
53
))
PVT6
=
Column
(
Float
(
53
))
PVT12
=
Column
(
Float
(
53
))
TRIX5
=
Column
(
Float
(
53
))
TRIX10
=
Column
(
Float
(
53
))
UOS
=
Column
(
Float
(
53
))
MA10RegressCoeff12
=
Column
(
Float
(
53
))
MA10RegressCoeff6
=
Column
(
Float
(
53
))
PLRC6
=
Column
(
Float
(
53
))
PLRC12
=
Column
(
Float
(
53
))
SwingIndex
=
Column
(
Float
(
53
))
Ulcer10
=
Column
(
Float
(
53
))
Ulcer5
=
Column
(
Float
(
53
))
Hurst
=
Column
(
Float
(
53
))
ACD6
=
Column
(
Float
(
53
))
ACD20
=
Column
(
Float
(
53
))
EMA12
=
Column
(
Float
(
53
))
EMA26
=
Column
(
Float
(
53
))
APBMA
=
Column
(
Float
(
53
))
BBI
=
Column
(
Float
(
53
))
BBIC
=
Column
(
Float
(
53
))
TEMA10
=
Column
(
Float
(
53
))
TEMA5
=
Column
(
Float
(
53
))
MA10Close
=
Column
(
Float
(
53
))
AR
=
Column
(
Float
(
53
))
BR
=
Column
(
Float
(
53
))
ARBR
=
Column
(
Float
(
53
))
CR20
=
Column
(
Float
(
53
))
MassIndex
=
Column
(
Float
(
53
))
BearPower
=
Column
(
Float
(
53
))
BullPower
=
Column
(
Float
(
53
))
Elder
=
Column
(
Float
(
53
))
NVI
=
Column
(
Float
(
53
))
PVI
=
Column
(
Float
(
53
))
RC12
=
Column
(
Float
(
53
))
RC24
=
Column
(
Float
(
53
))
JDQS20
=
Column
(
Float
(
53
))
Variance20
=
Column
(
Float
(
53
))
Variance60
=
Column
(
Float
(
53
))
Variance120
=
Column
(
Float
(
53
))
Kurtosis20
=
Column
(
Float
(
53
))
Kurtosis60
=
Column
(
Float
(
53
))
Kurtosis120
=
Column
(
Float
(
53
))
Alpha20
=
Column
(
Float
(
53
))
Alpha60
=
Column
(
Float
(
53
))
Alpha120
=
Column
(
Float
(
53
))
Beta20
=
Column
(
Float
(
53
))
Beta60
=
Column
(
Float
(
53
))
Beta120
=
Column
(
Float
(
53
))
SharpeRatio20
=
Column
(
Float
(
53
))
SharpeRatio60
=
Column
(
Float
(
53
))
SharpeRatio120
=
Column
(
Float
(
53
))
TreynorRatio20
=
Column
(
Float
(
53
))
TreynorRatio60
=
Column
(
Float
(
53
))
TreynorRatio120
=
Column
(
Float
(
53
))
InformationRatio20
=
Column
(
Float
(
53
))
InformationRatio60
=
Column
(
Float
(
53
))
InformationRatio120
=
Column
(
Float
(
53
))
GainVariance20
=
Column
(
Float
(
53
))
GainVariance60
=
Column
(
Float
(
53
))
GainVariance120
=
Column
(
Float
(
53
))
LossVariance20
=
Column
(
Float
(
53
))
LossVariance60
=
Column
(
Float
(
53
))
LossVariance120
=
Column
(
Float
(
53
))
GainLossVarianceRatio20
=
Column
(
Float
(
53
))
GainLossVarianceRatio60
=
Column
(
Float
(
53
))
GainLossVarianceRatio120
=
Column
(
Float
(
53
))
RealizedVolatility
=
Column
(
Float
(
53
))
REVS60
=
Column
(
Float
(
53
))
REVS120
=
Column
(
Float
(
53
))
REVS250
=
Column
(
Float
(
53
))
REVS750
=
Column
(
Float
(
53
))
REVS5m20
=
Column
(
Float
(
53
))
REVS5m60
=
Column
(
Float
(
53
))
REVS5Indu1
=
Column
(
Float
(
53
))
REVS20Indu1
=
Column
(
Float
(
53
))
Volumn1M
=
Column
(
Float
(
53
))
Volumn3M
=
Column
(
Float
(
53
))
Price1M
=
Column
(
Float
(
53
))
Price3M
=
Column
(
Float
(
53
))
Price1Y
=
Column
(
Float
(
53
))
Rank1M
=
Column
(
Float
(
53
))
CashDividendCover
=
Column
(
Float
(
53
))
DividendCover
=
Column
(
Float
(
53
))
DividendPaidRatio
=
Column
(
Float
(
53
))
RetainedEarningRatio
=
Column
(
Float
(
53
))
CashEquivalentPS
=
Column
(
Float
(
53
))
DividendPS
=
Column
(
Float
(
53
))
EPSTTM
=
Column
(
Float
(
53
))
NetAssetPS
=
Column
(
Float
(
53
))
TORPS
=
Column
(
Float
(
53
))
TORPSLatest
=
Column
(
Float
(
53
))
OperatingRevenuePS
=
Column
(
Float
(
53
))
OperatingRevenuePSLatest
=
Column
(
Float
(
53
))
OperatingProfitPS
=
Column
(
Float
(
53
))
OperatingProfitPSLatest
=
Column
(
Float
(
53
))
CapitalSurplusFundPS
=
Column
(
Float
(
53
))
SurplusReserveFundPS
=
Column
(
Float
(
53
))
UndividedProfitPS
=
Column
(
Float
(
53
))
RetainedEarningsPS
=
Column
(
Float
(
53
))
OperCashFlowPS
=
Column
(
Float
(
53
))
CashFlowPS
=
Column
(
Float
(
53
))
NetNonOIToTP
=
Column
(
Float
(
53
))
NetNonOIToTPLatest
=
Column
(
Float
(
53
))
PeriodCostsRate
=
Column
(
Float
(
53
))
InterestCover
=
Column
(
Float
(
53
))
NetProfitGrowRate3Y
=
Column
(
Float
(
53
))
NetProfitGrowRate5Y
=
Column
(
Float
(
53
))
OperatingRevenueGrowRate3Y
=
Column
(
Float
(
53
))
OperatingRevenueGrowRate5Y
=
Column
(
Float
(
53
))
NetCashFlowGrowRate
=
Column
(
Float
(
53
))
NetProfitCashCover
=
Column
(
Float
(
53
))
OperCashInToAsset
=
Column
(
Float
(
53
))
CashConversionCycle
=
Column
(
Float
(
53
))
OperatingCycle
=
Column
(
Float
(
53
))
PEG3Y
=
Column
(
Float
(
53
))
PEG5Y
=
Column
(
Float
(
53
))
PEIndu
=
Column
(
Float
(
53
))
PBIndu
=
Column
(
Float
(
53
))
PSIndu
=
Column
(
Float
(
53
))
PCFIndu
=
Column
(
Float
(
53
))
PEHist20
=
Column
(
Float
(
53
))
PEHist60
=
Column
(
Float
(
53
))
PEHist120
=
Column
(
Float
(
53
))
PEHist250
=
Column
(
Float
(
53
))
StaticPE
=
Column
(
Float
(
53
))
ForwardPE
=
Column
(
Float
(
53
))
EnterpriseFCFPS
=
Column
(
Float
(
53
))
ShareholderFCFPS
=
Column
(
Float
(
53
))
ROEDiluted
=
Column
(
Float
(
53
))
ROEAvg
=
Column
(
Float
(
53
))
ROEWeighted
=
Column
(
Float
(
53
))
ROECut
=
Column
(
Float
(
53
))
ROECutWeighted
=
Column
(
Float
(
53
))
ROIC
=
Column
(
Float
(
53
))
ROAEBIT
=
Column
(
Float
(
53
))
ROAEBITTTM
=
Column
(
Float
(
53
))
OperatingNIToTP
=
Column
(
Float
(
53
))
OperatingNIToTPLatest
=
Column
(
Float
(
53
))
InvestRAssociatesToTP
=
Column
(
Float
(
53
))
InvestRAssociatesToTPLatest
=
Column
(
Float
(
53
))
NPCutToNP
=
Column
(
Float
(
53
))
SuperQuickRatio
=
Column
(
Float
(
53
))
TSEPToInterestBearDebt
=
Column
(
Float
(
53
))
DebtTangibleEquityRatio
=
Column
(
Float
(
53
))
TangibleAToInteBearDebt
=
Column
(
Float
(
53
))
TangibleAToNetDebt
=
Column
(
Float
(
53
))
NOCFToTLiability
=
Column
(
Float
(
53
))
NOCFToInterestBearDebt
=
Column
(
Float
(
53
))
NOCFToNetDebt
=
Column
(
Float
(
53
))
TSEPToTotalCapital
=
Column
(
Float
(
53
))
InteBearDebtToTotalCapital
=
Column
(
Float
(
53
))
NPParentCompanyCutYOY
=
Column
(
Float
(
53
))
SalesServiceCashToORLatest
=
Column
(
Float
(
53
))
CashRateOfSalesLatest
=
Column
(
Float
(
53
))
NOCFToOperatingNILatest
=
Column
(
Float
(
53
))
TotalAssets
=
Column
(
Float
(
53
))
MktValue
=
Column
(
Float
(
53
))
NegMktValue
=
Column
(
Float
(
53
))
TEAP
=
Column
(
Float
(
53
))
NIAP
=
Column
(
Float
(
53
))
TotalFixedAssets
=
Column
(
Float
(
53
))
IntFreeCL
=
Column
(
Float
(
53
))
IntFreeNCL
=
Column
(
Float
(
53
))
IntCL
=
Column
(
Float
(
53
))
IntDebt
=
Column
(
Float
(
53
))
NetDebt
=
Column
(
Float
(
53
))
NetTangibleAssets
=
Column
(
Float
(
53
))
WorkingCapital
=
Column
(
Float
(
53
))
NetWorkingCapital
=
Column
(
Float
(
53
))
TotalPaidinCapital
=
Column
(
Float
(
53
))
RetainedEarnings
=
Column
(
Float
(
53
))
OperateNetIncome
=
Column
(
Float
(
53
))
ValueChgProfit
=
Column
(
Float
(
53
))
NetIntExpense
=
Column
(
Float
(
53
))
EBIT
=
Column
(
Float
(
53
))
EBITDA
=
Column
(
Float
(
53
))
EBIAT
=
Column
(
Float
(
53
))
NRProfitLoss
=
Column
(
Float
(
53
))
NIAPCut
=
Column
(
Float
(
53
))
FCFF
=
Column
(
Float
(
53
))
FCFE
=
Column
(
Float
(
53
))
DA
=
Column
(
Float
(
53
))
TRevenueTTM
=
Column
(
Float
(
53
))
TCostTTM
=
Column
(
Float
(
53
))
RevenueTTM
=
Column
(
Float
(
53
))
CostTTM
=
Column
(
Float
(
53
))
GrossProfitTTM
=
Column
(
Float
(
53
))
SalesExpenseTTM
=
Column
(
Float
(
53
))
AdminExpenseTTM
=
Column
(
Float
(
53
))
FinanExpenseTTM
=
Column
(
Float
(
53
))
AssetImpairLossTTM
=
Column
(
Float
(
53
))
NPFromOperatingTTM
=
Column
(
Float
(
53
))
NPFromValueChgTTM
=
Column
(
Float
(
53
))
OperateProfitTTM
=
Column
(
Float
(
53
))
NonOperatingNPTTM
=
Column
(
Float
(
53
))
TProfitTTM
=
Column
(
Float
(
53
))
NetProfitTTM
=
Column
(
Float
(
53
))
NetProfitAPTTM
=
Column
(
Float
(
53
))
SaleServiceRenderCashTTM
=
Column
(
Float
(
53
))
NetOperateCFTTM
=
Column
(
Float
(
53
))
NetInvestCFTTM
=
Column
(
Float
(
53
))
NetFinanceCFTTM
=
Column
(
Float
(
53
))
GrossProfit
=
Column
(
Float
(
53
))
Beta252
=
Column
(
Float
(
53
))
RSTR504
=
Column
(
Float
(
53
))
EPIBS
=
Column
(
Float
(
53
))
CETOP
=
Column
(
Float
(
53
))
DASTD
=
Column
(
Float
(
53
))
CmraCNE5
=
Column
(
Float
(
53
))
HsigmaCNE5
=
Column
(
Float
(
53
))
SGRO
=
Column
(
Float
(
53
))
EgibsLong
=
Column
(
Float
(
53
))
STOM
=
Column
(
Float
(
53
))
STOQ
=
Column
(
Float
(
53
))
STOA
=
Column
(
Float
(
53
))
NLSIZE
=
Column
(
Float
(
53
))
ROEAfterNonRecurring
=
Column
(
Float
(
53
))
EPSAfterNonRecurring
=
Column
(
Float
(
53
))
EODPrice
=
Column
(
Float
(
53
))
LogFloatCap
=
Column
(
Float
(
53
))
BPS
=
Column
(
Float
(
53
))
SPS
=
Column
(
Float
(
53
))
DebtToAsset
=
Column
(
Float
(
53
))
DROEAfterNonRecurring
=
Column
(
Float
(
53
))
LogTotalCap
=
Column
(
Float
(
53
))
BP
=
Column
(
Float
(
53
))
SP
=
Column
(
Float
(
53
))
EPAfterNonRecurring
=
Column
(
Float
(
53
))
DivToB
=
Column
(
Float
(
53
))
DivP
=
Column
(
Float
(
53
))
EBITToSales
=
Column
(
Float
(
53
))
EBITAToSales
=
Column
(
Float
(
53
))
EVToSales
=
Column
(
Float
(
53
))
EVToEBIT
=
Column
(
Float
(
53
))
EVToEBITDA
=
Column
(
Float
(
53
))
EVToNOPLAT
=
Column
(
Float
(
53
))
EVToIC
=
Column
(
Float
(
53
))
FCFFPS
=
Column
(
Float
(
53
))
FCFFToEarningAfterNonRecurring
=
Column
(
Float
(
53
))
FCFFP
=
Column
(
Float
(
53
))
ProfitToAsset
=
Column
(
Float
(
53
))
GrossProfitRatio
=
Column
(
Float
(
53
))
LATO
=
Column
(
Float
(
53
))
FATO
=
Column
(
Float
(
53
))
TATO
=
Column
(
Float
(
53
))
EquityTO
=
Column
(
Float
(
53
))
PayableTO
=
Column
(
Float
(
53
))
RecievableTO
=
Column
(
Float
(
53
))
RevenueGrowth
=
Column
(
Float
(
53
))
GrossProfitGrowth
=
Column
(
Float
(
53
))
NetProfitGrowth
=
Column
(
Float
(
53
))
GrossCFToRevenue
=
Column
(
Float
(
53
))
CFToRevenue
=
Column
(
Float
(
53
))
CFToProfit
=
Column
(
Float
(
53
))
CFToAsset
=
Column
(
Float
(
53
))
GrossCFGrowth
=
Column
(
Float
(
53
))
CFGrowth
=
Column
(
Float
(
53
))
ICFGrowth
=
Column
(
Float
(
53
))
AveAmount60
=
Column
(
Float
(
53
))
PeriodReturn60
=
Column
(
Float
(
53
))
AmountRatio60to250
=
Column
(
Float
(
53
))
CFPS
=
Column
(
Float
(
53
))
CFP
=
Column
(
Float
(
53
))
NetCFGrowth
=
Column
(
Float
(
53
))
NetCFGrowthP
=
Column
(
Float
(
53
))
NetCash
=
Column
(
Float
(
53
))
NetCashP
=
Column
(
Float
(
53
))
BVPSGrowth
=
Column
(
Float
(
53
))
EquityPSGrowth
=
Column
(
Float
(
53
))
WholeSales
=
Column
(
Float
(
53
))
WholeProfitAfterNonRecurring
=
Column
(
Float
(
53
))
ExpenseRatio
=
Column
(
Float
(
53
))
AcidTestRatio
=
Column
(
Float
(
53
))
TimeInterestEarnedRatio
=
Column
(
Float
(
53
))
DepositReceivedVsSale
=
Column
(
Float
(
53
))
DebtRatioExcemptDepRec
=
Column
(
Float
(
53
))
SNBARatio
=
Column
(
Float
(
53
))
CFinc1
=
Column
(
Float
(
53
))
BDTO
=
Column
(
Float
(
53
))
RVOL
=
Column
(
Float
(
53
))
CHV
=
Column
(
Float
(
53
))
VAL
=
Column
(
Float
(
53
))
BETA
=
Column
(
Float
(
53
))
MOMENTUM
=
Column
(
Float
(
53
))
SIZE
=
Column
(
Float
(
53
))
EARNYILD
=
Column
(
Float
(
53
))
RESVOL
=
Column
(
Float
(
53
))
GROWTH
=
Column
(
Float
(
53
))
BTOP
=
Column
(
Float
(
53
))
LEVERAGE
=
Column
(
Float
(
53
))
LIQUIDTY
=
Column
(
Float
(
53
))
SIZENL
=
Column
(
Float
(
53
))
Bank
=
Column
(
BigInteger
)
RealEstate
=
Column
(
BigInteger
)
Health
=
Column
(
BigInteger
)
Transportation
=
Column
(
BigInteger
)
Mining
=
Column
(
BigInteger
)
NonFerMetal
=
Column
(
BigInteger
)
HouseApp
=
Column
(
BigInteger
)
LeiService
=
Column
(
BigInteger
)
MachiEquip
=
Column
(
BigInteger
)
BuildDeco
=
Column
(
BigInteger
)
CommeTrade
=
Column
(
BigInteger
)
CONMAT
=
Column
(
BigInteger
)
Auto
=
Column
(
BigInteger
)
Textile
=
Column
(
BigInteger
)
FoodBever
=
Column
(
BigInteger
)
Electronics
=
Column
(
BigInteger
)
Computer
=
Column
(
BigInteger
)
LightIndus
=
Column
(
BigInteger
)
Utilities
=
Column
(
BigInteger
)
Telecom
=
Column
(
BigInteger
)
AgriForest
=
Column
(
BigInteger
)
CHEM
=
Column
(
BigInteger
)
Media
=
Column
(
BigInteger
)
IronSteel
=
Column
(
BigInteger
)
NonBankFinan
=
Column
(
BigInteger
)
ELECEQP
=
Column
(
BigInteger
)
AERODEF
=
Column
(
BigInteger
)
Conglomerates
=
Column
(
BigInteger
)
COUNTRY
=
Column
(
BigInteger
)
DROE
=
Column
(
Float
(
53
))
IVR
=
Column
(
Float
(
53
))
xueqiu_hotness
=
Column
(
Float
(
53
))
con_eps
=
Column
(
Float
(
53
))
con_eps_rolling
=
Column
(
Float
(
53
))
con_na
=
Column
(
Float
(
53
))
con_na_rolling
=
Column
(
Float
(
53
))
con_np
=
Column
(
Float
(
53
))
con_npcgrate_1w
=
Column
(
Float
(
53
))
con_npcgrate_4w
=
Column
(
Float
(
53
))
con_npcgrate_13w
=
Column
(
Float
(
53
))
con_npcgrate_26w
=
Column
(
Float
(
53
))
con_npcgrate_52w
=
Column
(
Float
(
53
))
con_npcgrate_2y
=
Column
(
Float
(
53
))
con_np_rolling
=
Column
(
Float
(
53
))
con_np_yoy
=
Column
(
Float
(
53
))
con_pb
=
Column
(
Float
(
53
))
con_pb_order
=
Column
(
Float
(
53
))
con_pb_rolling
=
Column
(
Float
(
53
))
con_pb_rolling_order
=
Column
(
Float
(
53
))
con_or
=
Column
(
Float
(
53
))
con_pe
=
Column
(
Float
(
53
))
con_pe_order
=
Column
(
Float
(
53
))
con_pe_rolling
=
Column
(
Float
(
53
))
con_pe_rolling_order
=
Column
(
Float
(
53
))
con_peg
=
Column
(
Float
(
53
))
con_peg_order
=
Column
(
Float
(
53
))
con_peg_rolling
=
Column
(
Float
(
53
))
con_peg_rolling_order
=
Column
(
Float
(
53
))
con_roe
=
Column
(
Float
(
53
))
con_target_price
=
Column
(
Float
(
53
))
market_confidence_5d
=
Column
(
Float
(
53
))
market_confidence_10d
=
Column
(
Float
(
53
))
market_confidence_15d
=
Column
(
Float
(
53
))
market_confidence_25d
=
Column
(
Float
(
53
))
market_confidence_75d
=
Column
(
Float
(
53
))
mcap
=
Column
(
Float
(
53
))
optimism_confidence_5d
=
Column
(
Float
(
53
))
optimism_confidence_10d
=
Column
(
Float
(
53
))
optimism_confidence_15d
=
Column
(
Float
(
53
))
optimism_confidence_25d
=
Column
(
Float
(
53
))
optimism_confidence_75d
=
Column
(
Float
(
53
))
pessimism_confidence_5d
=
Column
(
Float
(
53
))
pessimism_confidence_10d
=
Column
(
Float
(
53
))
pessimism_confidence_15d
=
Column
(
Float
(
53
))
pessimism_confidence_25d
=
Column
(
Float
(
53
))
pessimism_confidence_75d
=
Column
(
Float
(
53
))
tcap
=
Column
(
Float
(
53
))
d_srisk
=
Column
(
Float
(
53
))
s_srisk
=
Column
(
Float
(
53
))
l_srisk
=
Column
(
Float
(
53
))
class
DailyPortfolios
(
Base
):
class
DailyPortfolios
(
Base
):
__tablename__
=
'daily_portfolios'
__tablename__
=
'daily_portfolios'
__table_args__
=
(
__table_args__
=
(
...
@@ -924,29 +316,6 @@ class Positions(Base):
...
@@ -924,29 +316,6 @@ class Positions(Base):
weight
=
Column
(
JSON
)
weight
=
Column
(
JSON
)
class
QuantileAnalysis
(
Base
):
__tablename__
=
'quantile_analysis'
__table_args__
=
(
Index
(
'quantile_idx'
,
'trade_date'
,
'portfolio'
,
'universe'
,
'benchmark'
,
unique
=
True
),
)
trade_date
=
Column
(
DateTime
,
primary_key
=
True
,
nullable
=
False
)
portfolio
=
Column
(
String
(
50
),
primary_key
=
True
,
nullable
=
False
)
universe
=
Column
(
String
(
20
),
primary_key
=
True
,
nullable
=
False
)
benchmark
=
Column
(
Integer
,
primary_key
=
True
,
nullable
=
False
)
source
=
Column
(
String
(
20
),
primary_key
=
True
,
nullable
=
False
)
q1
=
Column
(
Float
(
53
))
q2
=
Column
(
Float
(
53
))
q3
=
Column
(
Float
(
53
))
q4
=
Column
(
Float
(
53
))
q5
=
Column
(
Float
(
53
))
q6
=
Column
(
Float
(
53
))
q7
=
Column
(
Float
(
53
))
q8
=
Column
(
Float
(
53
))
q9
=
Column
(
Float
(
53
))
q10
=
Column
(
Float
(
53
))
class
RebalanceLog
(
Base
):
class
RebalanceLog
(
Base
):
__tablename__
=
'rebalance_log'
__tablename__
=
'rebalance_log'
__table_args__
=
(
__table_args__
=
(
...
@@ -1124,8 +493,6 @@ class RiskExposure(Base):
...
@@ -1124,8 +493,6 @@ class RiskExposure(Base):
trade_date
=
Column
(
DateTime
,
primary_key
=
True
,
nullable
=
False
)
trade_date
=
Column
(
DateTime
,
primary_key
=
True
,
nullable
=
False
)
code
=
Column
(
Integer
,
primary_key
=
True
,
nullable
=
False
)
code
=
Column
(
Integer
,
primary_key
=
True
,
nullable
=
False
)
exchangeCD
=
Column
(
String
(
4
))
secShortName
=
Column
(
String
(
20
))
BETA
=
Column
(
Float
(
53
))
BETA
=
Column
(
Float
(
53
))
MOMENTUM
=
Column
(
Float
(
53
))
MOMENTUM
=
Column
(
Float
(
53
))
SIZE
=
Column
(
Float
(
53
))
SIZE
=
Column
(
Float
(
53
))
...
@@ -1165,7 +532,6 @@ class RiskExposure(Base):
...
@@ -1165,7 +532,6 @@ class RiskExposure(Base):
AERODEF
=
Column
(
BigInteger
)
AERODEF
=
Column
(
BigInteger
)
Conglomerates
=
Column
(
BigInteger
)
Conglomerates
=
Column
(
BigInteger
)
COUNTRY
=
Column
(
BigInteger
)
COUNTRY
=
Column
(
BigInteger
)
updateTime
=
Column
(
DateTime
)
class
RiskMaster
(
Base
):
class
RiskMaster
(
Base
):
...
...
alphamind/data/engines/universe.py
View file @
fbb26530
...
@@ -14,8 +14,8 @@ from sqlalchemy import select
...
@@ -14,8 +14,8 @@ from sqlalchemy import select
from
sqlalchemy
import
join
from
sqlalchemy
import
join
from
sqlalchemy
import
outerjoin
from
sqlalchemy
import
outerjoin
from
alphamind.data.dbmodel.models
import
Universe
as
UniverseTable
from
alphamind.data.dbmodel.models
import
Universe
as
UniverseTable
from
alphamind.data.dbmodel.models
import
FullFactor
from
alphamind.data.engines.utilities
import
_map_factors
from
alphamind.data.engines.utilities
import
_map_factors
from
alphamind.data.dbmodel.models
import
Market
from
alphamind.data.engines.utilities
import
factor_tables
from
alphamind.data.engines.utilities
import
factor_tables
from
alphamind.data.transformer
import
Transformer
from
alphamind.data.transformer
import
Transformer
from
alphamind.utilities
import
encode
from
alphamind.utilities
import
encode
...
@@ -86,22 +86,22 @@ class Universe(object):
...
@@ -86,22 +86,22 @@ class Universe(object):
dependency
=
transformer
.
dependency
dependency
=
transformer
.
dependency
factor_cols
=
_map_factors
(
dependency
,
factor_tables
)
factor_cols
=
_map_factors
(
dependency
,
factor_tables
)
big_table
=
FullFactor
big_table
=
Market
for
t
in
set
(
factor_cols
.
values
()):
for
t
in
set
(
factor_cols
.
values
()):
if
t
.
__table__
.
name
!=
FullFactor
.
__table__
.
name
:
if
t
.
__table__
.
name
!=
Market
.
__table__
.
name
:
big_table
=
outerjoin
(
big_table
,
t
,
and_
(
FullFactor
.
trade_date
==
t
.
trade_date
,
big_table
=
outerjoin
(
big_table
,
t
,
and_
(
Market
.
trade_date
==
t
.
trade_date
,
FullFactor
.
code
==
t
.
code
,
Market
.
code
==
t
.
code
,
FullFactor
.
trade_date
.
in_
(
Market
.
trade_date
.
in_
(
dates
)
if
dates
else
FullFactor
.
trade_date
.
between
(
dates
)
if
dates
else
Market
.
trade_date
.
between
(
start_date
,
end_date
)))
start_date
,
end_date
)))
big_table
=
join
(
big_table
,
UniverseTable
,
big_table
=
join
(
big_table
,
UniverseTable
,
and_
(
FullFactor
.
trade_date
==
UniverseTable
.
trade_date
,
and_
(
Market
.
trade_date
==
UniverseTable
.
trade_date
,
FullFactor
.
code
==
UniverseTable
.
code
,
Market
.
code
==
UniverseTable
.
code
,
universe_cond
))
universe_cond
))
query
=
select
(
query
=
select
(
[
FullFactor
.
trade_date
,
FullFactor
.
code
]
+
list
(
factor_cols
.
keys
()))
\
[
Market
.
trade_date
,
Market
.
code
]
+
list
(
factor_cols
.
keys
()))
\
.
select_from
(
big_table
)
.
distinct
()
.
select_from
(
big_table
)
.
distinct
()
df
=
pd
.
read_sql
(
query
,
engine
.
engine
)
.
sort_values
([
'trade_date'
,
'code'
])
.
dropna
()
df
=
pd
.
read_sql
(
query
,
engine
.
engine
)
.
sort_values
([
'trade_date'
,
'code'
])
.
dropna
()
...
...
alphamind/data/processing.py
View file @
fbb26530
...
@@ -9,6 +9,7 @@ from typing import Optional
...
@@ -9,6 +9,7 @@ from typing import Optional
from
typing
import
List
from
typing
import
List
import
numpy
as
np
import
numpy
as
np
from
alphamind.data.neutralize
import
neutralize
from
alphamind.data.neutralize
import
neutralize
from
alphamind.utilities
import
alpha_logger
def
factor_processing
(
raw_factors
:
np
.
ndarray
,
def
factor_processing
(
raw_factors
:
np
.
ndarray
,
...
@@ -29,6 +30,8 @@ def factor_processing(raw_factors: np.ndarray,
...
@@ -29,6 +30,8 @@ def factor_processing(raw_factors: np.ndarray,
if
post_process
:
if
post_process
:
for
p
in
post_process
:
for
p
in
post_process
:
if
p
.
__name__
==
'winsorize_normal'
:
alpha_logger
.
warning
(
"winsorize_normal normally should not be done after neutralize"
)
new_factors
=
p
(
new_factors
,
groups
=
groups
)
new_factors
=
p
(
new_factors
,
groups
=
groups
)
return
new_factors
return
new_factors
pfopt
@
1dcecd88
Subproject commit
d04d8ac4ca33c4482ac2baae9b537a0df0d1b036
Subproject commit
1dcecd88728512ff50730f418d9d58195bf33851
alphamind/portfolio/linearbuilder.py
View file @
fbb26530
...
@@ -17,7 +17,8 @@ def linear_builder(er: np.ndarray,
...
@@ -17,7 +17,8 @@ def linear_builder(er: np.ndarray,
risk_constraints
:
np
.
ndarray
,
risk_constraints
:
np
.
ndarray
,
risk_target
:
Tuple
[
np
.
ndarray
,
np
.
ndarray
],
risk_target
:
Tuple
[
np
.
ndarray
,
np
.
ndarray
],
turn_over_target
:
float
=
None
,
turn_over_target
:
float
=
None
,
current_position
:
np
.
ndarray
=
None
)
->
Tuple
[
str
,
np
.
ndarray
,
np
.
ndarray
]:
current_position
:
np
.
ndarray
=
None
,
method
:
str
=
'simplex'
)
->
Tuple
[
str
,
np
.
ndarray
,
np
.
ndarray
]:
er
=
er
.
flatten
()
er
=
er
.
flatten
()
n
,
m
=
risk_constraints
.
shape
n
,
m
=
risk_constraints
.
shape
...
@@ -36,7 +37,7 @@ def linear_builder(er: np.ndarray,
...
@@ -36,7 +37,7 @@ def linear_builder(er: np.ndarray,
if
not
turn_over_target
:
if
not
turn_over_target
:
cons_matrix
=
np
.
concatenate
((
risk_constraints
.
T
,
risk_lbound
,
risk_ubound
),
axis
=
1
)
cons_matrix
=
np
.
concatenate
((
risk_constraints
.
T
,
risk_lbound
,
risk_ubound
),
axis
=
1
)
opt
=
LPOptimizer
(
cons_matrix
,
lbound
,
ubound
,
-
er
)
opt
=
LPOptimizer
(
cons_matrix
,
lbound
,
ubound
,
-
er
,
method
)
status
=
opt
.
status
()
status
=
opt
.
status
()
...
@@ -77,7 +78,7 @@ def linear_builder(er: np.ndarray,
...
@@ -77,7 +78,7 @@ def linear_builder(er: np.ndarray,
risk_ubound
=
np
.
concatenate
((
risk_ubound
,
np
.
inf
*
np
.
ones
((
n
,
1
))),
axis
=
0
)
risk_ubound
=
np
.
concatenate
((
risk_ubound
,
np
.
inf
*
np
.
ones
((
n
,
1
))),
axis
=
0
)
cons_matrix
=
np
.
concatenate
((
risk_constraints
,
risk_lbound
,
risk_ubound
),
axis
=
1
)
cons_matrix
=
np
.
concatenate
((
risk_constraints
,
risk_lbound
,
risk_ubound
),
axis
=
1
)
opt
=
LPOptimizer
(
cons_matrix
,
lbound
,
ubound
,
-
er
)
opt
=
LPOptimizer
(
cons_matrix
,
lbound
,
ubound
,
-
er
,
method
)
status
=
opt
.
status
()
status
=
opt
.
status
()
...
...
alphamind/tests/portfolio/test_linearbuild.py
View file @
fbb26530
...
@@ -37,6 +37,25 @@ class TestLinearBuild(unittest.TestCase):
...
@@ -37,6 +37,25 @@ class TestLinearBuild(unittest.TestCase):
expected_risk
=
np
.
zeros
(
self
.
risk_exp
.
shape
[
1
])
expected_risk
=
np
.
zeros
(
self
.
risk_exp
.
shape
[
1
])
np
.
testing
.
assert_array_almost_equal
(
calc_risk
,
expected_risk
)
np
.
testing
.
assert_array_almost_equal
(
calc_risk
,
expected_risk
)
def
test_linear_build_with_interior
(
self
):
bm
=
self
.
bm
/
self
.
bm
.
sum
()
eplson
=
1e-6
status
,
_
,
w
=
linear_builder
(
self
.
er
,
0.
,
0.01
,
self
.
risk_exp
,
(
bm
@
self
.
risk_exp
,
bm
@
self
.
risk_exp
),
method
=
'interior'
)
self
.
assertEqual
(
status
,
'optimal'
)
self
.
assertAlmostEqual
(
np
.
sum
(
w
),
1.
)
self
.
assertTrue
(
np
.
all
(
w
<=
0.01
+
eplson
))
self
.
assertTrue
(
np
.
all
(
w
>=
-
eplson
))
calc_risk
=
(
w
-
bm
)
@
self
.
risk_exp
expected_risk
=
np
.
zeros
(
self
.
risk_exp
.
shape
[
1
])
np
.
testing
.
assert_array_almost_equal
(
calc_risk
,
expected_risk
)
def
test_linear_build_with_inequality_constraints
(
self
):
def
test_linear_build_with_inequality_constraints
(
self
):
bm
=
self
.
bm
/
self
.
bm
.
sum
()
bm
=
self
.
bm
/
self
.
bm
.
sum
()
eplson
=
1e-6
eplson
=
1e-6
...
...
build_linux_dependencies.sh
View file @
fbb26530
...
@@ -21,7 +21,10 @@ fi
...
@@ -21,7 +21,10 @@ fi
cd
../..
cd
../..
cd
alphamind/pfopt
cd
alphamind/pfopt
export
BUILD_TEST
=
OFF
./build_linux.sh
./build_linux.sh
if
[
$?
-ne
0
]
;
then
if
[
$?
-ne
0
]
;
then
cd
../..
cd
../..
exit
1
exit
1
...
...
build_windows_dependencies.bat
View file @
fbb26530
...
@@ -19,6 +19,7 @@ cd ../..
...
@@ -19,6 +19,7 @@ cd ../..
cd alphamind\pfopt
cd alphamind\pfopt
set BUILD_TEST=OFF
call build_windows.bat
call build_windows.bat
if %errorlevel% neq 0 exit /b 1
if %errorlevel% neq 0 exit /b 1
...
...
notebooks/Example 3 - Multi Weight Gap Comparison.ipynb
View file @
fbb26530
...
@@ -44,7 +44,7 @@
...
@@ -44,7 +44,7 @@
"batch = 0\n",
"batch = 0\n",
"horizon = map_freq(freq)\n",
"horizon = map_freq(freq)\n",
"universe = Universe(\"custom\", ['zz800'])\n",
"universe = Universe(\"custom\", ['zz800'])\n",
"data_source = 'postgres+psycopg2://postgres:
A12345678!@10.63.6.220
/alpha'\n",
"data_source = 'postgres+psycopg2://postgres:
we083826@localhost
/alpha'\n",
"benchmark_code = 905\n",
"benchmark_code = 905\n",
"method = 'tv'\n",
"method = 'tv'\n",
"target_vol = 0.05\n",
"target_vol = 0.05\n",
...
@@ -269,9 +269,9 @@
...
@@ -269,9 +269,9 @@
"outputs": [],
"outputs": [],
"source": [
"source": [
"def create_report(ret_df, windows):\n",
"def create_report(ret_df, windows):\n",
" sharp_calc = MovingSharp(windows)\n",
" sharp_calc = MovingSharp(windows
, x='ret', y='riskFree'
)\n",
" drawdown_calc = MovingMaxDrawdown(windows)\n",
" drawdown_calc = MovingMaxDrawdown(windows
, x='ret'
)\n",
" max_drawdown_calc = MovingMaxDrawdown(len(ret_df))\n",
" max_drawdown_calc = MovingMaxDrawdown(len(ret_df)
, x='ret'
)\n",
"\n",
"\n",
" res_df = pd.DataFrame(columns=['daily_return', 'cum_ret', 'sharp', 'drawdown', 'max_drawn', 'leverage'])\n",
" res_df = pd.DataFrame(columns=['daily_return', 'cum_ret', 'sharp', 'drawdown', 'max_drawn', 'leverage'])\n",
" total_returns = 0.\n",
" total_returns = 0.\n",
...
@@ -285,8 +285,8 @@
...
@@ -285,8 +285,8 @@
"\n",
"\n",
" res_df.loc[date, 'daily_return'] = ret\n",
" res_df.loc[date, 'daily_return'] = ret\n",
" res_df.loc[date, 'cum_ret'] = total_returns\n",
" res_df.loc[date, 'cum_ret'] = total_returns\n",
" res_df.loc[date, 'drawdown'] = drawdown_calc.result()
[0]
\n",
" res_df.loc[date, 'drawdown'] = drawdown_calc.result()\n",
" res_df.loc[date, 'max_drawn'] = max_drawdown_calc.result()
[0]
\n",
" res_df.loc[date, 'max_drawn'] = max_drawdown_calc.result()\n",
" res_df.loc[date, 'leverage'] = ret_df.loc[date, 'leverage']\n",
" res_df.loc[date, 'leverage'] = ret_df.loc[date, 'leverage']\n",
"\n",
"\n",
" if i < 5:\n",
" if i < 5:\n",
...
@@ -328,13 +328,6 @@
...
@@ -328,13 +328,6 @@
" alpha_logger.info(f\"target_vol: {target_vol:.4f} finished\")"
" alpha_logger.info(f\"target_vol: {target_vol:.4f} finished\")"
]
]
},
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": null,
"execution_count": null,
...
@@ -349,18 +342,6 @@
...
@@ -349,18 +342,6 @@
"language": "python",
"language": "python",
"name": "python3"
"name": "python3"
},
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.4"
},
"varInspector": {
"varInspector": {
"cols": {
"cols": {
"lenName": 16,
"lenName": 16,
...
...
notebooks/Example 7 - Portfolio Optimizer Performance.ipynb
View file @
fbb26530
...
@@ -57,7 +57,7 @@
...
@@ -57,7 +57,7 @@
"factor = 'EPS'\n",
"factor = 'EPS'\n",
"lb = 0.0\n",
"lb = 0.0\n",
"ub = 0.1\n",
"ub = 0.1\n",
"data_source = 'postgres+psycopg2://postgres:
A12345678!@10.63.6.220
/alpha'\n",
"data_source = 'postgres+psycopg2://postgres:
we083826@localhost
/alpha'\n",
"engine = SqlEngine(data_source)\n",
"engine = SqlEngine(data_source)\n",
"\n",
"\n",
"universes = [Universe('custom', [u_name]) for u_name in u_names]\n",
"universes = [Universe('custom', [u_name]) for u_name in u_names]\n",
...
@@ -82,12 +82,12 @@
...
@@ -82,12 +82,12 @@
"name": "stderr",
"name": "stderr",
"output_type": "stream",
"output_type": "stream",
"text": [
"text": [
"2018-03-
28 12:46:50,110
- ALPHA_MIND - INFO - sh50 is finished\n",
"2018-03-
30 16:52:12,784
- ALPHA_MIND - INFO - sh50 is finished\n",
"2018-03-
28 12:46:50,131
- ALPHA_MIND - INFO - hs300 is finished\n",
"2018-03-
30 16:52:12,796
- ALPHA_MIND - INFO - hs300 is finished\n",
"2018-03-
28 12:46:50,156
- ALPHA_MIND - INFO - zz500 is finished\n",
"2018-03-
30 16:52:12,813
- ALPHA_MIND - INFO - zz500 is finished\n",
"2018-03-
28 12:46:50,193
- ALPHA_MIND - INFO - zz800 is finished\n",
"2018-03-
30 16:52:12,842
- ALPHA_MIND - INFO - zz800 is finished\n",
"2018-03-
28 12:46:50,244
- ALPHA_MIND - INFO - zz1000 is finished\n",
"2018-03-
30 16:52:12,879
- ALPHA_MIND - INFO - zz1000 is finished\n",
"2018-03-
28 12:46:50,830
- ALPHA_MIND - INFO - ashare_ex is finished\n"
"2018-03-
30 16:52:13,184
- ALPHA_MIND - INFO - ashare_ex is finished\n"
]
]
}
}
],
],
...
@@ -171,21 +171,21 @@
...
@@ -171,21 +171,21 @@
" <tbody>\n",
" <tbody>\n",
" <tr>\n",
" <tr>\n",
" <th>cvxpy</th>\n",
" <th>cvxpy</th>\n",
" <td>
2.69
</td>\n",
" <td>
1.68
</td>\n",
" <td>
6.37
</td>\n",
" <td>
3.13
</td>\n",
" <td>
7.28
</td>\n",
" <td>
5.02
</td>\n",
" <td>1
2.5
5</td>\n",
" <td>1
0.1
5</td>\n",
" <td>
20.09
</td>\n",
" <td>
13.55
</td>\n",
" <td>
325.88
</td>\n",
" <td>
145.17
</td>\n",
" </tr>\n",
" </tr>\n",
" <tr>\n",
" <tr>\n",
" <th>alphamind</th>\n",
" <th>alphamind</th>\n",
" <td>0.34</td>\n",
" <td>0.20</td>\n",
" <td>0.33</td>\n",
" <td>0.44</td>\n",
" <td>0.58</td>\n",
" <td>0.58</td>\n",
" <td>0.70</td>\n",
" <td>0.72</td>\n",
" <td>0.92</td>\n",
" <td>2.94</td>\n",
" <td>1.08</td>\n",
" <td>3.08</td>\n",
" </tr>\n",
" </tr>\n",
" </tbody>\n",
" </tbody>\n",
"</table>\n",
"</table>\n",
...
@@ -193,8 +193,8 @@
...
@@ -193,8 +193,8 @@
],
],
"text/plain": [
"text/plain": [
" sh50 hs300 zz500 zz800 zz1000 ashare_ex\n",
" sh50 hs300 zz500 zz800 zz1000 ashare_ex\n",
"cvxpy
2.69 6.37 7.28 12.55 20.09 325.88
\n",
"cvxpy
1.68 3.13 5.02 10.15 13.55 145.17
\n",
"alphamind 0.
34 0.58 0.70 0.92 1.08 3.08
"
"alphamind 0.
20 0.33 0.44 0.58 0.72 2.94
"
]
]
},
},
"execution_count": 4,
"execution_count": 4,
...
@@ -223,19 +223,19 @@
...
@@ -223,19 +223,19 @@
"name": "stderr",
"name": "stderr",
"output_type": "stream",
"output_type": "stream",
"text": [
"text": [
"2018-03-
28 12:46:51,048
- ALPHA_MIND - INFO - sh50 is finished\n",
"2018-03-
30 16:52:13,374
- ALPHA_MIND - INFO - sh50 is finished\n",
"2018-03-
28 12:46:51,157
- ALPHA_MIND - INFO - hs300 is finished\n",
"2018-03-
30 16:52:13,509
- ALPHA_MIND - INFO - hs300 is finished\n",
"2018-03-
28 12:46:51,44
2 - ALPHA_MIND - INFO - zz500 is finished\n",
"2018-03-
30 16:52:13,73
2 - ALPHA_MIND - INFO - zz500 is finished\n",
"2018-03-
28 12:46:52,102
- ALPHA_MIND - INFO - zz800 is finished\n",
"2018-03-
30 16:52:14,220
- ALPHA_MIND - INFO - zz800 is finished\n",
"2018-03-
28 12:46:53,172
- ALPHA_MIND - INFO - zz1000 is finished\n",
"2018-03-
30 16:52:14,956
- ALPHA_MIND - INFO - zz1000 is finished\n",
"2018-03-
28 12:47:04,91
6 - ALPHA_MIND - INFO - ashare_ex is finished\n"
"2018-03-
30 16:52:21,24
6 - ALPHA_MIND - INFO - ashare_ex is finished\n"
]
]
}
}
],
],
"source": [
"source": [
"from cvxpy import pnorm\n",
"from cvxpy import pnorm\n",
"\n",
"\n",
"df = pd.DataFrame(columns=u_names, index=['cvxpy', 'alphamind'])\n",
"df = pd.DataFrame(columns=u_names, index=['cvxpy', 'alphamind
(simplex)', 'alphamind (interior)
'])\n",
"turn_over_target = 0.5\n",
"turn_over_target = 0.5\n",
"number = 1\n",
"number = 1\n",
"\n",
"\n",
...
@@ -254,8 +254,22 @@
...
@@ -254,8 +254,22 @@
" risk_constraints = np.ones((len(er), 1))\n",
" risk_constraints = np.ones((len(er), 1))\n",
" risk_target = (np.array([1.]), np.array([1.]))\n",
" risk_target = (np.array([1.]), np.array([1.]))\n",
"\n",
"\n",
" status, y, x1 = linear_builder(er, lbound, ubound, risk_constraints, risk_target, turn_over_target=turn_over_target, current_position=current_position)\n",
" status, y, x1 = linear_builder(er,\n",
" elasped_time1 = timeit.timeit(\"linear_builder(er, lbound, ubound, risk_constraints, risk_target, turn_over_target=turn_over_target, current_position=current_position)\", number=number, globals=globals()) / number * 1000\n",
" lbound,\n",
" ubound,\n",
" risk_constraints,\n",
" risk_target,\n",
" turn_over_target=turn_over_target,\n",
" current_position=current_position,\n",
" method='interior')\n",
" elasped_time1 = timeit.timeit(\"\"\"linear_builder(er,\n",
" lbound,\n",
" ubound,\n",
" risk_constraints,\n",
" risk_target,\n",
" turn_over_target=turn_over_target,\n",
" current_position=current_position,\n",
" method='interior')\"\"\", number=number, globals=globals()) / number * 1000\n",
" \n",
" \n",
" w = cvxpy.Variable(n)\n",
" w = cvxpy.Variable(n)\n",
" curr_risk_exposure = risk_constraints.T @ w\n",
" curr_risk_exposure = risk_constraints.T @ w\n",
...
@@ -271,9 +285,21 @@
...
@@ -271,9 +285,21 @@
" prob.solve(solver='GLPK')\n",
" prob.solve(solver='GLPK')\n",
" elasped_time2 = timeit.timeit(\"prob.solve(solver='GLPK')\",\n",
" elasped_time2 = timeit.timeit(\"prob.solve(solver='GLPK')\",\n",
" number=number, globals=globals()) / number * 1000\n",
" number=number, globals=globals()) / number * 1000\n",
" \n",
" elasped_time3 = timeit.timeit(\"\"\"linear_builder(er,\n",
" lbound,\n",
" ubound,\n",
" risk_constraints,\n",
" risk_target,\n",
" turn_over_target=turn_over_target,\n",
" current_position=current_position,\n",
" method='simplex')\"\"\", number=number, globals=globals()) / number * 1000\n",
" \n",
" \n",
" np.testing.assert_almost_equal(x1 @ er, np.array(w.value).flatten() @ er, 4)\n",
" np.testing.assert_almost_equal(x1 @ er, np.array(w.value).flatten() @ er, 4)\n",
"\n",
"\n",
" df.loc['alphamind', u_name] = elasped_time1\n",
" df.loc['alphamind (interior)', u_name] = elasped_time1\n",
" df.loc['alphamind (simplex)', u_name] = elasped_time3\n",
" df.loc['cvxpy', u_name] = elasped_time2\n",
" df.loc['cvxpy', u_name] = elasped_time2\n",
" alpha_logger.info(f\"{u_name} is finished\")"
" alpha_logger.info(f\"{u_name} is finished\")"
]
]
...
@@ -315,21 +341,21 @@
...
@@ -315,21 +341,21 @@
" <tbody>\n",
" <tbody>\n",
" <tr>\n",
" <tr>\n",
" <th>cvxpy</th>\n",
" <th>cvxpy</th>\n",
" <td>
4.26
</td>\n",
" <td>
2.49
</td>\n",
" <td>2
8.31
</td>\n",
" <td>2
2.19
</td>\n",
" <td>
67.48
</td>\n",
" <td>
51.62
</td>\n",
" <td>1
64.81
</td>\n",
" <td>1
23.42
</td>\n",
" <td>
256.14
</td>\n",
" <td>
190.62
</td>\n",
" <td>
2,561.17
</td>\n",
" <td>
1,946.96
</td>\n",
" </tr>\n",
" </tr>\n",
" <tr>\n",
" <tr>\n",
" <th>alphamind</th>\n",
" <th>alphamind</th>\n",
" <td>
1.22
</td>\n",
" <td>
3.77
</td>\n",
" <td>
20.28
</td>\n",
" <td>
33.59
</td>\n",
" <td>
67.10
</td>\n",
" <td>
55.49
</td>\n",
" <td>1
54.95
</td>\n",
" <td>1
16.31
</td>\n",
" <td>
267.17
</td>\n",
" <td>
165.62
</td>\n",
" <td>
3,314.10
</td>\n",
" <td>
1,239.47
</td>\n",
" </tr>\n",
" </tr>\n",
" </tbody>\n",
" </tbody>\n",
"</table>\n",
"</table>\n",
...
@@ -337,8 +363,8 @@
...
@@ -337,8 +363,8 @@
],
],
"text/plain": [
"text/plain": [
" sh50 hs300 zz500 zz800 zz1000 ashare_ex\n",
" sh50 hs300 zz500 zz800 zz1000 ashare_ex\n",
"cvxpy
4.26 28.31 67.48 164.81 256.14 2,561.17
\n",
"cvxpy
2.49 22.19 51.62 123.42 190.62 1,946.96
\n",
"alphamind
1.22 20.28 67.10 154.95 267.17 3,314.10
"
"alphamind
3.77 33.59 55.49 116.31 165.62 1,239.47
"
]
]
},
},
"execution_count": 6,
"execution_count": 6,
...
@@ -362,19 +388,19 @@
...
@@ -362,19 +388,19 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count":
7
,
"execution_count":
8
,
"metadata": {},
"metadata": {},
"outputs": [
"outputs": [
{
{
"name": "stderr",
"name": "stderr",
"output_type": "stream",
"output_type": "stream",
"text": [
"text": [
"2018-03-
28 12:47:05,225
- ALPHA_MIND - INFO - sh50 is finished\n",
"2018-03-
30 16:52:21,418
- ALPHA_MIND - INFO - sh50 is finished\n",
"2018-03-
28 12:47:06,047
- ALPHA_MIND - INFO - hs300 is finished\n",
"2018-03-
30 16:52:21,888
- ALPHA_MIND - INFO - hs300 is finished\n",
"2018-03-
28 12:47:07,931
- ALPHA_MIND - INFO - zz500 is finished\n",
"2018-03-
30 16:52:23,360
- ALPHA_MIND - INFO - zz500 is finished\n",
"2018-03-
28 12:47:13,124
- ALPHA_MIND - INFO - zz800 is finished\n",
"2018-03-
30 16:52:29,066
- ALPHA_MIND - INFO - zz800 is finished\n",
"2018-03-
28 12:47:20,586
- ALPHA_MIND - INFO - zz1000 is finished\n",
"2018-03-
30 16:52:37,334
- ALPHA_MIND - INFO - zz1000 is finished\n",
"2018-03-
28 12:50:28,528
- ALPHA_MIND - INFO - ashare_ex is finished\n"
"2018-03-
30 16:56:15,174
- ALPHA_MIND - INFO - ashare_ex is finished\n"
]
]
}
}
],
],
...
@@ -423,7 +449,7 @@
...
@@ -423,7 +449,7 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count":
8
,
"execution_count":
9
,
"metadata": {},
"metadata": {},
"outputs": [
"outputs": [
{
{
...
@@ -458,33 +484,33 @@
...
@@ -458,33 +484,33 @@
" <tbody>\n",
" <tbody>\n",
" <tr>\n",
" <tr>\n",
" <th>cvxpy</th>\n",
" <th>cvxpy</th>\n",
" <td>
13.26
</td>\n",
" <td>
9.81
</td>\n",
" <td>1
66.37
</td>\n",
" <td>1
85.93
</td>\n",
" <td>
493.95
</td>\n",
" <td>
638.17
</td>\n",
" <td>
1,635.7
1</td>\n",
" <td>
2,501.4
1</td>\n",
" <td>
2,359.2
2</td>\n",
" <td>
3,665.0
2</td>\n",
" <td>
51,708.15
</td>\n",
" <td>
94,592.70
</td>\n",
" </tr>\n",
" </tr>\n",
" <tr>\n",
" <tr>\n",
" <th>alphamind</th>\n",
" <th>alphamind</th>\n",
" <td>
68.57
</td>\n",
" <td>
0.33
</td>\n",
" <td>
205.78
</td>\n",
" <td>
5.66
</td>\n",
" <td>
317.68
</td>\n",
" <td>
14.24
</td>\n",
" <td>
670.81
</td>\n",
" <td>
47.29
</td>\n",
" <td>
915.77
</td>\n",
" <td>
109.08
</td>\n",
" <td>
32,733.73
</td>\n",
" <td>
2,515.06
</td>\n",
" </tr>\n",
" </tr>\n",
" </tbody>\n",
" </tbody>\n",
"</table>\n",
"</table>\n",
"</div>"
"</div>"
],
],
"text/plain": [
"text/plain": [
"
sh50 hs300 zz500 zz800 zz1000 ashare_ex\n",
" sh50 hs300 zz500 zz800 zz1000 ashare_ex\n",
"cvxpy
13.26 166.37 493.95 1,635.71 2,359.22 51,708.15
\n",
"cvxpy
9.81 185.93 638.17 2,501.41 3,665.02 94,592.70
\n",
"alphamind
68.57 205.78 317.68 670.81 915.77 32,733.73
"
"alphamind
0.33 5.66 14.24 47.29 109.08 2,515.06
"
]
]
},
},
"execution_count":
8
,
"execution_count":
9
,
"metadata": {},
"metadata": {},
"output_type": "execute_result"
"output_type": "execute_result"
}
}
...
@@ -497,24 +523,25 @@
...
@@ -497,24 +523,25 @@
"cell_type": "markdown",
"cell_type": "markdown",
"metadata": {},
"metadata": {},
"source": [
"source": [
"## 4. Mean - Variance 优化 (Box约束以及线性约束)"
"## 4. Mean - Variance 优化 (Box约束)\n",
"---------------"
]
]
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count":
9
,
"execution_count":
10
,
"metadata": {},
"metadata": {},
"outputs": [
"outputs": [
{
{
"name": "stderr",
"name": "stderr",
"output_type": "stream",
"output_type": "stream",
"text": [
"text": [
"2018-03-
28 12:50:29,489
- ALPHA_MIND - INFO - sh50 is finished\n",
"2018-03-
30 16:56:16,044
- ALPHA_MIND - INFO - sh50 is finished\n",
"2018-03-
28 12:50:30,29
5 - ALPHA_MIND - INFO - hs300 is finished\n",
"2018-03-
30 16:56:16,70
5 - ALPHA_MIND - INFO - hs300 is finished\n",
"2018-03-
28 12:50:33,286
- ALPHA_MIND - INFO - zz500 is finished\n",
"2018-03-
30 16:56:19,651
- ALPHA_MIND - INFO - zz500 is finished\n",
"2018-03-
28 12:50:39,842
- ALPHA_MIND - INFO - zz800 is finished\n",
"2018-03-
30 16:56:30,227
- ALPHA_MIND - INFO - zz800 is finished\n",
"2018-03-
28 12:50:55,691
- ALPHA_MIND - INFO - zz1000 is finished\n",
"2018-03-
30 16:56:44,840
- ALPHA_MIND - INFO - zz1000 is finished\n",
"2018-03-
28 12:57:46,604
- ALPHA_MIND - INFO - ashare_ex is finished\n"
"2018-03-
30 17:08:25,343
- ALPHA_MIND - INFO - ashare_ex is finished\n"
]
]
}
}
],
],
...
@@ -531,30 +558,25 @@
...
@@ -531,30 +558,25 @@
" sec_cov = risk_exposure @ risk_cov @ risk_exposure.T / 10000 + np.diag(special_risk ** 2) / 10000\n",
" sec_cov = risk_exposure @ risk_cov @ risk_exposure.T / 10000 + np.diag(special_risk ** 2) / 10000\n",
" er = factor_data[factor].values\n",
" er = factor_data[factor].values\n",
" n = len(er)\n",
" n = len(er)\n",
"
\n",
"\n",
" bm = np.zeros(n)\n",
" bm = np.zeros(n)\n",
" lbound = np.zeros(n)\n",
" lbound = np.zeros(n)\n",
" ubound = np.ones(n) * 0.1\n",
" ubound = np.ones(n) * 0.1\n",
" \n",
" risk_constraints = np.ones((len(er), 1))\n",
" risk_target = (np.array([1.]), np.array([1.]))\n",
"\n",
"\n",
" status, y, x1 = mean_variance_builder(er, sec_cov, bm, lbound, ubound,
risk_constraints, risk_target
, lam=1)\n",
" status, y, x1 = mean_variance_builder(er, sec_cov, bm, lbound, ubound,
None, None
, lam=1)\n",
" elasped_time1 = timeit.timeit(\"mean_variance_builder(er, sec_cov, bm, lbound, ubound, None, None, lam=1)\",\n",
" elasped_time1 = timeit.timeit(\"mean_variance_builder(er, sec_cov, bm, lbound, ubound, None, None, lam=1)\",\n",
" number=number, globals=globals()) / number * 1000\n",
" number=number, globals=globals()) / number * 1000\n",
" \n",
" \n",
" w = cvxpy.Variable(n)\n",
" w = cvxpy.Variable(n)\n",
" risk = quad_form(w, sec_cov)\n",
" risk = quad_form(w, sec_cov)\n",
" objective = cvxpy.Minimize(-w.T * er + 0.5 * risk)\n",
" objective = cvxpy.Minimize(-w.T * er + 0.5 * risk)\n",
" curr_risk_exposure = risk_constraints.T @ w\n",
" constraints = [w >= lbound,\n",
" constraints = [w >= lbound,\n",
" w <= ubound,\n",
" w <= ubound]\n",
" curr_risk_exposure == risk_target[0]]\n",
" prob = cvxpy.Problem(objective, constraints)\n",
" prob = cvxpy.Problem(objective, constraints)\n",
" prob.solve(solver='CVXOPT')\n",
" prob.solve(solver='CVXOPT')\n",
" elasped_time2 = timeit.timeit(\"prob.solve(solver='CVXOPT')\",\n",
" elasped_time2 = timeit.timeit(\"prob.solve(solver='CVXOPT')\",\n",
" number=number, globals=globals()) / number * 1000\n",
" number=number, globals=globals()) / number * 1000\n",
"\n",
"
\n",
" u1 = -x1 @ er + 0.5 * x1 @ sec_cov @ x1\n",
" u1 = -x1 @ er + 0.5 * x1 @ sec_cov @ x1\n",
" x2 = np.array(w.value).flatten()\n",
" x2 = np.array(w.value).flatten()\n",
" u2 = -x2 @ er + 0.5 * x2 @ sec_cov @ x2\n",
" u2 = -x2 @ er + 0.5 * x2 @ sec_cov @ x2\n",
...
@@ -568,7 +590,7 @@
...
@@ -568,7 +590,7 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": 1
0
,
"execution_count": 1
1
,
"metadata": {},
"metadata": {},
"outputs": [
"outputs": [
{
{
...
@@ -603,21 +625,21 @@
...
@@ -603,21 +625,21 @@
" <tbody>\n",
" <tbody>\n",
" <tr>\n",
" <tr>\n",
" <th>cvxpy</th>\n",
" <th>cvxpy</th>\n",
" <td>
20.25
</td>\n",
" <td>
10.78
</td>\n",
" <td>3
14.35
</td>\n",
" <td>3
00.36
</td>\n",
" <td>1,3
06.26
</td>\n",
" <td>1,3
77.87
</td>\n",
" <td>
2,795.40
</td>\n",
" <td>
5,244.64
</td>\n",
" <td>
7,386.27
</td>\n",
" <td>
6,758.95
</td>\n",
" <td>
195,024.34
</td>\n",
" <td>
334,990.46
</td>\n",
" </tr>\n",
" </tr>\n",
" <tr>\n",
" <tr>\n",
" <th>alphamind</th>\n",
" <th>alphamind</th>\n",
" <td>
21.20
</td>\n",
" <td>
0.32
</td>\n",
" <td>4
1.34
</td>\n",
" <td>4
.78
</td>\n",
" <td>
60.28
</td>\n",
" <td>
16.02
</td>\n",
" <td>
91.30
</td>\n",
" <td>
41.63
</td>\n",
" <td>
77.67
</td>\n",
" <td>
64.54
</td>\n",
" <td>
868.86
</td>\n",
" <td>
1,134.95
</td>\n",
" </tr>\n",
" </tr>\n",
" </tbody>\n",
" </tbody>\n",
"</table>\n",
"</table>\n",
...
@@ -625,11 +647,11 @@
...
@@ -625,11 +647,11 @@
],
],
"text/plain": [
"text/plain": [
" sh50 hs300 zz500 zz800 zz1000 ashare_ex\n",
" sh50 hs300 zz500 zz800 zz1000 ashare_ex\n",
"cvxpy
20.25 314.35 1,306.26 2,795.40 7,386.27 195,024.34
\n",
"cvxpy
10.78 300.36 1,377.87 5,244.64 6,758.95 334,990.46
\n",
"alphamind
21.20 41.34 60.28 91.30 77.67 868.86
"
"alphamind
0.32 4.78 16.02 41.63 64.54 1,134.95
"
]
]
},
},
"execution_count": 1
0
,
"execution_count": 1
1
,
"metadata": {},
"metadata": {},
"output_type": "execute_result"
"output_type": "execute_result"
}
}
...
@@ -642,28 +664,97 @@
...
@@ -642,28 +664,97 @@
"cell_type": "markdown",
"cell_type": "markdown",
"metadata": {},
"metadata": {},
"source": [
"source": [
"## 5.
线性优化(带二次限制条件
)\n",
"## 5.
Mean - Variance 优化 (Box约束以及线性约束
)\n",
"----------------
---------
"
"----------------"
]
]
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count":
11
,
"execution_count":
null
,
"metadata": {},
"metadata": {},
"outputs": [
"outputs": [
{
{
"name": "stderr",
"name": "stderr",
"output_type": "stream",
"output_type": "stream",
"text": [
"text": [
"2018-03-28 12:57:47,308 - ALPHA_MIND - INFO - sh50 is finished\n",
"2018-03-30 17:08:25,991 - ALPHA_MIND - INFO - sh50 is finished\n",
"2018-03-28 12:57:48,181 - ALPHA_MIND - INFO - hs300 is finished\n",
"2018-03-30 17:08:26,763 - ALPHA_MIND - INFO - hs300 is finished\n",
"2018-03-28 12:57:51,075 - ALPHA_MIND - INFO - zz500 is finished\n",
"2018-03-30 17:08:30,276 - ALPHA_MIND - INFO - zz500 is finished\n",
"2018-03-28 12:58:05,881 - ALPHA_MIND - INFO - zz800 is finished\n",
"2018-03-30 17:08:41,580 - ALPHA_MIND - INFO - zz800 is finished\n",
"2018-03-28 12:58:19,775 - ALPHA_MIND - INFO - zz1000 is finished\n",
"2018-03-30 17:09:18,856 - ALPHA_MIND - INFO - zz1000 is finished\n"
"2018-03-28 13:05:47,684 - ALPHA_MIND - INFO - ashare_ex is finished\n"
]
]
}
}
],
],
"source": [
"df = pd.DataFrame(columns=u_names, index=['cvxpy', 'alphamind'])\n",
"number = 1\n",
"\n",
"for u_name, sample_data in zip(u_names, data_set):\n",
" all_styles = risk_styles + industry_styles + ['COUNTRY']\n",
" factor_data = sample_data['factor']\n",
" risk_cov = sample_data['risk_cov'][all_styles].values\n",
" risk_exposure = factor_data[all_styles].values\n",
" special_risk = factor_data.srisk.values\n",
" sec_cov = risk_exposure @ risk_cov @ risk_exposure.T / 10000 + np.diag(special_risk ** 2) / 10000\n",
" er = factor_data[factor].values\n",
" n = len(er)\n",
" \n",
" bm = np.zeros(n)\n",
" lbound = np.zeros(n)\n",
" ubound = np.ones(n) * 0.1\n",
" \n",
" risk_constraints = np.ones((len(er), 1))\n",
" risk_target = (np.array([1.]), np.array([1.]))\n",
"\n",
" status, y, x1 = mean_variance_builder(er, sec_cov, bm, lbound, ubound, risk_constraints, risk_target, lam=1)\n",
" elasped_time1 = timeit.timeit(\"mean_variance_builder(er, sec_cov, bm, lbound, ubound, None, None, lam=1)\",\n",
" number=number, globals=globals()) / number * 1000\n",
" \n",
" w = cvxpy.Variable(n)\n",
" risk = quad_form(w, sec_cov)\n",
" objective = cvxpy.Minimize(-w.T * er + 0.5 * risk)\n",
" curr_risk_exposure = risk_constraints.T @ w\n",
" constraints = [w >= lbound,\n",
" w <= ubound,\n",
" curr_risk_exposure == risk_target[0]]\n",
" prob = cvxpy.Problem(objective, constraints)\n",
" prob.solve(solver='CVXOPT')\n",
" elasped_time2 = timeit.timeit(\"prob.solve(solver='CVXOPT')\",\n",
" number=number, globals=globals()) / number * 1000\n",
"\n",
" u1 = -x1 @ er + 0.5 * x1 @ sec_cov @ x1\n",
" x2 = np.array(w.value).flatten()\n",
" u2 = -x2 @ er + 0.5 * x2 @ sec_cov @ x2\n",
" \n",
" np.testing.assert_array_almost_equal(u1, u2, 4)\n",
"\n",
" df.loc['alphamind', u_name] = elasped_time1\n",
" df.loc['cvxpy', u_name] = elasped_time2\n",
" alpha_logger.info(f\"{u_name} is finished\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"df"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 6. 线性优化(带二次限制条件)\n",
"-------------------------"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"source": [
"df = pd.DataFrame(columns=u_names, index=['cvxpy', 'alphamind'])\n",
"df = pd.DataFrame(columns=u_names, index=['cvxpy', 'alphamind'])\n",
"number = 1\n",
"number = 1\n",
...
@@ -720,72 +811,9 @@
...
@@ -720,72 +811,9 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count":
12
,
"execution_count":
null
,
"metadata": {},
"metadata": {},
"outputs": [
"outputs": [],
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>sh50</th>\n",
" <th>hs300</th>\n",
" <th>zz500</th>\n",
" <th>zz800</th>\n",
" <th>zz1000</th>\n",
" <th>ashare_ex</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>cvxpy</th>\n",
" <td>23.83</td>\n",
" <td>360.10</td>\n",
" <td>1,261.50</td>\n",
" <td>9,555.63</td>\n",
" <td>6,260.97</td>\n",
" <td>212,776.88</td>\n",
" </tr>\n",
" <tr>\n",
" <th>alphamind</th>\n",
" <td>22.55</td>\n",
" <td>32.70</td>\n",
" <td>60.94</td>\n",
" <td>59.65</td>\n",
" <td>116.27</td>\n",
" <td>824.79</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" sh50 hs300 zz500 zz800 zz1000 ashare_ex\n",
"cvxpy 23.83 360.10 1,261.50 9,555.63 6,260.97 212,776.88\n",
"alphamind 22.55 32.70 60.94 59.65 116.27 824.79"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"source": [
"df"
"df"
]
]
...
...
setup.py
View file @
fbb26530
...
@@ -51,7 +51,16 @@ else:
...
@@ -51,7 +51,16 @@ else:
"alphamind/pfopt/include/pfopt"
,
"alphamind/pfopt/include/pfopt"
,
"alphamind/pfopt/include/eigen"
,
"alphamind/pfopt/include/eigen"
,
"alphamind/pfopt/include/alglib"
],
"alphamind/pfopt/include/alglib"
],
libraries
=
[
'pfopt'
,
'alglib'
,
'libClp'
,
'libCoinUtils'
,
'libipopt'
,
'libcoinhsl'
,
'libcoinblas'
,
'libcoinlapack'
,
'libcoinmetis'
],
libraries
=
[
'pfopt'
,
'alglib'
,
'libClp'
,
'libCoinUtils'
,
'libipopt'
,
'libcoinhsl'
,
'libcoinblas'
,
'libcoinlapack'
,
'libcoinmetis'
,
'libcoinmumps'
],
library_dirs
=
[
'alphamind/pfopt/lib'
],
library_dirs
=
[
'alphamind/pfopt/lib'
],
extra_compile_args
=
[
'/MD'
]),
extra_compile_args
=
[
'/MD'
]),
]
]
...
...
xgboost
@
a187ed6c
Subproject commit
bf4367184164e593cd2856ef38f8dd4f8cc76999
Subproject commit
a187ed6c8f3aa40b47d5be80667cbbe6a6fd563d
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment