Need help merging VFI and RSI - I am not able to assign color in a right way, here is what I am trying to do -
1. When RSI crosses over 60 change the VFI color to Green as long as RSI doesn't close below 40.
2. When RSI crosses below 40 change the VFI color to Red as long as RSI doesn't close above 60.
Here is the code I am trying to work on -
1. When RSI crosses over 60 change the VFI color to Green as long as RSI doesn't close below 40.
2. When RSI crosses below 40 change the VFI color to Red as long as RSI doesn't close above 60.
Here is the code I am trying to work on -
Code:
#
# TD Ameritrade IP Company, Inc. (c) 2007-2024
#
declare lower;
input length = 130;
input maxVolumeCutOff = 2.5;
Assert(maxVolumeCutOff > 0, "'max volume cut off' must be positive: " + maxVolumeCutOff);
def cutOff = 0.2 * StDev(Log(hlc3) - Log(hlc3[1]), 30) * close;
def hlcChange = hlc3 - hlc3[1];
def avgVolume = Average(volume, length)[1];
def minVolume = Min(volume, avgVolume * maxVolumeCutOff);
def dirVolume = if hlcChange > cutOff
then minVolume
else if hlcChange < -cutOff
then -minVolume
else 0;
plot VFI = ExpAverage(Sum(dirVolume, length) / avgVolume, 3);
plot ZeroLine = 0;
VFI.SetDefaultColor(GetColor(3));
ZeroLine.SetDefaultColor(GetColor(8));
#
# TD Ameritrade IP Company, Inc. (c) 2007-2024
#
input length_RIS = 14;
input over_Bought = 60;
input over_Sold = 40;
input price = close;
input averageType = AverageType.WILDERS;
input showBreakoutSignals = no;
def NetChgAvg = MovingAverage(averageType, price - price[1], length_RIS);
def TotChgAvg = MovingAverage(averageType, AbsValue(price - price[1]), length_RIS);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;
def RSI = 50 * (ChgRatio + 1);
def OverSold = over_Sold;
def OverBought = over_Bought;
def UpSignal = if RSI crosses above OverSold then OverSold else Double.NaN;
def DownSignal = if RSI crosses below OverBought then OverBought else Double.NaN;
VFI.DefineColor("Above", Color.GREEN);
VFI.DefineColor("None", Color.YELLOW);
VFI.DefineColor("Below", Color.RED);
VFI.AssignValueColor(if RSI crosses above OverBought and (RSI > 60 or RSI > 40) then VFI.color("Above") else if RSI crosses below OverSold and (RSI < 40 or RSI < 60) then VFI.color("Below") else VFI.color("None"));
Last edited: