Pine Script如果上一笔交易亏损,将仓位大小加倍(马丁格尔)。

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

Pine Script double position size if the last trade was in loss (Martingale)

问题

以下是翻译好的代码部分:

//开放交易
//
//
// 计算当前仓位大小
var float positionSize = strategy.position_size != 0 ? strategy.position_size : na
if (not na(positionSize))
    positionSize := positionSize >= 0 ? positionSize : -positionSize

// 确定上一笔交易是否亏损
var bool lastTradeLoss = strategy.losstrades[0] > strategy.losstrades[1]

// 确定仓位大小乘数
var float multiplier = lastTradeLoss ? 2.0 : 1.0

// 计算新的仓位大小
var float newPositionSize = multiplier * positionSize

if (buy and strategy.position_size <= 0)
    strategy.entry("Long", strategy.long, qty=newPositionSize)
else if (buy and strategy.position_size >= 0)
    strategy.entry("Long", strategy.long, qty=positionSize)

if (sell and strategy.position_size >= 0)
    strategy.entry("Short", strategy.short, qty=newPositionSize)
else if (sell and strategy.position_size <= 0)
    strategy.entry("Short", strategy.short, qty=positionSize)

strategy.exit("Exit Long", from_entry="Long", profit=600, loss = 2000)
strategy.exit("Exit Short", from_entry="Short", profit=600, loss = 2000)
英文:

My code did not make its function to enter with double position size if the last trade was in loss. can you figure my mistake?


//Open Trades
//
//
// Calculate the current position size
var float positionSize = strategy.position_size != 0 ? strategy.position_size : na
if (not na(positionSize))
    positionSize := positionSize &gt;= 0 ? positionSize : -positionSize

// Determine if the last trade was a loss
var bool lastTradeLoss = strategy.losstrades[0] &gt; strategy.losstrades[1]

// Determine the position size multiplier
var float multiplier = lastTradeLoss ? 2.0 : 1.0

// Calculate the new position size
var float newPositionSize = multiplier * positionSize

if (buy and strategy.position_size &lt;= 0)
    strategy.entry(&quot;Long&quot;, strategy.long, qty=newPositionSize)
else if (buy and strategy.position_size &gt;= 0)
    strategy.entry(&quot;Long&quot;, strategy.long, qty=positionSize)

if (sell and strategy.position_size &gt;= 0)
    strategy.entry(&quot;Short&quot;, strategy.short, qty=newPositionSize)
else if (sell and strategy.position_size &lt;= 0)
    strategy.entry(&quot;Short&quot;, strategy.short, qty=positionSize)

strategy.exit(&quot;Exit Long&quot;, from_entry=&quot;Long&quot;, profit=600, loss = 2000)
strategy.exit(&quot;Exit Short&quot;, from_entry=&quot;Short&quot;, profit=600, loss = 2000)

答案1

得分: 1

  1. 你在错误的地方使用了var声明。你可能并不完全了解它的工作原理。var声明允许你只初始化一次变量,并在接下来的柱子上保持这个值不变,直到下次有意的重新分配,对此有一个单独的运算符。
  2. 你试图通过比较当前柱和上一个柱上的亏损交易数量来识别亏损交易。你的条件只会在亏损交易退出的柱上为真。在亏损交易之后再次进入交易时,不能保证这个条件会再次为真。尝试检查上一个关闭交易的利润是否为负数,这样效果会更好:strategy.closedtrades.profit(strategy.closedtrades-1) < 0
英文:
  1. You're using a var declaration in the wrong place. You probably don't fully understand how it works. The var declaration allows you to initialize a variable only once, and keep this value in the series unchanged on bars until the next deliberate reassignment, for which there is a separate operator.
  2. You are trying to identify a losing trade by comparing the number of losing trades on the current and previous bar. Your condition will only be true on the exit bar of a losing trade. There is no guarantee that this condition will be true the next time you enter a trade after a losing one.
    Try to check if the profit of the previous closed trade was negative, it will work better strategy.closedtrades.profit(strategy.closedtrades-1) &lt; 0

huangapple
  • 本文由 发表于 2023年4月11日 00:54:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75979020.html
匿名

发表评论

匿名网友

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

确定