首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >最后一篇问财技术文,同花顺问财策略回测的几种技术方案

最后一篇问财技术文,同花顺问财策略回测的几种技术方案

作者头像
子晓聊技术
发布2026-04-23 14:08:06
发布2026-04-23 14:08:06
1750
举报
文章被收录于专栏:子晓AI量化子晓AI量化

一些同学通过问财界面输入了策略, 想用于实盘,希望回测该策略的胜率怎么样。 最后不得已每天下载数据手工统计excel方式去回测每天的自选股情况效果表现怎么样。

耗时精力不说,还容易出错。

那有没有更简单便捷的方式呢?这里提供3种方式,可能前面的方式对于超短线并不实用,那就看最后1种。

1、不懂Python代码的同学,可以直接用问财策略回测功能

具体可以看上面截图,这个对于超短并不适用。

2、懂python技术的同学,可以用supermind的策略回测功能,上面自带了不少策略。形如问财日级模板 和分钟级模板。这里附上分钟级模板示例

代码语言:javascript
复制
# 爱问财策略回测框架(分钟级回测)

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)
                    continue

3、自己用python写套程序,传递日期范围,根据条件把符合条件的筛选个股excel保存下来,记录下买入卖出点, 类似 模拟的交割单, 然后自己编写回测程序, 回测下收益、最大回测。

比如某些同学喜欢玩超短, 竞价买入,第二天竞价卖出或尾盘卖出, 我们就可以通过这种方式回测。 把手工excel处理 变成 python程序去实现。 这里需要注意下滑点问题, 比如9点25分竞价选的股,如果你是想抓涨停这种,这个价格不一定能买到的。 比如跌停不一定能卖出的。 回测只是模拟理想情况。


备注:上面是20250406中午左右写的文章, 刚写完傍晚的时候发现问财接口升级了。本不想发这篇文章的,但写都写了,就这样吧。

这里就不提供例子了,毕竟授之以鱼,不如授之以渔吧。 主要还有一个原因,问财普及多了,官方接口都改了。 开源库pywencai做了更新, 但需要传递cookie。 要cookie后,至于有效期什么的就不受我们控制了。

另外这应该是最后一篇普及问财了, 发觉普及得越多,就容易遭遇官方改版。 就像我不再公开写爬虫一样。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2025-05-07,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 子晓聊技术 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档