You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Pine脚本策略对接WunderTrading:头寸单位适配问题问询

Pine脚本策略对接WunderTrading的头寸规模适配问题

我有一套回测表现稳定的Pine脚本策略,打算通过向WunderTrading发送警报实现自动化交易。策略核心逻辑是根据当前权益和每笔订单的风险百分比,计算自定义头寸规模后传入strategy.entry函数的qty参数。

但现在遇到了核心矛盾:WunderTrading会把所有qty值解读为现金金额,而非合约数量;但TradingView的strategy.entry函数默认是把qty当成合约数量来处理的。

举个实际例子就能明白:策略计算出某笔多头订单应该开1 ETH的头寸,WunderTrading收到后会理解成“购买价值1美元的以太坊”;如果我反过来把对应现金价值(比如1600美元)传入qty参数,TradingView又会把这个值当成合约数量,直接执行“购买1600 ETH”的操作——完全搞反了。

这两种情况的回测和实际执行结果天差地别,现在看起来没法让WunderTrading支持合约数量格式的订单,那有没有办法让TradingView接受以自定义现金金额为单位的订单?

更新1

有用户建议修改default_qty_type为strategy.cash,但测试后问题依旧。以下是两种模式的回测差异:

  • 合约格式运行的策略:回测表现符合预期
  • 修改default_qty_type为strategy.cash后:回测结果完全偏离预期

我怀疑自己处理头寸规模的代码存在根本性错误,所以打算贴出相关代码片段。

更新2

合约格式头寸规模计算代码

long_position_opened = strategy.position_size == 0 and long_condition
short_position_opened = strategy.position_size == 0 and short_condition
entry_price = ta.valuewhen(long_condition or short_condition, close, 0)
long_diff = (ta.valuewhen(long_position_opened, math.abs(stopvalue - entry_price), 0))
short_diff = (ta.valuewhen(short_position_opened, math.abs(entry_price - short_stopvalue),0))
balance = (strategy.initial_capital + strategy.netprofit)
balance_limited = (balance > 0 ? balance : 0)
equityrisked = (i_risk/100) * balance_limited
pos_size = (equityrisked / long_diff)
short_pos_size = (equityrisked / short_diff)

现金格式头寸规模计算代码

仅做了两处修改:

  1. 将策略的default_qty_type设置为strategy.cash
  2. 头寸规模的计算逻辑调整为:
pos_size = (equityrisked / long_diff) * entry_price
short_pos_size = (equityrisked / short_diff) * entry_price

内容的提问来源于stack exchange,提问作者Matt

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 08:30:35