Upper SQ RVI Alerts

Chuck

Moderator
Staff member
WcgvafN.png
Code:
declare upper;

input ArrowLimit= 124;

input length = 14;
input ob  = 80;
input ob2 = 0;
input os = 20;
input os2 = 0;

def highPrice = stDev(high, 10);
def lowPrice = stDev(low, 10);

def highAvgUp = ExpAverage(if high > high[1] then highPrice else 0, length);
def highAvgDown = ExpAverage(if high < high[1] then highPrice else 0, length);

def lowAvgUp = ExpAverage(if low > low[1] then lowPrice else 0, length);
def lowAvgDown = ExpAverage(if low < low[1] then lowPrice else 0, length);

def highRVI = 100 - 100 / (1 + highAvgUp / highAvgDown);
def lowRVI = 100 - 100 / (1 + lowAvgUp / lowAvgDown);

plot RVI = (highRVI + lowRVI) / 2;
plot OverBought = ob;
plot OverBoughtmore = ob2;
plot OverSold = os;
plot OverSoldmore = os2;

RVI.DefineColor("OverBought", Color.RED);
RVI.DefineColor("Normal", Color.DARK_GRAY);
RVI.DefineColor("OverSold", Color.GREEN);
RVI.AssignValueColor(if RVI > overBought then RVI.color("OverBought") else if RVI < overSold then RVI.color("OverSold") else RVI.color("Normal"));

OverBought.setDefaultColor(Color.RED);
overSold.setDefaultColor(Color.GREEN);

input over_bought = 80.0;
input over_sold = 20.0;
input percentDLength = 3;
input percentKLength = 14;
input AudibleAlert = yes;



def min_low = Lowest(low, percentKLength);
def max_high = Highest(high, percentKLength);
def rel_diff = close - (max_high + min_low) / 2;
def diff = max_high - min_low;
def avgrel = Average(Average(rel_diff, percentDLength), percentDLength);
def avgdiff = Average(Average(diff, percentDLength), percentDLength);
def SMIData = (avgrel / (avgdiff / 2) + 1) * 50;

def isLow = If (SMIData < SMIData[-1] and SMIData < SMIData[1], 1, 0);
def isHigh =   If (SMIData > SMIData[-1] and SMIData > SMIData[1], 1, 0);

rec prevLowSMI = CompoundValue(1, If(isLow[1], SMIData[1], prevLowSMI[1]), 0);
rec prevHighSMI = CompoundValue(1, If(isHigh[1], SMIData[1], prevHighSMI[1]), 0);

rec prevLow =  CompoundValue(1, If(isLow[1], low, prevLow[1]), low);
rec prevHigh =  CompoundValue(1, If(isHigh[1], high, prevHigh[1]), high);

def barlimit = if(IsNaN(open[-ArrowLimit]) && !IsNaN(open), yes, no);

def positiveDivergenceReg = If (SMIData > prevLowSMI and low < prevLow, 1, 0);
def positiveDivergenceHid = If (SMIData < prevLowSMI and low > prevLow, 1, 0);

plot posDiv = If(barlimit and isLow and (positiveDivergenceReg or positiveDivergenceHid), LOW, Double.NaN);
posDiv.AssignValueColor(if positiveDivergenceReg then Color.GREEN else Color.GREEN);
posDiv.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
posDiv.SetLineWeight(2);


def negativeDivergenceReg =  If (SMIData < prevHighSMI and high > prevHigh, 1, 0);
def negativeDivergenceHid = If (SMIData > prevHighSMI and high < prevHigh, 1, 0);

plot negDiv = If(barlimit and isHigh and ( negativeDivergenceReg or negativeDivergenceHid), HIGH, Double.NaN);
negDiv.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
negDiv.AssignValueColor(Color.GREEN);
negDiv.SetLineWeight(2);



plot AvgSMI = Average(SMIData, percentDLength);
AvgSMI.SetDefaultColor(Color.GRAY);


plot SMI = SMIData;
SMI.AssignValueColor(if SMI > SMI[1] then Color.GREEN else Color.RED);
SMI.SetLineWeight(2);


plot highdots = if RVI >= over_bought then high else Double.NaN;
plot lowdots = if RVI <= over_sold then low else Double.NaN;
highdots.SetPaintingStrategy(PaintingStrategy.SQUARES);
highdots.SetDefaultColor(Color.RED);
highdots.SetLineWeight(3);
lowdots.SetPaintingStrategy(PaintingStrategy.SQUARES);
lowdots.SetDefaultColor(Color.GREEN);
lowdots.SetLineWeight(3);

# Sound alerts

Alert(AudibleAlert and posdiv[1], "Bullish Divergence", Alert.BAR, Sound.Chimes);
Alert(AudibleAlert and negdiv[1], "Bearish Divergence", Alert.BAR, Sound.Bell);
 
Top