//@version=5 indicator('Darvas Box Buy Sell[iceking2023]', overlay=true) boxp = input.int(defval=5, title='Darvas Box Length', minval=1, maxval=500) tpMultiplier = input.float(defval=2, title='Take Profit Multiplier', minval=1) slCol = input.color(color.red, 'Stop Loss Color') tpCol = input.color(color.lime, 'Take Profit Color') slW = input.int(3, 'Stop Loss Line Width') tpW = input.int(3, 'Take Profit Line Width') LL = ta.lowest(low, boxp) k1 = ta.highest(high, boxp) k2 = ta.highest(high, boxp - 1) k3 = ta.highest(high, boxp - 2) NH = ta.valuewhen(high > k1[1], high, 0) box1 = k3 < k2 TopBox = ta.valuewhen(ta.barssince(high > k1[1]) == boxp - 2 and box1, NH, 0) BottomBox = ta.valuewhen(ta.barssince(high > k1[1]) == boxp - 2 and box1, LL, 0) plot(TopBox, linewidth=2, color=color.new(#4CAF50, 0), title='TBbox') plot(BottomBox, linewidth=2, color=color.new(#FF0000, 0), title='BBbox') var bool buyActive = false var bool sellActive = false Buy = ta.crossover(close, TopBox) and not buyActive and not sellActive Sell = ta.crossunder(close, BottomBox) and not sellActive and not buyActive SL_Buy = ta.valuewhen(Buy, open, 0) SL_Sell = ta.valuewhen(Sell, open, 0) TP_Buy = ta.valuewhen(Buy, close + (close - open) * tpMultiplier, 0) TP_Sell = ta.valuewhen(Sell, close - (open - close) * tpMultiplier, 0) var line slLine = na var line tpLine = na var label lslLab = na var label ltpLab = na if Buy buyActive := true sellActive := false line.delete(slLine) line.delete(tpLine) label.delete(lslLab) label.delete(ltpLab) slLine := line.new(x1=bar_index[1], y1=SL_Buy, x2=bar_index, y2=SL_Buy, color=slCol, width = slW, extend=extend.right) tpLine := line.new(x1=bar_index[1], y1=TP_Buy, x2=bar_index, y2=TP_Buy, color=tpCol, width = tpW, extend=extend.right) lslLab := label.new(bar_index[1]+20,SL_Buy, "Long SL", textcolor=color.black, style=label.style_label_center, color=slCol) ltpLab := label.new(bar_index[1]+20,TP_Buy, "Long TP", textcolor=color.black, style=label.style_label_center, color=tpCol) if Sell sellActive := true buyActive := false line.delete(slLine) line.delete(tpLine) label.delete(lslLab) label.delete(ltpLab) slLine := line.new(x1=bar_index[1], y1=SL_Sell, x2=bar_index, y2=SL_Sell, color=slCol, width = slW, extend=extend.right) tpLine := line.new(x1=bar_index[1], y1=TP_Sell, x2=bar_index, y2=TP_Sell, color=tpCol, width = tpW, extend=extend.right) lslLab := label.new(bar_index[1],SL_Sell, "Short SL", textcolor=color.black, style=label.style_label_center, color=slCol) ltpLab := label.new(bar_index[1],TP_Sell, "Short TP", textcolor=color.black, style=label.style_label_center, color=tpCol) if (buyActive and (close >= TP_Buy or close <= SL_Buy)) or (sellActive and (close <= TP_Sell or close >= SL_Sell)) buyActive := false sellActive := false line.delete(slLine) line.delete(tpLine) label.delete(lslLab) label.delete(ltpLab) alertcondition(Buy, title='Buy Signal', message='Buy') alertcondition(Sell, title='Sell Signal', message='Sell') alertcondition(close>=TP_Buy, 'Buy TP', 'Buy Position Take Profit Hit') alertcondition(close<=SL_Buy, 'Buy SL', 'Buy Position Stop Loss Hit') alertcondition(close<=TP_Sell,'Sell TP','Sell Position Take Profit Hit') alertcondition(close>=SL_Sell, 'Sell SL','Sell Position Stop Loss Hit') plotshape(Buy, style=shape.labelup, location=location.belowbar, color=color.new(#4CAF50, 0), size=size.tiny, title='Buy Signal', text='Buy', textcolor=color.new(color.black, 0)) plotshape(Sell, style=shape.labeldown, location=location.abovebar, color=color.new(#FF0000, 0), size=size.tiny, title='Sell Signal', text='Sell', textcolor=color.new(color.white, 0))