英文:
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 >= 0 ? positionSize : -positionSize
// Determine if the last trade was a loss
var bool lastTradeLoss = strategy.losstrades[0] > 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 <= 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)
答案1
得分: 1
- 你在错误的地方使用了var声明。你可能并不完全了解它的工作原理。var声明允许你只初始化一次变量,并在接下来的柱子上保持这个值不变,直到下次有意的重新分配,对此有一个单独的运算符。
- 你试图通过比较当前柱和上一个柱上的亏损交易数量来识别亏损交易。你的条件只会在亏损交易退出的柱上为真。在亏损交易之后再次进入交易时,不能保证这个条件会再次为真。尝试检查上一个关闭交易的利润是否为负数,这样效果会更好:
strategy.closedtrades.profit(strategy.closedtrades-1) < 0
。
英文:
- 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.
- 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 betterstrategy.closedtrades.profit(strategy.closedtrades-1) < 0
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论