如何在松木脚本中计算多头和空头的输赢交易

  • 本文关键字:交易 计算 脚本 pine-script
  • 更新时间 :
  • 英文 :


在pine脚本中,对于一个策略,我想得到的数字是:

  1. 做多交易
  2. 长期亏损交易
  3. 空头获利交易
  4. 空头交易

策略测试人员的性能摘要部分提供了这些信息,但在pine脚本中获得这些信息的代码是什么?

您可以使用内置变量。

strategy.wintrades: Number of profitable trades for the whole trading interval.
strategy.eventrades: Number of breakeven trades for the whole trading interval.
strategy.losstrades: Number of unprofitable trades for the whole trading interval.

下面是一个完整的例子:

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © vitruvius
//@version=5
strategy("My strategy", overlay=true, margin_long=100, margin_short=100)
sma = ta.sma(close, 50)
longCondition = ta.crossover(close, sma)
shortCondition = ta.crossunder(close, sma)
pnl = 100000
if (longCondition)
strategy.entry("Long", strategy.long)
if (strategy.position_size > 0)
strategy.exit("Long Exit", "Long", profit=pnl, loss=pnl, comment_profit="TP", comment_loss="SL")
if (shortCondition)
strategy.entry("Short", strategy.short)
if (strategy.position_size < 0)
strategy.exit("Short Exit", "Short", profit=pnl, loss=pnl, comment_profit="TP", comment_loss="SL")
var long_win = 0
var long_loss = 0
var short_win = 0
var short_loss = 0
is_pos_closed = (strategy.position_size[1] != 0 and strategy.position_size == 0) or ((strategy.position_size[1] * strategy.position_size) < 0)
was_long = strategy.position_size[1] > 0
was_short = strategy.position_size[1] < 0
is_win = strategy.wintrades > strategy.wintrades[1]
is_loss = strategy.losstrades > strategy.losstrades[1]
long_win := is_pos_closed and was_long and is_win ? long_win + 1 : long_win
long_loss := is_pos_closed and was_long and is_loss ? long_loss + 1 : long_loss
short_win := is_pos_closed and was_short and is_win ? short_win + 1 : short_win
short_loss := is_pos_closed and was_short and is_loss ? short_loss + 1 : short_loss
plot(sma)
plotchar(strategy.wintrades, "strategy.wintrades", "")
plotchar(strategy.losstrades, "strategy.losstrades", "")
plotchar(long_win, "long_win", "")
plotchar(long_loss, "long_loss", "")
plotchar(short_win, "short_win", "")
plotchar(short_loss, "short_loss", "")

最新更新