返回介绍

第一部分 新手入门

第二部分 股票量化相关

第三部分 基金、利率互换、固定收益类

第四部分 衍生品相关

5.2 Joseph Piotroski 9 F-Score Value Investing Model

发布于 2022-02-20 22:26:16 字数 6212 浏览 879 评论 0 收藏 0

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文