英文:
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 > 1
for i = 0 to strategy.closedtrades - 1
if strategy.closedtrades.profit(i) > 0
SuccessiveWin := SuccessiveWin + 1
if SuccessiveWin > MaxStreakWin
MaxStreakWin := MaxStreakWin + 1
SuccessiveLoss := 0
else if strategy.closedtrades.profit(i) < 0
SuccessiveLoss := SuccessiveLoss + 1
if SuccessiveLoss > MaxStreakLoss
MaxStreakLoss := MaxStreakLoss + 1
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)
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).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论