pine-script - Pine Script Strategy - How to set a simultaneous STOP exit for 100% and TAKE exit for only 50%?
问题描述
I am entering a trade...
if (enterLong and strategy.position_size == 0)
strategy.entry("Long", true)
This works as expected and enters when I want it to. But from here I would like to have a couple different exits:
- If hit the INITIAL STOP -> Get out of the trade 100%
- If hit the FIRST TAKE -> Get 50% out of the trade, move the STOP to break even, and move the SECOND TAKE to new condition
Currently, I have a sort of working version of this...
if (strategy.position_size > 0 and strategy.position_size == initial_size)
strategy.exit("LT1", limit=longTakePrice, qty_percent=50)
strategy.close("Long", when=low<longStopPrice)
else
if (strategy.position_size > 0 and strategy.position_size < initial_size)
strategy.exit("LS2", stop=entryPrice)
strategy.close("Long", when = takeCondition)
This mostly works, except the close()
functions don't trigger until the next candle (instead of when the condition is met), and usually has significant (unrealistic) slippage.
I've tried the following...
if (strategy.position_size > 0 and strategy.position_size == initial_size)
strategy.exit("LT1", limit=longTakePrice, qty_percent=50)
strategy.exit("LS1", stop=longStopPrice)
But this doesn't work. Instead, when LS1
is hit, it only sells 50% of the position. Swapping the order of the statements doesn't work either.
Edit 1:
It makes no difference to add qty_percent=100
to the stop exit.
解决方案
试试这个:
if (strategy.position_size > 0 and strategy.position_size == initial_size)
strategy.exit("LT1", limit=longTakePrice, stop=longStopPrice, qty_percent=50)
strategy.exit("LS1", stop=longStopPrice)
推荐阅读
- r - 从 R 中的线性模型列表中映射 emmeans
- javascript - 如何从另一个任务运行器执行/捆绑 JS 与 Webpack?
- django - Django:在结果中填充用户可选择的数据库字段
- vue.js - 如何只添加一次全局分隔符以使其适用于所有组件?
- c# - 隐式转换问题(整数文字)
- powershell - Send-MailMessage 是否有 Integrated Security=SSPI 选项?
- javascript - 我怎样才能得到 [ Promise {
}]? - scala - 从 Spark 1.6 升级到 2.1 - 不兼容的类更改错误
- redis - 我的 redis 服务器每隔几分钟就会丢失一次密钥
- r - 汇总计数数据并返回 R 中因子的最高级别