Avtor
Avtor

Reputation: 1

Pine Script Multiple Exits with Conditions from trade

I created an indicator that works only for long and shows enters and exits(take-profits) for my position.But during development of strategy based on indicator i faced several problems :

1.When i try to setup two different exits for strategy - stop-loss and take-profit, only one of exits works(take-profit triggers when condition to exit trade is true, condition becomes true when several indicators give signals to exit)

2.If i try to setup stop-loss it most of the time works incorrect - i want to exit from trade by stop-loss by the price of current candle's low,but stop-loss triggers when stop price condition(ta.lowest(low,20)) is true for future candle and not sticks for the one on which i entered to trade

Desired point of stop-loss is marked green and undesired marked red

Here is example code for my strategy :

// Long enter conditions
emaConditionE = false  
macdConditionE = false    
rsiConditionE = false   
buyConditionE = false   

buyConditionEnter := emaConditionE and 
     macdConditionE and 
     rsiConditionE and 

stopPrice = ta.lowest(low,20)

// Long escape conditions
buyConditionEscape := macdConditionEs and rsiConditionEs 

// Strategy Orders
if (buyConditionEnter and inTradeWindow) 
    qty = [enter image description here](https://i.sstatic.net/c2XjW.png)
    strategy.entry("ENTER", strategy.long,qty,limit = high,stop = stopPrice)
if(buyConditionEscape and inTradeWindow)
    strategy.exit(id = "TAKE" ,from_entry = "ENTER",limit = low)


strategy.exit(id = "STOP LOSS" ,from_entry = "ENTER",stop = stopPrice)

I tried to unite stop-loss with entry strategy.order("ENTER", strategy.long,qty,limit = high,stop = stopPrice) but that still didn't work because my stop-loss won't trigger

Upvotes: 0

Views: 560

Answers (1)

Andrey D
Andrey D

Reputation: 1714

You need use one strategy.exit call like this

// Long enter conditions
emaConditionE = false  
macdConditionE = false    
rsiConditionE = false   
buyConditionE = false   

buyConditionEnter := emaConditionE and 
     macdConditionE and 
     rsiConditionE and 

stopPrice = ta.lowest(low,20)

// Long escape conditions
buyConditionEscape := macdConditionEs and rsiConditionEs 

// Strategy Orders
if (buyConditionEnter and inTradeWindow) 
    qty = [enter image description here](https://i.sstatic.net/c2XjW.png)
    strategy.entry("ENTER", strategy.long,qty,limit = high,stop = stopPrice)

strategy.exit(id = "STOP LOSS" ,from_entry = "ENTER",stop = stopPrice,limit = buyConditionEscape and inTradeWindow ? low : na)

Upvotes: 0

Related Questions