siat 3.1.23__py3-none-any.whl → 3.1.28__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/markowitz2-20240620.py +2614 -0
- siat/markowitz2.py +52 -4
- siat/sector_china.py +17 -9
- siat/security_price2.py +11 -0
- siat/stock_technical.py +58 -41
- siat/valuation_china.py +7 -5
- {siat-3.1.23.dist-info → siat-3.1.28.dist-info}/METADATA +1 -1
- {siat-3.1.23.dist-info → siat-3.1.28.dist-info}/RECORD +10 -9
- {siat-3.1.23.dist-info → siat-3.1.28.dist-info}/WHEEL +0 -0
- {siat-3.1.23.dist-info → siat-3.1.28.dist-info}/top_level.txt +0 -0
siat/markowitz2.py
CHANGED
@@ -252,6 +252,23 @@ if __name__=='__main__':
|
|
252
252
|
printout=True
|
253
253
|
graph=False
|
254
254
|
|
255
|
+
#测试3
|
256
|
+
Market={'Market':('China','000300.SS','锂电池1号')}
|
257
|
+
Stocks={'300750.SZ':0.4,#宁德时代
|
258
|
+
'002594.SZ':0.3,#比亚迪
|
259
|
+
'300014.SZ':0.2,#亿纬锂能
|
260
|
+
'300207.SZ':0.1,#欣旺达
|
261
|
+
}
|
262
|
+
portfolio=dict(Market,**Stocks)
|
263
|
+
|
264
|
+
indicator='Adj Close'
|
265
|
+
adjust='qfq'; source='auto'; ticker_type='bond'
|
266
|
+
thedate='2024-6-19'
|
267
|
+
pastyears=2
|
268
|
+
printout=True
|
269
|
+
graph=False
|
270
|
+
|
271
|
+
|
255
272
|
pf_info=portfolio_build(portfolio,thedate,pastyears,printout,graph)
|
256
273
|
|
257
274
|
"""
|
@@ -260,12 +277,30 @@ def portfolio_cumret(portfolio,thedate,pastyears=1, \
|
|
260
277
|
printout=True,graph=True):
|
261
278
|
"""
|
262
279
|
def portfolio_build(portfolio,thedate='default',pastyears=1, \
|
280
|
+
indicator='Adj Close', \
|
281
|
+
adjust='qfq',source='auto',ticker_type='auto', \
|
263
282
|
printout=True,graph=False):
|
264
283
|
"""
|
265
284
|
功能:收集投资组合成份股数据,绘制收益率趋势图,并与等权和期间内交易额加权策略组合比较
|
266
285
|
注意:此处无需RF,待到优化策略时再指定
|
267
286
|
printout=True控制获取股价时是否逐个显示
|
287
|
+
|
288
|
+
特别注意:若ticker_type='fund'可能导致无法处理股票的复权价!
|
289
|
+
若要指定特定的证券为债券,则需要使用列表逐一指定证券的类型(股票,债券,基金)
|
268
290
|
"""
|
291
|
+
#判断复权标志
|
292
|
+
indicator_list=['Close','Adj Close']
|
293
|
+
if indicator not in indicator_list:
|
294
|
+
print(" Warning(portfolio_build): invalid indicator",indicator)
|
295
|
+
print(" Supported indicator:",indicator_list)
|
296
|
+
indicator='Adj Close'
|
297
|
+
|
298
|
+
adjust_list=['','qfq','hfq']
|
299
|
+
if adjust not in adjust_list:
|
300
|
+
print(" Warning(portfolio_build): invalid indicator",adjust)
|
301
|
+
print(" Supported adjust:",adjust_list)
|
302
|
+
adjust='qfq'
|
303
|
+
|
269
304
|
import datetime
|
270
305
|
stoday = datetime.date.today()
|
271
306
|
if thedate=='default':
|
@@ -329,18 +364,23 @@ def portfolio_build(portfolio,thedate='default',pastyears=1, \
|
|
329
364
|
|
330
365
|
# 抓取投资组合股价
|
331
366
|
#prices=get_prices(tickerlist,start,thedate)
|
332
|
-
|
367
|
+
#判断是否赚取复权价
|
368
|
+
if indicator == 'Adj Close' and adjust == '':
|
369
|
+
adjust='qfq'
|
370
|
+
if indicator == 'Close' and adjust != '':
|
371
|
+
indicator = 'Adj Close'
|
372
|
+
|
333
373
|
if printout:
|
334
374
|
#prices=get_prices_simple(tickerlist,start,thedate) #有待改造?
|
335
375
|
#债券优先
|
336
376
|
prices,found=get_price_mticker(tickerlist,start,thedate, \
|
337
|
-
adjust=
|
377
|
+
adjust=adjust,source=source,ticker_type=ticker_type,fill=False)
|
338
378
|
else:
|
339
379
|
with HiddenPrints():
|
340
380
|
#prices=get_prices_simple(tickerlist,start,thedate) #有待改造?
|
341
381
|
prices,found=get_price_mticker(tickerlist,start,thedate, \
|
342
|
-
adjust=
|
343
|
-
|
382
|
+
adjust=adjust,source=source,ticker_type=ticker_type,fill=False)
|
383
|
+
|
344
384
|
if found == 'Found':
|
345
385
|
ntickers=len(list(prices['Close']))
|
346
386
|
nrecords=len(prices)
|
@@ -360,6 +400,14 @@ def portfolio_build(portfolio,thedate='default',pastyears=1, \
|
|
360
400
|
print(" #Error(portfolio_build): retrieved empty prices for",pname)
|
361
401
|
return None
|
362
402
|
#..........................................................................
|
403
|
+
#判断是否使用复权价:若是,使用Adj Close直接覆盖Close。方法最简单,且兼容后续处理!
|
404
|
+
if (indicator =='Adj Close') or (adjust !=''):
|
405
|
+
prices_collist=list(prices)
|
406
|
+
for pc in prices_collist:
|
407
|
+
pc1=pc[0]; pc2=pc[1]
|
408
|
+
if pc1=='Close':
|
409
|
+
pc_adj=('Adj Close',pc2)
|
410
|
+
prices[pc]=prices[pc_adj]
|
363
411
|
|
364
412
|
# 取各个成份股的收盘价
|
365
413
|
aclose=prices['Close']
|
siat/sector_china.py
CHANGED
@@ -1161,7 +1161,7 @@ if __name__=='__main__':
|
|
1161
1161
|
period="day"
|
1162
1162
|
industry_list='all'
|
1163
1163
|
|
1164
|
-
def get_industry_sw(itype='1',period="day",industry_list='all'):
|
1164
|
+
def get_industry_sw(itype='1',period="day",industry_list='all',max_sleep=8):
|
1165
1165
|
"""
|
1166
1166
|
功能:遍历某类申万指数,下载数据
|
1167
1167
|
itype: F表征指数,n=1/2/3行业指数,S风格指数,B大类风格,C金创类
|
@@ -1174,7 +1174,7 @@ def get_industry_sw(itype='1',period="day",industry_list='all'):
|
|
1174
1174
|
if not (itype in typelist):
|
1175
1175
|
print(" #Error(get_industry_sw): unsupported industry category",itype)
|
1176
1176
|
print(" Supported industry category",typelist)
|
1177
|
-
print(" F: Featured, n-Level n
|
1177
|
+
print(" F: Featured, n-Level n industry, S-Styled, B- Big Styled, C- Financial Innovation, A-All (more time))")
|
1178
1178
|
return None
|
1179
1179
|
|
1180
1180
|
#获得指数代码
|
@@ -1192,10 +1192,10 @@ def get_industry_sw(itype='1',period="day",industry_list='all'):
|
|
1192
1192
|
#循环获取指标
|
1193
1193
|
import pandas as pd
|
1194
1194
|
import akshare as ak
|
1195
|
-
import datetime
|
1195
|
+
import datetime; import random; import time
|
1196
1196
|
df=pd.DataFrame()
|
1197
1197
|
|
1198
|
-
print("
|
1198
|
+
print(" Start searching industry data, it takes time, please wait ...")
|
1199
1199
|
num=len(ilist)
|
1200
1200
|
if num <= 10:
|
1201
1201
|
steps=5
|
@@ -1205,7 +1205,7 @@ def get_industry_sw(itype='1',period="day",industry_list='all'):
|
|
1205
1205
|
total=len(ilist)
|
1206
1206
|
fail_list=[]
|
1207
1207
|
for i in ilist:
|
1208
|
-
|
1208
|
+
print_progress_percent2(i,ilist,steps=5,leading_blanks=4)
|
1209
1209
|
#print(" Retrieving information for industry",i)
|
1210
1210
|
|
1211
1211
|
#抓取指数价格
|
@@ -1235,7 +1235,11 @@ def get_industry_sw(itype='1',period="day",industry_list='all'):
|
|
1235
1235
|
df=df._append(dft2)
|
1236
1236
|
|
1237
1237
|
current=ilist.index(i)
|
1238
|
-
print_progress_percent(current,total,steps=steps,leading_blanks=2)
|
1238
|
+
#print_progress_percent(current,total,steps=steps,leading_blanks=2)
|
1239
|
+
|
1240
|
+
#生成随机数睡眠,试图防止被反爬虫,不知是否管用!
|
1241
|
+
random_int=random.randint(1,max_sleep)
|
1242
|
+
time.sleep(random_int)
|
1239
1243
|
|
1240
1244
|
#num=list(set(list(df['ticker'])))
|
1241
1245
|
if len(fail_list)==0:
|
@@ -1266,7 +1270,7 @@ if __name__=='__main__':
|
|
1266
1270
|
industry_list=['850831.SW','801785.SW','801737.SW','801194.SW',
|
1267
1271
|
'801784.SW','801783.SW','801782.SW']
|
1268
1272
|
|
1269
|
-
def get_industry_sw2(industry_list,period="day"):
|
1273
|
+
def get_industry_sw2(industry_list,period="day",max_sleep=8):
|
1270
1274
|
"""
|
1271
1275
|
功能:遍历指定的申万指数列表,下载数据
|
1272
1276
|
period="day"; choice of {"day", "week", "month"}
|
@@ -1280,10 +1284,10 @@ def get_industry_sw2(industry_list,period="day"):
|
|
1280
1284
|
#循环获取指标
|
1281
1285
|
import pandas as pd
|
1282
1286
|
import akshare as ak
|
1283
|
-
import datetime
|
1287
|
+
import datetime; import random; import time
|
1284
1288
|
df=pd.DataFrame()
|
1285
1289
|
|
1286
|
-
print("Searching industry information, please wait ...")
|
1290
|
+
print(" Searching industry information, it takes time, please wait ...")
|
1287
1291
|
ilist=industry_list
|
1288
1292
|
num=len(ilist)
|
1289
1293
|
if num <= 10:
|
@@ -1323,6 +1327,10 @@ def get_industry_sw2(industry_list,period="day"):
|
|
1323
1327
|
|
1324
1328
|
current=ilist.index(i)
|
1325
1329
|
print_progress_percent(current,total,steps=steps,leading_blanks=2)
|
1330
|
+
|
1331
|
+
#生成随机数睡眠,试图防止被反爬虫,不知是否管用!
|
1332
|
+
random_int=random.randint(1,max_sleep)
|
1333
|
+
time.sleep(random_int)
|
1326
1334
|
|
1327
1335
|
#num=list(set(list(df['ticker'])))
|
1328
1336
|
if len(fail_list) > 0:
|
siat/security_price2.py
CHANGED
@@ -231,6 +231,17 @@ if __name__=='__main__':
|
|
231
231
|
source='auto'
|
232
232
|
fill=True
|
233
233
|
|
234
|
+
#测试复权价
|
235
|
+
ticker=['300750.SZ','300014.SZ']
|
236
|
+
fromdate="2023-4-20"
|
237
|
+
todate="2023-4-30"
|
238
|
+
ticker_type='fund'
|
239
|
+
|
240
|
+
adjust='qfq'
|
241
|
+
source='auto'
|
242
|
+
fill=False
|
243
|
+
|
244
|
+
|
234
245
|
prices,found=get_price_mticker(ticker,fromdate,todate,adjust,source,ticker_type,fill)
|
235
246
|
|
236
247
|
def get_price_mticker(ticker,fromdate,todate, \
|
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:
|
@@ -2462,14 +2479,13 @@ def security_technical2(ticker,start='default',end='default', \
|
|
2462
2479
|
technical=['MACD'],indicator='Close', \
|
2463
2480
|
graph=['ALL'],printout=False, \
|
2464
2481
|
loc1='best',loc2='best', \
|
2465
|
-
ticker_type='auto', \
|
2482
|
+
ticker_type='auto',source='auto', \
|
2466
2483
|
|
2467
2484
|
attention_values=[0,30,50,80], \
|
2468
2485
|
facecolor='papayawhip',price_line_color='red', \
|
2469
2486
|
|
2470
2487
|
more_details=False):
|
2471
2488
|
"""
|
2472
|
-
|
2473
2489
|
功能:计算和绘制证券技术分析指标的简易图,仅供进一步探索使用,仅用于单个证券(股债基)
|
2474
2490
|
|
2475
2491
|
支持的探索指标:仅供探索使用
|
@@ -2515,7 +2531,7 @@ def security_technical2(ticker,start='default',end='default', \
|
|
2515
2531
|
#抓取抓取价格数据
|
2516
2532
|
fromdate1=date_adjust(fromdate,adjust=-ahead_days)
|
2517
2533
|
price,found=get_price_1ticker_mixed(ticker=ticker,fromdate=fromdate1, \
|
2518
|
-
todate=todate,ticker_type=ticker_type,fill=False)
|
2534
|
+
todate=todate,ticker_type=ticker_type,fill=False,source=source)
|
2519
2535
|
|
2520
2536
|
if found not in ['Found']:
|
2521
2537
|
print(" #Warning(security_technical2): no prices found for",ticker,'as type',ticker_type)
|
@@ -2558,7 +2574,8 @@ def security_technical2(ticker,start='default',end='default', \
|
|
2558
2574
|
WR_days=WR_days, \
|
2559
2575
|
ROC_day=ROC_day,ROC_madays=ROC_madays, \
|
2560
2576
|
DMI_DIdays=DMI_DIdays,DMI_ADXdays=DMI_ADXdays, \
|
2561
|
-
|
2577
|
+
|
2578
|
+
indicator=indicator, \
|
2562
2579
|
more_details=more_details)
|
2563
2580
|
|
2564
2581
|
#技术指标的绘图线
|
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:
|
@@ -66,7 +66,8 @@ siat/holding_risk_test.py,sha256=FRlw_9wFG98BYcg_cSj95HX5WZ1TvkGaOUdXD7-V86s,474
|
|
66
66
|
siat/local_debug_test.py,sha256=CDAOffW1Rvs-TcNN5giWVvHMlch1w4dp-w5SIV9jXL0,3936
|
67
67
|
siat/market_china.py,sha256=nabx24qm7N51OafTrwUw542pNeFJ3JaQ1wqyv-nLN5I,37883
|
68
68
|
siat/markowitz.py,sha256=c06gCRhMABnrb30F-npJsKVv8nFfEoNNR3bzrkMCyGg,97406
|
69
|
-
siat/markowitz2.py,sha256=
|
69
|
+
siat/markowitz2-20240620.py,sha256=irZAPnjaatFsKQmFRMENP-cO6bEUl2narYtkU5NKTWI,108019
|
70
|
+
siat/markowitz2.py,sha256=0FfGv8JzBWTjq3mjCDmT5K-lqpW6RvIggFD4h4SAGzM,109539
|
70
71
|
siat/markowitz_ccb_test.py,sha256=xBkkoaNHdq9KSUrNuHGgKTdNYUvgi84kNYcf719eoyE,1593
|
71
72
|
siat/markowitz_ef_test.py,sha256=wjNlICkgRIqnonPeSIHo4Mu2GRtb9dr21wDt2kMNEcI,4032
|
72
73
|
siat/markowitz_old.py,sha256=Lf7O_4QWT8RsdkHiUyc_7kKY3eZjKDtFR89Fz3pwYnY,33046
|
@@ -91,10 +92,10 @@ siat/risk_evaluation.py,sha256=I6B3gty-t--AkDCO0tKF-291YfpnF-IkXcFjqNKCt9I,76286
|
|
91
92
|
siat/risk_evaluation_test.py,sha256=YEXM96gKzTfwN4U61AS4Rr1tV7KgUvn4rRC6f3iMw9s,3731
|
92
93
|
siat/risk_free_rate.py,sha256=ZMr4cHikPvXvywr54gGqiI3Nvb69am6tq3zj2hwzANE,12384
|
93
94
|
siat/risk_free_rate_test.py,sha256=CpmhUf8aEAEZeNu4gvWP2Mz2dLoIgBX5bI41vfUBEr8,4285
|
94
|
-
siat/sector_china.py,sha256=
|
95
|
+
siat/sector_china.py,sha256=nP6kfYsnaQWZj8dK-zklwSDW8FDS-obZWp_zL0ec2Ig,118603
|
95
96
|
siat/sector_china_test.py,sha256=1wq7ef8Bb_L8F0h0W6FvyBrIcBTEbrTV7hljtpj49U4,5843
|
96
97
|
siat/security_price.py,sha256=2oHskgiw41KMGfqtnA0i2YjNNV6cYgtlUK0j3YeuXWs,29185
|
97
|
-
siat/security_price2.py,sha256=
|
98
|
+
siat/security_price2.py,sha256=581xPduTjxEEHSkDNGn4v7VxsZ2C6vHeGJ51XB1GLuQ,25825
|
98
99
|
siat/security_prices.py,sha256=a5U5qM5iX3Fl2GM8X1ceM5TGE5z32Q3ovMewKgE-6pg,103410
|
99
100
|
siat/security_prices_test.py,sha256=OEphoJ87NPKoNow1QA8EU_5MUYrJF-qKoWKNapVfZNI,10779
|
100
101
|
siat/security_trend.py,sha256=o0vpWdrJkmODCP94X-Bvn-w7efHhj9HpUYBHtLl55D0,17240
|
@@ -112,7 +113,7 @@ siat/stock_list_china_test.py,sha256=gv14UwMMvkZqtb6G7DCTSuehIwVHuVwu7w60p6gyHoo
|
|
112
113
|
siat/stock_prices_kneighbors.py,sha256=WfZvo5EyeBsm-T37zDj7Sl9dPSRq5Bx4JxIJ9IUum6s,36738
|
113
114
|
siat/stock_prices_linear.py,sha256=-OUKRr27L2aStQgJSlJOrJ4gay_G7P-m-7t7cU2Yoqk,13991
|
114
115
|
siat/stock_profile.py,sha256=B3eIwzEmiCqiCaxIlhfdEPsQBoW1PFOe1hkiY3mVF6Y,26038
|
115
|
-
siat/stock_technical.py,sha256=
|
116
|
+
siat/stock_technical.py,sha256=A4x18mZgYSA8SSiDz4u_O3gd5oVRgbI6JIiBfFY0tVw,116013
|
116
117
|
siat/stock_test.py,sha256=E9YJAvOw1VEGJSDI4IZuEjl0tGoisOIlN-g9UqA_IZE,19475
|
117
118
|
siat/stooq.py,sha256=dOc_S5HLrYg48YAKTCs1eX8UTJOOkPM8qLL2KupqlLY,2470
|
118
119
|
siat/temp.py,sha256=gbJ0ioauuo4koTPH6WKUkqcXiQPafnbhU5eKJ6lpdLA,1571
|
@@ -130,10 +131,10 @@ siat/translate.py,sha256=mldxeFwkxFrh_7EV1O_nRju8W0wIPnKAtN_mbN621w8,215280
|
|
130
131
|
siat/translate_20240606.py,sha256=63IyHWEU3Uz9mjwyuAX3fqY4nUMdwh0ICQAgmgPXP7Y,215121
|
131
132
|
siat/universal_test.py,sha256=CDAOffW1Rvs-TcNN5giWVvHMlch1w4dp-w5SIV9jXL0,3936
|
132
133
|
siat/valuation.py,sha256=NKfeZMdDJOW42oLVHob6eSVBXUqlN1OCnnzwyGAst8c,48855
|
133
|
-
siat/valuation_china.py,sha256=
|
134
|
+
siat/valuation_china.py,sha256=EkZQaVkoBjM0c4MCNbaX-bMnlG0e3FXeaWczZDnkptU,67784
|
134
135
|
siat/valuation_market_china_test.py,sha256=gbJ0ioauuo4koTPH6WKUkqcXiQPafnbhU5eKJ6lpdLA,1571
|
135
136
|
siat/var_model_validation.py,sha256=f-oDewg7bPzyNanz_Y_jLH68NowAA3gXFehW_weKGG0,14898
|
136
|
-
siat-3.1.
|
137
|
-
siat-3.1.
|
138
|
-
siat-3.1.
|
139
|
-
siat-3.1.
|
137
|
+
siat-3.1.28.dist-info/METADATA,sha256=74IePv7Bc_JMuhwxyIrBUnhSp7ECuFEUoRRSQ8BnN-w,1448
|
138
|
+
siat-3.1.28.dist-info/WHEEL,sha256=2wepM1nk4DS4eFpYrW1TTqPcoGNfHhhO_i5m4cOimbo,92
|
139
|
+
siat-3.1.28.dist-info/top_level.txt,sha256=r1cVyL7AIKqeAmEJjNR8FMT20OmEzufDstC2gv3NvEY,5
|
140
|
+
siat-3.1.28.dist-info/RECORD,,
|