1
votes

I tried to write a strategy script in the Pine editor.

  • The strategy.entry() is working as I expected.
  • But the strategy.exit() is not working as per my logic.
  • My exit calculation is high minus low of the previous candle.

I am new to Pine. Below is my code. Can someone point out what is wrong?

    Short = ( ( close[1] > open[1] ) and ( high < high[1] ) and ( close < low[1] ) )
    if ( Short )
        Target = high[1] - low[1]
        Loss = high[1] - low[1]
        strategy.entry("Enter Short", strategy.short, 1, when = window() )
        strategy.exit("Enter Short", "Enter Short", stop=Loss, limit=Target, when = window())
2
Are you sure you have a correct formatting, i.e. 4 spaces for the block inside of the if statement?Palo
Hi Palo, Yes. I am thinking somehow the variable(Target, Loss) value not passing into strategy.exit(). Am I missing something to pass a price value into the strategy.exit() for stop and limit?Rajasekaran Rajendran
Hi Palo, Achieved using following code ================================================ 'Target = valuewhen(( ( close[1] > open[1] ) and ( high < high[1] ) and ( close < low[1] ) ), high[1] - low[1], 0 ) strategy.exit("Enter Short", "Enter Short", stop=strategy.position_avg_price+Target, limit=strategy.position_avg_price-Target, when = window())'Rajasekaran Rajendran
You can write it as an answer, and accept it.Palo

2 Answers

0
votes

According to pine script manual: when (bool) An optional parameter. Condition of the order. The order is placed if condition is 'true'. If condition is 'false', nothing happens (the previously placed order with the same ID is not cancelled). Default value is 'true'. So I think that you have not fulfilled the condition window (). Use strategy.close() instead.

0
votes

Answer

Target = valuewhen(( ( close[1] > open[1] ) and ( high < high[1] ) and ( close < low[1] ) ), high[1] - low[1], 0 ) strategy.exit("Enter Short", "Enter Short", stop=strategy.position_avg_price+Target, limit=strategy.position_avg_price-Target, when = window())