You are on page 1of 74

A MATLAB

2012

0. ....................................................................................................................................... 4
0.1 ............................................................................................................................ 4
0.2 ........................................................................................................................ 4
0.3 ........................................................................................................................... 5
1. ............................................................................................................................... 5
1.1 AandB ........................................................................................................ 5
1.2 AorB........................................................................................................... 5
1.3 AoffB ......................................................................................................... 6
1.4 AppendSHSZ ................................................................................. 6
1.5 AppendName ............................................................................................. 7
1.6 Code2Name................................................................. 7
1.7 AppendIndu........................................................................................ 8
1.8 NaN FillNaN............................................................................................. 8
1.9 DelBlank .................................................................................... 9
1.10 SortRow ............................................................................. 10
1.11 GetAlign......................................................................................... 10
1.12 GetPrice.......................................................................................... 12
1.13 Get1Price........................................................................................ 13
1.14 GetSpread....................................................................................... 14
1.15 PickPrice ............................................................................ 16
1.16 (GetTradeDay) ............................................................................... 17
1.17 GetPreDay.................................................................................. 18
1.18 300 GetHS300Price....................................................... 18
1.19 Price2Ret................................................................................ 19
1.20 Ret2Price................................................................................ 20
1.21 GetAbnormRet ........................................................................... 23
1.22 GetCov ............................................................................................... 24
1.23 GetCorrcoef.................................................................................... 25
1.24 GetHighAndLow................................................................ 26
1.25 Alpha(GetAlpha) ................................................................................................... 27
1.26 BetaGetBeta .................................................................................................. 28
1.27 CAPMGetCAPM ........................................................................................... 30
1.28 GetStatis................................................................................................. 31
1.29 GetDynamic ....................................................................................... 32
1.30 VaRGetVaR................................................................................................... 34
1.31 MarketPrctile.............................................................................. 35
2 ........................................................................................................................ 37
2.1 (GetInduList)............................................................................................. 37
2.2 GetInduCorrcoef ............................................................... 38
2.3 GetInduIndexCorrcoef ...................................................... 39
2.4 GetCatlog ................................................................. 40


3 .......................................................................................................................................... 41
3.1 GetPortflioValue .......................................................................... 41
3.2 BetaGetPortflioBeta............................................................................ 43
3.3 GetAppraisal .................................................................................... 44
4 ............................................................................................................................................ 46
4.1 PlotPrice............................................................................................... 46
5. ............................................................................................................................... 47
5.1 GetFieldList ......................................................................................... 47
5.2 GetTableList..................................................................................... 48
5.3 IsfieldEqual .......................................................................... 48
5.4 SQL RunSQL.......................................................................................... 49
6. ................................................................................................................................... 50
6.1 GetIFSpread ............................................................................................. 50
6.2 GetIndexRepli .......................................................................................... 51
7. ................................................................................................................................. 52
7.1 GetHedgeValue ................................................................................. 52
7.2 HedgeTest ................................................................................................ 54
7.3 HegdeTable .............................................................. 56
7.4 HegdeTableAnalysis ................................................................ 57
7.5 GetHedgeSim ....................................................... 57
8 ............................................................................................................................ 58
8.1 GetFundAppraisal ......................................................... 58
9.GetStockList ........................................................................................................ 61
10. ................................................................................................................... 61
10.1 GetToday........................................................................................ 61
10.2 GetOwn .............................................................................................. 62
11. ........................................................................................................................................... 63
11.1 ................................................................................................. 63
11.2 ....................................................................................................................... 66
11.2.1 300 300 ................................................................. 66
11.2.2 ETF 300 .................................................................... 66
11.2.3 ............................................................................................. 67
11.3 ...................................................................................... 68
11.4 .................................................................................... 70
12. ........................................................................................................................................... 71
12.1 ................................................................................................................... 71
12.2 ................................................................................ 72
13. ....................................................................................................................... 72
14. ................................................................................................................................... 73

0.
0.1
.
zhangader@126.comzhangader@21cn.com
http://www.matlab-finance.com/bbs
QQ1289311509
MSNbeijinghedgefund@hotmail.com

0.2
ODBCsmalldatabase

>>conn1=database('smalldatabase,'',''')
ODBC
MATLAB
Start=>ToolBoxs=
VQB

ODBC Query ODBC

0.3
l

cell 1 1

>>Price
''
'SH000300'
'SH600177'
[734872]
[2.2988e+003]
[ 9.2500]
[734873]
[2.2764e+003]
[ 9.0200]
[734874]
[2.2906e+003]
[ 9.0900]
[734877]
[2.3686e+003]
[ 9.3500]
cell 1

>>weight ={
'SH600000'
[0.3000]
'SH000300'
[0.7000]}

1.
1.1 AandB

CommStock=AandB(StockListA, StockListB)

StockListA
A
StockListB
B

CommStock

A B

1.2 AorB

CommStock=AorB(StockListA, StockListB)

StockListA
A
StockListB
B

CommStock

A B

1.3 AoffB
StockListA StockListBStockListC
CommStock=AoffB(StockListA, StockListB,StockListB,...)

StockListA
A
StockListB
B
StockListC
C

CommStock

>>AoffB({'SH000300','SH600000','SH600002'})
ans =
'SH000300'

1.4 AppendSHSZ
SH SZSH SZ

Symbol SHSZ=AppendSHSZ(Symbol)

Symbol
600000

SymbolSHSZ SH600000

1 600000, SH
>>data5=AppendSHSZ({'600000'})
data5 =
'SH600000'
>>data5=AppendSHSZ({'600000'},2)
data5 =
'600000.SH'
>>data5=AppendSHSZ('600000')
data5 =
'SH600000'

1.5 AppendName

: StockName=AppendName(StockCode)

StockCode

StockName

---------------------------------------------------------------------SH600267%
StockName=AppendName({'SH600267'})
StockName =
'SH600267'
' '

1.6 Code2Name

StockName=Code2Name(StockCode)

StockCode

StockName

---------------------------------------------------------------------SH600267%
StockName=AppendName({'SH600267'})
StockName =
'SH600267'
' '
Beta
Beta = {
''
''
''
''
'SH600714'
[ 0.3869]
'SH601179'
[ 0.6435]
'SH600330'
[ 0.3862]
'SH601139'
[ 0.6438]
'SH600769'
[ 0.3860]
'SH600145'
[ 0.6451]
'SH600252'
[ 0.3856]
'SH600239'
[ 0.6456]
'SH600130'
[ 0.3854]
'SH600496'
[ 0.6458]
'SH600619'
[ 0.3851]
'SH600227'
[ 0.6468]}
Code2Name(Beta,[1 3])
ans =
''
''
''
''
''
[ 0.3869]
''
[ 0.6435]
7


''
'ST '
''
'ST '
''

[ 0.3862]
[ 0.3860]
[ 0.3856]
[ 0.3854]
[ 0.3851]

''
'ST '
''
''
''

[ 0.6438]
[ 0.6451]
[ 0.6456]
[ 0.6458]
[ 0.6468]

1.7 AppendIndu

StockName=AppendIndu(StockCode)

StockCode

StockName

---------------------------------------------------------------------SH600267
StockName=AppendIndu({'SH600267'})
StockName =
'SH600267'
' ' ''

1.8 NaN FillNaN


NaN
: price=FillNaN(data7)

data7

price

Price 600004 2011 4


6
>>Price
''
'SH600000' 'SH600004' 'SH600005' 'SH600008'
'2011-04-06' 11.10
8.46
NaN
6.82
'2011-04-07' 11.06
8.51
4.51
6.73
'2011-04-08' 11.05
8.52
4.61
6.71
8


'2011-04-11' 11.05
8.52
4.69
6.67
'2011-04-12' 10.97
8.68
4.92
6.82
'2011-04-13' 11.40
8.71
4.82
6.90
'2011-04-14' 11.38
8.59
4.78
6.76
'2011-04-15' 11.47
8.61
4.71
6.76
'2011-04-18' 11.42
8.69
4.78
6.82
'2011-04-19' 11.07
8.527
4.76
6.67
---------------------------------------------------------------------------price=FillNaN(Price)
''
'SH600000' 'SH600004' 'SH600005' 'SH600008'
'2011-04-06' 11.10
8.46
4.51
6.82
'2011-04-07' 11.06
8.51
4.51
6.73
'2011-04-08' 11.05
8.52
4.61
6.71
'2011-04-11' 11.05
8.52
4.69
6.67
'2011-04-12' 10.97
8.68
4.92
6.82
'2011-04-13' 11.40
8.71
4.82
6.90
'2011-04-14' 11.38
8.59
4.78
6.76
'2011-04-15' 11.47
8.61
4.71
6.76
'2011-04-18' 11.42
8.69
4.78
6.82
'2011-04-19' 11.07
8.527
4.76
6.67
Price 1 3

>>Price
11.10000038 8.46
NaN
6.82 14.80 7.90
11.06999969 8.51
4.51
6.74 15.01 7.80
11.05000019 8.52
4.61
6.72 14.78 8.58
11.05000019 8.52
4.69
6.68 14.57 9.108
10.97999954 8.68 4.92
6.83 14.60 8.888
.........
>>price=FillNaN(Price)
11.10000038 8.46
4.51
6.82 14.80 7.90
11.06999969 8.51
4.51
6.74 15.01 7.80
11.05000019 8.52
4.61
6.72 14.78 8.58
11.05000019 8.52
4.69
6.68 14.57 9.108
10.97999954 8.68
4.92
6.83
14.60
8.888
.........

1.9 DelBlank

chank1=DelBlank(char)


char

chank1
-------------------------------------------------
StockName=' '
StockName =

delblank(StockName)
ans =
''

StockName=[{' '},{' A'}]


StockName =
' '
' A'
delblank(StockName)
chank1 =
''
' A'
ans =
''
' A'

1.10 SortRow
N
SortedPrice=SortRow(Price,N)

Price
N

SortedPrice

1.11 GetAlign

1[cellPrice matPrice]=GetAlign(PriceA,PriceB)

PriceA
A
PriceB
B

10


cellPrice A B
matPrice A B
PriceA = {
''
'SH000001'
'SH600000'
'2012-01-04'
[2.1694e+03]
[ 8.4100]
'2012-01-05'
[2.1484e+03]
[ 8.6500]
'2012-01-06'
[2.1634e+03]
[ 8.7100]
'2012-01-09'
[2.2259e+03]
[ 8.9500]
'2012-01-10'
[2.2857e+03]
[ 9.0700]
'2012-01-11'
[2.2761e+03]
[
9]
'2012-01-12'
[2.2750e+03]
[ 9.0900]
'2012-01-13'
[2.2446e+03]
[ 9.0400]}
''
'SH000001'
'2012-01-11'
[2.2761e+03]
'2012-01-12'
[2.2750e+03]
'2012-01-13'
[2.2446e+03]
'2012-01-16'
[2.2062e+03]
'2012-01-17'
[2.2984e+03]
'2012-01-18'
[2.2664e+03]
'2012-01-19'
[2.2961e+03]
'2012-01-20'
[2.3191e+03]
AlignPrice=GetAlign(PriceA,PriceB)

AlignPrice =
''
'SH000001'
'2012-01-04'
[2.1694e+03]
'2012-01-05'
[2.1484e+03]
'2012-01-06'
[2.1634e+03]
'2012-01-09'
[2.2259e+03]
'2012-01-10'
[2.2857e+03]
'2012-01-11'
[2.2761e+03]
'2012-01-12'
[2.2750e+03]
'2012-01-13'
[2.2446e+03]
'2012-01-16'
[2.2062e+03]
'2012-01-17'
[2.2984e+03]
'2012-01-18'
[2.2664e+03]
'2012-01-19'
[2.2961e+03]
'2012-01-20'
[2.3191e+03]

'SH600267'
[ 29.1900]
[ 28.4100]
[ 26.4900]
[ 25.4900]
[ 26.1900]
[ 24.9000]
[ 24.7500]
[ 25.9400]

'SH600000'
[ 8.4100]
[ 8.6500]
[ 8.7100]
[ 8.9500]
[ 9.0700]
[
9]
[ 9.0900]
[ 9.0400]
[
NaN]
[
NaN]
[
NaN]
[
NaN]
[
NaN]

11

'SH600267'
[
NaN]
[
NaN]
[
NaN]
[
NaN]
[
NaN]
[ 29.1900]
[ 28.4100]
[ 26.4900]
[ 25.4900]
[ 26.1900]
[ 24.9000]
[ 24.7500]
[ 25.9400]

1.12 GetPrice

1[cellPrice,matPrice]=GetPrice(StockList,FromDate,ToDate)
2[cellPrice,matPrice]=GetPrice(Weight,FromDate,ToDate)
3[cellPrice,matPrice]=GetPrice(Price)

StockList

FromDate

ToDate

Price

Weight

cellPrice

matPrice
,
price1
NaN
>> price2=GetPrice(price1)
'SH000001' '2011-10-10' 2344.79003906250
'SH000001' '2011-10-11' 2348.52001953125
'SH000001' '2011-10-12' 2420
'SH000001' '2011-10-13' 2438.79003906250
'SH000001' '2011-10-14' 2431.37011718750
......
>> price2=GetPrice(price1)
''
'SH000001'
'SH000002'
'SH000003'
'2011-10-10' 2344.79003906250 2456.02001953125 237.089996337891
'2011-10-11' 2348.52001953125 2460.09008789063 234.259994506836
'2011-10-12' 2420
2534.95996093750 241.619995117188
'2011-10-13' 2438.79003906250 2554.56005859375 245.149993896484
'2011-10-14' 2431.37011718750 2546.80004882813 244.089996337891
'2011-10-17' 2440.39990234375 2556.27001953125 244.860000610352
'2011-10-18' 2383.48999023438 2496.63989257813 239.300003051758
......
price1
NaN
>>price1=
'SH000001' '2011-05-03 00:00:00.0' 2932.18994140625
'SH000001' '2011-05-04 00:00:00.0' 2866.02001953125
'SH000001' '2011-05-05 00:00:00.0' 2872.39990234375
'SH000001' '2011-05-06 00:00:00.0' 2863.88989257813
'SH000001' '2011-05-09 00:00:00.0' 2872.45996093750
......

12


>> price2=GetPrice(price1)

price2
''
'SH000001'
'SH000002'
'SH000003'
'2011-10-10' 2344.79
2456.02
237.08
'2011-10-11' 2348.52
2460.09
234.26
'2011-10-12' 2420
2534.96
241.62
'2011-10-13' 2438.79
2554.56
245.15
'2011-10-14' 2431.37
2546.80
244.09
'2011-10-17' 2440.40
2556.27
244.86
'2011-10-18' 2383.49
2496.64
239.30
SH000300SH600000 2011 5 1
2011 6 1
>> Stock={'SH000300','SH600000'}
Stock =
'SH000300'
'SH600000'
>> Price=GetPrice(Stock,'2011-05-01','2011-06-01')

Price =
''
'SH000300'
'SH600000'
[734626]
[3.2111e+003]
[ 10.8700]
[734627]
[3.1290e+003]
[ 10.5200]
[734628]
[3.1261e+003]
[ 10.5500]
[734629]
[3.1214e+003]
[ 10.5100]
[734632]
[3.1298e+003]
[ 10.4600]

1.13 Get1Price
1.
1[cellValue,matValue] =Get1Price(StockList,FromDate,ToDate)
2[cellValue,matValue] =Get1Price(Price)

StockList
FromDate
ToDate

Weight

Price

cellPrice
matPrice
300
300
13


StockList={'SH000300','SH600000'}'
FromDate='2012-01-01'
ToDate='2012-04-01'
[cellValue,matValue]=Get1Price(StockList,FromDate,ToDate)
[cellValue,matValue]=Get1Price(cellPrice)
[cellValue,matValue]=Get1Price(matPrice)
[cellValue,matValue]=Get1Price(Weight,FromDate,ToDate)

cellValue =
''
' 1'
' 2'
'2012-03-12'
[
1]
[
1]
'2012-03-13'
[1.0099]
[1.0098]
'2012-03-14'
[0.9864]
[0.9913]
'2012-03-15'
[0.9758]
[0.9775]
'2012-03-16'
[0.9877]
[0.9870]
'2012-03-19'
[0.9904]
[0.9899]
'2012-03-20'
[0.9732]
[0.9728]
'2012-03-21'
[0.9728]
[0.9708]
matValue =
1.0e+05 *
7.3494
0.0000
0.0000
7.3494
0.0000
0.0000
7.3494
0.0000
0.0000
7.3494
0.0000
0.0000
7.3494
0.0000
0.0000
7.3495
0.0000
0.0000
7.3495
0.0000
0.0000
7.3495
0.0000
0.0000

1.14 GetSpread

1[cellSpread matSpread]=GetSpread(StockList,FromDate,ToDate)
2[cellSpread matSpread]=GetSpread(Weight,FromDate,ToDate)
3[cellSpread matSpread]=GetSpreada(Price)

StockList
FromDate
ToDate

Weight

Price

14


cellSpread
matSpread

300
300
StockList=[{'SH000300','SH600000'}]';
FromDate='2012-01-01';
ToDate='2012-04-01';
[cellValue,matValue]=Get1Price(StockList,FromDate,ToDate);
[cellVa,matVal]=GetSpread(cellValue);
[cellVa,matVal]=GetSpread(matValue);

FromDate='2012-03-11';
ToDate='2012-03-21';
Weight ={
'SH600000'
[0.3000] [0.6000]
'SH000300'
[0.7000] [0.4000]};
[cellValue,matValue]=Get1Price(Weight,FromDate,ToDate);
[cellVa,matVal]=GetSpread(cellValue);
[cellVa,matVal]=GetSpread(matValue);
cellVa(:,:,1) =
[0]
[0]
[0]
[0]
cellVa(:,:,2) =
[
0]
[-1.3578e-04]
[1.3578e-04]
[
0]
cellVa(:,:,3) =
[
0]
[0.0049]
[-0.0049]
[
0]
cellVa(:,:,4) =
[
0]
[0.0017]
[-0.0017]
[
0]
cellVa(:,:,5) =
[
0]
[-6.6773e-04]
[6.6773e-04]
[
0]
cellVa(:,:,6) =
[
0]
[-4.4174e-04]
[4.4174e-04]
[
0]
cellVa(:,:,7) =
[
0]
[-4.0982e-04]
[4.0982e-04]
[
0]
cellVa(:,:,8) =
[
0]
[-0.0021]

15


[0.0021]

0]

1.15 PickPrice

[cellPrice,matPrice]=PickPrice(StockList,DateList)

StockList
DateList

cellPrice
matPrice

1
StockList={'SH000001'}
DateList={'2012-01-04'}
[cellPrice,matPrice]=PickPrice(StockList,DateList)
cellPrice =
''
'SH000001'
'2012-01-04'
[2.1694e+03]
matPrice =
1.0e+05 *
7.3487
0.0217
2
StockList={'SH000001'}
DateList=734872
[cellPrice,matPrice]=PickPrice(StockList,DateList)
cellPrice =
''
'SH000001'
'2012-01-04'
[2.1694e+03]
matPrice =
1.0e+05 *
7.3487
0.0217
2
StockList={'SH000001'}
DateList={734872}
[cellPrice,matPrice]=PickPrice(StockList,DateList)
cellPrice =
''
'SH000001'
'2012-01-04'
[2.1694e+03]
matPrice =
1.0e+05 *
16


7.3487

0.0217

1.16 (GetTradeDay)
A
1 [cellDay,matDay]=GetTradeDay(FromDate ToDate)
2 [cellDay,matDay]=GetTradeDay(StockList FromDate ToDate)

StockList
A
FromDate
ToDate

cellDay

matDay

1
StockList=[{'SH000300','SH600000'}]'
FromDate='2012-01-01'
ToDate='2012-04-01'
[cellPrice,matPrice]=GetTradeDay(StockList,FromDate,ToDate)

cellPrice =
{59x1 cell}
{59x1 cell}
matPrice =
[58x1 double]
[58x1 double]

cellPrice{1}
ans =
'SH000300'
'2012-04-01'
'2012-05-01'
'2012-06-01'
'2012-09-01'
'2012-10-01'
'2012-11-01'
2
[cellDay,matDay]=GetTradeDay('2012-01-01','2012-01-11')
cellDay =
'2012-01-04'
'2012-01-05'
'2012-01-06'
17


'2012-01-09'
'2012-01-10'
'2012-01-11'
matDay =
734872
734873
734874
734877
734878
734879

1.17 GetPreDay
Date 1
1 [cellDay,matDay]=GetPreDay(Date,N)
2 [cellDay,matDay]=GetPreDay(StockList Date,N)

StockList
FromDate
ToDate

cellDay

matDay

1
Date='2012-04-01'
[cellday matDay]=GetPreDay(Date)
cellday =
'2012-03-30'
matDay =
734958

1.18 300 GetHS300Price


300
HS300Price=GetHS300Price(FromDate,ToDate)

18


FromDate

ToDate

HS300Price
300
300 2012 1 1 2012 2 1
>>HS300Price=GetHS300Price('2012-01-01','2012-02-01')
HS300Price =
Columns 1 through 8
''
'SH000300'
'SH600177'
'SH600183'
'SH600188'

[734872]
[2.2988e+003]
[ 9.2500]
[ 7.1200]
[ 21.6300]

[734873]
[2.2764e+003]
[ 9.0200]
[ 7.0800]
[ 21.0800]

[734874]
[2.2906e+003]
[ 9.0900]
[ 7.2400]
[ 21.4900]

[734877]
[2.3686e+003]
[ 9.3500]
[ 7.4200]
[ 23.2600]

......

1.19 Price2Ret

[cellret,matret]=Price2Ret(HSprice)

HSprice

cellret

matret

''
'SH600000'
'SH600004'
'2011-04-06 '
14.59
8.76
'2011-04-07 '
14.55
8.81
'2011-04-08 '
14.52
8.82
'2011-04-11 '
14.52
8.81
'2011-04-12 '
14.43
8.97

>>Price(1,1:4)={'','SH600000','SH600004','SH600008'}
>>Price(2,1:4)={'2011-04-06 ',14.59,8.76,4.61};
>>Price(3,1:4)={'2011-04-07 ',14.55,8.81,4.61};
>>Price(4,1:4)={'2011-04-08 ',14.52,8.82,4.71};
>>Price(5,1:4)={'2011-04-11 ',14.52,8.81,4.78};
>>Price(6,1:4)={'2011-04-12 ',14.43,8.97,5.01};

>>cellret= Price2Ret (Price)


cellret =
19

'SH600008'
4.61
4.61
4.71
4.78
5.01


''
'2011-04-07'
'2011-04-08'
'2011-04-11'
'2011-04-12'

'SH600000'
'SH600004'
[ -0.0027]
[ 0.0057]
[ -0.0021]
[ 0.0011]
[
0]
[ -0.0011]
[ -0.0062]
[ 0.0180]

[
[
[
[

'SH600008'
0]
0.0215]
0.0148]
0.0470]

1.20 Ret2Price

[cellPrice matPrice]=Ret2Price(ret,BaseDate)

ret

BaseDate

cellPrice
matPrice
1
StockList=[{'SH000300','SH600000'}]'
FromDate='2012-01-01'
ToDate='2012-02-01'
Price=GetPrice(StockList,FromDate,ToDate)
FirstDate='2011-12-31'
[cellret,matret]=Price2Ret(Price)
[cellPrice matPrice]=Ret2Price(cellret,FirstDate)
cellPrice =
''
'SH000300'
'SH600000'
'2011-12-31'
[
1]
[
1]
'2012-01-05'
[ 0.9903]
[ 1.0285]
'2012-01-06'
[ 0.9965]
[ 1.0357]
'2012-01-09'
[ 1.0304]
[ 1.0642]
'2012-01-10'
[ 1.0646]
[ 1.0785]
'2012-01-11'
[ 1.0595]
[ 1.0702]
'2012-01-12'
[ 1.0594]
[ 1.0809]
'2012-01-13'
[ 1.0416]
[ 1.0749]
'2012-01-16'
[ 1.0204]
[ 1.0666]
'2012-01-17'
[ 1.0704]
[ 1.0939]
'2012-01-18'
[ 1.0537]
[ 1.0749]
'2012-01-19'
[ 1.0738]
[ 1.0939]
'2012-01-20'
[ 1.0893]
[ 1.1201]
'2012-01-30'
[ 1.0705]
[ 1.0963]
'2012-01-31'
[ 1.0720]
[ 1.0963]
'2012-02-01'
[ 1.0567]
[ 1.0797]
matPrice =
20


1.0000
1.0000
0.9903
1.0285
0.9965
1.0357
1.0304
1.0642
1.0646
1.0785
1.0595
1.0702
1.0594
1.0809
1.0416
1.0749
1.0204
1.0666
1.0704
1.0939
1.0537
1.0749
1.0738
1.0939
1.0893
1.1201
1.0705
1.0963
1.0720
1.0963
1.0567
1.0797
[cellPrice matPrice]=Ret2Price(matret,FirstDate)

[cellPrice matPrice]=Ret2Price(ret,FirstDate)

ret

FirstDate

cellPrice
matPrice
1
StockList={'SH000300','SH600000'}'
FromDate='2012-01-01'
ToDate='2012-02-01'
Price=GetPrice(StockList,FromDate,ToDate)
FirstDate='2011-12-31'
[cellret,matret]=Price2Ret(Price)
[cellPrice matPrice]=Ret2Price(cellret,FirstDate)
cellPrice =
''
'SH000300'
'SH600000'
'2011-12-31'
[
1]
[
1]
'2012-01-05'
[ 0.9903]
[ 1.0285]
'2012-01-06'
[ 0.9965]
[ 1.0357]
'2012-01-09'
[ 1.0304]
[ 1.0642]
'2012-01-10'
[ 1.0646]
[ 1.0785]
'2012-01-11'
[ 1.0595]
[ 1.0702]
'2012-01-12'
[ 1.0594]
[ 1.0809]
'2012-01-13'
[ 1.0416]
[ 1.0749]

21


'2012-01-16'
[
'2012-01-17'
[
'2012-01-18'
[
'2012-01-19'
[
'2012-01-20'
[
'2012-01-30'
[
'2012-01-31'
[
'2012-02-01'
[
matPrice =
1.0e+05 *
7.3487
0.0000
7.3487
0.0000
7.3487
0.0000
7.3488
0.0000
7.3488
0.0000
7.3488
0.0000
7.3488
0.0000
7.3488
0.0000
7.3488
0.0000
7.3488
0.0000
7.3489
0.0000
7.3489
0.0000
7.3489
0.0000
7.3490
0.0000
7.3490
0.0000
7.3490
0.0000

1.0204]
1.0704]
1.0537]
1.0738]
1.0893]
1.0705]
1.0720]
1.0567]

[
[
[
[
[
[
[
[

1.0666]
1.0939]
1.0749]
1.0939]
1.1201]
1.0963]
1.0963]
1.0797]

0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000

[cellPrice matPrice]=Ret2Price(matret,FirstDate)

cellPrice =
'2011-12-31'
[
1]
[
1]
'2012-01-05'
[0.9903]
[1.0285]
'2012-01-06'
[0.9965]
[1.0357]
'2012-01-09'
[1.0304]
[1.0642]
'2012-01-10'
[1.0646]
[1.0785]
'2012-01-11'
[1.0595]
[1.0702]
'2012-01-12'
[1.0594]
[1.0809]
'2012-01-13'
[1.0416]
[1.0749]
'2012-01-16'
[1.0204]
[1.0666]
'2012-01-17'
[1.0704]
[1.0939]
'2012-01-18'
[1.0537]
[1.0749]
'2012-01-19'
[1.0738]
[1.0939]
'2012-01-20'
[1.0893]
[1.1201]
'2012-01-30'
[1.0705]
[1.0963]

22


'2012-01-31'
[1.0720]
[1.0963]
'2012-02-01'
[1.0567]
[1.0797]
matPrice =
1.0e+05 *
7.3487
0.0000
0.0000
7.3487
0.0000
0.0000
7.3487
0.0000
0.0000
7.3488
0.0000
0.0000
7.3488
0.0000
0.0000
7.3488
0.0000
0.0000
7.3488
0.0000
0.0000
7.3488
0.0000
0.0000
7.3488
0.0000
0.0000
7.3488
0.0000
0.0000
7.3489
0.0000
0.0000
7.3489
0.0000
0.0000
7.3489
0.0000
0.0000
7.3490
0.0000
0.0000
7.3490
0.0000
0.0000
7.3490
0.0000
0.0000

1.21 GetAbnormRet
1
1[cellRet matRet] =GetAbnormRet(StockList,FromDate,ToDate)
2[cellRet matRet] =GetAbnormRet(Weight,FromDate,ToDate)
3[cellRet matRet] =GetAbnormRet(Price)

StockList
FromDate
ToDate

Weight

Price

cellRet
matRet

300
300
StockList={'SH000300','SH600000'}'
FromDate='2012-01-01'
23


ToDate='2012-04-01'
[cellPrice,matPrice]=GetPrice(StockList,FromDate,ToDate)
[cellRet,matRet]=GetAbnormRet(matPrice)

2
FromDate='2012-03-11'
ToDate='2012-03-21'
Weight ={
'SH600000'
[0.3000] [0.6000]
'SH000300'
[0.7000] [0.4000]}
[cellBeta matBeta] =GetAbnormRet(Weight,FromDate,ToDate)
3
StockList={'SH000300','SH600000'}'
FromDate='2012-01-01'
ToDate='2012-02-01'
AbnormRet=GetAbnormRet(StockList,FromDate,ToDate)
AbnormRet =
''
'SH000300'
'SH600000'
'2012-01-05'
[-7.5272e-05]
[
0.0378]
'2012-01-06'
[-7.1147e-04]
[-2.1936e-05]
'2012-01-09'
[
0.0050]
[
-0.0013]
'2012-01-10'
[
0.0062]
[
-0.0132]
'2012-01-11'
[-5.6023e-04]
[
-0.0035]
'2012-01-12'
[ 2.9690e-04]
[
0.0104]
'2012-01-13'
[
-0.0035]
[
0.0080]
'2012-01-16'
[
-0.0033]
[
0.0095]
'2012-01-17'
[
0.0069]
[
-0.0156]
'2012-01-18'
[
-0.0017]
[
-0.0035]
'2012-01-19'
[
0.0059]
[
0.0045]
'2012-01-20'
[
0.0044]
[
0.0136]
'2012-01-30'
[
-0.0027]
[
-0.0067]
'2012-01-31'
[
-0.0019]
[
-0.0033]
'2012-02-01'
[
-0.0037]
[
-0.0045]

1.22 GetCov

1[cellCov,matCov]=GetCov(StockList,FromDate,ToDate)
2[cellCov,matCov]=GetCov(Weight,FromDate,ToDate)
3[cellCov,matCov]=GetCov(Price)
24

StockList

FromDate

ToDate

Price

Weight

cellCorrcoef

matCorrcoef

cellCov
matCov

>>StockName={'SH600000';'SH000300'}
StockName =
'SH600000'
'SH000300'
>>StockPrice=GetPrice(StockName,'2012-01-01','2012-01-10')
StockPrice =
''
'SH600000'
'SH000300'
'2012-01-04'
[ 8.4100]
[2.2988e+003]
'2012-01-05'
[ 8.6500]
[2.2764e+003]
'2012-01-06'
[ 8.7100]
[2.2906e+003]
'2012-01-09'
[ 8.9500]
[2.3686e+003]
'2012-01-10'
[ 9.0700]
[2.4473e+003]

>>[~,Stockcov]=GetCov(StockPrice)
Stockcov =
''
'SH600000'
'SH000300'
'SH600000'
[ 1.0959e-004]
[-2.3176e-005]
'SH000300'
[-2.3176e-005]
[ 4.4810e-004]

1.23 GetCorrcoef

1[cellCorrcoef,matCorrcoef]=GetCorrcoef(StockList,FromDate,ToDate)
2[cellCorrcoef,matCorrcoef]=GetCorrcoef(Weight,FromDate,ToDate)
3[cellCorrcoef,matCorrcoef]=GetCorrcoef(Price)

StockList

FromDate

ToDate

25


Price

Weight

cellCorrcoef

matCorrcoef
300
300
>>Name={'SH000300','SH600000'}'
>>Price=GetPrice(Name,'2012-01-01','2012-01-20')
Price =
''
'SH000300'
'SH600000'
'2012-01-04'
[2.2988e+003]
[ 8.4100]
'2012-01-05'
[2.2764e+003]
[ 8.6500]
'2012-01-06'
[2.2906e+003]
[ 8.7100]
'2012-01-09'
[2.3686e+003]
[ 8.9500]
'2012-01-10'
[2.4473e+003]
[ 9.0700]
'2012-01-11'
[2.4356e+003]
[
9]
'2012-01-12'
[2.4352e+003]
[ 9.0900]
'2012-01-13'
[2.3943e+003]
[ 9.0400]
'2012-01-16'
[2.3457e+003]
[ 8.9700]
'2012-01-17'
[2.4606e+003]
[ 9.2000]
'2012-01-18'
[2.4222e+003]
[ 9.0400]
'2012-01-19'
[2.4683e+003]
[ 9.2000]
'2012-01-20'
[2.5041e+003]
[ 9.4200]
>>Stockcov=GetCorrcoef(Price)
Stockcov =
1
0.7048
0.7048] 1
>>[~,Stockcov]=GetCorrcoef(Price)
Stockcov =
''
'SH000300'
'SH600000'
'SH000300'
[
1]
[ 0.7048]
'SH600000'
[ 0.7048]
[
1]

1.24 GetHighAndLow

HighAndLoeprice=GetHighAndLow(StockList,From,ToDate)

StockList

Interval

Date

HighAndLoeprice
300 2012 1 1
26

>>StockList={'SH000300','SH600000'}'
>>FromDate='2010-01-01'
>>ToDate='2012-01-01'
>>HighAndLoeprice=GetHighAndLow(StockList,FromDate,ToDate)
HighAndLoeprice =
'2012-01-01'
''
''
''
'SH000300'
[3.5486e+003]
[2.3050e+003]
[2.3457e+003]
'SH600000'
[
11.6900]
[
8.3500]
[
8.4900]

1.25 Alpha(GetAlpha)
Alpha Alpha
1[cellAlphaBeta,matAlphaBeta]=GetAlpha(StockList,FromDate,ToDate,Method)
2[cellAlphaBeta,matAlphaBeta]=GetAlpha(Weight,FromDate,ToDate,Method)
3[cellAlphaBeta,matAlphaBeta]=GetAlpha(Price,Method)

StockList
FromDate
ToDate

Weight

Price

Method
'CAPM'

cellAlpha Alpha
matAlpha Alpha
300 Alpha Alpha
300
>>StockList={'SH000300','SH600000'}'
>>FromDate='2012-01-01'
>>ToDate='2012-02-01'
>>[cellAlpha,matAlpha]=GetAlpha(StockList,FromDate,ToDate)
cellAlpha =
''
'Alpha'
'Alpha '
'SH000300'
[1.3213e-004]
[
0.0035]
'SH600000'
[
0.0030]
[
0.0021]
matAlpha =
0.0001
0.0035
0.0030
0.0021
Alpha Alpha
FromDate='2012-03-11'
ToDate='2012-03-21'
27


Weight ={
'SH600000'
[0.3000] [0.6000]
'SH000300'
[0.7000] [0.4000]}
[cellAlpha,matAlpha]=GetAlpha(Weight,FromDate,ToDate)
cellAlpha =
''
'Alpha'
'Alpha '
' 1'
[-6.2688e-004]
[
-0.0033]
' 2'
[
-0.0014]
[
-0.0028]
matAlpha =
-0.0006 -0.0033
-0.0014 -0.0028
Alpha Alpha
>>StockList={'SH000300','SH000001'}'
>>FromDate='2012-01-01'
>>ToDate='2012-02-01'
>>Price=GetPrice(StockList,FromDate,ToDate)
>>[cellAlpha,matAlpha]=GetAlpha(Price)
cellAlpha =
''
'Alpha'
'Alpha '
'SH000300'
[1.3213e-004]
[
0.0035]
matAlpha =
0.0001
0.0035

1.26 BetaGetBeta
1 Beta
1[cellBeta matBeta] =GetBeta(StockList,FromDate,ToDate)
2[cellBeta matBeta] =GetBeta(Weight,FromDate,ToDate)
3[cellBeta matBeta] =GetBeta(Price)

StockList
FromDate
ToDate

Weight

Price

cellBeta Beta
matBeta Beta
300 Beta
300
StockList=[{'SH000300'},{'SH600000'}]'
FromDate='2012-01-01'
28


ToDate='2012-04-01'
[cellPrice,matPrice]=GetPrice(StockList,FromDate,ToDate)
[cellBeta matBeta] =GetBeta(cellPrice)
[cellBeta matBeta] =GetBeta(matPrice)
2 Beta
FromDate='2012-03-11'
ToDate='2012-03-21'
Weight ={
'SH600000'
[0.3000] [0.6000]
'SH000300'
[0.7000] [0.4000]}
[cellBeta matBeta] =GetBeta(Weight,FromDate,ToDate)
15% 10
15% 10
cellBeta =
' Beta'
'Beta'
' 1'
[ NaN]
' 2'
[ NaN]
matBeta =
NaN
NaN
3 Beta
StockList={'SH000300','SH000001'}'
StockList=[{'SH000300'},{'SH600000'}]'
FromDate='2012-01-01'
ToDate='2012-02-01'
Price=GetPrice(StockList,FromDate,ToDate)
[cellBeta matBeta] =GetBeta(Price)
cellBeta =
' Beta'
'Beta'
'SH000300'
[5.2170]
matBeta =
5.21705
Beta 2012 1 1 2012 1 31
>>StockList=[{'SH600000','SH600267'}]'
StockList =
'SH600000'
'SH600267'
>>FromDate='2012-01-01';
>>ToDate='2012-01-31';
>>Beta =GetBeta(StockList,FromDate,ToDate)

Beta =
''
'Beta'
'SH600000'
[0.3631]

29


'SH600267'
[1.0472]
Beta
>>Weight={
''
''
'SH600000'
0.7
'SH600267'
0.3}
>>FromDate='2012-01-01';
>>ToDate='2012-01-31';
>>Beta =GetBeta(Weight,FromDate,ToDate)
Beta =
' Beta'
[0.5683]

1.27 CAPMGetCAPM
CAPM
Y=Aplha+Beta(YM-Rf)
riskPara =GetCAPM(StockList,FromDate,ToDate)
1[cellCAPM,matCAPM]=GetCAPM(StockList,FromDate,ToDate)
2[cellCAPM,matCAPM]=GetCAPM(Weight,FromDate,ToDate)
3[cellCAPM,matCAPM]=GetCAPM(Price)

StockList

FromDate

ToDate

Price

Weight

cellCAPM
CAPM
matCAPM
CAPM
300 CAPM
300
>>StockList=[{'SH000300'},{'SH600000'}]'
>>FromDate='2012-01-01'
>>ToDate='2012-04-01'
>>[cellCAPM,matCAPM]=GetCAPM(StockList,FromDate,ToDate)
cellCAPM =
'CAPM '
'Alpha'
'Beta'
'Alpha/Sigma'
'Beta/Sigma'
'SH000300'
[3.1315e-04]
[1.1621]
[
1.7912]
[ 85.4149]
'SH600000'
[4.2754e-04]
[0.8149]
[
0.3899]
[
9.5501]
matCAPM =
0.0003
1.1621
1.7912 85.4149
30


0.0004

0.8149

0.3899

9.5501

2 CAPM
FromDate='2012-03-11'
ToDate='2012-03-21'
Weight ={
'SH600000'
[0.3000] [0.6000]
'SH000300'
[0.7000] [0.4000]}
[cellCAPM,matCAPM] =GetCAPM(Weight,FromDate,ToDate)
15% 10
15% 10
cellCAPM =
'CAPM '
'Alpha'
'Beta'
'Alpha/Sigma'
'Beta/Sigma'
' 1'
[ NaN]
[ NaN]
[
NaN]
[
NaN]
matCAPM =
NaN NaN NaN NaN
3 CAPM
>>StockList=[{'SH000300'},{'SH600276'}]'
>>FromDate='2012-01-01'
>>ToDate='2012-02-01'
>>Price=GetPrice(StockList,FromDate,ToDate)
>>[cellCAPM,matCAPM] =GetCAPM(Price)
cellCAPM =
'CAPM '
'Alpha'
'Beta'
'Alpha/Sigma'
'Beta/Sigma'
'SH000300'
[1.5555e-04]
[1.1942]
[
0.3756]
[ 50.6627]
'SH600276'
[ -0.0094]
[1.0360]
[
1.6375]
[
3.1605]
matCAPM =
0.0002
1.1942
0.3756 50.6627
-0.0094
1.0360
1.6375
3.1605

1.28 GetStatis
HedgePara=GetStatis(HedgeValue)

A10%50%90%
Long =[{'SH600000'},{'SZ000001'}]'
Short =[{'SH000300'},{'SH000001'},{'SH600276'}]'
Date='2012-01-01'
PortHedgeValue1=GetHedgeValue(Long,Short,Date)

31

1.29 GetDynamic

l
l
l
l
l
l
l
l
l
l
l
1[cellDynamic,matDynamic]=GetDynamic(StockList,FromDate,ToDate)
2[cellDynamic,matDynamic]=GetDynamic(Weight,FromDate,ToDate)
3[cellDynamic,matDynamic]=GetDynamic(Price)

StockList

FromDate

ToDate

Price

Weight

cellDynamic

matDynamic

300
300
>>StockList=[{'SH000300'},{'SH600000'}]'
>>FromDate='2012-01-01'
>>ToDate='2012-02-01'
>>[cellDynamic,matDynamic]=GetDynamic(StockList,FromDate,ToDate)
cellDynamic =
Columns 1 through 7
'' ''
''
''
'' '' ''
'SH000300' [2.5041e+003] [2.2764e+003] [2.5041e+003] [ 0.0300] [ 0.0670] [ 0.0416]
'SH600000' [9.4200]
[8.4100]
[9.4200]
[0.0361]
[0.0797]
[ 0.0361]
Columns 8 through 12
''
''
''
''
''
[ 0.1000]
[0.0215]
[4.6209e-004]
[
0]
[
0]
[ 0.1201]
[0.0172]
[2.9455e-004]
[
0]
[
0]
matDynamic =

32


1.0e+003 *
2.2764
2.5041
0.0000
0.0001 0.0000 0.0001 0.0000 0.0000 0
0
0.0084
0.0094
0.0000
0.0001
0.0000 0.0001 0.0000 0.0000 0
0

FromDate='2012-03-11'
ToDate='2012-03-21'
Weight ={
'SH600000'
[0.3000] [0.6000]
'SH000300'
[0.7000] [0.4000]}
[cellDynamic,matDynamic]=GetDynamic(Weight,FromDate,ToDate)
cellDynamic =
Columns 1 through 9
''
''
''
''
''
''
'
'
''
''
' 1'
[1.0986]
[
1]
[1.0986]
[
0.0319]
[
0.0636]
[ 0.0323]
[ 0.0986]
[0.0192]
' 2'
[1.1078]
[
1]
[1.1078]
[
0.0337]
[
0.0705]
[ 0.0337]
[ 0.1078]
[0.0177]
Columns 10 through 12
''
''
''
[3.6753e-004]
[
0]
[
0]
[3.1165e-004]
[
0]
[
0]
matDynamic =
1.0000
1.0986
0.0319
0.0636
0.0323
0.0986
0.0192
0.0004
0
0
1.0000
1.1078
0.0337
0.0705
0.0337
0.1078
0.0177
0.0003
0
0

>>StockList=[{'SH000300'},{'SH600000'}]'
>>FromDate='2012-01-01'
>>ToDate='2012-02-01'
>>Price=GetPrice(StockList,FromDate,ToDate)
>>[cellDynamic,matDynamic]=GetDynamic(Price)
cellDynamic =
Columns 1 through 7
''
''
''
''
''
''
[2.5041e+003]
[2.2764e+003]
[2.5041e+003]
[
0.0300]
[ 0.0416]
[ 0.1000]
[
9.4200]
[
8.4100]
[
9.4200]
[
0.0361]
[ 0.0361]
[ 0.1201]
Columns 8 through 11
''
''
''
''

33

''
[

0.0670]

0.0797]


[0.0215]
[0.0172]
matDynamic =
1.0e+003 *
2.2764
0
0
0.0084
0
0

[4.6209e-004]
[2.9455e-004]

[
[

0]
0]

[
[

0]
0]

2.5041

0.0000

0.0001

0.0000

0.0001

0.0000

0.0000

0.0094

0.0000

0.0001

0.0000

0.0001

0.0000

0.0000

1.30 VaRGetVaR
VaR
1[cellVaR MatVaR]=GetVaR(StockList,FromDate,ToDate)
2[cellVaR MatVaR]=GetVaR(Weight,FromDate,ToDate)
3[cellVaR MatVaR]=GetVaR(Price)

StockList

FromDate

ToDate

Price

Weight

cellVaR
VaR
MatVaR
VaR
[] VaR
>>StockList=[{'SH000300'},{'SH600000'}]'
>>FromDate='2012-01-01'
>>ToDate='2012-02-01'
>>[CellVar,PortVar]=GetVaR(StockList,FromDate,ToDate)
CellVar =
''
'VaR'
'SH000300'
[0.0325]
'SH600000'
[0.0258]
PortVar =
0.0325
0.0258
VaR
>>StockList=[{'SH000300'},{'SH600000'}]'
>>FromDate='2012-01-01'
>>ToDate='2012-02-01'
>>Price=GetPrice(StockList,FromDate,ToDate)
Price =
''
'SH000300'
'SH600000'
'2012-01-04'
[2.2988e+003]
[ 8.4100]
34


'2012-01-05'
[2.2764e+003]
[ 8.6500]
'2012-01-06'
[2.2906e+003]
[ 8.7100]
'2012-01-09'
[2.3686e+003]
[ 8.9500]
'2012-01-10'
[2.4473e+003]
[ 9.0700]

>>[CellVar,PortVar]=GetVaR(Price)

CellVar =
''
'VaR'
'SH000300'
[0.0322]
'SH600000'
[0.0262]
PortVar =
0.0322
0.0262
VaR
FromDate='2012-03-11'
ToDate='2012-03-21'
Weight ={
'SH600000'
[0.3000] [0.6000]
'SH000300'
[0.7000] [0.4000]}
[CellVar,PortVar]=GetVaR(Weight,FromDate,ToDate)
CellVar =
''
'VaR'
' 1'
[0.0022]
' 2'
[0.0216]
PortVar =
0.0022
0.0216

1.31 MarketPrctile

1[cellPrctil,matPrctil]=MarketPrctile
2[cellPrctil,matPrctil]=MarketPrctile(ToDate)
3[cellPrctil,matPrctil]=MarketPrctile(FromDate, ToDate)
4[cellPrctil,matPrctil]=MarketPrctile(StockList,FromDate, ToDate)
5[cellPrctil,matPrctil]=MarketPrctile(Price)

StockList
FromDate
ToDate

Weight

35


Price
Method

cellPrctil
matPrctil

'CAPM'

FromDate='2012-01-01';
ToDate='2012-02-01';
StockList=GetStockList(1);
[cellPrice,matPrice]=MarketPrctile(StockList,FromDate, ToDate);
cellPrice =
''
' 1'
' 2'
' 3'
'2012-01-05'
[-0.0508]
[-0.0353]
[-0.0219]
'2012-01-06'
[
0]
[ 0.0079]
[ 0.0152]
'2012-01-09'
[ 0.0339]
[ 0.0406]
[ 0.0473]
'2012-01-10'
[ 0.0317]
[ 0.0387]
[ 0.0471]
'2012-01-11'
[-0.0080]
[-0.0024]
[ 0.0040]
'2012-01-12'
[-0.0072]
[-0.0013]
[ 0.0049]
'2012-01-13'
[-0.0440]
[-0.0348]
[-0.0254]
'2012-01-16'
[-0.0388]
[-0.0278]
[-0.0181]
'2012-01-17'
[ 0.0468]
[ 0.0548]
[ 0.0624]
'2012-01-18'
[-0.0254]
[-0.0181]
[-0.0113]
'2012-01-19'
[ 0.0040]
[ 0.0098]
[ 0.0162]
'2012-01-20'
[ 0.0053]
[ 0.0114]
[ 0.0173]
'2012-01-30'
[-0.0163]
[-0.0101]
[-0.0025]
'2012-01-31'
[-0.0033]
[ 0.0019]
[ 0.0078]
'2012-02-01'
[-0.0163]
[-0.0102]
[-0.0038]
matPrice =
-0.0508 -0.0353 -0.0219
0
0.0079
0.0152
0.0339
0.0406
0.0473
0.0317
0.0387
0.0471
-0.0080 -0.0024
0.0040
-0.0072 -0.0013
0.0049
-0.0440 -0.0348 -0.0254
-0.0388 -0.0278 -0.0181
0.0468
0.0548
0.0624
-0.0254 -0.0181 -0.0113
0.0040
0.0098
0.0162
0.0053
0.0114
0.0173
-0.0163 -0.0101 -0.0025
-0.0033
0.0019
0.0078

36


-0.0163

-0.0102

-0.0038

2
2.1 (GetInduList)

InduList=GetInduList(InduCode)

InduCode
5.2

InduList

[ 78]
[ 3.1225]
L

[ 29]
[ 1.1609]
G

[181]
[ 7.2458]
C99

[ 15]
[ 0.6005]
A

[ 41]
[ 1.6413]
C8

[144]
[ 5.7646]
E

[ 57]
[ 2.2818]
J

[162]
[ 6.4852]
H

[161]
[ 6.4452]
C2

[ 14]
[ 0.5604]
C7

[507]
[20.2962]
D

[ 75]
[ 3.0024]
C5

[146]
[ 5.8447]
C4

[266]
[10.6485]
K

[ 75]
[ 3.0024]
C1

[ 83]
[ 3.3227]
M

[ 20]
[ 0.8006]
C3

[ 46]
[ 1.8415]
B

[ 60]
[ 2.4019]
C6

[200]
[ 8.0064]
I

[ 42]
[ 1.6813]
C0

[ 96]
[ 3.8431]

>>FromDate='2012-01-01'
>>ToDate='2012-05-12'
>>InduList=GetInduList('F')
InduList =
37


'SZ000022'
'SZ000088'
'SZ000089'
'SZ000099'
'SZ000429'
'SZ000582'
'SZ000520'
......

2.2 GetInduCorrcoef

[cellInduCorrcoef,matInduCorrcoeff]=GetInduCorrcoef(FromDate,ToDate,N)

FromDate
ToDate
InduCode

[ 78]
[ 3.1225]
L

[ 29]
[ 1.1609]
G

[181]
[ 7.2458]
C99

[ 15]
[ 0.6005]
A

[ 41]
[ 1.6413]
C8

[144]
[ 5.7646]
E

[ 57]
[ 2.2818]
J

[162]
[ 6.4852]
H

[161]
[ 6.4452]
C2

[ 14]
[ 0.5604]
C7

[507]
[20.2962]
D

[ 75]
[ 3.0024]
C5

[146]
[ 5.8447]
C4

[266]
[10.6485]
K

[ 75]
[ 3.0024]
C1

[ 83]
[ 3.3227]
M

[ 20]
[ 0.8006]
C3

[ 46]
[ 1.8415]
B

[ 60]
[ 2.4019]
C6

[200]
[ 8.0064]
I

[ 42]
[ 1.6813]
C0

[ 96]
[ 3.8431]

cellInduCorrcoef
38


matInduCorrcoeff
'F' 2012-01-01
2012-05-12
>>FromDate='2012-01-01'
>>ToDate='2012-05-12'
>>GetInduCorrcoef(FromDate,ToDate,'F')
cellInduCorrcoef =
Columns 1 through 9
[]
[]
'SZ000022'
'SZ000088'
'SZ000089'
[]
[]
''
''
''
'SZ000022'
''
[
1]
[
0.0712]
[ 0.8433]
'SZ000088'
''
[ 0.0712] [
1]
[ -0.0365]
'SZ000089'
''
[ 0.8433]
[
-0.0365]
[
1]
......

2.3 GetInduIndexCorrcoef

[cellInduCorrcoef,matInduCorrcoeff]=GetInduIndexCorrcoef(FromDate,ToDate,N)

FromDate

ToDate

cellInduCorrcoef

matInduCorrcoeff

cellInduCorrcoef
matInduCorrcoeff
180
FromDate='2012-01-01'
ToDate='2012-01-01'
[cellInduCorrcoef,matInduCorrcoeff]=GetInduIndexCorrcoef(FromDate,ToDate,6)
Columns 1 through 9
[]
[]
'SH000018'
'SH000021'
'SH000025'
[]
[]
'180 '
'180 '
'180 '
'180 '
'SH000018'
'180 '
[
1]
[ 0.9537]
[ 0.8536]
[ 0.7745]
'SH000021'
'180 '
[ 0.9537]
[
1]
[ 0.9528]
[ 0.8992]
'SH000025'
'180 '
[ 0.8536]
[ 0.9528]
[
1]
[ 0.8813]
'SH000026'
'180 '
[ 0.7745]
[ 0.8992]
[ 0.8813]
[
1]
'SH000027'
'180 '
[ 0.8289]
[ 0.9314]
[ 0.9426]
[ 0.8803]
'SH000028'
'180 '
[ 0.9143]
[ 0.9798]
[ 0.9449]
[ 0.9278]
'SH000029'
'180 '
[ 0.9816]
[ 0.9843]
[ 0.9215]
[ 0.8382]
39


'SH000030'
'180R '
'SH000031'
'180R '
'SH000051'
'180 '
'SH000053'
'180 '
Columns 10 through 13
'SH000030'
'SH000031'
'180R '
'180R '
[ 0.8349]
[ 0.9671]
[ 0.9503]
[ 0.9979]
[ 0.9410]
[ 0.9465]
[ 0.9561]
[ 0.8826]
[ 0.9323]
[ 0.9203]
[ 0.9783]
[ 0.9746]
[ 0.8917]
[ 0.9922]
[
1]
[ 0.9350]
[ 0.9350]
[
1]
[ 0.9902]
[ 0.9394]
[ 0.9053]
[ 0.9582]

[
[
[
[

0.8349]
0.9671]
0.8332]
0.9312]

'SH000051'
'180 '
[ 0.8332]
[ 0.9539]
[ 0.9530]
[ 0.9421]
[ 0.9382]
[ 0.9643]
[ 0.8947]
[ 0.9902]
[ 0.9394]
[
1]
[ 0.9079]

[
[
[
[

0.9503]
0.9979]
0.9539]
0.9569]

[
[
[
[

0.9410]
0.9465]
0.9530]
0.9205]

'SH000053'
'180 '
[ 0.9312]
[ 0.9569]
[ 0.9205]
[ 0.8329]
[ 0.8987]
[ 0.9364]
[ 0.9467]
[ 0.9053]
[ 0.9582]
[ 0.9079]
[
1]

2.4 GetCatlog

Catloged=GetCatlog(Data,N,Dict)

Data

Dict

N
Data N

Catloged
Beta
'A'
[-0.3000]
'B'
[-0.1000]
'C'
[
0]
'D'
[ 0.2000]
'E'
[ 0.5000]
'F'
[ 0.7000]
'G'
[ 0.9000]
'H'
[ 1.1000]
'I'
[ 1.5000]
'J'
[ 1.8000]
'K'
[
2]
'L'
[
Inf]
'M'
[
NaN]

40

[
[
[
[

0.9561]
0.8826]
0.9421]
0.8329]

3
3.1 GetPortflioValue
1:portflio=PortflioValue(WeightFromDateToDate)
2:portflio=PortflioValue(Price,weight)

Weight

FromDate

ToDate

Price

weight

portflio

SH600000

SH600004

SH600008

0.7

0.2

0.1

2011-04-06

14.59

8.76

4.61

2011-04-07

14.55

8.81

4.61

2011-04-08

14.52

8.82

4.71

2011-04-11

14.52

8.81

4.78

2011-04-12

14.43

8.97

5.01

1
>>Price(1,1:4)={'','SH600000' ,'SH600004','SH600008'}
>>Price(2,1:4)={'2011-04-06 ',14.59,8.76,4.61};
>>Price(3,1:4)={'2011-04-07 ',14.55,8.81,4.61};
>>Price(4,1:4)={'2011-04-08 ',14.52,8.82,4.71};
>>Price(5,1:4)={'2011-04-11 ',14.52,8.81,4.78};
>>Price(6,1:4)={'2011-04-12 ',14.43,8.97,5.01};
2
>>weight=[0.7,0.2,0.1]
3
>>portfliovalue=GetPortflioValue(Price,weight)
portfliovalue =
1.0000
0.9992
1.0002
1.0015
1.0058

41

portfliovalue =PortflioValue(Price)
1.0000
1.0010
1.0079
1.0126
1.0333

'

SH600000

0.3

SH000300

0.7

>>weight ={
''
''
'SH600000'
[0.3000]
'SH000300'
[0.7000]}

>>portfliovalue=GetPortflioValue(weight)
portfliovalue =
''
''
[732316]
[
1]
[732317]
[ 0.9927]
[732318]
[ 0.9879]
[732319]
[ 0.9879]
[732322]
[ 1.0024]
......
2011 1 1
>>portfliovalue=GetPortflioValue(weight,'2011-01-01')
portfliovalue =
''
''
[734507]
[
1]
[734508]
[ 0.9991]
[734509]
[ 0.9947]
[734510]
[ 1.0097]
......
2012 3 1 2012 3 21
portfliovalue=GetPortflioValue(weight,'2012-03-11','2012-03-21')
portfliovalue =
''
''
[734940]
[
1]
[734941]
[ 1.0099]
[734942]
[ 0.9864]
[734943]
[ 0.9758]
[734944]
[ 0.9877]
......
42

>> weight ={
''
''
'SH600000'
[0.3000]
'SH000300'
[0.7000]}
2012 1 1 2 1
>>Val=GetPortflioValue(weight,'2012-01-01','2012-02-01')
Val =
''
''
[734872]
[
1]
[734873]
[ 1.0018]
[734874]
[ 1.0082]
[734877]
[ 1.0405]
[734878]
[ 1.0688]
[734879]
[ 1.0627]
[734880]
[ 1.0658]
[734881]
[ 1.0516]
[734884]
[ 1.0343]
[734885]
[ 1.0775]
[734886]
[ 1.0601]
[734887]
[ 1.0798]
[734888]
[ 1.0986]
[734898]
[ 1.0782]
[734899]
[ 1.0793]
[734900]
[ 1.0636]

3.2 BetaGetPortflioBeta
Beta
[cellBeta,matBeta]=GetPortflioBeta(Weight,FromDate,ToDate)

Weight

FromDate
ToDate

riskBeta Beta
Beta
>>Weight={
'SH600000'
0.7
'SH600267'
0.3}
>>FromDate='2012-01-01';
>>ToDate='2012-01-31';
43


>>Beta =GetPortflioBeta(Weight,FromDate,ToDate)
Beta =
' Beta'
[0.5683]
Beta =GetPortflioBeta(Weight,FromDate,ToDate)
Beta
>>Weight={
'SH600000'
0.7 0.4 0.9
'SH600267'
0.3 0.6 0.1}
>>FromDate='2012-01-01';
>>ToDate='2012-01-31';
>>Beta =GetPortflioBeta(Weight,FromDate,ToDate)
Beta =
' 1Beta'
[0.5683]
' 2Beta'
[0.7736]
' 3Beta'
[0.4315]

3.3 GetAppraisal

1[cellApp,matApp]=GetAppraisal(StockList,FromDate,ToDate,ModelType)
2[cellApp,matApp]=GetAppraisal(Weight,FromDate,ToDate,ModelType)
3[cellApp,matApp]=GetAppraisal(Price,ModelType)

StockList

FromDate

ToDate

Price

Weight

ModelType
'TM' TM
HM HM

cellAppraisal
matAppraisal
TM
Rp - Rf =a + b(Rm -Rf) + c(Rm - Rf)^2 + ep
Rp a>0 c>0 .
HM
Rp - Rf =a + b(Rm -Rf) + c(Rm - Rf)D + ep
Rm - Rf>0,D=1, D=0
Rp a>0 c>0 .

FromDate='2010-01-01'
ToDate='2012-04-21'
Weight ={
44


'SH600000'
[0.3000] [0.6000]
'SH000300'
[0.7000] [0.4000]}
[cellAppraisal,matAppraisal] =GetAppraisal(Weight,FromDate,ToDate)
15% 10
15% 10
cellAppraisal =
''
'a'
'c'
' 1'
[-2.3873e-04]
[0.4330]
' 2'
[-4.8906e-04]
[0.2483]
matAppraisal =
-0.0002
0.4330
-0.0005
0.2483

FundCode ={
'SZ160105'
'SZ160106'
'SZ160119'
'SZ160123'
'SZ160125'
'SH000001'
'rf'
}
FromDate='2012-01-01'
ToDate='2012-03-01'
Price=GetPrice(FundCode,FromDate,ToDate)
[cellAppraisal,matAppraisal]= GetAppraisal(Price)
cellAppraisal =
''
' a'
' c'
'SZ160105'
[
0.0018]
[-3.9607]
'SZ160106'
[
-0.0019]
[ 8.2259]
'SZ160119'
[-7.6560e-04]
[-0.0208]
'SZ160123'
[-1.0904e-04]
[-0.3522]
'SZ160125'
[
0.0030]
[ 3.0276]
matAppraisal =
0.0018 -3.9607
-0.0019
8.2259
-0.0008 -0.0208
-0.0001 -0.3522
0.0030
3.0276

AppendName(cellAppraisal)
ans =
''
''
' a'
' c'

45


'SZ160105'
'SZ160106'
'SZ160119'
'SZ160123'
'SZ160125'

''
''
' 500'
' 50 '
''

[
0.0018]
[
-0.0019]
[-7.6560e-04]
[-1.0904e-04]
[
0.0030]

[-3.9607]
[ 8.2259]
[-0.0208]
[-0.3522]
[ 3.0276]

4
4.1 PlotPrice

H=PlotPrice(Price,NumOfInterval)

Price
1
NumOfInterval

>> StockList=GetStockList(26)
StockList =
'IFL0'
'IFL1'
'IFL2'
'IFL3'
>> FPrice=GetPrice(StockList,'2012-01-01','2012-02-01')
FPrice=
''
'2012-01-04'
'2012-01-05'
'2012-01-06'
'2012-01-09'
'2012-01-10'
'2012-01-11'
'2012-01-12'
'2012-01-13'
'2012-01-16'
'2012-01-17'
'2012-01-18'
'2012-01-19'
'2012-01-20'

'IFL0'
[2.3056e+003]
[
2291]
[2.2976e+003]
[2.3768e+003]
[2.4626e+003]
[2.4456e+003]
[2.4456e+003]
[2.4102e+003]
[
2352]
[2.4874e+003]
[2.4274e+003]
[2.4702e+003]
[2.4982e+003]

'IFL1'
[2.3166e+003]
[
2299]
[2.3064e+003]
[2.3852e+003]
[2.4714e+003]
[2.4552e+003]
[2.4546e+003]
[2.4188e+003]
[2.3594e+003]
[2.5122e+003]
[2.4414e+003]
[2.4884e+003]
[2.5158e+003]
46

'IFL2'
[2.3318e+003]
[
2314]
[2.3216e+003]
[2.3958e+003]
[
2479]
[2.4612e+003]
[2.4638e+003]
[2.4262e+003]
[
2369]
[2.5218e+003]
[2.4568e+003]
[2.5032e+003]
[
2530]

'IFL3'
[
2352]
[
2334]
[2.3436e+003]
[
2412]
[2.4926e+003]
[
2481]
[
2483]
[2.4466e+003]
[2.3928e+003]
[2.5352e+003]
[
2477]
[2.5254e+003]
[2.5466e+003]


'2012-01-30'
[2.4692e+003]
'2012-01-31'
[
2476]
'2012-02-01'
[2.4398e+003]

>> PlotPrice(IFPrice)

[2.4842e+003]
[2.4902e+003]
[2.4518e+003]

[2.5072e+003]
[2.5108e+003]
[2.4714e+003]

5.
5.1 GetFieldList

FieldName=GetFieldList(conn1,TableName)

conn1

TableName
,

FieldName

1''
z= GetFieldList (conn1,'')
z=
''
''
''
''
''
''
''
...................
''
z= GetFieldList (conn1,'')
z=
''
''
''
''
''
''
------------------------------''
z= GetFieldList (conn1,'',1)
47

[2.5374e+003]
[2.5326e+003]
[2.4932e+003]


z=
'','','','','','','','B ','H ',' A ','
',.....

5.2 GetTableList

z=GetTableList(conn1,TableType)

conn1

TableType
TABLEVIEW
TABLE

TableList

--------------------------------------------------------------------------%

>>z=GetTableList(conn1)
''
''

>>z=GetTableList(conn1,'TABLE')
''
''%

z=GetTableList(conn1,'VIEW')
z=
'Task_01_00_ SHZS'
'task_01_00_'
'task_01_00_ K '
'task_02_21_'
'task_02_3_'
'Task_03_1_ 2 '

5.3 IsfieldEqual

Ans=IsfieldEqual(conn1,TableA,TableB)

conn1

TableA
A
TableB
B

Ans
0 11 0
48


' 201109'' 201109'

>> z3=isfieldequal(conn1,' 201109',' 201109')


z3 =
1

' 201109'' 201109'

z3=isfieldequal(conn2,' 201109',' 201109')


??? Error using ==> isfieldequal at 15
201109

5.4 SQL RunSQL


SQL

Ans=RunSQL(conn1,SqlQuer)

conn1

SqlQuer
SQL

Ans
select
------------------------------------------------------------------------->0.55
1.
>>conn1=database('SmallFinData','','') ;
>>FindTableByField(conn1,'')

1.comput
2. 201103
3. 201106
4. 201109
5. 201106
6. 201109
7.
8.
9.
10.
11.
2.
>>GetTableField(conn1,'')
ans =
''
''
49


''
''
''
''
''
''
''
......
>0.55
>>SqlQuer='select from where >0.55 and
=''201109'''
>>Ans=RunSQL(conn1,SqlQuer)
Ans =
'000022'
'000024'
'000028'
'000030'
'000039'
'000042'
'000157'
......

6.
6.1 GetIFSpread
300 Stock Index Future Spread, 4
[Spread,InstSpread]=GetIFSpread(FromDate,ToDate)

FromDate
ToDate

Spread

InstSpread
1
FromDate='2012-01-01'
ToDate='2012-01-04'
IFspread=GetIFSpread('2012-01-01','2012-01-04')
IFspread =
1.0e+005 *
Columns 1 through 13
7.3487
0
-0.0001
-0.0002
-0.0003
-0.0005
0.0001
50


-0.0001
-0.0003 -0.0005
0.0002
0.0001
Columns 14 through 26
0
-0.0002
-0.0004
0.0003
0.0003
0.0005
0.0005
0.0004
0.0002
0

>>reshape(IFspread(:,2:end),5,5)
ans =
0
6.8500 17.8500 33.0500 53.2500
-6.8500
0 11.0000 26.2000 46.4000
-17.8500 -11.0000
0 15.2000 35.4000
-33.0500 -26.2000 -15.2000
0 20.2000
-53.2500 -46.4000 -35.4000 -20.2000
0

0.0002

-0.0002

6.2 GetIndexRepli
1.
weight=GetIndexRepli(HS300Price)

HS300Price
300

Weight

------------------------------------------------------------------- 300 HS300


>>HS300Price=GetHS300Price('','2006-05-18');
>>weight=GetIndexRepli(HS300Price)
weight =
''
''
'SH600177'
[
0.0084]
'SH600183'
[9.1623e-004]
'SH600188'
[
0.0033]
'SH600196'
[
0.0026]
'SH600220'
[6.8916e-004]
'SH600221'
[
0.0020]
'SH600256'
[
0.0054]
'SH600266'
[
0.0019]
'SH600269'
[
0.0022]
'SH600307'
[
0.0014]
'SH600309'
[
0.0024]
......
50ETF 180 ETF 100ETF 300
>>P1=GetPrice([{'SH000300'},{'SH510050'},{'SH510180'},{'SZ159901'}]','2006-05-18')
>>weight2=GetIndexRepli(P1)
51


weight2 =
''
'SH510050'
'SH510180'
'SZ159901'

''
[0.3382]
[0.2116]
[0.4502]

300 60
eval(toolpath(2))
weight=RunSQL(conn1,'select , from HS300 Wind where =
#2012-01-01#');
weight(:,1)=AppendSHSZ(weight(:,1));
weight=[[{''},{''}];weight];
weight=GetIndexRepli(weight,60);

7.
7.1 GetHedgeValue

1[cellHedgeValue,matHedgeValue]=GetHedgeValue(Long,Short,FromDate,ToDate)
2
[cellHedgeValue,matHedgeValue]=GetHedgeValue(LongWeight,ShortWeight,FromDate,ToDate)
3[cellHedgeValue,matHedgeValue]=GetHedgeValue(LongPrice,ShortPrice)

LongPrice
ShortPrice
LongWeight
ShortWeight
Long

Short

FromDate

ToDate

cellHedgeValue
matHedgeValue

'SH600000''SZ000001''SH000300''SH000001''SH600276'

LongWeight =[[{'SH600000'},{'SZ000001'}]',{0.1 0.9}',{0.2 0.8}']


ShortWeight =[[{'SH000300'},{'SH000001'},{'SH600276'}]',{0.1 0.1 0.8}',{0.2 0.4 0.4}']
FromDate='2012-01-01'
52


ToDate ='2012-01-31'
[PortHedgeValue,matHedgeValue]=GetHedgeValue(LongWeight,ShortWeight,FromDate,ToDate)
PortHedgeValue =
'2012-01-04'
[
0]
[-0.4000]
'2012-01-05'
[0.0592]
[-0.3356]
'2012-01-06'
[0.0689]
[-0.3276]
'2012-01-09'
[0.0671]
[-0.3414]
'2012-01-10'
[0.0525]
[-0.3663]
'2012-01-11'
[0.0540]
[-0.3637]
'2012-01-12'
[0.0645]
[-0.3521]
'2012-01-13'
[0.0831]
[-0.3281]
'2012-01-16'
[0.1086]
[-0.2959]
'2012-01-17'
[0.1015]
[-0.3180]
'2012-01-18'
[0.1410]
[-0.2744]
'2012-01-19'
[0.1244]
[-0.2952]
'2012-01-20'
[0.1421]
[-0.2862]
'2012-01-30'
[0.1528]
[-0.2708]
'2012-01-31'
[0.1535]
[-0.2709]
matHedgeValue =
1.0e+005 *
7.3487
0 -0.0000
7.3487
0.0000
-0.0000
7.3487
0.0000
-0.0000
7.3488
0.0000
-0.0000
7.3488
0.0000
-0.0000
7.3488
0.0000
-0.0000
7.3488
0.0000
-0.0000
7.3488
0.0000
-0.0000
7.3488
0.0000
-0.0000
7.3488
0.0000
-0.0000
7.3489
0.0000
-0.0000
7.3489
0.0000
-0.0000
7.3489
0.0000
-0.0000
7.3490
0.0000
-0.0000
7.3490
0.0000
-0.0000
1 2
>>portflio1={
''
''
'2011-01-04' 1
'2011-01-05' 0.95};
>>portflio2={
''
''
'2011-01-04' 1
'2011-01-05' 0.99};

53


>>[PortHedgeValue,matHedgeValue]=GetHedgeValue(portflio1,portflio2)
PortHedgeValue =
'2011-01-04'
[
0]
'2011-01-05'
[ -0.0400]
matHedgeValue =
1.0e+005 *
7.3451
0
7.3451 -0.0000

7.2 HedgeTest

1[cellHedgeValue,matHedgeValue]=HedgeTest(Long,Short,FromDate,ToDate)
2[cellHedgeValue,matHedgeValue]=HedgeTest(LongPrice,ShortPrice)

LongPrice
ShortPrice
Long

Short

FromDate

ToDate

cellHedge

matHedge

matL

matS

LongPrice

ShortPrice
1'SH600000''SZ000001''SH000300''SH000001''SH600276'

Long = {'SH600000','SZ000001'}'
Short = {'SH000300','SH000001','SH600276'}'
FromDate='2012-01-01'
ToDate ='2012-01-31'
[cellHedge,matHedge,matL,matS,LongPrice,ShortPrice]=HedgeTest(Long,Short,FromDate,ToDat
e)
PortHedgeValue =
'2012-01-04'
[
0]
[-0.4000]
'2012-01-05'
[0.0592]
[-0.3356]
'2012-01-06'
[0.0689]
[-0.3276]
'2012-01-09'
[0.0671]
[-0.3414]
54


'2012-01-10'
[0.0525]
[-0.3663]
'2012-01-11'
[0.0540]
[-0.3637]
'2012-01-12'
[0.0645]
[-0.3521]
'2012-01-13'
[0.0831]
[-0.3281]
'2012-01-16'
[0.1086]
[-0.2959]
'2012-01-17'
[0.1015]
[-0.3180]
'2012-01-18'
[0.1410]
[-0.2744]
'2012-01-19'
[0.1244]
[-0.2952]
'2012-01-20'
[0.1421]
[-0.2862]
'2012-01-30'
[0.1528]
[-0.2708]
'2012-01-31'
[0.1535]
[-0.2709]
matHedgeValue =
1.0e+005 *
7.3487
0 -0.0000
7.3487
0.0000
-0.0000
7.3487
0.0000
-0.0000
7.3488
0.0000
-0.0000
7.3488
0.0000
-0.0000
7.3488
0.0000
-0.0000
7.3488
0.0000
-0.0000
7.3488
0.0000
-0.0000
7.3488
0.0000
-0.0000
7.3488
0.0000
-0.0000
7.3489
0.0000
-0.0000
7.3489
0.0000
-0.0000
7.3489
0.0000
-0.0000
7.3490
0.0000
-0.0000
7.3490
0.0000
-0.0000
2 1 2
LongPrice ={
''
'SH600000'
'SZ000001'
'2012-01-04'
[ 8.4100]
[ 15.1600]
'2012-01-05'
[ 8.6500]
[ 15.3900]
'2012-01-06'
[ 8.7100]
[ 15.3500]
'2012-01-09'
[ 8.9500]
[ 15.7800]
'2012-01-10'
[ 9.0700]
[
NaN]
'2012-01-11'
[
9]
[
NaN]
'2012-01-12'
[ 9.0900]
[
NaN]
'2012-01-13'
[ 9.0400]
[
NaN]
'2012-01-16'
[ 8.9700]
[
NaN]
'2012-01-17'
[ 9.2000]
[
NaN]
'2012-01-18'
[ 9.0400]
[
NaN]
'2012-01-19'
[ 9.2000]
[
NaN]
'2012-01-20'
[ 9.4200]
[ 16.7800]

55


'2012-01-30'
[ 9.2200]
[ 16.7100]
'2012-01-31'
[ 9.2200]
[ 16.6400]};
ShortPrice = {
''
'SH000300'
'SH000001'
'SH600276'
'2012-01-04'
[2.2988e+03]
[2.1694e+03]
[ 28.4500]
'2012-01-05'
[2.2764e+03]
[2.1484e+03]
[
27]
'2012-01-06'
[2.2906e+03]
[2.1634e+03]
[ 26.5500]
'2012-01-09'
[2.3686e+03]
[2.2259e+03]
[ 27.4000]
'2012-01-10'
[2.4473e+03]
[2.2857e+03]
[ 27.7500]
'2012-01-11'
[2.4356e+03]
[2.2761e+03]
[ 27.7000]
'2012-01-12'
[2.4352e+03]
[2.2750e+03]
[ 27.3700]
'2012-01-13'
[2.3943e+03]
[2.2446e+03]
[ 26.8000]
'2012-01-16'
[2.3457e+03]
[2.2062e+03]
[
26]
'2012-01-17'
[2.4606e+03]
[2.2984e+03]
[ 26.0200]
'2012-01-18'
[2.4222e+03]
[2.2664e+03]
[ 24.6600]
'2012-01-19'
[2.4683e+03]
[2.2961e+03]
[ 25.2000]
'2012-01-20'
[2.5041e+03]
[2.3191e+03]
[ 26.6800]
'2012-01-30'
[2.4607e+03]
[2.2850e+03]
[ 26.1900]
'2012-01-31'
[2.4643e+03]
[2.2926e+03]
[
26]};
>>[cellHedgeValue,matHedgeValue]=HedgeTest(LongPrice,ShortPrice)
3
>>Long = {'SH600000','SZ000001'}'
>>Short = {'SH000300','SH000001','SH600276'}'
>>FromDate='2012-01-01'
>>ToDate ='2012-01-31'
>> [~,LongPrice] =GetPrice(Long, FromDate,ToDate)
>> [~,ShortPrice] =GetPrice(Short,FromDate,ToDate)
>> [cellHedgeValue,matHedgeValue]=HedgeTest(LongPrice,ShortPrice)

7.3 HegdeTable
[retAnan,numLStats]=HegdeTable(HedgeResult)

HedgeResult

retAnan

numLStats

1
load HedgeResult
[retAnan,numLStats]=HegdeTable(HedgeResult)

retAnan(1:5,1:5)
ans =
56


''
'2010-08-20'
'2010-08-21'
'2010-08-22'
'2010-08-23'

''
[
0]
[ 6.6613e-16]
[
0]
[-1.1102e-16]

''
'5 '
[ -0.1280]
[
0]
[ -0.1090]
[ 6.6613e-16]
[
[ -0.1023]
[
0]
[ -0.0784]
[-1.1102e-16]
[

'5 '
[
-0.0379]
-0.0427]
[
-0.0401]
-0.0319]

7.4 HegdeTableAnalysis

[loss,turnRate]=HegdeTableAnalysis(retAnan,numLStats)

retAnan

numLStats

loss

turnRate

load HedgeResult
[retAnan,numLStats]=HegdeTable(HedgeResult)
[loss,turnRate]=HegdeTableAnalysis(retAnan,numLStats)

loss =
Columns 1 through 8
' 0.010'
' 0.015'
' 0.020'
' 0.025'
[ 0.4356]
[ 0.3190]
[ 0.2270]
[ 0.1641]
turnRate =
''
''
''
[
0.9336]
[
0.0649]
[
0.0685]
''
''
''
[
0.9460] [
0.0525]
[
0.0540]

7.5 GetHedgeSim

PortHedgeValue=GetHedgeValue(Long,Short,FromDate,ToDate)

Long

Short

FromDate

ToDate

57


Long =[{'SH600000'},{'SZ000001'}]
Short =[{'SH000300'},{'SH000001'},{'SH600276'}]
FromDate='2012-01-01'
ToDate ='2012-01-31'
PortHedgeValue=GetHedgeValue(Long,Short,FromDate,ToDate)
PortHedgeValue=
'2012-01-04' 0 0 0
'2012-01-05' 0.0460706053872939 0.0493924182195944 0.0453623358004550
'2012-01-06' 0.0491946013309152 0.0544231868614515 0.0481958152179411
'2012-01-09' 0.0476908110635608 0.0522147821118459 0.0459553659072136
'2012-01-10' 0.0304397640078999 0.0353808909219617 0.0274510509216190
'2012-01-11' 0.0308889143585329 0.0356902927057483 0.0280419744726554
'2012-01-12' 0.0396295684545560 0.0452851228807087 0.0365432751715287
'2012-01-13' 0.0543868050918153 0.0609792004639360 0.0517514826092087
'2012-01-16' 0.0736576640557938 0.0815069965760262 0.0713550183244915
'2012-01-17' 0.0566516044288470 0.0669434528286512 0.0525120087495157
'2012-01-18' 0.0778348837700202 0.0910140256209711 0.0734091397013132
'2012-01-19' 0.0681048677087108 0.0800856984185028 0.0628266840661116
'2012-01-20' 0.0918682445791106 0.0999341000916090 0.0865836107010594
'2012-01-30' 0.0970119951626205 0.105581259926553
0.0921674079736046
'2012-01-31' 0.0948826885611243 0.104503154964631
0.0901490612065670

8
8.1 GetFundAppraisal

1
[cellAppraisal,matAppraisal]=GetFundAppraisal(StockList,FromDate,ToDate,ModelType)
2
[cellAppraisal,matAppraisal]=GetFundAppraisal(Weight,FromDate,ToDate,ModelType)
3[cellAppraisal,matAppraisal]=GetFundAppraisal(Price,ModelType)

StockList

FromDate

ToDate

Price

Weight

ModelType
'TM' TM
HM HM HM

58


cellAppraisal
matAppraisal
TM
Rp - Rf =a + b(Rm -Rf) + c(Rm - Rf)^2 + ep
Rp a>0 c>0 .
HM
Rp - Rf =a + b(Rm -Rf) + c(Rm - Rf)D + ep
Rm - Rf>0,D=1, D=0
Rp a>0 c>0
300
300
FundCode ={
'SZ160105'
'SZ160106'
'SZ160119'
'SZ160123'
'SZ160125'
}
FromDate='2010-01-01'
ToDate='2012-04-01'
[cellAppraisal,matAppraisal] =GetFundAppraisal(FundCode,FromDate,ToDate)
cellAppraisal =
''
' a'
' c'
'SZ160105'
[5.0142e-05]
[-1.7054]
'SZ160106'
[6.9805e-05]
[-1.0855]
'SZ160119'
[1.9685e-05]
[-0.2014]
'SZ160123'
[
NaN]
[
NaN]
'SZ160125'
[
NaN]
[
NaN]
matAppraisal =
0.0001 -1.7054
0.0001 -1.0855
0.0000 -0.2014
NaN
NaN
NaN
NaN

FromDate='2010-01-01'
ToDate='2012-04-21'
Weight ={
'SH600000'
[0.3000] [0.6000]
'SH000300'
[0.7000] [0.4000]}
[cellAppraisal,matAppraisal] =GetFundAppraisal(Weight,FromDate,ToDate)
15% 10
15% 10

59


cellAppraisal =
''
'a'
'c'
' 1'
[-2.3873e-04]
[0.4330]
' 2'
[-4.8906e-04]
[0.2483]
matAppraisal =
-0.0002
0.4330
-0.0005
0.2483

FundCode ={
'SZ160105'
'SZ160106'
'SZ160119'
'SZ160123'
'SZ160125'
'SH000001'
'rf'
}
FromDate='2012-01-01'
ToDate='2012-03-01'
Price=GetPrice(FundCode,FromDate,ToDate)
[cellAppraisal,matAppraisal]= GetFundAppraisal(Price)
cellAppraisal =
''
'a'
'c'
'SZ160105'
[
0.0018]
[-3.9607]
'SZ160106'
[
-0.0019]
[ 8.2259]
'SZ160119'
[-7.6560e-04]
[-0.0208]
'SZ160123'
[-1.0904e-04]
[-0.3522]
'SZ160125'
[
0.0030]
[ 3.0276]
matAppraisal =
0.0018 -3.9607
-0.0019
8.2259
-0.0008 -0.0208
-0.0001 -0.3522
0.0030
3.0276

AppendName(cellAppraisal)
ans =
''
''
'a'
'c'
'SZ160105'
''
[
0.0018]
[-3.9607]
'SZ160106'
''
[
-0.0019]
[ 8.2259]
'SZ160119'
' 500'
[-7.6560e-04]
[-0.0208]
'SZ160123'
' 50 '
[-1.0904e-04]
[-0.3522]
'SZ160125'
''
[
0.0030]
[ 3.0276]
8.

60

9.GetStockList

StockList=GetStockList(N)

1, A ,1
2, A ,1
3, A ,1
4, 300300
5, ,4
6, ,287
7,
8,
9,
10

StockList
5
>> StockList=GetStockList(i);
>> StockList(1:5,1)
ans =
'SZ399979'
'SH000979'
'SZ399372'
'SZ399373'
'SZ399374'

10.
10.1 GetToday

1GetTodayN)

N 1-8 2
1. beta

61


2. Alpha
3. beta
4. Alpha
5. beta
6. Alpha
7. beta
8. Alpha

>>GetToday(1)

''
''
''
''
''
' Beta
'
''
' Beta '
''
[
0.7404]
''
[
-0.5769]
''
[
5.3672]
''
[
-3.7558]
''
[
0.4953]
''
[
-0.5351]
''
[
4.9583]
''
[
-2.2840]
''
[
0.4672]
''
[
-0.5347]
''
[
3.7435]
''
[
-2.1562]

>>GetToday(2)
.
''
' Alpha ' ' Alpha ' ''
' Alpha '
'
Alpha '
''
[
0.0206]
[
7.4488e-04]
''
[
-0.0334]
[
3.3906e-04]
''
[
0.0162]
[
-7.1118e-04]
''
[
-0.0291]
[
-3.1449e-04]
''
[
0.0149]
[
-1.0891e-04]
''
[
-0.0291]
[
-3.5932e-04]
''
[
0.0135]
[
1.7269e-04]
''
[
-0.0290]
[
0.0015]

10.2 GetOwn

1todayStata=GetOwn(StockList,FromDate,ToDate)
2todayStata=GetOwn(Weight,FromDate,ToDate)
2todayStata=GetOwn(Price)

1GetOwn(N)

62


N 1 2

>>GetOwn(1)
ans =
'04-24 06-03'
''
'
''
''
'SH600879'
''
[ 0.1037]
[ 0.0928]
[0.0211]
'SZ000001'
''
[ 0.0735]
[ 0.0198]
[0.0103]
'SH600153'
''
[ 0.0862]
[ 0.0616]
[0.0176]
'SH600815'
''
[ 0.1059]
[ 0.2859]
[0.0341]
'SH000300'
' 300'
[ 0.0532]
[ 0.0433]
[0.0105]
>>GetOwn(2)
ans =
'04-24 06-03'
'5 VaR'
'SH600879'
[7.1575]
'SZ000001'
[0.0155]
'SH600153'
[0.0099]
'SH600815'
[0.0469]
'SH000300'
[0.0171]

''

''

''

'

9.2600]

0.0626]

0.0458]

16.8600]

0.0641]

0.0102]

8.3500]

0.0455]

0.0446]

10.7500]

0.0716]

0.1938]

[2.7178e+03]

0.0312]

0.0233]

'Alpha '

''

'Alpha'

''

[ 2.4240e-04]

-3.3092e-04]

[1.2230]

''

-0.0021]

-2.4998e-04]

[1.0038]

[-6.8064e-04]

-2.7261e-04]

[1.0651]

0.0065]

-9.5204e-04]

[2.9054]

[ 7.3919e-04]

-3.2607e-04]

[1.2099]

''
''
' 300'

'Beta'

11.
11.1
1 2011 1.2 1.3
toolpath.m addpath stockdata.mat

63


>>SqlQuer='select from 201106 where >1.2 and
<1.30'
>> data21=RunSQL(conn1, SqlQuer)
data21 =
'000550'
'000596'
'002588'
'300215'
'600362'
'600971'
SH SZ
>>StockCode=AppendSHSZ(data21)

'SZ000550'
'SZ000596'
'SZ002588'
'SZ300215'
'SH600362'
'SH600971'

>>AppendName(StockCode)
'SZ000550'
' '
'SZ000596'
' '
'SZ002588'
''
'SZ300215'
''
'SH600362'
' '
'SH600971'
' '
2 2011 2.0 2.1
>> data31=RunSQL(conn1,'select from 201106 where
>2.00 and <2.1')
data31 =
'000040'
'000428'
'000540'
'000594'
'000626'
'000889'
'000899'
'002018'
'002035'
'002114'
'002140'
'002160'
'002173'

64


'002176'
'002188'
'002222'
'002260'
'300242'
'600010'
'600091'
'600173'
'600203'
'600212'
'600327'
'600452'
'600599'
'600689'
'600724'
'600777'
'601188'
3
A2011 0.8 1.3
B 2.0 4.0
>>conditionA='select from 201106 where >0.8 and
<1.30'
>>conditionB='select from 201106 where >2.00 and
<4'
>>data21=RunSQL(conn1, conditionA)
>>data31=RunSQL(conn1, conditionB)
A B
>>data41=AandB(data21, data31) ;
data41 =
'002038'
'600693'
'600805'
SHSZ
>> data51=AppendSHSZ(data41)
data51 =
'SZ002038'
'SH600693'
'SH600805'
AppendName
>> AppendName(data51)
ans =
'SZ002038'
' '
'SH600693'
' '
'SH600805'
' '

65

11.2
11.2.1 300 300
1 300 300
300
>>HS300Price=GetHS300Price('2012-01-18')

>>[weight,portflio]=GetIndexRepli(HS300Price)

>>portflio1=GetPortflioValue(HS300Price(:,[1,2]),1)

>> plot(portflio-portflio1)
>> grid on
>> title(' 300 ')

11.2.2 ETF 300


3 50ETF 180 ETF 100ETF 300
50ETF 180 ETF 100ETF
>>P1=GetPrice([{'SH000300 '},{'SH510050'},{'SH510180'},{'SZ159901'}]','2006-05-18')
66

>>[weight,portflio]=GetIndexRepli(P1)

300
>>portflio21=PortflioValue(P1(:,[1,2]),1)

p>>lot(portflio-portflio21)
>>grid on

% 300

11.2.3
300 60 300 60
60
300
>>weight=RunSQL(conn1,'select , from HS300 Wind where
= #2012-01-01#')
SHSZ
>> weight(:,1)=AppendSHSZ(weight(:,1));

>>weight=[[{''},{''}];weight]
weight =
''
''
'SH600832'
[0.1900]
67


'SH600839'
[0.1800]
'SH600879'
[0.1200]
'SH600881'
[0.2100]
'SH600895'
[0.1200]
'SH600900'
[0.7200]
'SH600997'
[0.1600]
..........

>>weight=IndexRepli(weight,60)

>>Pri=PortflioValue(weight,'2012-01-01','2012-02-01')
>>Pri=cell2mat(Pri( 2:end,2))
300
index300=GetPrice({'SH000300'},'2012-01-01','2012-02-01')
>> index=PortflioValue(index300,1)
index =
1.0000
0.9903
0.9965
1.0304
1.0646
1.0595
1.0594
1.0416
1.0204
1.0704
1.0537
1.0738
1.0893
1.0705
1.0720
1.0567

>>plot(index-Pri)

11.3

300 2010 7 20

'SH601988'
''
'I'
''
'SH600795'
''
'D'
''
68


'SH601989'
''
'C7'
''
'SH601179'
''
'C7'
''

'SH600546'
''
'H'
''

0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1

10

20

30

69

40

50

60

70

11.4

Value2GrowthHedge.m
toolpath
conn1=database('SmallFinData','','');
%%
%%
data21=RunSQL(conn1,'select from 201109 where
>0.8 and <1.30');
data31=RunSQL(conn1,'select from 201109 where
>2.00 and <4');
data41=intersect(data21, data31) ;
;
% A B
data51=AppendSHSZ(data41);
%%
data2=RunSQL(conn1,'select from 201109 where
>0.30 ');
data3=RunSQL(conn1,'select from 201109 where
<2 and >1');
data4=intersect(data2, data3) ;
;
% A B
data5=AppendSHSZ(data4);
%%
Price=GetPrice(data51,'2011-09-01','2011-12-01');
Price1=GetPrice(data5,'2011-09-01','2011-12-01');
portflio =PortflioValue(Price);
portflio1=PortflioValue(Price1);
70


% 4.
plot(portflio);
title(' A ');
figure
H1=plot([portflio-portflio1]);

12.
12.1

F
L
G
C99
A
C8
E
J
H
C2
C7
D
C5
C4
K
C1
M
C3
B
C6
I
C0

[ 78]
[ 3.1225]

[ 29]
[ 1.1609]

[181]
[ 7.2458]

[ 15]
[ 0.6005]

[ 41]
[ 1.6413]

[144]
[ 5.7646]

[ 57]
[ 2.2818]

[162]
[ 6.4852]

[161]
[ 6.4452]

[ 14]
[ 0.5604]

[507]
[20.2962]

[ 75]
[ 3.0024]

[146]
[ 5.8447]

[266]
[10.6485]

[ 75]
[ 3.0024]

[ 83]
[ 3.3227]

[ 20]
[ 0.8006]

[ 46]
[ 1.8415]

[ 60]
[ 2.4019]

[200]
[ 8.0064]

[ 42]
[ 1.6813]

[ 96]
[ 3.8431]

71

12.2
1 VS

-0.0500

-0.1000

-0.1500

-0.2000

-0.3000

-0.4000

-0.5000

-0.0513

-0.1054

-0.1625

-0.2231

-0.3567

-0.5108

-0.6931

0.0500

0.1000

0.1500

0.2000

0.300

0.4000

0.5000

0.0488

0.0953

0.1398

0.1823

0.2624

0.3365

0.4055

2 VS

-0.0500

-0.1000

-0.1500

-0.2000

-0.3000

-0.4000

-0.5000

-0.0488

-0.0952

-0.1393

-0.1813

-0.2592

-0.3297

-0.3935

0.0500

0.1000

0.1500

0.2000

0.300

0.4000

0.5000

0.0513

0.1052

0.1618

0.2214

0.3499

0.4918

0.6487

13.
Q :
Error using load
Can't read file D:\\\stockdata.mat.
A
Qtoolpath.p
Atoolpath
Q
HS300Repli warning: Trust-region-reflective algorithm does not solve this type of
problem, usingactive-set algorithm. You could also try the interior-point-convex
algorithm: set the
72


Algorithm option to interior-point-convex and rerun. For more help, see Choosing the
Algorithm
in the documentation.
> In quadprog at 371
In C:\Program Files\MATLAB\R2011b\toolbox\ a matlab \
\IndexOptimiRepli.p>IndexOptimiRepli at 87
In C:\Program Files\MATLAB\R2011b\toolbox\ a matlab \
\IndexRepli.p>IndexRepli at 77
In HS300Repli at 15
Ans Trust-region-reflective
Q:

data41=AandB(data21,data31)
data41 =
Empty cell array: 0-by-1
A data21 data31
Q
SqlQuer='select from 201106 where >1.2 and
<1.30'
SqlQuer =
select from 201106 where >1.2 and
<1.30
>> data21=RunSQL(conn1,SqlQuer)
data21 =
0
A
201106

14.
-MATLAB
2007.8
MATLAB
2008.3
73


MATLAB
2008.11

2009.11

2010.8
MATLAB6.5
2003.3

2008.8

74

You might also like