input price = close;
input stdevLength = 5;
input avgOfStdevLength = 10;
input DYMILength = 30;
input DYMILengthLowerLimit = 3;
input DYMILengthUpperLimit = 30;
assert(DYMILengthLowerLimit > 0, "'dymi length lower limit' must be positive: " + DYMILengthLowerLimit);
assert(DYMILength between DYMILengthLowerLimit and DYMILengthUpperLimit, "'dymi length' must be between lower and upper limit: " + DYMILength);
def std = stdev(price, stdevLength);
def ratio = std / Average(std, avgOfStdevLength);
def dynamicLength = Floor(DYMILength / ratio);
def limitedLength = if dynamicLength between DYMILengthLowerLimit and DYMILengthUpperLimit then dynamicLength else 0;
def sf = 2 / (limitedLength + 1);
def bn = Max(barNumber(), 0);
# 10^-5 precision for ema multiplier
def expIndex = if limitedLength == 0 then 1 else max(1, bn - ceil(-5 / lg(1 - sf)));
def fromIndex = if isNan(expIndex) then 1 else expIndex;
def chg = price - price[1];
def absChg = AbsValue(chg);
def netChgAvg = fold indexN = fromIndex to bn + 1 with accuN do sf * (if isnan(getValue(chg, bn - indexN)) then 0 else getValue(chg, bn - indexN)) + (1 - sf) * accuN;
def totChgAvg = fold indexT = fromIndex to bn + 1 with accuT do sf * (if isnan(getValue(absChg, bn - indexT)) then 0 else getValue(absChg, bn - indexT)) + (1 - sf) * accuT;
def RSI = if totChgAvg != 0 and limitedLength != 0 then 50 * (netChgAvg / totChgAvg + 1) else RSI[1];
def DYMI = RSI;
def OverBought = 90;
def OverSold = 10;
Alert (DYMI>=OverBought, "OverBought", Alert.BAR, Sound.ring);
Alert (DYMI<=OverSold, "OverSold", Alert.BAR, Sound.ring);
plot DotDn = DYMI>=OverBought;
DotDn.setStyle(curve.POINTS);
DotDn.setPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
DotDn.setLineWeight(3);
DotDn.setDefaultColor(color.RED);
plot DotUp = DYMI<=OverSold;
Dotup.setStyle(curve.POINTS);
Dotup.setPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
Dotup.setLineWeight(3);
Dotup.setDefaultColor(color.GREEN);
alert(Dotup, "Scalper Buy Signal", Alert.BAR, Sound.ring);
alert(DotDn, "Scalper Buy Signal", Alert.BAR, Sound.ring);