首页 > 解决方案 > 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:

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.

标签: pine-script

解决方案


试试这个:

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)

推荐阅读