How To Make Part Of A Line Disappear

Guille

New member
Partially show Upper VWAP band when above Upper Linear Regression Line.
Can anyone help me figure out how to write a script to show the Upper VWAP band only when it's above the Upper Linear Regression Line?
 
Solution
Here is just an example that only shows the vwap upper band when the close is greater than the 20 SMA. I do not know what do you mean by upper regression line. Do you mean the upper band itself, perhaps, or some other form of regression line or channel?
HhEXqaC.png


Ruby:
def Upper = Reference VWAP(-2,2,"DAY").UpperBand;
plot VwapUpper = if close > SimpleMovingAvg(close,20) then Upper else Double.NaN;
plot SMA_Example = SimpleMovingAvg(close,20);
Basically I don't want to see the VWAP upper band at all times in the chart. I'm only interested in seeing the VWAP upper band WHEN it crosses over the Upper Linear Regression line.
 
@Guille So technically, you cannot make part of a line disappear. BUT you can make it appear as though part of the line disappeared by assigning the color of the line to be the same color as the chart background thus it blends with the background and is not 'seen'.

You didn't provide what script you are working with so this is just an EXAMPLE of what your code would look like:
AssignValueColor(if VWAP_upper_band is greater than Upper_Linear_Regression_line then purple_color else Background_color);

You can see an example and directions here: https://usethinkscript.com/threads/disappearing-moving-average-for-thinkorswim.5881/#post-56173
 
Last edited:
Here is just an example that only shows the vwap upper band when the close is greater than the 20 SMA. I do not know what do you mean by upper regression line. Do you mean the upper band itself, perhaps, or some other form of regression line or channel?
HhEXqaC.png


Ruby:
def Upper = Reference VWAP(-2,2,"DAY").UpperBand;
plot VwapUpper = if close > SimpleMovingAvg(close,20) then Upper else Double.NaN;
plot SMA_Example = SimpleMovingAvg(close,20);
 
Solution
@Guille So technically, you cannot make part of a line disappear. BUT you can make it appear as though part of the line disappeared by assigning the color of the line to be the same color as the chart background thus it blends with the background and is not 'seen'.

You didn't provide what script you are working with so this is just an EXAMPLE of what your code would look like:
AssignValueColor(if VWAP_upper_band is greater than Upper_Linear_Regression_line then purple_color else Background_color);

You can see an example and directions here: https://usethinkscript.com/threads/disappearing-moving-average-for-thinkorswim.5881/#post-56173
This is script with VWAP and LRL's:
input numDevDn = -2.0;
input numDevUp = 2.0;
input timeFrame = {default DAY, WEEK, MONTH};

def cap = GetAggregationPeriod();
def errorInAggregation =
timeFrame == timeFrame.DAY and cap >= AggregationPeriod.WEEK or
timeFrame == timeFrame.WEEK and cap >= AggregationPeriod.MONTH;
Assert(!errorInAggregation, "timeFrame should be not less than current chart aggregation period");

def yyyyMmDd = GetYYYYMMDD();
def periodIndx;
switch (timeFrame) {
case DAY:
periodIndx = yyyyMmDd;
case WEEK:
periodIndx = Floor((DaysFromDate(First(yyyyMmDd)) + GetDayOfWeek(First(yyyyMmDd))) / 7);
case MONTH:
periodIndx = RoundDown(yyyyMmDd / 100, 0);
}
def isPeriodRolled = CompoundValue(1, periodIndx != periodIndx[1], yes);

def volumeSum;
def volumeVwapSum;
def volumeVwap2Sum;

if (isPeriodRolled) {
volumeSum = volume;
volumeVwapSum = volume * vwap;
volumeVwap2Sum = volume * Sqr(vwap);
} else {
volumeSum = CompoundValue(1, volumeSum[1] + volume, volume);
volumeVwapSum = CompoundValue(1, volumeVwapSum[1] + volume * vwap, volume * vwap);
volumeVwap2Sum = CompoundValue(1, volumeVwap2Sum[1] + volume * Sqr(vwap), volume * Sqr(vwap));
}
def price = volumeVwapSum / volumeSum;
def deviation = Sqrt(Max(volumeVwap2Sum / volumeSum - Sqr(price), 0));

plot VWAP = price;
plot UpperBand = price + numDevUp * deviation;
plot LowerBand = price + numDevDn * deviation;

VWAP.SetDefaultColor(GetColor(0));
UpperBand.SetDefaultColor(GetColor(2));
LowerBand.SetDefaultColor(GetColor(4));

input price = close;

plot MiddleLR = InertiaAll(price);
def dist = HighestAll(AbsValue(MiddleLR - price));
plot UpperBand = MiddleLR + dist;
plot LowerLR = MiddleLR - dist;

MiddleLR.setDefaultColor(GetColor(5));
UpperLR.setDefaultColor(GetColor(5));
LowerLR.setDefaultColor(GetColor(5));

AssignValueColor(if UpperBand is greater than UpperBand then purple_color else Background_color);

But the following are mistakes:
Identifier Already Used: price at 47:7
Identifier Already Used: UpperBand at 51:6
No such variable: UpperLR at 55:1
Can not call setDefaultColor on UpperLR at 55:1
AssignValueColor should be called on a IBasePlot at 58:1
Expected class com.devexperts.tos.thinkscript.data.CustomColor at 58:1
Already assigned: UpperBand at 51:6
No such variable: purple_color at 58:62
No such variable: Background_color at 58:80
Identifier Already Used: price at 47:7
Identifier Already Used: UpperBand at 51:6
No such variable: UpperLR at 55:1
Can not call setDefaultColor on UpperLR at 55:1
AssignValueColor should be called on a IBasePlot at 58:1
Expected class com.devexperts.tos.thinkscript.data.CustomColor at 58:1
Already assigned: UpperBand at 51:6
No such variable: purple_color at 58:62
No such variable: Background_color at 58:80
 
Based on the code you provided, this at least looks like this is what you're trying to do. I can assure you that the code does in fact work. The problem, however, is that it is rare for the upper vwap band to be higher than the upper regression line. I had to experiment with several different aggregations and lengths of chart just to get this occur. Change the fist three plots to def if you do not want those displayed.

Code:
plot MiddleLR = InertiaAll(close);
def dist = HighestAll(AbsValue(MiddleLR - close));
plot UpperBand = MiddleLR + dist;
plot LowerLR = MiddleLR - dist;
def vUpper = reference VWAP(-2,2,"DAY").UpperBand;
plot vWapUpper = if vUpper > UpperBand then vUpper else double.nan;

Here is another example, this one shows the upper vwap band only when it is above the center regression line.

Code:
def MiddleLR = InertiaAll(close);
def dist = HighestAll(AbsValue(MiddleLR - close));
def UpperBand = MiddleLR + dist;
def LowerLR = MiddleLR - dist;
def vUpper = reference VWAP(-2,2,"DAY").UpperBand;
plot vWapUpper = if vUpper > MiddleLR then vUpper else double.nan;
 
Last edited:
Based on the code you provided, this at least looks like this is what you're trying to do. I can assure you that the code does in fact work. The problem, however, is that it is rare for the upper vwap band to be higher than the upper regression line. I had to experiment with several different aggregations and lengths of chart just to get this occur. Change the fist three plots to def if you do not want those displayed.

Code:
plot MiddleLR = InertiaAll(close);
def dist = HighestAll(AbsValue(MiddleLR - close));
plot UpperBand = MiddleLR + dist;
plot LowerLR = MiddleLR - dist;
def vUpper = reference VWAP(-2,2,"DAY").UpperBand;
plot vWapUpper = if vUpper > UpperBand then vUpper else double.nan;

Here is another example, this one shows the upper vwap band only when it is above the center regression line.

Code:
def MiddleLR = InertiaAll(close);
def dist = HighestAll(AbsValue(MiddleLR - close));
def UpperBand = MiddleLR + dist;
def LowerLR = MiddleLR - dist;
def vUpper = reference VWAP(-2,2,"DAY").UpperBand;
plot vWapUpper = if vUpper > MiddleLR then vUpper else double.nan;
I think the issue is that both VWAP and LRL def's use "price"
 
Based on the code you provided, this at least looks like this is what you're trying to do. I can assure you that the code does in fact work. The problem, however, is that it is rare for the upper vwap band to be higher than the upper regression line. I had to experiment with several different aggregations and lengths of chart just to get this occur. Change the fist three plots to def if you do not want those displayed.

Code:
plot MiddleLR = InertiaAll(close);
def dist = HighestAll(AbsValue(MiddleLR - close));
plot UpperBand = MiddleLR + dist;
plot LowerLR = MiddleLR - dist;
def vUpper = reference VWAP(-2,2,"DAY").UpperBand;
plot vWapUpper = if vUpper > UpperBand then vUpper else double.nan;

Here is another example, this one shows the upper vwap band only when it is above the center regression line.

Code:
def MiddleLR = InertiaAll(close);
def dist = HighestAll(AbsValue(MiddleLR - close));
def UpperBand = MiddleLR + dist;
def LowerLR = MiddleLR - dist;
def vUpper = reference VWAP(-2,2,"DAY").UpperBand;
plot vWapUpper = if vUpper > MiddleLR then vUpper else double.nan;
I cannot make it to work.
This is my work in progress:
https://tos.mx/le6pbZu
 
The code I gave you can be used by itself, it should not be added it to your existing code. Still, be aware that the upper vwap band hardly ever crosses above the upper regression line anyway, at least in any of the examples I cared to test.
 
The code I gave you can be used by itself, it should not be added it to your existing code. Still, be aware that the upper vwap band hardly ever crosses above the upper regression line anyway, at least in any of the examples I cared to test.
Got it! It works beautifully. I changed the upper linear regression line to 75% as a compromised between the 50% and the full 100%. Check SONM and IPHA on the 1 minute chart on Friday 9/17/21 for examples. Thank you very much, Joshua!
 
A one minute chart with how much historical data? I assume you know this, but I'll repeat it anyway; the linear regression line is highly dependent on how much historical data is on the chart. It will change direction and position dramatically as you change the chart's time interval setting. I don't mean to discourage you, but it is an arbitrary line that could be almost anywhere and nowhere, all at the same time, within reason. I would need to match your exact settings to see it as you do.
 
Joshua,
May I request your help once again? I do like very much how the vUpper line is working. Now I would like to add a 5% extension from the Hull Moving average to show only when it's above the upper linear regression (probably the 50% or 75% line. This the code for the Hull MA and the 5% extension:
input price = close;
input length = 20;
input displace = 0;

plot HMA = MovingAverage(AverageType.HULL, price, length)[-displace];

plot r5=HMA*1.05;


HMA.DefineColor("Up", GetColor(1));
HMA.DefineColor("Down", GetColor(0));
HMA.AssignValueColor(if HMA > HMA[1] then HMA.color("Up") else HMA.color("Down"));

However, I don't know how to code it to show only when peaking above the Upper LRL.
 
Ruby:
Input DistPercent = 50;
Input HullLength = 20;
Input HullPercExt = 5;

plot MiddleLR = InertiaAll(close);
def dist = HighestAll(AbsValue(MiddleLR - close)) * (DistPercent * 0.01);
plot UpperBand = MiddleLR + dist;
plot LowerLR = MiddleLR - dist;
def vUpper = reference VWAP(-2,2,"DAY").UpperBand;
def Hull = hullMovingAvg(close,HullLength);
def hMA = Hull +  (Hull * (HullPercExt * 0.01));
plot vWapUpper = if vUpper > UpperBand then vUpper else double.nan;
plot HullUpper = if hMA > UpperBand then hMA else double.nan;
 
Another idea, if you don't mind: How about plotting TWAP with 20% and 30% extensions only when they are above the upper Linear Regression Line? This is my TWAP and extension code:

input price = FundamentalType.HLC3;
input timeFrame = {default Day, Week, Month, Chart};

script TimeWAP {

input price = hlc3;
input timeFrame = {default Day, Week, Month, Chart};



def yyyyMmDd = GetYYYYMMDD();
def periodIndx;
switch (timeFrame) {
case Day:
periodIndx = yyyyMmDd;
case Week:
periodIndx = Floor((DaysFromDate(First(yyyyMmDd)) + GetDayOfWeek(First(yyyyMmDd))) / 7);
case Month:
periodIndx = RoundDown(yyyyMmDd / 100, 0);
case Chart:
periodIndx = 0;
}

def newday = CompoundValue(1, periodIndx != periodIndx[1], yes);
rec cumeprice = if newday then price else price + cumeprice[1];
rec cumebarnumber = if newday then 1 else 1 + cumebarnumber[1];

plot TWAP = Round(cumeprice / cumebarnumber, 4);

}


plot TWAP = TimeWAP(Fundamental(price));
TWAP.AssignValueColor(if TWAP > TWAP[1] then Color.DARK_GREEN else if TWAP is equal to TWAP[1] then Color.GRAY else Color.RED);

plot r20= TWAP * 1.2;
plot r30= TWAP * 1.3;

TWAP.SetStyle(Curve.SHORT_DASH);
TWAP.SetLineWeight(2);
TWAP.HideBubble();
 
I did it. It seems redundant and I had to uncheck the full TWAP extensions at the end, but it works:
input DistPercent = 75;
input HullLength = 20;
input HullPercExt = 5;

plot MiddleLR = InertiaAll(close);
def dist = HighestAll(AbsValue(MiddleLR - close)) * (DistPercent * 0.01);
plot UpperBand = MiddleLR + dist;
plot LowerLR = MiddleLR - dist;
def vUpper = reference VWAP(-2, 2, "DAY").UpperBand;
def Hull = HullMovingAvg(close, HullLength);
def hMA = Hull + (Hull * (HullPercExt * 0.01));
plot vWapUpper = if vUpper > UpperBand then vUpper else Double.NaN;
plot HullUpper = if hMA > UpperBand then hMA else Double.NaN;
input price = FundamentalType.HLC3;
input timeFrame = {default Day, Week, Month, Chart};

script TimeWAP {

input price = hlc3;
input timeFrame = {default Day, Week, Month, Chart};



def yyyyMmDd = GetYYYYMMDD();
def periodIndx;
switch (timeFrame) {
case Day:
periodIndx = yyyyMmDd;
case Week:
periodIndx = Floor((DaysFromDate(First(yyyyMmDd)) + GetDayOfWeek(First(yyyyMmDd))) / 7);
case Month:
periodIndx = RoundDown(yyyyMmDd / 100, 0);
case Chart:
periodIndx = 0;
}

def newday = CompoundValue(1, periodIndx != periodIndx[1], yes);
rec cumeprice = if newday then price else price + cumeprice[1];
rec cumebarnumber = if newday then 1 else 1 + cumebarnumber[1];

plot TWAP = Round(cumeprice / cumebarnumber, 4);

}


plot TWAP = TimeWAP(Fundamental(price));
TWAP.AssignValueColor(if TWAP > TWAP[1] then Color.Gray else if TWAP is equal to TWAP[1] then Color.GRAY else Color.GRAY);

plot r20= TWAP * 1.2;
plot r30= TWAP * 1.3;
plot r40= TWAP * 1.4;
plot r20Upper= if r20 > UpperBand then r20 else Double.NaN;
plot r30Upper= if r30 > UpperBand then r30 else Double.NaN;
plot r40Upper= if r40 > UpperBand then r40 else Double.NaN;
TWAP.SetStyle(Curve.SHORT_DASH);
TWAP.SetLineWeight(2);
TWAP.HideBubble();
 
How do I plot where I only want to show certain portions of a plot line?

I plot the MACD. Then I plot the peaks or valleys I want (valley code below).

Then I want to connect "some" of the valleys. In the example below, I want to draw the line between point 1 and 2 and also between 3 and 4. But not between 2 and 3.

How do I code the plot to "ignore" that line?

thanks for the help

Ruby:
def MACD = MovingAverage(MACDAveType, c, MACDFastLength) - MovingAverage(MACDAveType, c, MACDSlowLength);
def MACDValley = If(((MACD <= 0) && (MACD[0] < MACD[-1]) && (MACD[0] < Lowest(MACD[1], lookback4HighLow))), MACD, Double.NaN);

plot MACDValleyPlot = If(showMACD, MACDValley, Double.NaN);
    MACDValleyPlot.SetPaintingStrategy(PaintingStrategy.POINTS);
    MACDValleyPlot.SetDefaultColor(Color.LIME);
    MACDValleyPlot.HideBubble();

plot MACDValleyLine = MACDValleyPlot;
    MACDValleyLine.EnableApproximation();
    MACDValleyLine.SetStyle(Curve.Short_DASH);
    MACDValleyLine.SetLineWeight(3);


image example
 
How do I plot where I only want to show certain portions of a plot line?

I plot the MACD. Then I plot the peaks or valleys I want (valley code below).

Then I want to connect "some" of the valleys. In the example below, I want to draw the line between point 1 and 2 and also between 3 and 4. But not between 2 and 3.

How do I code the plot to "ignore" that line?
@Cre8able I move your post here. There are a couple of solutions documented in this thread to allow a portion of a plot to be 'ignored'
 
Last edited:

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
355 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

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?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top