declare upper;
input period = 45;
input stdev_length = 1;
input extrema_stdev_length = 20;
input extrema_stdev_number = 2;
################################################
def starttime = 0930;
def endtime = 1600;
def RTH = secondstilltime(endtime) >= 0 and secondsfromtime(starttime) >=0;
################################################
def vol = volume;
def op = open;
def cl = close;
def p = (cl - op) * vol;
def osc = EhlersSuperSmootherFilter(p, period);
def osc_diff = osc - osc[1];
def unclear_band = StDev(osc, stdev_length);
def extrema = extrema_stdev_number * StDev(osc, extrema_stdev_length);
def bullish_condition = RTH and Crosses(osc, unclear_band, CrossingDirection.ABOVE);
Alert(bullish_condition, "Bullish!", Alert.ONCE, Sound.Ding);
def bearish_condition = RTH and Crosses(osc, -unclear_band, CrossingDirection.BELOW);
Alert(bearish_condition, "Bearish!", Alert.ONCE, Sound.Chimes);
def out_bullish_trade_condition = RTH and Crosses(osc_diff, 0, CrossingDirection.BELOW);
Alert(out_bullish_trade_condition, "Take Profit on Bullish Trades!", Alert.ONCE, Sound.Ding);
def out_bearish_trade_condition = RTH and Crosses(osc_diff, 0, CrossingDirection.ABOVE);
Alert(out_bearish_trade_condition, "Take Profit on Bearish Trades!", Alert.ONCE, Sound.Chimes);
AddLabel(yes, if osc >= 0 and osc_diff >= 0 then "BUY" else if osc < 0 and osc_diff < 0 then "SELL" else if osc >= 0 and osc_diff < 0 then "LONG OUT" else "SHORT OUT", if osc >= 0 and osc_diff >= 0 then Color.GREEN else if osc < 0 and osc_diff < 0 then Color.RED else if osc >= 0 and osc_diff < 0 then Color.DARK_GREEN else Color.YELLOW);
def long_sl = close - ATR();
def short_sl = close + ATR();
AddChartBubble(bullish_condition, long_sl, "Long " + close + " " + "stop loss " + long_sl, color=Color.GREEN, up=no);
AddChartBubble(bearish_condition, short_sl, "Short " + close + " " + "stop loss " + short_sl, color=Color.RED, up=yes);
plot bull = bullish_condition;
bull.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
bull.SetDefaultColor(Color.GREEN);
plot bear = bearish_condition; bear.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
bear.SetDefaultColor(Color.RED);