Once an indicator is true, how would I highlight it forever?

Stoynks

Member
Let's say my indicator which is (close crosses vwap) is true for one time, how would I leave it true the whole time that way it doesn't turn false or in this case back to 0? I want it to keep 1/true forever once my indicator gets triggered just for one time instead of having to go back to false/0 when close is done crossing vwap.

Code:
plot x = if close crosses vwap then 1 else 0;
I am looking for something like this, is it doable?

Code:
Def vwapcross: if close crosses vwap then plot on the bar “close crossed vwap” else 0.

Addlabel( if vwapcross == 1 then color.dark_green else color.black);

Plot x = vwap;
 
Last edited:
It is something that will be checked on every bar so it will change accordingly. If you want to plot just for that bar then you can run your check against the current bar compared to the previous bar and check for difference in state. If it is true then plot if not then do not plot
 

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

It is something that will be checked on every bar so it will change accordingly. If you want to plot just for that bar then you can run your check against the current bar compared to the previous bar and check for difference in state. If it is true then plot if not then do not plot
How would I plot/or highlight that on my columnlist/watchlist?
If you want to plot just for that bar then you can run your check against the current bar compared to the previous bar and check for difference in state
Can you elaborate on this more or show an example? I am not understanding. Sorry.

Another question, Is it possible to find an indicator within a timeframe? and highlighting or coloring it?
for example:

Code:
if close crosses vwap within any timeframe from 0930 to 1600 then color.dark_green else color.white;
 
Last edited:
@Stoynks You want to use something like the following to retain the state of your plot variable so it only changes on crossover in an opposite direction...

Ruby:
plot x = if close crosses above vwap then 1 else if close crosses below vwap then -1 else x[1];
x.AssignValueColor(if x == 1 then Color.GREEN else if x == -1 the Color.RED else Color.WHITE);
 
@Stoynks You want to use something like the following to retain the state of your plot variable so it only changes on crossover in an opposite direction...

Ruby:
plot x = if close crosses above vwap then 1 else if close crosses below vwap then -1 else x[1];
x.AssignValueColor(if x == 1 then Color.GREEN else if x == -1 the Color.RED else Color.WHITE);
I tried your code but it's saying too early to access. so I change x[1] to just 0. And also the color green is not being retained once close crosses above vwap on my top left label. Unless that is what you mean when you say it changes when crossover happens. How do I prevent that if all I need to know is just only one time close crosses vwap, just retain the color green forever without ever changing?

Code:
plot x = if close crosses above vwap then 1 else if close crosses below vwap then -1 else 0;
x.AssignValueColor(if x == 1 then Color.GREEN else if x == -1 then Color.RED else Color.WHITE);

AddLabel (yes, " VWAP: " + (VWAP), (if x == 1 then Color.GREEN else if x == -1 then Color.RED else Color.WHITE));
 
The code should be correct if placed in the proper location within the script - if the original script was coded correctly... The following code works for just displaying a chart label based on the standard VWAP Study...

Ruby:
#
# TD Ameritrade IP Company, Inc. (c) 2011-2021
#

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

# Label code - rad14733

def x = if close crosses above VWAP then 1 else if close crosses below VWAP then -1 else x[1];
AddLabel (yes, " VWAP: " + VWAP, (if x == 1 then Color.GREEN else if x == -1 then Color.RED else Color.WHITE));
 
Last edited:
Let's say my indicator which is (close crosses vwap) is true for one time, how would I leave it true the whole time that way it doesn't turn false or in this case back to 0? I want it to keep 1/true forever once my indicator gets triggered just for one time instead of having to go back to false/0 when close is done crossing vwap.


I am looking for something like this, is it doable?

Code:
Def vwapcross: if close crosses vwap then plot on the bar “close crossed vwap” else 0.

Addlabel( if vwapcross == 1 then color.dark_green else color.black);

Plot x = vwap;
here are a couple of studies,
that on the first trigger of some variable,
sets a variable, trig1, to 1 for the remainder of the chart.
i drew arrows for all of the crossings, so you can see that the line level changes only on the first crossing.

the key part of this is the second line of this, which holds the value after it has been triggered.
# def trig1 = if bn == 1 then 0
# else if (bn > 1 and trig1[1] == 1) then trig1[1]
# else if x then 1
# else trig1[1];


VWAP
the first study uses VWAP. i copied the vwap study and added to the end of it.
Code:
# triggeronce_vwapx_00


# halcyonguy
# 21-07-01
# check if price crosses vwap
# on the 1st crossing, change variable trig1, from 0 to 1, and keep it set to 1

# IBM 5min

input showband_lines = no;

# vwap
# TD Ameritrade IP Company, Inc. (c) 2011-2021
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 = if showband_lines then (price + numDevUp * deviation) else double.nan;
plot LowerBand = if showband_lines then (price + numDevDn * deviation) else double.nan;
VWAP.setDefaultColor(getColor(0));
UpperBand.setDefaultColor(getColor(2));
LowerBand.setDefaultColor(getColor(4));



# -------------

# add code to keep a var set after a trigger

def bn = barnumber();
def levelbefore = if bn == 1 then close else levelbefore[1];

def x = if close crosses vwap then 1 else 0;

# draw an arrow on every crossing
plot arrows = x;
arrows.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
arrows.setDefaultColor(color.cyan);
arrows.setlineweight(4);
arrows.hidebubble();


# on the 1st crossing, chg a var and keep it changed
def trig1 = if bn == 1 then 0
 else if (bn > 1 and trig1[1] == 1) then 1
 else if x then 1
 else trig1[1];


# draw a line to visualize trig1
input factor = 1.01;
plot stepline = if trig1 == 0 then levelbefore else ( levelbefore * factor);
stepline.setDefaultColor(color.cyan);
stepline.setlineweight(2);


#
vwap , IBM 5min 5day
hHfENkB.jpg




Moving average
when using vwap, most of the stocks tend to be triggered in the first few candles.
so i made another version, with the trigger as price crossing a moving average
Code:
# triggeronce_avgx_00

# halcyonguy
# 21-07-01
# check if price crosses a moving average
# on the 1st crossing, change variable trig1, from 0 to 1, and keep it set to 1

def bn = barnumber();

# draw an average line to test for a crossing
input avg1_len = 20;
input avg1_type =  AverageType.simple;
def ma1 = MovingAverage(avg1_type, close, avg1_len);
plot ma = ma1;
ma.setDefaultColor(color.cyan);
ma.setlineweight(1);
ma.hidebubble();

def x = if close crosses ma1 then 1 else 0;

# draw an arrow on every crossing. just to show only the 1st causes the trigger line level to change
plot arrows = x;
arrows.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
arrows.setDefaultColor(color.cyan);
arrows.setlineweight(3);
arrows.hidebubble();


# on the 1st crossing, chg a var, from 0 to 1, and keep it changed
#  if a crossing happens on the first bar, it will be missed
def trig1 = if bn == 1 then 0
 else if (bn > 1 and trig1[1] == 1) then trig1[1]
 else if x then 1
 else trig1[1];

# set a starting level for the trigger line
def levelbefore = if bn == 1 then close else levelbefore[1];

# draw a line to visualize the output of trig1
input factor = 1.01;
plot stepline = if trig1 == 0 then levelbefore else ( levelbefore * factor);
stepline.setDefaultColor(color.cyan);
stepline.setlineweight(2);

#
avg , TNDM 15min 5day
WQe7FWJ.jpg
 
Let's say my indicator which is (close crosses vwap) is true for one time, how would I leave it true the whole time that way it doesn't turn false or in this case back to 0? I want it to keep 1/true forever once my indicator gets triggered just for one time instead of having to go back to false/0 when close is done crossing vwap.


I am looking for something like this, is it doable?

Code:
Def vwapcross: if close crosses vwap then plot on the bar “close crossed vwap” else 0.

Addlabel( if vwapcross == 1 then color.dark_green else color.black);

Plot x = vwap;
if you want it to stay, maybe you can use 'is greater than or equal to' instead of crosses, like this:
plot x = if close is greater than or equal to vwap then 1 else 0;
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
315 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