My buying conditions for taking long position are
Place order between 09:15 to 13:30
opening price of recent candle greater than EMA(20)
I will go for long postion when price crosses the high of last 7 candles
After placing a buy order with strategy.entry, I have three targets (+20, +40, +60) and one stop loss price (-20). I would like to exit order with following conditions
- Sell whole quantity at Stop Loss (-20) or Sell 25% at Target 1 (+20) and Trail SL to buy price (0)
- Sell remaining 75% at buy price (trailed SL) (0) or Sell another 25% at Target 2 (+40) and Trail SL to Target 1 (+20)
- Sell final 50% at Target 1 (trailed SL) (+20) or Target 3 (+60)
In view of the above please review the code and tell me the what is going on wrong here?
//@version=4
strategy("BreakOutStrategy", overlay=true)
length = input(title="Length", type=input.integer, minval=1, maxval=1000, defval=7)
upBound = highest(high, length)
downBound = lowest(low, length)
timeFilter = (year == 2019) and (time > 0915 and time < 1330)
c1 = (open>ema(hl2,20))
buy_entry = (c1==true) and (timeFilter==true)
strategy.entry("buy", strategy.long, 4, stop=upBound + syminfo.mintick, when=buy_entry and strategy.position_size <= 0, comment="BUY")
strategy.exit("bracket1", "buy", stop=20, when=strategy.position_size==4)
strategy.exit("bracket2", "buy", 1, profit=20, when=strategy.position_size==4)
strategy.exit("bracket3", "buy", stop=0, when=strategy.position_size==3)
strategy.exit("bracket4", "buy", 1, profit=40, when=strategy.position_size==3)
strategy.exit("bracket5", "buy", 2, profit=60, stop=-20, when=strategy.position_size==2)
strategy.exit()
function? - Baris Yakut