0

嗨,我是 pine 编辑器的新手,所以我需要你的帮助。

我制作了自己的指标,它提供多头和空头交易,现在效果很好但是我想知道在每笔交易结束时有多少利润并将其打印在图表上。

让我举个例子

我的指标给出了一个多头交易,这个多头交易终止了,因为机器人放置了一个空头头寸

如果有人可以通过示例向我解释此代码,我正在等待您的帮助。

4

1 回答 1

0

有一个内置变量strategy.closedtrades.profit可用于您的示例。您只需将其strategy.closedtrades-1作为索引(零索引)传递,这样您就可以获得最后关闭交易的利润。

这是一个例子:

//@version=5
strategy("My strategy", overlay=true, margin_long=100, margin_short=100)

smaFast = input.int(14)
smaSlow = input.int(28)

sma14 = ta.sma(close, smaFast)
sma28 = ta.sma(close, smaSlow)
plot(sma14, color=color.yellow, linewidth=2)
plot(sma28, linewidth=2)

longCondition = ta.crossover(sma14, sma28)
if (longCondition)
    strategy.entry("Long", strategy.long)

shortCondition = ta.crossunder(sma14, sma28)
if (shortCondition)
    strategy.entry("Short", strategy.short)

var label _l = na
if (strategy.position_size[1] != strategy.position_size)
    profit = strategy.closedtrades.profit(strategy.closedtrades-1)
    _l := label.new(bar_index, low, str.tostring(profit), yloc=yloc.abovebar)

在此处输入图像描述

您可以使用以下算法以百分比计算您的利润:

profitInPercent = 0.
for i = 0 to strategy.closedtrades-1
    entryP = strategy.closedtrades.entry_price(i)
    exitP = strategy.closedtrades.exit_price(i)
    profitInPercent += (exitP - entryP) / entryP * strategy.closedtrades.size(i)
avgProfitInPercent = strategy.closedtrades > 0 ? profitInPercent / strategy.closedtrades * 100 : na
于 2022-01-28T07:40:55.417 回答