多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# 5.2 Joseph Piotroski 9 F-Score Value Investing Model · 基本面选股系统:Piotroski F-Score ranking system > 来源:https://uqer.io/community/share/56710b1d228e5b8d84f00ac7 ```py 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' # 策略类型,'d'表示日间策略使用日线回测,'m'表示日内策略使用分钟线回测 refresh_rate = 1 # 调仓频率,表示执行handle_data的时间间隔,若freq = 'd'时间间隔的单位为交易日,若freq = 'm'时间间隔为分钟 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') # 去除ST股 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)] # 调仓部分注意仓位控制,尽量满足80%股票仓位和单只股票不超过10%的条件 #return 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) ```