siat 3.1.23__py3-none-any.whl → 3.2.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- siat/capm_beta2.py +41 -19
- siat/markowitz2-20240620.py +2614 -0
- siat/markowitz2.py +52 -4
- siat/risk_adjusted_return2.py +45 -6
- siat/sector_china.py +17 -9
- siat/security_price2.py +12 -1
- siat/security_prices.py +38 -17
- siat/security_trend2-20240620.py +493 -0
- siat/security_trend2.py +17 -11
- siat/stock.py +76 -22
- siat/stock_technical-20240620.py +2736 -0
- siat/stock_technical.py +207 -52
- siat/valuation_china.py +7 -5
- {siat-3.1.23.dist-info → siat-3.2.1.dist-info}/METADATA +1 -1
- {siat-3.1.23.dist-info → siat-3.2.1.dist-info}/RECORD +17 -14
- {siat-3.1.23.dist-info → siat-3.2.1.dist-info}/WHEEL +0 -0
- {siat-3.1.23.dist-info → siat-3.2.1.dist-info}/top_level.txt +0 -0
siat/stock_technical.py
CHANGED
@@ -184,6 +184,8 @@ def calc_technical(df,start,end, \
|
|
184
184
|
WR_days=[10,6], \
|
185
185
|
ROC_day=12,ROC_madays=6, \
|
186
186
|
DMI_DIdays=14,DMI_ADXdays=6, \
|
187
|
+
|
188
|
+
indicator='Close', \
|
187
189
|
more_details=False):
|
188
190
|
"""
|
189
191
|
功能:计算股票的技术分析指标
|
@@ -192,7 +194,14 @@ def calc_technical(df,start,end, \
|
|
192
194
|
支持的指标:
|
193
195
|
RSI、OBV、MACD、 KDJ、 SAR、 VOL、 PSY、 ARBR、 CR、 EMV、
|
194
196
|
BOLL、 TRIX、 DMA、 BIAS、 CCI、 W%R、 ROC、 DMI
|
197
|
+
|
198
|
+
注意:indicator='Close'为不使用复权价,'Adj Close'为前复权价(需要指定source='yahoo')
|
195
199
|
"""
|
200
|
+
if indicator not in ['Close','Adj Close']:
|
201
|
+
print(" #Error(calc_technical): unsupported indicator",indicator)
|
202
|
+
print(" Supported indicator: Close, Adj Close")
|
203
|
+
|
204
|
+
return None
|
196
205
|
|
197
206
|
# 导入需要的包
|
198
207
|
try:
|
@@ -222,7 +231,7 @@ def calc_technical(df,start,end, \
|
|
222
231
|
if not isinstance(RSI_days,list):
|
223
232
|
RSI_days=[RSI_days]
|
224
233
|
for d in RSI_days:
|
225
|
-
df['rsi'+str(d)] = talib.RSI(df[
|
234
|
+
df['rsi'+str(d)] = talib.RSI(df[indicator], timeperiod=d)
|
226
235
|
#注意:rsi1没有意义
|
227
236
|
|
228
237
|
#=========== OBV:能量潮
|
@@ -254,7 +263,7 @@ def calc_technical(df,start,end, \
|
|
254
263
|
因此,对于那些达到涨跌停板的股票,OBV指标也无法正常发挥作用。
|
255
264
|
|
256
265
|
"""
|
257
|
-
df['obv'] = talib.OBV(df[
|
266
|
+
df['obv'] = talib.OBV(df[indicator],df['Volume'])
|
258
267
|
|
259
268
|
if not isinstance(OBV_days,list):
|
260
269
|
OBV_days=[OBV_days]
|
@@ -277,8 +286,8 @@ def calc_technical(df,start,end, \
|
|
277
286
|
MA_days=[MA_days]
|
278
287
|
|
279
288
|
for d in MA_days:
|
280
|
-
df['ma'+str(d)] = talib.MA(df[
|
281
|
-
df['ema'+str(d)] = talib.EMA(df[
|
289
|
+
df['ma'+str(d)] = talib.MA(df[indicator],timeperiod=d)
|
290
|
+
df['ema'+str(d)] = talib.EMA(df[indicator],timeperiod=d)
|
282
291
|
|
283
292
|
#=========== MACD:指数平滑异同平均线
|
284
293
|
"""
|
@@ -308,7 +317,7 @@ def calc_technical(df,start,end, \
|
|
308
317
|
3. MACD绿转红:MACD值由负变正,市场由空头转为多头。
|
309
318
|
4. MACD红转绿:MACD值由正变负,市场由多头转为空头。
|
310
319
|
"""
|
311
|
-
df['DIF'],df['DEA'],df['MACD']=talib.MACD(df[
|
320
|
+
df['DIF'],df['DEA'],df['MACD']=talib.MACD(df[indicator], \
|
312
321
|
fastperiod=MACD_fastperiod, \
|
313
322
|
slowperiod=MACD_slowperiod, \
|
314
323
|
signalperiod=MACD_signalperiod)
|
@@ -374,7 +383,7 @@ def calc_technical(df,start,end, \
|
|
374
383
|
PSY(N) = A/N × 100
|
375
384
|
说明:N为天数,A为在这N天之中股价上涨的天数
|
376
385
|
"""
|
377
|
-
df['ext_0'] = df[
|
386
|
+
df['ext_0'] = df[indicator]-df[indicator].shift(1)
|
378
387
|
df['ext_1'] = 0
|
379
388
|
df.loc[df['ext_0']>0,'ext_1'] = 1
|
380
389
|
|
@@ -499,7 +508,7 @@ def calc_technical(df,start,end, \
|
|
499
508
|
这条带状区的宽窄,随着股价波动幅度的大小而变化,股价涨跌幅度加大时,带状区变宽,
|
500
509
|
涨跌幅度狭小盘整时,带状区则变窄。
|
501
510
|
"""
|
502
|
-
df['upper'],df['mid'],df['lower'] = talib.BBANDS(df[
|
511
|
+
df['upper'],df['mid'],df['lower'] = talib.BBANDS(df[indicator], \
|
503
512
|
timeperiod=BULL_days, \
|
504
513
|
nbdevup=BULL_nbdevup,nbdevdn=BULL_nbdevdn,matype=BULL_matype)
|
505
514
|
|
@@ -507,7 +516,7 @@ def calc_technical(df,start,end, \
|
|
507
516
|
"""
|
508
517
|
|
509
518
|
"""
|
510
|
-
df['trix'] = talib.TRIX(df[
|
519
|
+
df['trix'] = talib.TRIX(df[indicator],timeperiod=TRIX_day)
|
511
520
|
|
512
521
|
if not isinstance(TRIX_madays,list):
|
513
522
|
TRIX_madays=[TRIX_madays]
|
@@ -530,8 +539,8 @@ def calc_technical(df,start,end, \
|
|
530
539
|
2)求DDD的10日移动平均数值
|
531
540
|
DMA(10) = DDD(10)÷10
|
532
541
|
"""
|
533
|
-
df['ma_shortperiod'] = talib.MA(df[
|
534
|
-
df['ma_longperiod'] = talib.MA(df[
|
542
|
+
df['ma_shortperiod'] = talib.MA(df[indicator],timeperiod=DMA_fastperiod)
|
543
|
+
df['ma_longperiod'] = talib.MA(df[indicator],timeperiod=DMA_slowperiod)
|
535
544
|
df['ddd'] = df['ma_shortperiod'] - df['ma_longperiod']
|
536
545
|
|
537
546
|
if not isinstance(DMA_madays,list):
|
@@ -554,8 +563,8 @@ def calc_technical(df,start,end, \
|
|
554
563
|
if not isinstance(BIAS_days,list):
|
555
564
|
BIAS_days=[BIAS_days]
|
556
565
|
for d in BIAS_days:
|
557
|
-
df['ma'] = talib.MA(df[
|
558
|
-
df['bias'+str(d)] = ((df[
|
566
|
+
df['ma'] = talib.MA(df[indicator],timeperiod=d)
|
567
|
+
df['bias'+str(d)] = ((df[indicator]-df['ma'])/df['ma'])*100
|
559
568
|
|
560
569
|
df.drop(columns = ['ma'],inplace=True)
|
561
570
|
|
@@ -603,7 +612,7 @@ def calc_technical(df,start,end, \
|
|
603
612
|
ROCMA = ROC的M日数值之和÷M
|
604
613
|
说明:M一般取值为6日
|
605
614
|
"""
|
606
|
-
df['roc'] = talib.ROC(df[
|
615
|
+
df['roc'] = talib.ROC(df[indicator],timeperiod=ROC_day)
|
607
616
|
|
608
617
|
if not isinstance(ROC_madays,list):
|
609
618
|
ROC_madays=[ROC_madays]
|
@@ -670,7 +679,7 @@ def security_MACD(ticker,start='default',end='default', \
|
|
670
679
|
MACD_fastperiod=12,MACD_slowperiod=26,MACD_signalperiod=9, \
|
671
680
|
resample_freq='6H',smooth=True,linewidth=1.5, \
|
672
681
|
loc1='lower left',loc2='lower right', \
|
673
|
-
graph=['ALL'],printout=True,ticker_type='auto'):
|
682
|
+
graph=['ALL'],printout=True,ticker_type='auto',source='auto'):
|
674
683
|
"""
|
675
684
|
套壳函数:可用于股票、交易所债券、交易所基金、部分期货期权(限美股)
|
676
685
|
"""
|
@@ -680,7 +689,7 @@ def security_MACD(ticker,start='default',end='default', \
|
|
680
689
|
MACD_signalperiod=MACD_signalperiod, \
|
681
690
|
resample_freq=resample_freq,smooth=smooth,linewidth=linewidth, \
|
682
691
|
loc1=loc1,loc2=loc2, \
|
683
|
-
graph=graph,printout=printout,ticker_type=ticker_type)
|
692
|
+
graph=graph,printout=printout,ticker_type=ticker_type,source=source)
|
684
693
|
return df
|
685
694
|
|
686
695
|
|
@@ -689,7 +698,7 @@ def stock_MACD(ticker,start='default',end='default', \
|
|
689
698
|
MACD_fastperiod=12,MACD_slowperiod=26,MACD_signalperiod=9, \
|
690
699
|
resample_freq='H',smooth=True,linewidth=1.5, \
|
691
700
|
loc1='lower left',loc2='lower right', \
|
692
|
-
graph=['ALL'],printout=True,ticker_type='auto'):
|
701
|
+
graph=['ALL'],printout=True,ticker_type='auto',source='auto'):
|
693
702
|
"""
|
694
703
|
功能:计算股票的技术分析指标MACD
|
695
704
|
输入:df,四种股价Open/Close/High/Low,成交量Volume
|
@@ -734,7 +743,7 @@ def stock_MACD(ticker,start='default',end='default', \
|
|
734
743
|
start1=date_adjust(start,adjust=-max_days * 3)
|
735
744
|
|
736
745
|
#df=get_price(ticker,start1,end)
|
737
|
-
df,found=get_price_1ticker_mixed(ticker=ticker,fromdate=start1,todate=end,ticker_type=ticker_type)
|
746
|
+
df,found=get_price_1ticker_mixed(ticker=ticker,fromdate=start1,todate=end,ticker_type=ticker_type,source=source)
|
738
747
|
if df is None:
|
739
748
|
print(" #Error(stock_MACD): no info found for",ticker,"from",start,"to",end)
|
740
749
|
return None
|
@@ -1041,7 +1050,7 @@ def security_RSI(ticker,start='default',end='default', \
|
|
1041
1050
|
RSI_days=[6,12,24],RSI_lines=[20,50,80], \
|
1042
1051
|
resample_freq='6H',smooth=True,linewidth=1.5, \
|
1043
1052
|
loc1='lower left',loc2='lower right', \
|
1044
|
-
graph=['ALL'],printout=True,ticker_type='auto'):
|
1053
|
+
graph=['ALL'],printout=True,ticker_type='auto',source='auto'):
|
1045
1054
|
"""
|
1046
1055
|
套壳函数,除了股票,还可用于交易所债券和交易所基金(如ETF和REITS)
|
1047
1056
|
"""
|
@@ -1049,7 +1058,7 @@ def security_RSI(ticker,start='default',end='default', \
|
|
1049
1058
|
RSI_days=RSI_days,RSI_lines=RSI_lines, \
|
1050
1059
|
resample_freq=resample_freq,smooth=smooth,linewidth=linewidth, \
|
1051
1060
|
loc1=loc1,loc2=loc2, \
|
1052
|
-
graph=graph,printout=printout,ticker_type=ticker_type)
|
1061
|
+
graph=graph,printout=printout,ticker_type=ticker_type,source=source)
|
1053
1062
|
return df
|
1054
1063
|
|
1055
1064
|
|
@@ -1057,7 +1066,7 @@ def stock_RSI(ticker,start='default',end='default', \
|
|
1057
1066
|
RSI_days=[6,12,24],RSI_lines=[20,50,80], \
|
1058
1067
|
resample_freq='H',smooth=True,linewidth=1.5, \
|
1059
1068
|
loc1='lower left',loc2='lower right', \
|
1060
|
-
graph=['ALL'],printout=True,ticker_type='auto'):
|
1069
|
+
graph=['ALL'],printout=True,ticker_type='auto',source='auto'):
|
1061
1070
|
"""
|
1062
1071
|
功能:计算股票的技术分析指标RSI
|
1063
1072
|
输入:df,四种股价Open/Close/High/Low,成交量Volume
|
@@ -1102,7 +1111,7 @@ def stock_RSI(ticker,start='default',end='default', \
|
|
1102
1111
|
start1=date_adjust(start,adjust=-max_days * 3)
|
1103
1112
|
|
1104
1113
|
#df=get_price(ticker,start1,end)
|
1105
|
-
df,found=get_price_1ticker_mixed(ticker=ticker,fromdate=start1,todate=end,ticker_type=ticker_type)
|
1114
|
+
df,found=get_price_1ticker_mixed(ticker=ticker,fromdate=start1,todate=end,ticker_type=ticker_type,source=source)
|
1106
1115
|
if df is None:
|
1107
1116
|
print(" #Error(stock_RSI): no info found for",ticker,"from",start,"to",end)
|
1108
1117
|
return None
|
@@ -1282,7 +1291,7 @@ def security_KDJ(ticker,start='default',end='default', \
|
|
1282
1291
|
KDJ_days=[9,3,3],matypes=[0,0],KDJ_lines=[20,50,80], \
|
1283
1292
|
resample_freq='6H',smooth=True,linewidth=1.5, \
|
1284
1293
|
loc1='lower left',loc2='lower right', \
|
1285
|
-
graph=['ALL'],printout=True,ticker_type='auto'):
|
1294
|
+
graph=['ALL'],printout=True,ticker_type='auto',source='auto'):
|
1286
1295
|
"""
|
1287
1296
|
套壳函数
|
1288
1297
|
"""
|
@@ -1290,7 +1299,7 @@ def security_KDJ(ticker,start='default',end='default', \
|
|
1290
1299
|
KDJ_days=KDJ_days,matypes=matypes,KDJ_lines=KDJ_lines, \
|
1291
1300
|
resample_freq=resample_freq,smooth=smooth,linewidth=linewidth, \
|
1292
1301
|
loc1=loc1,loc2=loc2, \
|
1293
|
-
graph=graph,printout=printout,ticker_type=ticker_type)
|
1302
|
+
graph=graph,printout=printout,ticker_type=ticker_type,source=source)
|
1294
1303
|
return df
|
1295
1304
|
|
1296
1305
|
|
@@ -1298,7 +1307,7 @@ def stock_KDJ(ticker,start='default',end='default', \
|
|
1298
1307
|
KDJ_days=[9,3,3],matypes=[0,0],KDJ_lines=[20,50,80], \
|
1299
1308
|
resample_freq='H',smooth=True,linewidth=1.5, \
|
1300
1309
|
loc1='lower left',loc2='lower right', \
|
1301
|
-
graph=['ALL'],printout=True,ticker_type='auto'):
|
1310
|
+
graph=['ALL'],printout=True,ticker_type='auto',source='auto'):
|
1302
1311
|
"""
|
1303
1312
|
功能:计算股票的技术分析指标KDJ
|
1304
1313
|
输入:df,四种股价Open/Close/High/Low,成交量Volume
|
@@ -1343,7 +1352,7 @@ def stock_KDJ(ticker,start='default',end='default', \
|
|
1343
1352
|
start1=date_adjust(start,adjust=-max_days * 3)
|
1344
1353
|
|
1345
1354
|
#df=get_price(ticker,start1,end)
|
1346
|
-
df,found=get_price_1ticker_mixed(ticker=ticker,fromdate=start1,todate=end,ticker_type=ticker_type)
|
1355
|
+
df,found=get_price_1ticker_mixed(ticker=ticker,fromdate=start1,todate=end,ticker_type=ticker_type,source=source)
|
1347
1356
|
if df is None:
|
1348
1357
|
print(" #Error(stock_RSI): no info found for",ticker,"from",start,"to",end)
|
1349
1358
|
return None
|
@@ -1950,20 +1959,20 @@ if __name__ =="__main__":
|
|
1950
1959
|
def security_Bollinger(ticker,start='default',end='default',boll_days=20, \
|
1951
1960
|
graph=True,smooth=True,loc='best', \
|
1952
1961
|
date_range=False,date_freq=False,annotate=False, \
|
1953
|
-
|
1962
|
+
ticker_type='auto',source='auto'):
|
1954
1963
|
"""
|
1955
1964
|
套壳函数,为了与security_MACD/RSI/KDJ保持相似
|
1956
1965
|
"""
|
1957
1966
|
df=stock_Bollinger(ticker=ticker,start=start,end=end,boll_days=boll_days, \
|
1958
1967
|
graph=graph,smooth=smooth,loc=loc, \
|
1959
1968
|
date_range=date_range,date_freq=date_freq, \
|
1960
|
-
annotate=annotate,ticker_type=ticker_type)
|
1969
|
+
annotate=annotate,ticker_type=ticker_type,source=source)
|
1961
1970
|
return df
|
1962
1971
|
|
1963
1972
|
def stock_Bollinger(ticker,start='default',end='default',boll_days=20, \
|
1964
1973
|
graph=True,smooth=True,loc='best', \
|
1965
1974
|
date_range=False,date_freq=False,annotate=False, \
|
1966
|
-
mark_end=True,ticker_type='auto'):
|
1975
|
+
mark_end=True,ticker_type='auto',source='auto'):
|
1967
1976
|
"""
|
1968
1977
|
套壳函数,为了与stock_MACD/RSI/KDJ保持相似
|
1969
1978
|
"""
|
@@ -1991,14 +2000,14 @@ def stock_Bollinger(ticker,start='default',end='default',boll_days=20, \
|
|
1991
2000
|
df=security_bollinger(ticker=ticker,fromdate=start,todate=end,boll_days=boll_days, \
|
1992
2001
|
graph=graph,smooth=smooth,loc=loc, \
|
1993
2002
|
date_range=date_range,date_freq=date_freq,annotate=annotate, \
|
1994
|
-
mark_end=mark_end,ticker_type=ticker_type)
|
2003
|
+
mark_end=mark_end,ticker_type=ticker_type,source=source)
|
1995
2004
|
return df
|
1996
2005
|
|
1997
2006
|
|
1998
2007
|
def security_bollinger(ticker,fromdate,todate,boll_days=20, \
|
1999
2008
|
graph=True,smooth=True,loc='best', \
|
2000
2009
|
date_range=False,date_freq=False,annotate=False, \
|
2001
|
-
mark_end=True,ticker_type='auto'):
|
2010
|
+
mark_end=True,ticker_type='auto',source='auto'):
|
2002
2011
|
"""
|
2003
2012
|
功能:单个证券,绘制布林带
|
2004
2013
|
date_range=False:指定开始结束日期绘图
|
@@ -2017,7 +2026,7 @@ def security_bollinger(ticker,fromdate,todate,boll_days=20, \
|
|
2017
2026
|
try:
|
2018
2027
|
#pricedf=get_price(ticker,fromdate1,todate)
|
2019
2028
|
pricedf,found=get_price_1ticker_mixed(ticker=ticker,fromdate=fromdate1, \
|
2020
|
-
todate=todate,ticker_type=ticker_type)
|
2029
|
+
todate=todate,ticker_type=ticker_type,source=source)
|
2021
2030
|
except:
|
2022
2031
|
print(" #Error(security_bollinger): price info not found for",ticker)
|
2023
2032
|
return None
|
@@ -2266,10 +2275,18 @@ def security_technical(ticker,start='default',end='default', \
|
|
2266
2275
|
graph=['ALL'],printout=False, \
|
2267
2276
|
date_range=False,date_freq=False,annotate=False, \
|
2268
2277
|
technical=['MACD'],indicator='Close', \
|
2269
|
-
ticker_type='auto'):
|
2278
|
+
ticker_type='auto',source='auto'):
|
2270
2279
|
|
2271
2280
|
"""
|
2272
|
-
|
2281
|
+
功能:技术分析中的MACD/RSI/KDJ/布林带,支持教学演示,支持参数调节。
|
2282
|
+
支持的产品:全球股票,债券(限中国内地的上市债券),基金(支持中国和美国的上市基金)。
|
2283
|
+
ticker:证券代码
|
2284
|
+
start/end:起止日期。支持简洁方式,仅需使用start指定近期的期间长度。
|
2285
|
+
简洁方式:mrw(近1周),l2w(近2周),l3w(近3周),mrm(近1个月),l2m(近2个月),
|
2286
|
+
mrq(近3个月),mrh(近6个月),mry(近1年),l2y(近2年),l3y(近3年),
|
2287
|
+
l5y(近5年),l8y(近8年),l10y(近10年),l20y(近20年),l30y(近30年),ytd(今年以来)
|
2288
|
+
|
2289
|
+
其他说明:套壳函数security_MACD/RSI/KDJ/Bollinger
|
2273
2290
|
"""
|
2274
2291
|
|
2275
2292
|
# 检查日期:如有错误自动更正
|
@@ -2317,27 +2334,27 @@ def security_technical(ticker,start='default',end='default', \
|
|
2317
2334
|
MACD_fastperiod=MACD_fastperiod,MACD_slowperiod=MACD_slowperiod,MACD_signalperiod=MACD_signalperiod, \
|
2318
2335
|
resample_freq=resample_freq,smooth=smooth,linewidth=linewidth, \
|
2319
2336
|
loc1=loc1,loc2=loc2, \
|
2320
|
-
graph=graph1,printout=printout,ticker_type=ticker_type)
|
2337
|
+
graph=graph1,printout=printout,ticker_type=ticker_type,source=source)
|
2321
2338
|
|
2322
2339
|
if 'RSI' in technical1:
|
2323
2340
|
df=security_RSI(ticker=ticker,start=fromdate,end=todate, \
|
2324
2341
|
RSI_days=RSI_days,RSI_lines=RSI_lines, \
|
2325
2342
|
resample_freq=resample_freq,smooth=smooth,linewidth=linewidth, \
|
2326
2343
|
loc1=loc1,loc2=loc2, \
|
2327
|
-
graph=graph1,printout=printout,ticker_type=ticker_type)
|
2344
|
+
graph=graph1,printout=printout,ticker_type=ticker_type,source=source)
|
2328
2345
|
|
2329
2346
|
if 'KDJ' in technical1:
|
2330
2347
|
df=security_KDJ(ticker=ticker,start=fromdate,end=todate, \
|
2331
2348
|
KDJ_days=KDJ_days,matypes=matypes,KDJ_lines=KDJ_lines, \
|
2332
2349
|
resample_freq=resample_freq,smooth=smooth,linewidth=linewidth, \
|
2333
2350
|
loc1=loc1,loc2=loc2, \
|
2334
|
-
graph=graph1,printout=printout,ticker_type=ticker_type)
|
2351
|
+
graph=graph1,printout=printout,ticker_type=ticker_type,source=source)
|
2335
2352
|
|
2336
2353
|
if 'Bollinger' in technical1 and 'Close' in indicator1:
|
2337
2354
|
df=security_Bollinger(ticker=ticker,start=fromdate,end=todate,boll_days=boll_days, \
|
2338
2355
|
graph=True,smooth=smooth,loc=loc1, \
|
2339
2356
|
date_range=date_range,date_freq=date_freq,annotate=annotate, \
|
2340
|
-
ticker_type=ticker_type)
|
2357
|
+
ticker_type=ticker_type,source=source)
|
2341
2358
|
|
2342
2359
|
"""
|
2343
2360
|
if 'Bollinger' in technical1 and 'MV' in indicator1:
|
@@ -2418,9 +2435,21 @@ if __name__ =="__main__":
|
|
2418
2435
|
|
2419
2436
|
start='2024-3-1'; end='2024-4-12'; ahead_days=30*3
|
2420
2437
|
|
2421
|
-
technical='EMV'; indicator='Close'
|
2438
|
+
technical='EMV'; indicator='Close'
|
2439
|
+
|
2440
|
+
attention_values=[0,25,50,75]
|
2441
|
+
ticker_type='auto'; source='auto'
|
2442
|
+
ahead_days=30*4
|
2443
|
+
resample_freq='6H'; smooth=True;linewidth=1.5
|
2444
|
+
date_range=False; date_freq=False; annotate=False
|
2445
|
+
graph=['ALL']; printout=False
|
2446
|
+
loc1='best'; loc2='best'
|
2447
|
+
|
2448
|
+
facecolor=['whitesmoke','papayawhip']
|
2449
|
+
price_line_style='dotted'; price_line_color='red'; price_line_width=5; price_line_marker='.'
|
2450
|
+
|
2422
2451
|
|
2423
|
-
df=
|
2452
|
+
df=security_technical2(ticker,start,end,technical=technical, \
|
2424
2453
|
loc1='lower left',loc2='lower right')
|
2425
2454
|
|
2426
2455
|
tlist=['RSI','OBV','MACD','KDJ','VOL','PSY','ARBR','CR','EMV','Bollinger', \
|
@@ -2429,6 +2458,11 @@ if __name__ =="__main__":
|
|
2429
2458
|
df=security_technical2(ticker,start,end,technical=t,loc1='lower left',loc2='lower right')
|
2430
2459
|
|
2431
2460
|
def security_technical2(ticker,start='default',end='default', \
|
2461
|
+
technical=['MACD'],indicator='Close', \
|
2462
|
+
ticker_type='auto',source='auto', \
|
2463
|
+
attention_values=[0,25,50,75,100], \
|
2464
|
+
|
2465
|
+
|
2432
2466
|
RSI_days=[6,24], \
|
2433
2467
|
OBV_days=5, \
|
2434
2468
|
|
@@ -2459,17 +2493,17 @@ def security_technical2(ticker,start='default',end='default', \
|
|
2459
2493
|
resample_freq='6H',smooth=True,linewidth=1.5, \
|
2460
2494
|
date_range=False,date_freq=False,annotate=False, \
|
2461
2495
|
|
2462
|
-
technical=['MACD'],indicator='Close', \
|
2463
2496
|
graph=['ALL'],printout=False, \
|
2464
2497
|
loc1='best',loc2='best', \
|
2465
|
-
ticker_type='auto', \
|
2466
2498
|
|
2467
|
-
|
2468
|
-
|
2499
|
+
facecolor=['whitesmoke','papayawhip'], \
|
2500
|
+
#price_line_style=(0,(1,1)), \
|
2501
|
+
price_line_style='dotted', \
|
2502
|
+
price_line_color=['red','green'], \
|
2503
|
+
price_line_width=5,price_line_marker='o', \
|
2469
2504
|
|
2470
2505
|
more_details=False):
|
2471
2506
|
"""
|
2472
|
-
|
2473
2507
|
功能:计算和绘制证券技术分析指标的简易图,仅供进一步探索使用,仅用于单个证券(股债基)
|
2474
2508
|
|
2475
2509
|
支持的探索指标:仅供探索使用
|
@@ -2515,12 +2549,13 @@ def security_technical2(ticker,start='default',end='default', \
|
|
2515
2549
|
#抓取抓取价格数据
|
2516
2550
|
fromdate1=date_adjust(fromdate,adjust=-ahead_days)
|
2517
2551
|
price,found=get_price_1ticker_mixed(ticker=ticker,fromdate=fromdate1, \
|
2518
|
-
todate=todate,ticker_type=ticker_type,fill=False)
|
2552
|
+
todate=todate,ticker_type=ticker_type,fill=False,source=source)
|
2519
2553
|
|
2520
2554
|
if found not in ['Found']:
|
2521
2555
|
print(" #Warning(security_technical2): no prices found for",ticker,'as type',ticker_type)
|
2522
2556
|
return None
|
2523
2557
|
|
2558
|
+
price['up_down']=price['Close']-price['Open']
|
2524
2559
|
|
2525
2560
|
#计算技术指标
|
2526
2561
|
df=calc_technical(price,fromdate,todate, \
|
@@ -2558,7 +2593,8 @@ def security_technical2(ticker,start='default',end='default', \
|
|
2558
2593
|
WR_days=WR_days, \
|
2559
2594
|
ROC_day=ROC_day,ROC_madays=ROC_madays, \
|
2560
2595
|
DMI_DIdays=DMI_DIdays,DMI_ADXdays=DMI_ADXdays, \
|
2561
|
-
|
2596
|
+
|
2597
|
+
indicator=indicator, \
|
2562
2598
|
more_details=more_details)
|
2563
2599
|
|
2564
2600
|
#技术指标的绘图线
|
@@ -2600,7 +2636,8 @@ def security_technical2(ticker,start='default',end='default', \
|
|
2600
2636
|
'W%R':[1,''],
|
2601
2637
|
'ROC':[1,''],
|
2602
2638
|
'DMI':[1,''],
|
2603
|
-
'DMA':[1,''],
|
2639
|
+
'DMA':[1,''],
|
2640
|
+
'Volume':[1/1000000,'百万']}
|
2604
2641
|
|
2605
2642
|
mag_times=magnitude_list[technical1][0]
|
2606
2643
|
mag_label=magnitude_list[technical1][1]
|
@@ -2632,18 +2669,55 @@ def security_technical2(ticker,start='default',end='default', \
|
|
2632
2669
|
for c in tech_line_collist:
|
2633
2670
|
df[c]=df[c] * mag_times
|
2634
2671
|
|
2672
|
+
df['Volume']=df['Volume'] * magnitude_list['Volume'][0]
|
2673
|
+
|
2635
2674
|
#字段排序
|
2636
2675
|
tech_line_collist.sort()
|
2637
|
-
df1=df[tech_line_collist+[indicator]]
|
2676
|
+
df1=df[tech_line_collist+[indicator,'Volume','up_down']]
|
2638
2677
|
|
2639
2678
|
#绘图----------------------------------------------------------------------
|
2640
2679
|
import matplotlib.pyplot as plt
|
2641
|
-
|
2680
|
+
import matplotlib.dates as mdates
|
2681
|
+
#import matplotlib.gridspec as gridspec
|
2682
|
+
"""
|
2642
2683
|
fig = plt.figure()
|
2643
2684
|
ax = fig.add_subplot(111)
|
2685
|
+
"""
|
2686
|
+
"""
|
2687
|
+
#图ax在上方,ax3在下方
|
2688
|
+
fig, (ax, ax3) = plt.subplots(2, sharex=True,figsize=(12,9))
|
2689
|
+
|
2644
2690
|
#plt.gca().set_facecolor('whitesmoke')
|
2645
2691
|
fig.gca().set_facecolor(facecolor) #放在这里生效,放尾部不生效
|
2692
|
+
"""
|
2693
|
+
# 创建两行的布局,上半部分高度为4,下半部分高度为1
|
2694
|
+
fig = plt.figure(figsize=(14,9))
|
2695
|
+
#fig.gca().set_facecolor(facecolor)
|
2696
|
+
|
2697
|
+
if isinstance(facecolor,str):
|
2698
|
+
facecolor1=facecolor2=facecolor
|
2699
|
+
elif isinstance(facecolor,list):
|
2700
|
+
if len(facecolor) >= 2:
|
2701
|
+
facecolor1=facecolor[0]
|
2702
|
+
facecolor2=facecolor[1]
|
2703
|
+
elif len(facecolor) == 1:
|
2704
|
+
facecolor1=facecolor2=facecolor[0]
|
2705
|
+
else:
|
2706
|
+
facecolor1='whitesmoke'; facecolor2='papayawhip'
|
2646
2707
|
|
2708
|
+
gs = fig.add_gridspec(2, 1, height_ratios=[4, 1], hspace=0.05)
|
2709
|
+
ax = fig.add_subplot(gs[0])
|
2710
|
+
try:
|
2711
|
+
ax.set_facecolor(facecolor1)
|
2712
|
+
except:
|
2713
|
+
ax.set_facecolor('whitesmoke')
|
2714
|
+
|
2715
|
+
ax3 = fig.add_subplot(gs[1], sharex=ax)
|
2716
|
+
try:
|
2717
|
+
ax3.set_facecolor(facecolor2)
|
2718
|
+
except:
|
2719
|
+
ax3.set_facecolor('papayawhip')
|
2720
|
+
|
2647
2721
|
"""
|
2648
2722
|
line0=False; line30=False; line50=False; line80=False
|
2649
2723
|
for l in tech_line_collist:
|
@@ -2672,7 +2746,13 @@ def security_technical2(ticker,start='default',end='default', \
|
|
2672
2746
|
attention_draws=[False] * len(attention_values)
|
2673
2747
|
|
2674
2748
|
for l in tech_line_collist:
|
2675
|
-
|
2749
|
+
labeltxt=l.upper()
|
2750
|
+
if labeltxt =='DEA':
|
2751
|
+
labeltxt='慢线(DEA)'
|
2752
|
+
if labeltxt =='DIF':
|
2753
|
+
labeltxt='快线(DIF)'
|
2754
|
+
|
2755
|
+
ax.plot(df1.index,df1[l],label=labeltxt)
|
2676
2756
|
|
2677
2757
|
#判断是否绘制关注线
|
2678
2758
|
lmax=df1[l].max(); lmin=df1[l].min()
|
@@ -2686,7 +2766,7 @@ def security_technical2(ticker,start='default',end='default', \
|
|
2686
2766
|
|
2687
2767
|
#如果需要绘制关注线,且尚未绘制过,则绘制
|
2688
2768
|
if line_al and not attention_draws[pos]:
|
2689
|
-
|
2769
|
+
ax.axhline(y=attention_values[pos],ls=":",c=color_list[pos],linewidth=2)
|
2690
2770
|
|
2691
2771
|
attention_draws[pos]=True
|
2692
2772
|
|
@@ -2695,17 +2775,92 @@ def security_technical2(ticker,start='default',end='default', \
|
|
2695
2775
|
ylabeltxt1=ylabeltxt1+'('+mag_label+')'
|
2696
2776
|
ax.set_ylabel(ylabeltxt1,fontsize=ylabel_txt_size)
|
2697
2777
|
ax.legend(loc=loc1,fontsize=legend_txt_size)
|
2778
|
+
|
2779
|
+
interval=int(len(df1)/10)+1
|
2780
|
+
ax.xaxis.set_major_locator(mdates.DayLocator(interval=interval)) # 隔interval天一个标记
|
2781
|
+
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
|
2782
|
+
#ax.autoscale_view()
|
2783
|
+
|
2784
|
+
#区分涨跌颜色:红涨绿跌
|
2785
|
+
df1up=df1[df1['up_down'] >= 0]
|
2786
|
+
df1down=df1[df1['up_down'] < 0]
|
2787
|
+
|
2788
|
+
#绘制收盘价
|
2789
|
+
if isinstance(price_line_color,str):
|
2790
|
+
price_line_color1=price_line_color2=price_line_color
|
2791
|
+
elif isinstance(price_line_color,list):
|
2792
|
+
if len(price_line_color) >= 2:
|
2793
|
+
price_line_color1=price_line_color[0]
|
2794
|
+
price_line_color2=price_line_color[1]
|
2795
|
+
elif len(price_line_color) == 1:
|
2796
|
+
price_line_color1=price_line_color2=price_line_color[0]
|
2797
|
+
else:
|
2798
|
+
price_line_color1='red'; price_line_color2='green'
|
2698
2799
|
|
2800
|
+
import numpy as np
|
2801
|
+
df1['segment'] = (np.sign(df1['up_down'].shift(1)) != np.sign(df1['up_down'])).cumsum()
|
2802
|
+
seg_list=list(set(list(df1['segment'])))
|
2803
|
+
|
2699
2804
|
ax2 = ax.twinx()
|
2700
2805
|
ylabeltxt2='收盘价'
|
2701
2806
|
ax2.set_ylabel(ylabeltxt2,fontsize=ylabel_txt_size)
|
2702
|
-
|
2807
|
+
|
2808
|
+
#细灰线先画出轮廓
|
2809
|
+
ax2.plot(df1.index,df1[indicator],label='', \
|
2810
|
+
linestyle=price_line_style,color='grey',lw=1)
|
2811
|
+
|
2812
|
+
#不同颜色绘制涨跌价格线
|
2813
|
+
first_time=True; second_time=False
|
2814
|
+
for seg in seg_list:
|
2815
|
+
df1seg=df1[df1['segment']==seg]
|
2816
|
+
if df1seg['up_down'].values[0] >=0:
|
2817
|
+
seg_color=price_line_color1
|
2818
|
+
#labeltxt=ylabeltxt2+'(当日↑)'
|
2819
|
+
#labeltxt=ylabeltxt2+'(当日≥开盘价)'
|
2820
|
+
labeltxt=ylabeltxt2+'(当日阳线)'
|
2821
|
+
else:
|
2822
|
+
seg_color=price_line_color2
|
2823
|
+
#labeltxt=ylabeltxt2+'(当日↓)'
|
2824
|
+
#labeltxt=ylabeltxt2+'(当日<开盘价)'
|
2825
|
+
labeltxt=ylabeltxt2+'(当日阴线)'
|
2826
|
+
|
2827
|
+
if first_time:
|
2828
|
+
first_time=False; second_time=True
|
2829
|
+
elif second_time:
|
2830
|
+
second_time=False
|
2831
|
+
else:
|
2832
|
+
labeltxt=''
|
2833
|
+
"""
|
2834
|
+
ax2.plot(df1seg.index,df1seg[indicator],label=labeltxt, \
|
2835
|
+
linestyle=':',color=seg_color,lw=price_line_width,marker=price_line_marker)
|
2836
|
+
"""
|
2837
|
+
ax2.scatter(df1seg.index,df1seg[indicator],label=labeltxt, \
|
2838
|
+
linestyle=':',color=seg_color,lw=price_line_width,marker=price_line_marker)
|
2839
|
+
|
2840
|
+
|
2841
|
+
"""
|
2842
|
+
ax2.plot(df1.index,df1[indicator],label=ylabeltxt2, \
|
2843
|
+
linestyle=price_line_style,color=price_line_color1,lw=price_line_width,marker=price_line_marker)
|
2844
|
+
ax2.scatter(df1down.index,df1down[indicator],label=ylabeltxt2, \
|
2845
|
+
linestyle=price_line_style,color=price_line_color2,lw=price_line_width,marker=price_line_marker)
|
2846
|
+
"""
|
2847
|
+
|
2703
2848
|
ax2.legend(loc=loc2,fontsize=legend_txt_size)
|
2704
2849
|
|
2850
|
+
#绘制交易量柱状图
|
2851
|
+
ax3.bar(df1up.index,df1up['Volume'],color=price_line_color1)
|
2852
|
+
ax3.bar(df1down.index,df1down['Volume'],color=price_line_color2)
|
2853
|
+
|
2854
|
+
ax3.set_ylabel("交易量(百万)",fontsize=ylabel_txt_size -4)
|
2855
|
+
|
2856
|
+
#fig.text(0.5, 0.04, 'x', ha='center')
|
2857
|
+
plt.subplots_adjust(hspace=0.2)
|
2858
|
+
|
2705
2859
|
titletxt=ticker_name(ticker)+': '+tech_list[technical1]+technical1
|
2706
2860
|
plt.title(titletxt,fontweight='bold',fontsize=title_txt_size)
|
2707
2861
|
|
2708
2862
|
plt.gcf().autofmt_xdate()
|
2863
|
+
#fig.autofmt_xdate()
|
2709
2864
|
|
2710
2865
|
plt.show(); plt.close()
|
2711
2866
|
|
siat/valuation_china.py
CHANGED
@@ -921,7 +921,7 @@ def compare_industry_valuation_sw(industries,start,end,vtypes='PE', \
|
|
921
921
|
df=pd.DataFrame()
|
922
922
|
for i in industries:
|
923
923
|
# debug
|
924
|
-
print("
|
924
|
+
print(" Searching valuation info for",i,'\b, which may take time ...')
|
925
925
|
dft=industry_valuation_history_sw(i,start=start,end=end,vtype=vtype,graph=False)
|
926
926
|
if not (dft is None):
|
927
927
|
dft.rename(columns={vtypec:i},inplace=True)
|
@@ -1297,7 +1297,7 @@ if __name__=='__main__':
|
|
1297
1297
|
|
1298
1298
|
lo_est_betas_list,betas_list,idfall=valuation2return_sw(start,end,valuation=valuation,return_delay=return_delay)
|
1299
1299
|
|
1300
|
-
def valuation2return_sw(start,end,itype='
|
1300
|
+
def valuation2return_sw(start,end,itype='1',industries='all'):
|
1301
1301
|
"""
|
1302
1302
|
功能:测试三种估值指标对滞后一段时间收益率的影响。
|
1303
1303
|
测试行业哑元变量对估值指标的调节作用,借此判断。若正向(负向)影响,行业估值未低估(高估)
|
@@ -1366,20 +1366,22 @@ def valuation2return_sw(start,end,itype='I',industries='all'):
|
|
1366
1366
|
sys.stdout.close()
|
1367
1367
|
sys.stdout = self._original_stdout
|
1368
1368
|
|
1369
|
-
# 步骤1
|
1369
|
+
# 步骤1:获取行业历史数据,本步骤所需时间较长==================================
|
1370
|
+
print("Step1: retrieving industry information, it may take up to hours ...")
|
1370
1371
|
industry_data=get_industry_sw(itype=itype)
|
1371
1372
|
if not (industries.lower() == 'all'):
|
1372
1373
|
industry_codes=industry_sw_codes(industries)
|
1373
1374
|
else:
|
1374
1375
|
industry_codes=list(set(list(industry_data['ticker'])))
|
1375
1376
|
|
1376
|
-
# 步骤2
|
1377
|
+
# 步骤2:计算基础数据,本步骤所需时间较长======================================
|
1378
|
+
print("Step2: Calculating industry valuations, it may take great time ...")
|
1377
1379
|
idf,idfall=calc_industry_sw(industry_data,start,end)
|
1378
1380
|
|
1379
1381
|
# 步骤3:构造回归数据,进行回归,记录回归结果
|
1380
1382
|
import pandas as pd
|
1381
1383
|
coefdflist=[]
|
1382
|
-
print("
|
1384
|
+
print("Step3: Analyzing industry performance, it may need quite some time ...")
|
1383
1385
|
|
1384
1386
|
total=len(measure_list)*len(valuation_list)
|
1385
1387
|
for m in measure_list:
|