5.2 Joseph Piotroski 9 F-Score Value Investing Model · 基本面选股系统:Piotroski F-Score ranking system
来源:https://uqer.io/community/share/56710b1d228e5b8d84f00ac7
from CAL.PyCAL import *
import numpy as np
from pandas import DataFrame , Series
start = '2014-01-01'
end = '2015-01-01'
benchmark = 'HS300'
universe = set_universe('HS300')
capital_base = 100000
csvs = []
security_base = {}
commission = Commission(buycost=0.0008, sellcost=0.0018)
slippage = Slippage()
freq = 'd'
refresh_rate = 1
cal = Calendar('China.SSE')
def initialize(account):
pass
def handle_data(account):
today = account.current_date.strftime('%Y%m%d')
yesterday = cal.advanceDate(account.current_date, '-1B', BizDayConvention.Following).strftime('%Y%m%d')
lastyear = cal.advanceDate(account.current_date, '-1Y', BizDayConvention.Following).strftime('%Y%m%d')
try:
STlist = DataAPI.SecSTGet(secID=account.universe, beginDate=yesterday, endDate=yesterday, field=['secID']).tolist()
account.universe = [s for s in account.universe if s not in STlist]
except:
pass
tv = account.get_attribute_history('turnoverValue', 20)
mtv = {sec: sum(tvs)/20. for sec,tvs in tv.items()}
account.universe = [s for s in account.universe if mtv.get(s, 0) >= 10**7]
opn = account.get_attribute_history('openPrice', 1)
account.universe = [s for s in account.universe if not (np.isnan(opn.get(s, 0)[0]) or opn.get(s, 0)[0] == 0)]
buylist = []
selllist = []
getData_yesterday = Series()
getData_lastyear = Series()
getData_yesterday = DataAPI.MktStockFactorsOneDayGet(tradeDate=yesterday,secID=account.universe,field=['secID','LFLO','ROA','OperCashGrowRate','CurrentRatio','DebtEquityRatio','GrossIncomeRatio','TotalAssetsTRate'],pandas="1")
getData_yesterday.drop_duplicates('secID', inplace = True)
getData_yesterday = getData_yesterday.sort('LFLO', ascending=True)[0:100]
getData_yesterday.set_index('secID',inplace=True)
getData_yesterday.dropna(inplace = True)
getData_lastyear = DataAPI.MktStockFactorsOneDayGet(tradeDate=lastyear,secID=account.universe,field=['secID','LFLO','ROA','OperCashGrowRate','CurrentRatio','DebtEquityRatio','GrossIncomeRatio','TotalAssetsTRate'],pandas="1")
getData_lastyear.drop_duplicates('secID', inplace = True)
getData_lastyear.set_index('secID',inplace=True)
getData_lastyear.dropna(inplace = True)
totallist = list(set(getData_yesterday.index)&set(getData_lastyear.index))
for s in totallist:
ROA1 = getData_yesterday[s]['ROA']>0
ROA2 = getData_yesterday[s]['ROA']>getData_lastyear[s]['ROA']
OperCashGrowRate = getData_yesterday[s]['CurrentRatio']>0
CurrentRatio = getData_yesterday[s]['CurrentRatio']>getData_lastyear[s]['CurrentRatio']
DebtEquityRatio = getData_yesterday[s]['DebtEquityRatio']<getData_lastyear[s]['DebtEquityRatio']
GrossIncomeRatio = getData_yesterday[s]['GrossIncomeRatio']>getData_lastyear[s]['GrossIncomeRatio']
TotalAssetsTRate = getData_yesterday[s]['TotalAssetsTRate']>getData_lastyear[s]['TotalAssetsTRate']
Scores = int(ROA1)+int(ROA2)+int(OperCashGrowRate)+int(CurrentRatio)+int(DebtEquityRatio)+int(GrossIncomeRatio)+int(TotalAssetsTRate)
if Scores>=6:
buylist.append(s)
for s in account.valid_secpos:
if s not in buylist:
order_to(s, 0)
for s in buylist:
if len(buylist)>=10:
order(s, account.referencePortfolioValue/len(buylist)/account.referencePrice[s])
else:
order_pct_to(s, 0.1)
for s in account.valid_secpos:
if account.referencePrice[s] * account.valid_secpos[s] / account.referencePortfolioValue >0.1:
order_pct_to(s, 0.1)