Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
567 views
in Technique[技术] by (71.8m points)

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.

question from:https://stackoverflow.com/questions/65947929/pine-script-strategy-how-to-set-a-simultaneous-stop-exit-for-100-and-take-exi

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

try this one:

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...