【股票量化系列-01】从单因子到策略的代码分析解读

从单因子到策略的代码分析解读

导入库

  1. jq-JoinQuant,首字母,jqdata是量化函数库
# 导入函数库
from jqdata import *

定义初始化函数

def initialize(context):

初始化函数详细内容

**** 一部分、初始化参数 ****
  1. 设定基准
  2. 开启复权
  3. 输出到日志
  4. 过滤order级别低的log
def initialize(context):
# 初始化函数,设定基准等等
def initialize(context):# 设定沪深300作为基准set_benchmark('000300.XSHG')# 开启动态复权模式(真实价格)set_option('use_real_price', True)# 输出内容到日志 log.info()log.info('初始函数开始运行且全局只运行一次')# 过滤掉order系列API产生的比error级别低的log# log.set_level('order', 'error')

*** 二部分、股票手续佣金***

  1. set_order_cost() 设置订单成本
  2. OrderCOst() 订购成本
  3. run_daily() 每日运行函数
  4. 每日运行的时间点,见③内的参数before_market_open开盘前,market_open开盘时,after_market_close收盘后。
    # 股票类每笔交易时的手续费是:买入时佣金万分之三,卖出时佣金万分之三加千分之一印花税, 每笔交易佣金最低扣5块钱set_order_cost(OrderCost(close_tax=0.001, open_commission=0.0003, close_commission=0.0003, min_commission=5), type='stock')## 运行函数(reference_security为运行时间的参考标的;传入的标的只做种类区分,因此传入'000300.XSHG'或'510300.XSHG'是一样的)# 开盘前、开盘时、 收盘后运行run_daily(before_market_open, time='before_open', reference_security='000300.XSHG')run_daily(market_open, time='open', reference_security='000300.XSHG')run_daily(after_market_close, time='after_close', reference_security='000300.XSHG')

开盘前运行函数

开盘前函数定义

def before_market_open(context):
  1. 运行信息日志log.info()
  2. g.security = ‘000001.XSHE’ 获取上交所股票代码
def before_market_open(context):# 输出运行时间log.info('函数运行时间(before_market_open):'+str(context.current_dt.time()))# 要操作的股票:平安银行(g.为全局变量)g.security = '000001.XSHE'

开盘时运行函数

第一部分:获取基本初始参数
  1. log.info() 记录程序运行时的时间的信息日志
  2. security保存了上交所股票代码
  3. close_data 获取股票收盘价(5天)
  4. MA5 获取五天平均价格
  5. current_price 上一时间点价格
  6. cash 当前的现金
def market_open(context):log.info('函数运行时间(market_open):'+str(context.current_dt.time()))security = g.security# 获取股票的收盘价close_data = get_bars(security, count=5, unit='1d', fields=['close'])# 取得过去五天的平均价格MA5 = close_data['close'].mean()# 取得上一时间点价格current_price = close_data['close'][-1]# 取得当前的现金cash = context.portfolio.available_cash
第二部分:获取交易过程
  1. 条件1:买入的条件,上一时间价格(因为购买股票价格要参照刚过去的价格)比均价高1%,有现金
  2. order_value()现金购买股票
  3. 条件2:上一时间点价格低于五天均价,且没有空仓时卖出
  4. 股票卖出方法order_target()
    # 如果上一时间点价格高出五天平均价1%, 则全仓买入if (current_price > 1.01*MA5) and (cash > 0):# 记录这次买入log.info("价格高于均价 1%%, 买入 %s" % (security))print("当前可用资金为{0}, position_value为{0}".format(cash, context.portfolio.positions_value))# 用所有 cash 买入股票order_value(security, cash)# 如果上一时间点价格低于五天平均价, 则空仓卖出elif current_price < MA5 and context.portfolio.positions[security].closeable_amount > 0:# 记录这次卖出log.info("价格低于均价, 卖出 %s" % (security))# 卖出所有股票,使这只股票的最终持有量为0order_target(security, 0)

收盘后运行函数

  • 当天成交记录trades

def after_market_close(context):
log.info(str(‘函数运行时间(after_market_close):’+str(context.current_dt.time())))
#得到当天所有成交记录
trades = get_trades()
for _trade in trades.values():
log.info(‘成交记录:’+str(_trade))
log.info(‘一天结束’)
log.info(‘##############################################################’)



本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部