0
votes

I'm trying to program 3 take profit targets in a pine strategy.

In the below code, when the condition to go long is triggered, an entry is opened with a quantity of 100. This works fine. The values for valueTakeProfit1, 2 and 3 are also correct. But once I put them into a strategy.exit(), they are or not triggered or the profits are taking at wrong levels. The values for variables ATR1/2/3percent are not percentages, it's just the amount taken from the 100 which are opened via the strategy.entry().

if(longCondition or re_entryCondition)
    alertLine := 1
    strategy.entry(id="Long Entry", long=true, when=alertLine==1, qty=100)
    valueTakeProfit1 := close+ATR1*ma_function(tr(true), lengthATR)
    valueTakeProfit2 := close+ATR2*ma_function(tr(true), lengthATR)
    valueTakeProfit3 := close+ATR3*ma_function(tr(true), lengthATR)
    strategy.exit("Take Profit 1 Long", from_entry="Long Entry", limit=valueTakeProfit1, qty=ATR1percent)
    strategy.exit("Take Profit 2 Long", from_entry="Long Entry", limit=valueTakeProfit2, qty=ATR2percent)
    strategy.exit("Take Profit 3 Long", from_entry="Long Entry", limit=valueTakeProfit3, qty=ATR3percent)


//End long positions
if(longCloseCondition)
    strategy.close(id="Long Entry")

Can someone help me out how I should define multiple take profit levels?

1
Using strategy.close_all(when=longCloseCondition) seems to do a better job, but the take profit levels are taken on the next candle and not the current candle in which the price is reaching the target. How can the profit be taken on the current candle?Greggy

1 Answers

0
votes

Ok, so I found the error. At the beginning of the script, I did declare the variables:

valueTakeProfit1 = 0.0
valueTakeProfit2 = 0.0
valueTakeProfit3 = 0.0

Before the if statement, I did reset the variables to zero:

valueTakeProfit1Short := 0.0
valueTakeProfit2Short := 0.0
valueTakeProfit3Short := 0.0

By declaring the variables with their proper value just before the if-statement, it did solve the problem:

valueTakeProfit1 = close +  (ATR1*ma_function(tr(true), lengthATR))
valueTakeProfit2 = close +  (ATR2*ma_function(tr(true), lengthATR))
valueTakeProfit3 = close + (ATR3*ma_function(tr(true), lengthATR))