Porfolio(现金比率+负债现金+现金保障倍数)+市盈率

来源:https://uqer.io/community/share/566a896bf9f06c6c8a91cae7

  1. ?DataAPI.MktStockFactorsOneDayGet
  1. import numpy as np
  2. import pandas as pd
  3. start = '2015-01-01' # 回测起始时间
  4. end = '2015-11-30' # 回测结束时间
  5. benchmark = 'HS300' # 策略参考标准
  6. universe = set_universe('HS300') # 证券池,支持股票和基金
  7. capital_base = 100000
  8. # 起始资金
  9. freq = 'd' # 策略类型,'d'表示日间策略使用日线回测,'m'表示日内策略使用分钟线回测
  10. refresh_rate = 1 # 调仓频率,表示执行handle_data的时间间隔,若freq = 'd'时间间隔的单位为交易日,若freq = 'm'时间间隔为分钟
  11. def initialize(account): # 初始化虚拟账户状态
  12. pass
  13. def handle_data(account): # 每个交易日的买入卖出指令
  14. market_val = DataAPI.MktEqudGet(tradeDate=account.current_date,field=u"secID,negMarketValue",pandas="1") #获取所有股票的市值
  15. factor = DataAPI.MktStockFactorsOneDayGet(tradeDate=account.current_date,field='secID,ROE,ROA,CashRateOfSales,FinancialExpenseRate,CashToCurrentLiability,OperCashInToCurrentLiability,GrossIncomeRatio,NetProfitRatio,PE,PB',pandas="1") #获取所有股票的相关因子
  16. # print factor
  17. factor.set_index('secID',inplace=True);
  18. sec_val_mkt = {'symbol':[], 'factor_value':[], 'market_value':[]}
  19. x='CashToCurrentLiability'
  20. y='OperCashInToCurrentLiability'
  21. z='PE'
  22. for stock in account.universe:
  23. sec_val_mkt['symbol'].append(stock)
  24. factor_va=float(1/3*factor.ix[stock][x]+1/3*factor.ix[stock][y]+1/3*factor.ix[stock][z]);
  25. sec_val_mkt['factor_value'].append(factor_va)
  26. sec_val_mkt['market_value'].append(float(market_val.negMarketValue[market_val.secID==stock]))
  27. sec_val_mkt = pd.DataFrame(sec_val_mkt).sort(columns='factor_value',ascending=True).reset_index()
  28. sec_val_mkt = sec_val_mkt[:int(len(sec_val_mkt)*0.1)] #排序并选择前10%
  29. buylist = list(sec_val_mkt.symbol)
  30. #买入股票列表
  31. sum_market_val = sum(sec_val_mkt.market_value)
  32. position = np.array(sec_val_mkt.market_value)/sum_market_val*account.cash
  33. for stock in account.valid_secpos:
  34. if stock not in buylist:
  35. order_to(stock, 0)
  36. for stock in buylist:
  37. if stock not in account.valid_secpos:
  38. order(stock, position[buylist.index(stock)])
  39. return

Porfolio(现金比率+负债现金+现金保障倍数)+市盈率 - 图1

  1. bt.blotter
  2. None