Ai全自动编写策略:牛起指标,收益率326%,年化73%,胜率63%

松鼠Quant
2025-06-12


图片
量化策略开发,高质量社群,交易思路分享等相关内容

工具推荐

· 参数筛选工具

·Ai帮你编写策略

· 订单流图表

· 加入2025俱乐部

· Ai投研助手课程


『正文』

ˇ

Ai编写策略是量化领域的新常态,松鼠Quant在早些时候推出了开源项目《gitee官方推荐,松鼠QuantAi+回测框架详细版教程(图文)让Ai自动编写策略,自动回测,给出总结和迭代意见。
现在我们将通达信指标:牛起指标,让Ai自动完成python编写,然后自动回测总结。
图片
通达信:














VAR1:=MA(V,5);VAR2:=MA(V,10);VAR3:=V>REF(HHV(V,2),1) AND RANGE(VAR1,VAR2,V) AND UPNDAY(VAR1,2) AND UPNDAY(VAR2,2);VAR4:=H>REF(HHV(H,2),1);VAR8:=MAX(C,O);VAR5:=VAR8>REF(HHV(VAR8,2),1);历史大底:=VAR3 AND VAR4 AND VAR5;VAR6:=BARSLAST(历史大底=1);VAR7:=REF(REF(H,1),VAR6);DB:=IF(历史大底,0,IF(C< VAR7,1,0));LJ:=REF(HHV(H,VAR6+1),BARSLAST(DB));NOTEXT:LJ COLORLIMAGENTA;STICKLINE(IF(C>LJ,IF(REF(VAR8< LJ,1),1,0),0),O,C,2.2,0),COLORYELLOW;DRAWICON(IF(C>LJ,IF(REF(VAR8< LJ,1),1,0),0),L,25);
接下来让Ai帮我们转化到松鼠Quant回测框架内,我们不需要写一行代码!!!

github地址(国外):

https://github.com/songshuquant/ssquant-ai

gitee地址(国内):

https://gitee.com/ssquant/ssquant-ai

如何使用,手把手教程:《gitee官方推荐,松鼠QuantAi+回测框架详细版教程(图文)
Python源码:






















































































































































































import pandas as pdimport numpy as npfrom ssquant.backtest.multi_source_backtest import MultiSourceBacktesterfrom ssquant.api.strategy_api import StrategyAPIdef initialize(api: StrategyAPI):    api.log("成交量突破+价格突破策略初始化")    api.entry_price = 0   # 添加入场价格变量    def strategy_function(api: StrategyAPI):    # 参数和数据验证    current_idx = api.get_idx()    if current_idx < 20:   # 确保有足够的数据计算指标        return        # 获取数据    close = api.get_close()    high = api.get_high()    low = api.get_low()    open_ = api.get_open()    volume = api.get_volume()    current_pos = api.get_pos()        # 增强数据验证    if any(data is None or len(data) == 0 for data in [close, high, low, open_, volume]):        api.log("警告: 数据为空")        return    if current_idx >= len(close):        api.log("警告: 索引超出数据范围")        return        # 计算指标    # VAR1: 5日成交量均线    var1 = volume.rolling(5).mean()    # VAR2: 10日成交量均线    var2 = volume.rolling(10).mean()    # VAR3: 成交量突破条件 - 进一步简化    var3 = volume > volume.rolling(3).mean()   # 改为3日均量,不要求突破最大值    # VAR4: 价格突破2日最高价    var4 = high > high.rolling(2).max().shift(1)    # VAR8: 当日K线实体高点    var8 = np.maximum(close, open_)    # VAR5: K线实体高点突破2日最高    var5 = var8 > var8.rolling(2).max().shift(1)    # 历史大底信号    history_bottom = var3 & var4 & var5        # 添加调试日志    if current_idx % 20 == 0:   # 每20个周期打印一次日志        api.log(f"===== 调试信息 [idx={current_idx}] =====")        api.log(f"成交量突破(var3): {var3.iloc[current_idx]}")        api.log(f"价格突破(var4): {var4.iloc[current_idx]}")        api.log(f"实体高点突破(var5): {var5.iloc[current_idx]}")        api.log(f"历史大底信号: {history_bottom.iloc[current_idx]}")        api.log(f"当前成交量: {volume.iloc[current_idx]}")        api.log(f"5日均量: {var1.iloc[current_idx]}")        api.log(f"10日均量: {var2.iloc[current_idx]}")        api.log(f"2日最高价: {high.rolling(2).max().shift(1).iloc[current_idx]}")        # 计算最近的历史大底位置    recent_history = history_bottom.iloc[max(0, current_idx-5):current_idx+1]   # 改为只看最近5个周期    var6 = 0    if recent_history.any():        var6 = len(recent_history) - 1 - recent_history[::-1].values.argmax()        # 添加var6调试日志    if current_idx % 20 == 0:        api.log(f"历史大底距离(var6): {var6}")        # 修改lj的计算逻辑    lj = 0    if var6 > 0:        # 从历史大底到当前的最高价        start_idx = current_idx - var6        lj = high.iloc[start_idx:current_idx+1].max()        # 添加lj调试日志    if current_idx % 20 == 0:        api.log(f"历史大底高点(lj): {lj}")        api.log("========================")    # 交易信号    if lj > 0:        current_close = close.iloc[current_idx]        current_high = high.iloc[current_idx]        prev_close = close.iloc[current_idx-1]                # 进一步放宽买入条件:        # 1. 当日最高价接近历史高点(允许10%的差距)        # 2. 收盘价站稳在历史高点附近(允许15%的回撤)        # 3. 相对前一日有上涨        price_threshold = lj * 0.85   # 允许15%的回撤        high_threshold = lj * 0.90   # 允许10%的差距        buy_signal = (current_high > high_threshold) and \                    (current_close > price_threshold) and \                    (current_close > prev_close * 1.005)   # 要求至少0.5%的涨幅                # 添加买入条件的详细日志        if current_idx % 5 == 0 or buy_signal:   # 每5个周期或有买入信号时打印            api.log(f"买入条件检查 [idx={current_idx}]:")            api.log(f"当日最高价: {current_high:.2f}")            api.log(f"当前收盘价: {current_close:.2f}")            api.log(f"前一日收盘价: {prev_close:.2f}")            api.log(f"历史高点(lj): {lj:.2f}")            api.log(f"价格阈值(85%): {price_threshold:.2f}")            api.log(f"最高价阈值(90%): {high_threshold:.2f}")            api.log(f"条件1(最高价>阈值): {current_high > high_threshold}")            api.log(f"条件2(收盘价>阈值): {current_close > price_threshold}")            api.log(f"条件3(上涨>0.5%): {current_close > prev_close * 1.005}")            api.log(f"买入信号: {buy_signal}")    else:        buy_signal = False        current_close = close.iloc[current_idx]        prev_close = close.iloc[current_idx-1]    # 执行交易    if current_pos > 0:   # 已有持仓,检查平仓条件        # 计算当前持仓的盈亏比例        if api.entry_price > 0:   # 确保有入场价格            profit_ratio = (current_close - api.entry_price) / api.entry_price                        # 平仓条件:            # 1. 止损:跌破历史高点15%            # 2. 止盈:盈利超过8%            # 3. 破位:收盘价跌破5日均线且跌幅超过2%            ma5 = close.rolling(5).mean()            sell_signal = (current_close < lj * 0.85) or \                         (profit_ratio > 0.08) or \                         ((current_close < ma5.iloc[current_idx]) and \                          (current_close < prev_close * 0.98))   # 破位                        if sell_signal:                api.close_all(order_type='next_bar_open')                if current_close < lj * 0.85:                    api.log(f"止损平仓,当前价: {current_close:.2f}")                elif profit_ratio > 0.08:                    api.log(f"止盈平仓,盈利: {profit_ratio*100:.1f}%")                else:                    api.log(f"破位平仓,当前价: {current_close:.2f}")                api.entry_price = 0   # 重置入场价格        elif buy_signal and current_pos <= 0:   # 没有持仓且有买入信号        api.close_all(order_type='next_bar_open')        api.buy(volume=1, order_type='next_bar_open')        api.entry_price = current_close   # 记录入场价格        api.log(f"买入信号触发,价格接近历史高点: {lj:.2f}")if __name__ == "__main__":    # 导入API认证信息    try:        from ssquant.config.auth_config import get_api_auth        API_USERNAME, API_PASSWORD = get_api_auth()    except ImportError:        print("警告:未找到 auth_config.py 文件,请在下方填写您的认证信息:API_USERNAME和API_PASSWORD")        API_USERNAME = "your_username"   # 替换为您的实际用户名        API_PASSWORD = "your_password"   # 替换为您的实际密码    # 回测配置    backtester = MultiSourceBacktester()    backtester.set_base_config({        'username': API_USERNAME,   # 添加用户名        'password': API_PASSWORD,   # 添加密码        'use_cache': True,        'save_data': True,        'debug': True    })        backtester.add_symbol_config(        symbol='au888',        config={            'start_date': '2021-01-01',            'end_date': '2025-12-31',            'initial_capital': 100000.0,            'commission': 0.0002,   # 手续费万分之0.2            'margin_rate': 0.08,   # 保证金率8%            'contract_multiplier': 1000,   # 沪金一手1000克            'periods': [                {'kline_period': '1d', 'adjust_type': '1'},            ]        }    )        results = backtester.run(        strategy=strategy_function,        initialize=initialize,        strategy_params={},    )
回测绩效
图片
图片
图片
图片
没错,整个开发过程我们没有手写一行代码,松鼠QuantAi助手会编写,回测,分析,修改。基于松鼠Quant的MCP服务器+Deepseek开启全新量化学习的路径,即使0基础也能完成策略的编写与验证!
如果你喜欢这个系列,请点赞,转发,在看三连。我们根据受欢迎程度决定是否继续更新,感谢您的支持。

防迷路


           图片

微   信|小松鼠-松鼠Quant

微信号|viquant01

25俱乐部源码VIP20》

加入2025量化俱乐部


分享