##CREATED BY GASMOS ;)
## barbaros - 2021/03/23 - clean up
# SlowRSI
input emaLength = 5;
input rsiLength = 10;
input over_bought = 80;
input over_sold = 20;
# Breakout signals of SLOWRSI
input averageType = AverageType.WILDERS;
input showBreakoutSignals = no;
def ema = ExpAverage(close, emaLength);
def netChgAvg = WildersAverage(close - ema, rsiLength);
def totChgAvg = WildersAverage(AbsValue(close - ema), rsiLength);
def chgRatio = if totChgAvg != 0 then netChgAvg / totChgAvg else 0;
def SlowRSI = 50 * (chgRatio + 1);
def MiddleLine = 50;
# Moving Averages of SLOWRSI
input MALength = 9;
input AverageType1 = AverageType.WEIGHTED;
input MALength2 = 26;
input AverageType2 = AverageType.SIMPLE;
input MALength3 = 57;
input AverageType3 = AverageType.SIMPLE;
input MALength4 = 200;
input AverageType4 = AverageType.HULL;
def MA = MovingAverage(AverageType1, SlowRSI, MALength);
def pMA = MA;
def MA2 = MovingAverage(AverageType2, SlowRSI, MALength2);
def pMA2 = MA2;
def MA3 = MovingAverage (AverageType3, SlowRSI, MALength3);
def pMA3 = MA3;
def MA4 = MovingAverage(AverageType4, SlowRSI, MALength4);
def pMA4 = MA4;
# MomentumSMA
input price = close;
input momentumLength = 26;
input smaLength = 26;
input AverageType11 = AverageType.SIMPLE;
input smaLength2 = 100;
input AverageType22 = AverageType.SIMPLE;
Assert(momentumLength > 0, "'momentum length' must be positive: " + momentumLength);
def Momentum = price - price[momentumLength];
# MomentumSMA Moving averages
def MA11 = MovingAverage(AverageType11, Momentum, smaLength);
def pMA11 = MA11;
def MA22 = MovingAverage(AverageType22, Momentum, smaLength2);
def pMA22 = MA22;
# Z SCORE
input length = 20;
input ZavgLength = 26;
def oneSD = StDev(price, length);
def avgClose = SimpleMovingAvg(price, length);
def Zscorevalue = ((price - avgClose) / oneSD);
def Zscore = ((price - avgClose) / oneSD);
def avgZscore = Average(Zscorevalue, ZavgLength);
# DEMANDIndex
input lengthDI = 20;
def wClose = (high + low + 2 * close) * 0.25;
def wCRatio = (wClose - wClose[1]) / Min(wClose, wClose[1]);
def closeRatio = 3 * wClose / Average(Highest(high, 2) - Lowest(low, 2), lengthDI) * AbsValue(wCRatio);
def volumeRatio = volume / Average(volume, lengthDI);
def volumePerClose = volumeRatio / Exp(Min(88, closeRatio));
def buyP;
def sellP;
if (wCRatio > 0) {
buyP = volumeRatio;
sellP = volumePerClose;
} else {
buyP = volumePerClose;
sellP = volumeRatio;
}
def buyPres = if IsNaN(buyPres[1]) then 0 else ((buyPres[1] * (lengthDI - 1)) + buyP) / lengthDI;
def sellPres = if IsNaN(sellPres[1]) then 0 else ((sellPres[1] * (lengthDI - 1)) + sellP) / lengthDI;
def tempDI;
if ((((sellPres[1] * (lengthDI - 1)) + sellP) / length - ((buyPres[1] * (lengthDI - 1)) + buyP) / lengthDI) > 0) {
tempDI = - if (sellPres != 0) then buyPres / sellPres else 1;
} else {
tempDI = if (buyPres != 0) then sellPres / buyPres else 1;
}
def DMIndx = if IsNaN(close) then Double.NaN else if tempDI < 0 then -1 - tempDI else 1 - tempDI;
# DEMANDIndex Moving Averages
input DMIndxMALength = 8;
input DMIndxAverageType = AverageType.SIMPLE;
input DMIndxMALength2 = 26;
input DMIndxAverageType2 = AverageType.WEIGHTED;
input DMIndxMALength3 = 40;
input DMIndxAverageType3 = AverageType.SIMPLE;
input DMIndxMALength4 = 50;
input DMIndxAverageType4 = AverageType.SIMPLE;
def DMIndxMA = MovingAverage(DMIndxAverageType, DMIndx, DMIndxMALength);
def pDMIndxMA = DMIndxMA;
def DMIndxMA2 = MovingAverage(DMIndxAverageType2, DMIndx, DMIndxMALength2);
def pDMIndxMA2 = DMIndxMA2;
def DMIndxMA3 = MovingAverage(DMIndxAverageType3, DMIndx, DMIndxMALength3);
def pDMIndxMA3 = DMIndxMA3;
def DMIndxMA4 = MovingAverage(DMIndxAverageType4, DMIndx, DMIndxMALength4);
def pDMIndxMA4 = DMIndxMA4;
# Strategy
def LE1 = SlowRSI crosses above pMA2 and Zscore > -1 and SlowRSI < MiddleLine and SLOWRSI > over_sold and dMIndx > pDMIndxMA2 and (Momentum > pMA11 or Momentum > 0);
def LE2 = SlowRSI crosses above pMA2 and Zscore > -1 and SLOWRSI > over_sold and (dMIndx > pDMIndxMA2);
def SE1 = SlowRSI crosses below pMA2 and Zscore < -1 and SLOWRSI > over_sold and (dMIndx < pDMIndxMA2);
def SE2 = SlowRSI crosses below pMA2 and Zscore < 1 and SlowRSI > MiddleLine and SLOWRSI < over_bought and dMIndx < pDMIndxMA2 and (Momentum < pMA11 or Momentum < 0);
AddOrder(OrderType.BUY_auto, LE1, tickcolor = GetColor(0), arrowcolor = GetColor(0), name = "LE1");
AddOrder(OrderType.BUY_auto, LE2, tickcolor = GetColor(0), arrowcolor = GetColor(0), name = "LE2");
AddOrder(OrderType.SELL_auto, SE1, arrowcolor = GetColor(1), name = "SE1");
AddOrder(OrderType.SELL_auto, SE2, tickcolor = GetColor(1), arrowcolor = GetColor(1), name = "SE2");
# Stoploss ATR
input stop_mult = 2.5;
plot stopb = EntryPrice() - ATR() * stop_mult;
stopb.SetDefaultColor(Color.RED);
plot stops = EntryPrice() + ATR() * stop_mult;
stops.SetDefaultColor(Color.RED);
AddOrder(OrderType.SELL_to_close, CLOSE <= stopb, tickcolor = Color.GRAY, arrowcolor = Color.GRAY, name = "Stop", price = stopb);
AddOrder(OrderType.BUY_to_close, CLOSE >= stops, tickcolor = Color.GRAY, arrowcolor = Color.GRAY, name = "Stop", price = stops);
# Plot Arrows
plot LE1Signal = !LE1[1] and LE1;
LE1Signal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
LE1Signal.SetDefaultColor(Color.GREEN);
plot LE2Signal = !LE2[1] and LE2;
LE2Signal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
LE2Signal.SetDefaultColor(Color.GREEN);
plot SE1Signal = !SE1[1] and SE1;
SE1Signal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
SE1Signal.SetDefaultColor(Color.RED);
plot SE2Signal = !SE2[1] and SE2;
SE2Signal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
SE2Signal.SetDefaultColor(Color.RED);