
一些同学通过问财界面输入了策略, 想用于实盘,希望回测该策略的胜率怎么样。 最后不得已每天下载数据手工统计excel方式去回测每天的自选股情况效果表现怎么样。
耗时精力不说,还容易出错。
那有没有更简单便捷的方式呢?这里提供3种方式,可能前面的方式对于超短线并不实用,那就看最后1种。
1、不懂Python代码的同学,可以直接用问财策略回测功能
具体可以看上面截图,这个对于超短并不适用。
2、懂python技术的同学,可以用supermind的策略回测功能,上面自带了不少策略。形如问财日级模板 和分钟级模板。这里附上分钟级模板示例
# 爱问财策略回测框架(分钟级回测)
import datetime
# 初始化账户
def init(context):
# 设置策略问句
get_iwencai('非停牌;非st;业绩预增大于50%;市值排名后30%;市盈率小于120;总市值从小到大')
# 设置持仓股票数量
context.n = 4
# 设置股票持有期
context.holding_period = 5
# 设置止盈涨幅
context.stop_gain = 0.2
# 设置止盈回撤
context.stop_gain_drawdown = 0.05
# 设置止损跌幅
context.stop_loss = 0.05
# 设置仓位比例
context.position_pct = 1
context.information = {}
run_daily(market_open,'after_open',0,1,'000001.SZ')
run_daily(market_close,'before_close',0,5,'000001.SZ')
class Information:
# 记录买入股票的信息,作为position的补充
def __init__(self, remain_days):
self.remain_days = remain_days
self.stop_gain = False
self.today_high = 0
# 开盘时买入股票
def market_open(context, bar_dict):
# 若持有天数到期则卖出(防止昨天未卖出)
for stock in list(context.portfolio.positions):
context.information[stock].today_high = 0
if (not context.information[stock].stop_gain
and context.information[stock].remain_days == 0):
order_target(stock, 0)
if stock not in list(context.portfolio.positions):
del context.information[stock]
log.info('持有天数到期,卖出%s' % stock)
continue
stocks_list = context.iwencai_securities
number_of_buys = context.n-len(context.portfolio.positions)
if number_of_buys == 0:
return
# 若持有股数不足设定值则买入
cash_for_stock = context.portfolio.available_cash*context.position_pct/number_of_buys
for stock in stocks_list:
if stock in context.portfolio.positions:
continue
#current = data.current([stock])[stock]
if not bar_dict[stock].open or not bar_dict[stock].close:
continue
# 开盘涨跌幅不小于-9%才买入
if bar_dict[stock].open/bar_dict[stock].close-1 >= -0.09:
order_value(stock, cash_for_stock)
if stock in list(context.portfolio.positions):
context.information[stock] = Information(context.holding_period)
log.info('买入%s' % stock)
number_of_buys -= 1
if number_of_buys == 0:
return
# 盘中止盈止损
def handle_bar(context, bar_dict):
price_dict = history(list(context.portfolio.positions), ['close'], 2, '1m')
for stock in price_dict:
if price_dict[stock].empty:
continue
price = price_dict[stock].values[-1][0]
# 更新今日最高价
if price > context.information[stock].today_high:
context.information[stock].today_high = price
# 若刚买入则不进行操作
if context.information[stock].remain_days == context.holding_period:
continue
# 若进入止盈状态,则根据要求止盈
if context.information[stock].stop_gain:
if price/context.information[stock].high-1 < -context.stop_gain_drawdown:
order_target(stock, 0)
if stock not in context.portfolio.positions:
del context.information[stock]
log.info('止盈,卖出%s' % stock)
# 若未进入止盈状态,则根据要求止损
else:
if price/context.portfolio.positions[stock].cost_basis-1 < -context.stop_loss:
order_target(stock, 0)
if stock not in context.portfolio.positions:
del context.information[stock]
log.info('止损,卖出%s' % stock)
# 收盘时卖出到期股票、判断止盈
def market_close(context, bar_dict):
for stock in list(context.portfolio.positions):
today_high = context.information[stock].today_high
# 若进入止盈状态,则更新最高价
if context.information[stock].stop_gain:
if today_high > context.information[stock].high:
context.information[stock].high = today_high
# 若未进入止盈状态,则卖出到期股票、判断止盈
else:
# 判断是否进入止盈状态
if today_high/context.portfolio.positions[stock].cost_basis-1 >= context.stop_gain:
context.information[stock].stop_gain = True
context.information[stock].high = today_high
log.info('%s进入止盈状态' % stock)
continue
# 若持有天数到期则卖出
context.information[stock].remain_days -= 1
if context.information[stock].remain_days == 0:
order_target(stock, 0)
if stock not in context.portfolio.positions:
del context.information[stock]
log.info('持有天数到期,卖出%s' % stock)
continue3、自己用python写套程序,传递日期范围,根据条件把符合条件的筛选个股excel保存下来,记录下买入卖出点, 类似 模拟的交割单, 然后自己编写回测程序, 回测下收益、最大回测。
比如某些同学喜欢玩超短, 竞价买入,第二天竞价卖出或尾盘卖出, 我们就可以通过这种方式回测。 把手工excel处理 变成 python程序去实现。 这里需要注意下滑点问题, 比如9点25分竞价选的股,如果你是想抓涨停这种,这个价格不一定能买到的。 比如跌停不一定能卖出的。 回测只是模拟理想情况。
备注:上面是20250406中午左右写的文章, 刚写完傍晚的时候发现问财接口升级了。本不想发这篇文章的,但写都写了,就这样吧。
这里就不提供例子了,毕竟授之以鱼,不如授之以渔吧。 主要还有一个原因,问财普及多了,官方接口都改了。 开源库pywencai做了更新, 但需要传递cookie。 要cookie后,至于有效期什么的就不受我们控制了。
另外这应该是最后一篇普及问财了, 发觉普及得越多,就容易遭遇官方改版。 就像我不再公开写爬虫一样。