avatar of 豆豆 豆豆
关注 私信
22
关注
17
关注者

【求助】为什么TradingView策略发送的消息在FMZ无法执行?

创建于: 2025-03-01 10:54:29, 更新于:
comments   4
hits   626

我之前使用的是指标消息。 做法是在每一个警报的消息设置中写入OpenLong 、 OpenShort,然后发送到FMZ机器人执行。

这样是OK的

现在使用策略发送消息,

TradingView的代码是

// 警报消息
var string  Message_open_long       = input.string('OpenLong',      '开多消息', group = '===================exit警报=======================', tooltip = '如果“创建警报”对话框的“消息”字段包含{{strategy.order.alert_message}}占位符,则警报消息将用此文本替换占位符。')
var string  Message_open_short      = input.string('OpenShort',     '开空消息', group = '===================exit警报=======================', tooltip = '如果“创建警报”对话框的“消息”字段包含{{strategy.order.alert_message}}占位符,则警报消息将用此文本替换占位符。')
var string  Message_close_long      = input.string('CloseLong',     '平多消息', group = '===================exit警报=======================', tooltip = '如果“创建警报”对话框的“消息”字段包含{{strategy.order.alert_message}}占位符,则警报消息将用此文本替换占位符。')
var string  Message_close_short     = input.string('CloseShort',    '平空消息', group = '===================exit警报=======================', tooltip = '如果“创建警报”对话框的“消息”字段包含{{strategy.order.alert_message}}占位符,则警报消息将用此文本替换占位符。')


////////========开仓处理==========//////////////
if (Open_long or Open_short) and barstate.isconfirmed
    Trade_vol := Trade_type == '合约' ? 1000 : 1000 / close // 设定交易量
    // 开多
    if Open_long
        strategy.entry('Long'
         , strategy.long
         , qty = Trade_vol
         , alert_message = Message_open_long  // 订单成交时发送的警报内容
         )
        State_position    := 1
    // 开空
    if Open_short
        strategy.entry('Short'
         , strategy.short
         , qty = Trade_vol
         , alert_message = Message_open_short  // 订单成交时发送的警报内容
         )
        State_position    := 0

然后在警报设置中使用{{strategy.order.alert_message}}方法

【求助】为什么TradingView策略发送的消息在FMZ无法执行?

FMZ平台接收到了消息,但是不开单 【求助】为什么TradingView策略发送的消息在FMZ无法执行?

请教一下这个问题是什么原因导致的,如何解决?

以下是FMZ代码

switch (command) {
        case 'OpenLong':        // 开多单
            _OpenPositions(1, 0, positions, account, ticker);
            break;
        case 'OpenShort':       // 开空单
            _OpenPositions(0, 1, positions, account, ticker);
            break;
        case 'CloseLong':       // TV信号平多
            if (positions.length > 0) {
                if (positions[0].Type === PD_LONG) {
                    _Cover_1();                                             // 快速平仓
                    _ResetVariables();                                             // 重置运算变量
                    _CalculateProfit(positions, account, ticker);           // 统计收益
                } else {
                    Log('当前持有空头仓位');
                }
            }
            else {
                Log('没有持仓,无法平多');
            }
            break;
        case 'CloseShort':      //TV信号平空
            if (positions.length > 0) {
                if (positions[0].Type === PD_SHORT) {
                    _Cover_1();                                             // 快速平仓
                    _ResetVariables();                                             // 重置运算变量
                    _CalculateProfit(positions, account, ticker);           // 统计收益
                } else {
                    Log('当前持有多头持仓');
                }
            } else {
                Log('没有持仓,无法平空');
            }
            break;
        default:
            break;
    }
相关推荐
全部留言
avatar of 发明者量化-小小梦
发明者量化-小小梦
您好,看您的截图中日志收到消息,策略不执行,应该和您的策略(FMZ平台上的)设计有关。 检查一下_OpenPositions函数中的代码,写一些Log函数,看这个函数是否执行。
2025-03-01 13:49:43
avatar of 豆豆
豆豆
谢谢,不太会写代码,将就着凑合用。
2025-04-20 23:20:32
avatar of 发明者量化-小小梦
发明者量化-小小梦
可以的,可以根据需求修改优化一下。
2025-04-01 11:51:02
avatar of 豆豆
豆豆
我后来重新处理了,把FMZ和TV的代码全部修改了,TV代码 ////////========警报消息========//////// Str_massage_group = '================-----------警报消息-----------================' var string Message_open_long = input.text_area('{"Action":"OpenLong"}', '开多消息', group = Str_massage_group, tooltip = "如果“创建警报”对话框的“消息”字段包含'{{strategy.order.alert_message}}'占位符,则警报消息将用此文本替换占位符。") var string Message_open_short = input.text_area('{"Action":"OpenShort"}', '开空消息', group = Str_massage_group, tooltip = "如果“创建警报”对话框的“消息”字段包含'{{strategy.order.alert_message}}'占位符,则警报消息将用此文本替换占位符。") var string Message_close_long = input.text_area('{"Action":"CloseLong"}', '平多消息', group = Str_massage_group, tooltip = "如果“创建警报”对话框的“消息”字段包含'{{strategy.order.alert_message}}'占位符,则警报消息将用此文本替换占位符。") var string Message_close_short = input.text_area('{"Action":"CloseShort"}', '平空消息', group = Str_massage_group, tooltip = "如果“创建警报”对话框的“消息”字段包含'{{strategy.order.alert_message}}'占位符,则警报消息将用此文本替换占位符。") // 多单出场 strategy.exit('E-Long' // 名称 , 'Long' // ID , stop = Price_SL , limit = Price_TP // , loss = Points_SL // 止损点数(通过止损百分比计算出来) // , profit = Points_TP // 止盈点数 , trail_points = Type_TP == 51 ? Points_trail : na // 追踪止盈开关 , trail_offset = Type_TP == 51 ? Points_trail_offset : na // 追踪止盈点数 , comment_loss = "L-SL" // 止损时绘制的图标名称 , comment_profit = "L-TP" // 止盈时绘制的图标名称 , comment_trailing = "L-TS" // 回调止盈时绘制的图标名称 , alert_message = Message_close_long // 警报消息 ) //// 根据TV消息执行对应的操作 function _RunTVCommand(command) { //// var positions = _C(exchange.GetPositions); //获取当前交易所账户的持仓信息,并返回一个包含持仓信息的对象。 var account = _C(exchange.GetAccount); //获取交易所账户信息 var ticker = _C(exchange.GetTicker); //获取当前交易对的市场行情信息 var action = command.Action; //获取要执行的处理 if (positions.length > 1) { ////如果同时持有多空仓位则退出 Log(positions); throw '同时有多空持仓!无法操作'; } switch (action) { case "OpenLong": // 开多单 _OpenPositions(1, 0, positions, account, ticker); break; case "OpenShort" : // 开空单 _OpenPositions(0, 1, positions, account, ticker); break; case "CloseLong" : // TV信号平多 if (positions.length > 0) { if (positions[0].Type === PD_LONG) { _Cover_1(); // 快速平仓 _ResetVariables(); // 重置运算变量 _CalculateProfit(positions, account, ticker); // 统计收益 } else { Log('当前持有空头仓位'); } } else { Log('没有持仓,无法平多'); } break; case "CloseShort" : //TV信号平空 if (positions.length > 0) { if (positions[0].Type === PD_SHORT) { _Cover_1(); // 快速平仓 _ResetVariables(); // 重置运算变量 _CalculateProfit(positions, account, ticker); // 统计收益 } else { Log('当前持有多头持仓'); } } else { Log('没有持仓,无法平空'); } break; default: break; } } 处理的方法是不在使用之前自己写的openshort这种简单的信号,而是用更接近TV输出的格式。然后在FMZ进行解析。
2025-04-01 11:11:40