如何计算连胜?

huangapple go评论82阅读模式
英文:

How To Calculate Streaks?

问题

我只是试图计算所有最大赢家和最大输家的交易数量,以及最大赢家和最大输家的百分比,并输出这些变量。这就是我要做的全部。

这是我在代码中完成的部分,但它不起作用,我对如何计算百分比感到困惑。

谢谢!

英文:

I am simply trying to tally up all the biggest winners and biggest losers, of both a number of winning and losing trades, and also the biggest size in a % and biggest lose in a %, and have those output variables. That's all I am trying to do.

This is as far as I got with my code, but it's not working, and I'm stumped how to get the percentage.

Thanks!

var int WinningStreak = 0
var int LosingStreak = 0
var int MaximumWinningStreak = 0
var int MaximumLosingStreak = 0
 
if strategy.position_size > 0 // Win!
    WinningStreak := WinningStreak + 1
    LosingStreak := 0
    if WinningStreak > MaximumWinningStreak
        MaximumWinningStreak := WinningStreak
 
else if strategy.position_size < 0 // Lose!
    LosingStreak := LosingStreak + 1
    WinningStreak := 0
    if LosingStreak > MaximumLosingStreak
        MaximumLosingStreak := LosingStreak
else // Reset When There Are No Open Positions To Win Or Lose.
    WinningStreak := 0
    LosingStreak := 0
 

//Calculate biggest winner and biggest loser %.

BiggestWinningTradePct = ?_?
BiggestLosingTradePct = ?_?

// Plot results
plot(MaximumWinningStreak, title="Max Win Streak", color=color.green)
plot(MaximumLosingStreak, title="Max Lose Streak", color=color.red)
plot(BiggestWinningTradePct, title="Biggest Winning Trade Size %", color=color.purple)
plot(BiggestLosingTradePct, title="Biggest Losing Trade Size %", color=color.blue)

答案1

得分: 1

在Pinescript中,strategy.position_size提供了实际仓位的大小,但无法确定交易是盈利还是亏损。你应该使用strategy.closedtrades和strategy.closedtrades(index).profit来了解你已关闭的交易是否盈利或亏损。

你可以像这样操作:

if barstate.isrealtime
    SuccessiveWin = 0
    SuccessiveLoss = 0
    MaxStreakWin = 0
    MaxStreakLoss = 0
    if strategy.closedtrades > 1
        for i = 0 to strategy.closedtrades - 1
            if strategy.closedtrades.profit(i) > 0
                SuccessiveWin := SuccessiveWin + 1
                if SuccessiveWin > MaxStreakWin
                    MaxStreakWin := SuccessiveWin
                SuccessiveLoss := 0
            else if strategy.closedtrades.profit(i) < 0
                SuccessiveLoss := SuccessiveLoss + 1
                if SuccessiveLoss > MaxStreakLoss
                    MaxStreakLoss := SuccessiveLoss
                SuccessiveWin := 0
label.new(bar_index, close, " MaxStreakWin = " + str.tostring(MaxStreakWin), yloc=yloc.abovebar)
label.new(bar_index, close, " MaxStreakLoss = " + str.tostring(MaxStreakLoss), yloc=yloc.belowbar)

这样你将获得类似下面这样的结果:
注意:一旦策略开始运行,你必须等待一个新的K线出现才能看到标签MaxStreakWin和MaxStreakLoss出现(可以使用短时间框架进行测试,以查看标签是否以正确的值显示出来)。

英文:

in Pinescript strategy.position_size give the position of the actual size, you can't know with this if the trade will be a win or a loss.
You should use strategy.closedtrades and strategy.closedtrades(index).profit to know if your closed trades are a win or a loss.

You can do like this for example :

if barstate.isrealtime
    SuccessiveWin = 0
    SuccessiveLoss = 0
    MaxStreakWin = 0
    MaxStreakLoss = 0
    if strategy.closedtrades &gt; 1
        
        for i = 0 to strategy.closedtrades - 1
            if strategy.closedtrades.profit(i) &gt; 0
                SuccessiveWin := SuccessiveWin + 1
                if SuccessiveWin &gt; MaxStreakWin
                    MaxStreakWin := MaxStreakWin + 1
                SuccessiveLoss := 0
            else if strategy.closedtrades.profit(i) &lt; 0
                SuccessiveLoss := SuccessiveLoss + 1
                if SuccessiveLoss &gt; MaxStreakLoss
                    MaxStreakLoss := MaxStreakLoss + 1
                SuccessiveWin := 0
    label.new(bar_index, close, &quot; MaxStreakWin = &quot;+ str.tostring(MaxStreakWin), yloc=yloc.abovebar)
    label.new(bar_index, close, &quot; MaxStreakLoss = &quot;+ str.tostring(MaxStreakLoss), yloc=yloc.belowbar) 

You will get something like this :
NB : Once the strategy is one, you must wait for a NEW bar to open to see the labels MaxStreakWin and MaxStreakLoss appears (test it with a short timeframe to see if the labels shop up with the correct value).

如何计算连胜?

huangapple
  • 本文由 发表于 2023年4月17日 06:07:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76030556.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定