简体   繁体   中英

Pine Script - Unable to write a Strategy.exit with my logic

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())

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.

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())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM