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

如何在Pine Script中设置strategy.commission.percent百分比佣金?

在Pine Script中通过代码设置百分比佣金

当然可以通过代码来配置百分比佣金!完全不用依赖手动设置,直接在策略代码里就能搞定,这里给你两种常用的实现方式:

方法1:在strategy()函数声明中直接配置

这是最简洁的静态设置方式,在定义策略的初始语句里,通过两个参数指定佣金规则:

  • commission_type=strategy.commission.percent:明确告诉脚本我们要使用百分比模式的佣金计算
  • commission_value:填入具体的佣金百分比数值(比如0.1就代表每笔交易按成交额的0.1%收取佣金)

下面是修改后的你的完整策略代码,我已经把佣金配置添加到了strategy()函数中:

//@version=5
strategy("Working 55 & 200 EMA strategy", overlay = true, initial_capital=1000,
         commission_type=strategy.commission.percent, commission_value=0.1) // 新增佣金配置

fast = input(defval = 55, step = 5)
slow = input(200)

ma1 = ta.ema(close, fast)
ma2 = ta.ema(close, slow)

plot(ma1, title = "Fast MA", color = color.lime, style = plot.style_line, linewidth = 3)
plot(ma2, title = "Slow MA", color = color.black, style = plot.style_line, linewidth = 3)

// === INPUT BACKTEST RANGE ===
FromMonth = input(defval = 1, title = "From Month", minval = 1, maxval = 12)
FromDay = input(defval = 1, title = "From Day", minval = 1, maxval = 31)
FromYear = input(defval = 2018, title = "From Year", minval = 2010)
ToMonth = input(defval = 1, title = "To Month", minval = 1, maxval = 12)
ToDay = input(defval = 1, title = "To Day", minval = 1, maxval = 31)
ToYear = input(defval = 9999, title = "To Year", minval = 2017)

// === FUNCTION EXAMPLE ===
start = timestamp(FromYear, FromMonth, FromDay, 00, 00) // backtest start window
finish = timestamp(ToYear, ToMonth, ToDay, 23, 59) // backtest finish window
window() => time >= start and time <= finish ? true : false // create function "within window of time"

strategy.entry("Buy", strategy.long, when = window() and ta.crossover(ma1, ma2))
strategy.entry("Sell", strategy.short, when = window() and ta.crossover(ma2, ma1))
strategy.exit("Buy")
strategy.exit("Sell")

方法2:使用strategy.commission.set_percent()动态调整

如果你的策略需要根据特定条件(比如不同时间段、不同品种)切换佣金率,可以用这个函数实现动态配置:

// 示例:2020年之后佣金率调整为0.2%,之前保持0.1%
if time >= timestamp(2020, 1, 1, 0, 0)
    strategy.commission.set_percent(0.2)
else
    strategy.commission.set_percent(0.1)

重要注意事项

TradingView的策略佣金是单边收取的:开仓时会按成交额的对应百分比收一次佣金,平仓时还会再收一次。如果你的实际佣金是双边总费率,记得把设置的数值减半(比如实际双边总佣金0.2%,就设置commission_value=0.1)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 06:58:23