# Paint VFI based on RSI

#### PAYtience

##### New member
VIP
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 -

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:
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 -

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"));``````

here is something to experiment with
it draws the RSI with changing colors

Code:
``````#vfi_rsi_crossing_move

#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.

declare lower;

input upper = 60;
input lower = 40;

def r = RSI();

def c = if R crosses above upper then 1
else if R crosses below lower then -1
else c[1];

plot z = r;
z.AssignValueColor(
if c > 0 then color.Green
else if c < 0 then color.Red
else color.gray);
z.setlineweight(3);
z.hidebubble();

plot zu = upper;
plot zl = lower;
#``````

#### Attachments

• img1.JPG
58.8 KB · Views: 63
here is something to experiment with
it draws the RSI with changing colors

Code:
``````#vfi_rsi_crossing_move

#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.

declare lower;

input upper = 60;
input lower = 40;

def r = RSI();

def c = if R crosses above upper then 1
else if R crosses below lower then -1
else c[1];

plot z = r;
z.AssignValueColor(
if c > 0 then color.Green
else if c < 0 then color.Red
else color.gray);
z.setlineweight(3);
z.hidebubble();

plot zu = upper;
plot zl = lower;
#``````
Thank a lot!! I should be able to work with this...

87k+ Posts
243 Online

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?