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

如何在Pine Script中指定出入场时间/价格,用于TW策略复盘分析?

在Pine Script中实现指定出入场并计算交易绩效数据

一、核心实现思路

通过手动定义精确的日期时间+价格条件触发出入场,持仓期间实时跟踪价格波动计算MFE(最大有利偏移)、MAE(最大不利偏移),离场时统计持仓时长、K线数等绩效数据。

二、完整代码示例

//@version=5
strategy("指定出入场与绩效分析", overlay=true, initial_capital=10000, default_qty_type=strategy.cash, default_qty_value=10000, commission_type=strategy.commission.percent, commission_value=0.1)

// 1. 自定义出入场条件(指定日期时间+价格阈值)
entryCondition = time >= timestamp("2024-05-01T09:30:00") and time < timestamp("2024-05-01T09:31:00") and close >= open
exitCondition = time >= timestamp("2024-05-02T15:00:00") and time < timestamp("2024-05-02T15:01:00") and close <= strategy.position_avg_price * 1.02

// 2. 初始化持仓跟踪变量
var float entryPrice = na
var int entryBar = na
var float maxFavorable = na
var float maxAdverse = na
var int mfeBar = na
var int maeBar = na

// 入场时重置跟踪数据
if (entryCondition and strategy.position_size == 0)
    strategy.entry("Long", strategy.long)
    entryPrice := strategy.position_avg_price
    entryBar := bar_index
    maxFavorable := na
    maxAdverse := na
    mfeBar := na
    maeBar := na

// 持仓期间实时计算MFE/MAE
if (strategy.position_size > 0)
    currentProfitPct = (close - entryPrice) / entryPrice * 100
    // 更新最大浮盈(MFE)及对应K线
    if (na(maxFavorable) or currentProfitPct > maxFavorable)
        maxFavorable := currentProfitPct
        mfeBar := bar_index
    // 更新最大浮亏(MAE)及对应K线
    if (na(maxAdverse) or currentProfitPct < maxAdverse)
        maxAdverse := currentProfitPct
        maeBar := bar_index

// 离场时统计并输出绩效数据
if (exitCondition and strategy.position_size > 0)
    strategy.close("Long")
    holdTimeSec = (time - time[bar_index - entryBar]) / 1000
    holdBarsCount = bar_index - entryBar
    mfeDelayBars = mfeBar - entryBar
    maeDelayBars = maeBar - entryBar

    // 绘制统计表格
    var table statsTable = table.new(position.bottom_right, 6, 2, title="交易绩效统计")
    table.cell(statsTable, 0, 0, "入场价格")
    table.cell(statsTable, 1, 0, "MFE(%)")
    table.cell(statsTable, 2, 0, "MAE(%)")
    table.cell(statsTable, 3, 0, "持仓时长(秒)")
    table.cell(statsTable, 4, 0, "持仓K线数")
    table.cell(statsTable, 5, 0, "MFE出现K线")
    
    table.cell(statsTable, 0, 1, str.tostring(entryPrice, "%.2f"))
    table.cell(statsTable, 1, 1, str.tostring(maxFavorable, "%.2f"))
    table.cell(statsTable, 2, 1, str.tostring(maxAdverse, "%.2f"))
    table.cell(statsTable, 3, 1, str.tostring(holdTimeSec, "%.0f"))
    table.cell(statsTable, 4, 1, str.tostring(holdBarsCount, "%.0f"))
    table.cell(statsTable, 5, 1, str.tostring(mfeDelayBars, "%.0f"))

// 标记出入场点
plotshape(entryCondition, title="入场", location=location.belowbar, color=color.green, style=shape.triangleup, size=size.small)
plotshape(exitCondition, title="离场", location=location.abovebar, color=color.red, style=shape.triangledown, size=size.small)

三、关键逻辑说明

  • 精确出入场:用timestamp()锁定具体时间窗口,搭配价格条件(如close >= open)触发交易,可根据需求修改时间戳和价格规则。
  • MFE/MAE计算:持仓中持续对比当前浮盈百分比,更新最大浮盈/浮亏值,并记录对应的K线位置,用于计算指标出现时间。
  • 绩效统计:离场时通过时间差计算持仓时长(转换为秒),通过K线索引差计算持仓K线数,同时得出MFE/MAE与入场的间隔K线数。
  • 可视化:用图形标记出入场位置,右下角表格展示统计数据,便于直观验证。

四、自定义扩展建议

  • 多笔交易跟踪:将单变量改为数组(如var array.float entryPrices = array.new_float()),存储每笔交易的关键数据。
  • 条件扩展:出入场可结合技术指标(如MA、RSI)与时间条件共同触发,或设置多个时间窗口。
  • 数据导出:利用strategy的日志功能,或通过变量累积数据后批量输出。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 10:52:55