# 导入函数库
from jqdata import *
# 初始化函数,设定基准等等
def initialize(context):
# 以三一重工为例,假定2018-03-01买入,到达1%的盈利即止盈卖出
set_benchmark('600031.XSHG')
# 开启动态复权模式(真实价格)
set_option('use_real_price', True)
# 输出内容到日志 log.info()
log.info('初始函数开始运行且全局只运行一次')
# 过滤掉order系列API产生的比error级别低的log
# log.set_level('order', 'error')
### 股票相关设定 ###
# 股票类每笔交易时的手续费是:买入时佣金万分之三,卖出时佣金万分之三加千分之一印花税, 每笔交易佣金最低扣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(market_open, time='10:00', reference_security='600031.XSHG')
## 开盘时运行函数
def market_open(context):
log.info('函数运行时间(market_open):'+str(context.current_dt.time()))
# 取得当前的现金
cash = context.portfolio.available_cash
if context.current_dt.strftime("%Y-%m-%d") == '2018-03-01':
# 记录这次买入
log.info("买入")
# 用所有 cash 买入股票
order_value('600031.XSHG', cash)
if context.portfolio.positions:
cost=context.portfolio.positions['600031.XSHG'].avg_cost
price=context.portfolio.positions['600031.XSHG'].price
ret = price/cost - 1
if ret >= 0.01:
order_target('600031.XSHG', 0)
log.info("止盈卖出")
2024-01-22