<jupyter_start><jupyter_text>Setting up parameter set<jupyter_code>params = {}
params['ticker'] = '000001'
#params['beginDate'] = datetime.strptime('20080101', '%Y%m%d')
params['endDate'] = datetime.today()

form_cf = 'getFdmtCF.json'
form_earning_report = '/api/fundamental/getFdmtEe.json'
form_is = '/api/fundamental/getFdmtIS.json'
form_is_latest = '/api/fundamental/getFdmtISAllLatest.json'
form_mkt_eq = '/api/market/getMktEqud.json'
form_mkt_eq_adj = '/api/market/getMktEqudAdj.json'<jupyter_output><empty_output><jupyter_text>1. get P/E factors (including LY- last year, TTM - Trailling 12 months, FY1 - Future year prediction)first we download Income Statement information, and do the cleaning steps:1. Sort the data by the publishDate and reportType2. as the datasource contains back-filled last year information, which is duplicate. We will need to delete the duplicates3. set the index as the publishdate, when the data is published.<jupyter_code>## get Income Statement data
f_is = getData(form_is, params)
f_is = f_is.sort(['publishDate', 'reportType'])
f_is.drop_duplicates('publishDate', inplace=True)
f_is = f_is.set_index('publishDate').sort()
f_is[['reportType', 'NIncome']].head(10)<jupyter_output>/api/fundamental/getFdmtIS.json?field=&secID=&ticker=000001&beginDate=&endDate=<jupyter_text>we can see in the last table, reportType indicate what the basicEPS field stands for: if reportType is A, basicEPS is then the sum of the last 4quaters EPS. S1 stands for the first half year aggregation. CQ3 stands for 3 quarters aggregation. To get a quarter-by-quarter data, we therefore need to clean and dissemble the aggregated basicEPS. 1. calculate the EPS by Quarter2. EPS TTM4. EPS LY - last year EPS (simply take the EPS from Annual report)3. EPS FY0 - 4* last quarter EPS<jupyter_code>f_earning = f_is[['reportType', 'NIncome']]
f_earning.loc[:, 'Q'] = f_earning.loc[:, 'NIncome'] - f_earning.loc[:,'NIncome'].shift(1)
f_earning.head()
index_q1 = f_earning.loc[:, 'reportType'] == 'Q1'
index_a = f_earning.loc[:, 'reportType'] == 'A'
f_earning.loc[index_q1, 'Q'] = f_earning.loc[index_q1, 'NIncome']
f_earning.loc[:,'TTM'] = pd.rolling_sum(f_earning['Q'], 4)
f_earning.loc[:, 'FY0'] = f_earning.loc[:, 'Q'] * 4
f_earning.loc[:, 'LY'] = f_earning.loc[index_a, 'NIncome']

f_earning.ffill(0, inplace=True)
f_earning.head()<jupyter_output><empty_output><jupyter_text>P/E calculationnow we calculate the p/e ratio 1. download price data2. calculate P/E ratio<jupyter_code>f_price = getData(form_mkt_eq, params)
f_price = f_price.set_index('tradeDate').sort()
f_price[['marketValue']].tail()
f_ep = f_price[['marketValue']]

f_ep = f_ep.join(f_earning[['Q', 'TTM', 'FY0', 'LY']], how='outer')
f_ep = f_ep.ffill().dropna()

f_ep.rename(columns = {'marketValue' : 'MV'}, inplace=True)

f_ep['EP_TTM'] = f_ep['TTM'] / f_ep['MV']
f_ep['EP_FY0'] = f_ep['FY0'] / f_ep['MV']
f_ep['EP_LY'] = f_ep['LY'] / f_ep['MV']
#f_ep['PE_TTM'] = 1/f_ep['EP_TTM']
f_ep[['EP_TTM', 'EP_FY0', 'EP_LY']].plot()<jupyter_output><empty_output><jupyter_text>encapsulate a functioncreate a function to automatically download and calculate these factors<jupyter_code>### get value factors
#1. cash flow to pride: CTOP
#2. EBITToTOR : EBIT to Total Operating Revenue
#3. ETOP : Earning to Price (inverse of PE)
#4. PB, PCF, PE, PS
#5. TA2EV, CFO2EV : Total Asset to Enterprise Value, and Cash Flow Operating to EV
#6. CashRateOfSales: CF Operating net vs. Salesb
factors = base.getFactorData(ticker, 'CTOP,EBITToTOR,ETOP,PB,PCF,PE,PS,TA2EV,CFO2EV,CashRateOfSales', \
    beginDate, endDate) # CTOP: cash flow to price, CTP5: 5y avg cash flow to price
factors
factors.sort_index(inplace=True)
factors['Sales2EV'] = factors['CFO2EV'] / factors['CashRateOfSales']
factors['Sales2EV'].plot()
### get fundamental values
#1. Net Cash Flow Operating
funda = pd.DataFrame()

f1 = base.getFundamental(form_cf, ticker, 'fiscalPeriod,NCFOperateA', beginDate, endDate)
f1.sort_index(inplace=True)

## create Earning data
f_earning = f1[['publishDate', 'reportType', '']]
#f1.drop_duplicates(['date', 'reportType'])
f1.drop_duplicates(inplace=True)
f1
funda2 = base.getFundamental(form_earning_report, ticker, 'fiscalPeriod,revenue', beginDate, endDate)
funda['CFO_Q'] = funda['NCFOperateA']
funda['CFO_TTM'] = pd.rolling_sum(funda['CFO_Q'], 4)

funda = funda.join(factors[['CFO2EV']], how='outer')
funda.drop_duplicates(inplace=True)
funda.fillna(axis=0, method='ffill', inplace=True)

funda['EV'] = funda['CFO_TTM'] / funda['CFO2EV']
funda2
factors
funda

results = pd.DataFrame()
mktcap = base.getMarketEqu(ticker, 'marketValue', beginDate, endDate) # total market cap
ncfo = base.getFundamental(ticker, 'NCFOperateA', beginDate, endDate) # net cashflow of operational

results = results.join(mktcap, how='outer')
results = results.join(ncfo, how='outer')

print results.dropna()
ticker = '600035'
base.getEquityDiv(ticker, 'perShareDivRatio,perShareTransRatio,perCashDiv',beginDate, endDate)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14951961569255404, 'fraction_numerical': 0.017814251401120897, 'mean_word_length': 6.795631825273011, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '321197', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1989, 'n_tokens_neox': 1894, 'n_words': 432, 'path': 'Trading_Strategies/Factors/demo.ipynb', 'repo_name': 'mikimaus78/ml_monorepo', 'size': '4996'}