In true TradingView spirit, the author of this script has published it open-source, so traders can understand and verify it. Cheers to the author! You may use it for free, but reuse of this code in a publication is governed by House Rules. You can favorite it to use it on a chart.
strategy(title="Mbit Moving Average",overlay=true) length = input(520) confirmBars = input(27) price = close ma = sma(price, length) bcond = price > ma bcount = bcond ? nz(bcount) + 1 : 0 scond = price < ma scount = scond ? nz(scount) + 1 : 0 long = scount == confirmBars short = bcount == confirmBars //Strategy strategy.entry("long", strategy.long, when=long) strategy.entry("short",strategy.short, when=short)