//@version=4 study("DMG-signals", overlay=true) inp_src = input(title="TBX по ", type=input.source, defval=open) length = timeframe.isintraday and timeframe.multiplier >= 1 ? 1440 / timeframe.multiplier * 7 : timeframe.isintraday and timeframe.multiplier < 60 ? 60 / timeframe.multiplier * 24 * 7 : 7 dev = stdev(close, length) upper = highest(close, length)[1] lower = lowest(close, length)[1] upper1 = upper + dev lower1 = lower - dev highRatio = (close - lower1) / (upper1 - lower1) * 500 lowRatio = -(upper1 - close) / (upper1 - lower1) * 500 z = highRatio + lowRatio longCond = crossover(z, 100) shortCond = crossunder(z, -100) sectionLongs = 0 sectionLongs := nz(sectionLongs[1]) sectionShorts = 0 sectionShorts := nz(sectionShorts[1]) if longCond sectionLongs := sectionLongs + 1 sectionShorts := 0 if shortCond sectionLongs := 0 sectionShorts := sectionShorts + 1 longCondition = longCond and sectionLongs <= 1 shortCondition = shortCond and sectionShorts <= 1 last_open_longCondition = float(na) last_open_shortCondition = float(na) last_open_longCondition := longCondition ? open : nz(last_open_longCondition[1]) last_open_shortCondition := shortCondition ? open : nz(last_open_shortCondition[1]) last_longCondition = float(na) last_shortCondition = float(na) last_longCondition := longCondition ? time : nz(last_longCondition[1]) last_shortCondition := shortCondition ? time : nz(last_shortCondition[1]) in_longCondition = last_longCondition > last_shortCondition in_shortCondition = last_shortCondition > last_longCondition plotshape(longCondition, title="buy alert", color=color.green, textcolor=color.green, transp=0, style=shape.triangleup, location=location.belowbar, size=size.small, text="LONG", offset=0) plotshape(shortCondition, title="sell alert", color=color.red, textcolor=color.red, transp=0, style=shape.triangledown, location=location.abovebar, size=size.small, text="SHORT", offset=0)