VolatilitySwitch Questions

msmmlee

New member
Plus
Hi all,

I’m trying to change the VolatilitySwitch indicator from a lower study to a study in my upper chart/price area, then add a label and a chart bubble when the indicator hits its highest level or 100%. I’ve moved the indicator up to the price area, plotted a “high” so I can use it for the label (the indicator already has a midline which I left in the code), and defined a "highsignal" so I can use it for the chart bubble. The label is working the way I want it to so I don't need any label help. I’m having two issues with the chart bubble: the first issue is that multiple bubbles are appearing at the very top of the screen which I think may be because of the “declare lower” line still in the code. However, when I remove that line, the entire indicator stops appearing in my upper chart altogether so I don’t know what to do but keep that line in the code. The second issue is that I only want one bubble to appear when the indicator first hits 100%; I don’t want the bubble to reprint for every candle as long as the indicator remains at its high. However, when the indicator goes down and then hits 100% again, I want to get a new bubble. (The label stays on during the entire time the indicator is at 100% but I only want one bubble to appear when the indicator first hits 100%.)

I need help getting the bubble to appear above the candle when the indicator first hits 100% instead of appearing at the top of the screen with multiple candles in a row. I’m new to coding and have only gotten to the point where I am by looking through this website and reading up on the thinkorswim Learning Center. Thanks for any help you can provide.

Here's the code with italicized notes showing changes I’ve made to the VolatilitySwitch's original code:


declare lower;

input length = 21;


#code added for aggperiod

def price2 = close(period = aggregationPeriod.TWO_Min);

Assert(length > 0, "'length' must be positive: " + length);


def dailyReturn = (price2 - price2[1]) / ( (price2 + price2[1]) / 2 );

def histVolatility = StDev(dailyReturn, length);


plot VolatilitySwitch = ( fold i = 0 to length with count do count + if histVolatility <= histVolatility then 1 else 0 ) / length;

plot MidLine = 0.5;

plot High = 1.0;#code added


def HighSignal = volatilityswitch >= High;#code added


VolatilitySwitch.DefineColor("Trending", Color.UPTICK);

VolatilitySwitch.DefineColor("Mean-Reverting", Color.DOWNTICK);

VolatilitySwitch.AssignValueColor(if VolatilitySwitch >= High then VolatilitySwitch.Color("Mean-Reverting") else VolatilitySwitch.Color("Trending"));

MidLine.SetDefaultColor(GetColor(7));



#AddLabel and AddChartBubble code added

AddLabel(yes, "2 Min VS", if volatilityswitch >= High then createcolor(249,233,167) else color.BLACK);#label background is black until condition is met

AddChartBubble(Highsignal, high, "2m", createcolor(249,233,167), no);
 
Hi all,

I’m trying to change the VolatilitySwitch indicator from a lower study to a study in my upper chart/price area, then add a label and a chart bubble when the indicator hits its highest level or 100%. I’ve moved the indicator up to the price area, plotted a “high” so I can use it for the label (the indicator already has a midline which I left in the code), and defined a "highsignal" so I can use it for the chart bubble. The label is working the way I want it to so I don't need any label help. I’m having two issues with the chart bubble: the first issue is that multiple bubbles are appearing at the very top of the screen which I think may be because of the “declare lower” line still in the code. However, when I remove that line, the entire indicator stops appearing in my upper chart altogether so I don’t know what to do but keep that line in the code. The second issue is that I only want one bubble to appear when the indicator first hits 100%; I don’t want the bubble to reprint for every candle as long as the indicator remains at its high. However, when the indicator goes down and then hits 100% again, I want to get a new bubble. (The label stays on during the entire time the indicator is at 100% but I only want one bubble to appear when the indicator first hits 100%.)

I need help getting the bubble to appear above the candle when the indicator first hits 100% instead of appearing at the top of the screen with multiple candles in a row. I’m new to coding and have only gotten to the point where I am by looking through this website and reading up on the thinkorswim Learning Center. Thanks for any help you can provide.

Here's the code with italicized notes showing changes I’ve made to the VolatilitySwitch's original code:


declare lower;

input length = 21;


#code added for aggperiod

def price2 = close(period = aggregationPeriod.TWO_Min);

Assert(length > 0, "'length' must be positive: " + length);


def dailyReturn = (price2 - price2[1]) / ( (price2 + price2[1]) / 2 );

def histVolatility = StDev(dailyReturn, length);


plot VolatilitySwitch = ( fold i = 0 to length with count do count + if histVolatility <= histVolatility then 1 else 0 ) / length;

plot MidLine = 0.5;

plot High = 1.0;#code added


def HighSignal = volatilityswitch >= High;#code added


VolatilitySwitch.DefineColor("Trending", Color.UPTICK);

VolatilitySwitch.DefineColor("Mean-Reverting", Color.DOWNTICK);

VolatilitySwitch.AssignValueColor(if VolatilitySwitch >= High then VolatilitySwitch.Color("Mean-Reverting") else VolatilitySwitch.Color("Trending"));

MidLine.SetDefaultColor(GetColor(7));



#AddLabel and AddChartBubble code added

AddLabel(yes, "2 Min VS", if volatilityswitch >= High then createcolor(249,233,167) else color.BLACK);#label background is black until condition is met

AddChartBubble(Highsignal, high, "2m", createcolor(249,233,167), no);

the fold loop is comparing a variable to itself , histVolatility ,
so it will always be 1
do count + if histVolatility <= histVolatility then 1 else 0
original has an offset,
do count + if histVolatility <= histVolatility then 1 else 0


to find just the first occurance of a series, look at the previous bar for a different value and check the current bar.
def HighSignal = volatilityswitch[1] < High and volatilityswitch >= High;

or use crosses above
def HighSignal = volatilityswitch crosses above High;


you are using variable names already assigned to constants. don't do that
plot High = 1.0;#code added
rename it to high1 or something


----------------------

i started over with the original code

make 2nd agg an input for user to change, not just 2 min
can input a minimum limit for the trigger, default is 100%
show a label when VolatilitySwitch * 100 is >= the minimum limit
show a bubble above the high, when VolatilitySwitch * 100 is >= the minimum limit


Code:
# VolatilitySwitch_01

# https://usethinkscript.com/threads/volatilityswitch-from-lower-to-upper-study.18058/

def na = double.nan;
def bn = barnumber();

input price = close;
input length = 21;
#Assert(length > 0, "'length' must be positive: " + length);

def chartagg = getaggregationperiod();
def chartmin = chartagg/(1000*60);

input price_agg = aggregationPeriod.TWO_Min;

# if agg time is < chart time, set the agg time to chart time
def price2;
def aggmin;
if price_agg < chartagg then {
  price2 = close(period = chartagg);
  aggmin = chartagg/(1000*60);
} else {
  price2 = close(period = price_agg);
  aggmin = price_agg/(1000*60);
}

input price_data = {chart_time , default secondary_agg};
# choose data source , 2nd agg data or chart data
def pr;
def mins;
switch(price_data) {
case chart_time:
  pr = close;
  mins = chartmin;
case secondary_agg:
  pr = price2;
  mins = aggmin;
}


def dailyReturn = (price2 - price2[1]) / ( (price2 + price2[1]) / 2 );
def histVolatility = StDev(dailyReturn, length);

def VolatilitySwitch = ( fold i = 0 to length with count do count + if histVolatility[i] <= histVolatility then 1 else 0 ) / length;

def volsw =  VolatilitySwitch * 100;
input min_limit = 100.0;

def firsthi = if bn == 1 then 0
 else if Volsw[1] < min_limit and Volsw >= min_limit then 1
 else 0;


input show_labels = yes;
addlabel(show_labels, "data agg min " + mins, color.light_gray);


AddLabel(
 show_labels and firsthi,
 "VS " + volsw ,
 color.yellow);
# createcolor(249,233,167));


input show_bubble = yes;
addchartbubble(
 show_bubble and firsthi,
 high,
 "vol sw\n" + VolatilitySwitch,
 color.yellow,
 yes);
#
 

Attachments

  • img1.JPG
    img1.JPG
    123.2 KB · Views: 91
Last edited:

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

the fold loop is comparing a variable to itself , histVolatility ,
so it will always be 1
do count + if histVolatility <= histVolatility then 1 else 0
original has an offset,
do count + if histVolatility <= histVolatility then 1 else 0


to find just the first occurance of a series, look at the previous bar for a different value and check the current bar.
def HighSignal = volatilityswitch[1] < High and volatilityswitch >= High;

or use crosses above
def HighSignal = volatilityswitch crosses above High;


you are using variable names already assigned to constants. don't do that
plot High = 1.0;#code added
rename it to high1 or something


----------------------

i started over with the original code

make 2nd agg an input for user to change, not just 2 min
can input a minimum limit for the trigger, default is 100%
show a label when VolatilitySwitch * 100 is >= the minimum limit
show a bubble above the high, when VolatilitySwitch * 100 is >= the minimum limit


Code:
# VolatilitySwitch_01

# https://usethinkscript.com/threads/volatilityswitch-from-lower-to-upper-study.18058/

def na = double.nan;
def bn = barnumber();

input price = close;
input length = 21;
#Assert(length > 0, "'length' must be positive: " + length);

def chartagg = getaggregationperiod();
def chartmin = chartagg/(1000*60);

input price_agg = aggregationPeriod.TWO_Min;

# if agg time is < chart time, set the agg time to chart time
def price2;
def aggmin;
if price_agg < chartagg then {
  price2 = close(period = chartagg);
  aggmin = chartagg/(1000*60);
} else {
  price2 = close(period = price_agg);
  aggmin = price_agg/(1000*60);
}

input price_data = {chart_time , default secondary_agg};
# choose data source , 2nd agg data or chart data
def pr;
def mins;
switch(price_data) {
case chart_time:
  pr = close;
  mins = chartmin;
case secondary_agg:
  pr = price2;
  mins = aggmin;
}


def dailyReturn = (price2 - price2[1]) / ( (price2 + price2[1]) / 2 );
def histVolatility = StDev(dailyReturn, length);

def VolatilitySwitch = ( fold i = 0 to length with count do count + if histVolatility[i] <= histVolatility then 1 else 0 ) / length;

def volsw =  VolatilitySwitch * 100;
input min_limit = 100.0;

def firsthi = if bn == 1 then 0
 else if Volsw[1] < min_limit and Volsw >= min_limit then 1
 else 0;


input show_labels = yes;
addlabel(show_labels, "data agg min " + mins, color.light_gray);


AddLabel(
 show_labels and firsthi,
 "VS " + volsw ,
 color.yellow);
# createcolor(249,233,167));


input show_bubble = yes;
addchartbubble(
 show_bubble and firsthi,
 high,
 "vol sw\n" + VolatilitySwitch,
 color.yellow,
 yes);
#
Thanks @halcyonguy. This is helpful and it's working. I'll get back to you if I have any questions.
 
Hi all,

Is there a way to count how many MTF indicators are at their high point at one time? I have multiple copies of the VolatilitySwitch indicator set up for different timeframes that I use on the 2-minute chart (the timeframes are 2-, 3-, 4-, 5-, 10-, 15-, 20-, 30-min, and 1-hr). The code for all of them is the same except for the agg period. I would like a chart bubble and alert whenever at least 6 of them are at their high point at the same time. If all of these need to be combined into one indicator before counting is even possible, let me know. I appreciate any help you can provide.

declare lower;

input length = 21;

def price2 = close(period = aggregationPeriod.TWO_Min);

Assert(length > 0, "'length' must be positive: " + length);

def dailyReturn = (price2 - price2[1]) / ( (price2 + price2[1]) / 2 );
def histVolatility = StDev(dailyReturn, length);

plot VolatilitySwitch = ( fold i = 0 to length with count do count + if histVolatility <= histVolatility then 1 else 0 ) / length;
plot MidLine = 0.5;
plot High1 = 1.0;

def HighSignal = volatilityswitch >= High1;

VolatilitySwitch.DefineColor("Trending", Color.UPTICK);
VolatilitySwitch.DefineColor("Mean-Reverting", Color.DOWNTICK);
VolatilitySwitch.AssignValueColor(if VolatilitySwitch >= MidLine then VolatilitySwitch.Color("Mean-Reverting") else VolatilitySwitch.Color("Trending"));
MidLine.SetDefaultColor(GetColor(7));

#AddLabel(yes, "2 Min VS", if volatilityswitch >= High then createcolor(249,233,167) else color.BLACK);

AddChartBubble(Highsignal, high1 + (high1*.02), "2m", createcolor(249,233,167), no);
 
Hi all,

Is there a way to count how many MTF indicators are at their high point at one time? I have multiple copies of the VolatilitySwitch indicator set up for different timeframes that I use on the 2-minute chart (the timeframes are 2-, 3-, 4-, 5-, 10-, 15-, 20-, 30-min, and 1-hr). The code for all of them is the same except for the agg period. I would like a chart bubble and alert whenever at least 6 of them are at their high point at the same time. If all of these need to be combined into one indicator before counting is even possible, let me know. I appreciate any help you can provide.

declare lower;

input length = 21;

def price2 = close(period = aggregationPeriod.TWO_Min);

Assert(length > 0, "'length' must be positive: " + length);

def dailyReturn = (price2 - price2[1]) / ( (price2 + price2[1]) / 2 );
def histVolatility = StDev(dailyReturn, length);

plot VolatilitySwitch = ( fold i = 0 to length with count do count + if histVolatility <= histVolatility then 1 else 0 ) / length;
plot MidLine = 0.5;
plot High1 = 1.0;

def HighSignal = volatilityswitch >= High1;

VolatilitySwitch.DefineColor("Trending", Color.UPTICK);
VolatilitySwitch.DefineColor("Mean-Reverting", Color.DOWNTICK);
VolatilitySwitch.AssignValueColor(if VolatilitySwitch >= MidLine then VolatilitySwitch.Color("Mean-Reverting") else VolatilitySwitch.Color("Trending"));
MidLine.SetDefaultColor(GetColor(7));

#AddLabel(yes, "2 Min VS", if volatilityswitch >= High then createcolor(249,233,167) else color.BLACK);

AddChartBubble(Highsignal, high1 + (high1*.02), "2m", createcolor(249,233,167), no);

EDIT - updated to have a label for count of green signals
can set a minimum quatity to trigger. default is 6.


======================
this is a lower study
that calcs VolatilitySwitch for 8 aggregations
it can plot lines or dots
the smallest agg time is at the bottom
the lines are pulses , 0 or 1, for low and high
the dots are green or red, green for a high


Code:
#volsw_8agg_numbers_dots

#https://usethinkscript.com/threads/count-multiple-studies-of-volatilityswitches-high-point-at-one-time.18137/
#Count multiple studies of VolatilitySwitches high point at one time

declare lower;

#Assert(length > 0, "'length' must be positive: " + length);
def na = double.nan;

input min_qty_green = 6;

script volsw {
  input price2 = close;
  input length = 0;
  input agg = AggregationPeriod.TWO_MIN;

#  def dailyReturn = (price2 - price2[1]) / ( (price2 + price2[1]) / 2 );
  def dailyReturn = 1 * (close(period = agg) - close(period = agg)[1]) / ( (close(period = agg) + close(period = agg)[1]) / 2 );
  def histVolatility = StDev(dailyReturn, length);

  def VolatilitySwitch = ( fold i = 0 to length with count do count + if histVolatility[1] <= histVolatility then 1 else 0 ) / length;
#  plot volsw = dailyReturn;
#  plot z =  histVolatility ;
   plot volsw = VolatilitySwitch;
}

input length = 21;
input agg1 = AggregationPeriod.TWO_MIN;
input agg2 = AggregationPeriod.THREE_MIN;
input agg3 = AggregationPeriod.FOUR_MIN;
input agg4 = AggregationPeriod.FIVE_MIN;
input agg5 = AggregationPeriod.TEN_MIN;
input agg6 = AggregationPeriod.FIFTEEN_MIN;
input agg7 = AggregationPeriod.TWENTY_MIN;
input agg8 = AggregationPeriod.THIRTY_MIN;

def vs1 = volsw(close(period = agg1), length, agg1);
def vs2 = volsw(close(period = agg2), length, agg2);
def vs3 = volsw(close(period = agg3), length, agg3);
def vs4 = volsw(close(period = agg4), length, agg4);
def vs5 = volsw(close(period = agg5), length, agg5);
def vs6 = volsw(close(period = agg6), length, agg6);
def vs7 = volsw(close(period = agg7), length, agg7);
def vs8 = volsw(close(period = agg8), length, agg8);

def greencnt = (vs1 + vs2 + vs3 + vs4 + vs5 + vs6 + vs7 + vs8);
def isgreen = greencnt >= min_qty_green;

#plot MidLine = 0.5;
def High1 = 1.0;

input plot_lines = yes;
def m = 2;
plot z8 = if plot_lines then (vs8 + (7*m)) else na;
plot z7 = if plot_lines then (vs7 + (6*m)) else na;
plot z6 = if plot_lines then (vs6 + (5*m)) else na;
plot z5 = if plot_lines then (vs5 + (4*m)) else na;
plot z4 = if plot_lines then (vs4 + (3*m)) else na;
plot z3 = if plot_lines then (vs3 + (2*m)) else na;
plot z2 = if plot_lines then (vs2 + (1*m)) else na;
plot z1 = if plot_lines then (vs1 + (0*m)) else na;

def x=5;
# bubble with agg times. shortest time at bottom
addchartbubble(plot_lines and (!isnan(close[x+1]) and isnan(close[x])), 0,
 (agg8/60000) + "\n" +
 (agg7/60000) + "\n" +
 (agg6/60000) + "\n" +
 (agg5/60000) + "\n" +
 (agg4/60000) + "\n" +
 (agg3/60000) + "\n" +
 (agg2/60000) + "\n" +
 (agg1/60000) + "\n" 
, color.yellow, yes);

def en = !isnan(close);

input plot_dots = no;
plot d8 = if plot_dots and en then 7 else na;
plot d7 = if plot_dots and en then 6 else na;
plot d6 = if plot_dots and en then 5 else na;
plot d5 = if plot_dots and en then 4 else na;
plot d4 = if plot_dots and en then 3 else na;
plot d3 = if plot_dots and en then 2 else na;
plot d2 = if plot_dots and en then 1 else na;
plot d1 = if plot_dots and en then 0 else na;
d8.SetPaintingStrategy(PaintingStrategy.POINTS);
d7.SetPaintingStrategy(PaintingStrategy.POINTS);
d6.SetPaintingStrategy(PaintingStrategy.POINTS);
d5.SetPaintingStrategy(PaintingStrategy.POINTS);
d4.SetPaintingStrategy(PaintingStrategy.POINTS);
d3.SetPaintingStrategy(PaintingStrategy.POINTS);
d2.SetPaintingStrategy(PaintingStrategy.POINTS);
d1.SetPaintingStrategy(PaintingStrategy.POINTS);
d8.AssignValueColor(if vs8 then color.green else color.red);
d7.AssignValueColor(if vs7 then color.green else color.red);
d6.AssignValueColor(if vs6 then color.green else color.red);
d5.AssignValueColor(if vs5 then color.green else color.red);
d4.AssignValueColor(if vs4 then color.green else color.red);
d3.AssignValueColor(if vs3 then color.green else color.red);
d2.AssignValueColor(if vs2 then color.green else color.red);
d1.AssignValueColor(if vs1 then color.green else color.red);


def hi1 = vs1 >= High1;
def hi2 = vs2 >= High1;
def hi3 = vs3 >= High1;
def hi4 = vs4 >= High1;
def hi5 = vs5 >= High1;
def hi6 = vs6 >= High1;
def hi7 = vs7 >= High1;
def hi8 = vs8 >= High1;


AddLabel(yes, (agg1/60000) + " Min VS", if hi1 then createcolor(249,233,167) else color.BLACK);
AddLabel(yes, (agg2/60000) + " Min VS", if hi2 then createcolor(249,233,167) else color.BLACK);
AddLabel(yes, (agg3/60000) + " Min VS", if hi3 then createcolor(249,233,167) else color.BLACK);
AddLabel(yes, (agg4/60000) + " Min VS", if hi4 then createcolor(249,233,167) else color.BLACK);
AddLabel(yes, (agg5/60000) + " Min VS", if hi5 then createcolor(249,233,167) else color.BLACK);
AddLabel(yes, (agg6/60000) + " Min VS", if hi6 then createcolor(249,233,167) else color.BLACK);
AddLabel(yes, (agg7/60000) + " Min VS", if hi7 then createcolor(249,233,167) else color.BLACK);
AddLabel(yes, (agg8/60000) + " Min VS", if hi8 then createcolor(249,233,167) else color.BLACK);

addlabel(1, greencnt + " / 8 green signals. (min " + min_qty_green + ")", (if isgreen then color.green else color.gray));


addchartbubble(0, 0,
vs1 + "\n" +
vs2 + "\n" +
vs3 + "\n" +
vs4 + "\n" +
vs5 + "\n" +
vs6 + "\n" +
vs7 + "\n" +
vs8 + "\n" 
, color.yellow, no);
#

on the lines and dots, 1, 4,5 are high and the labels are white
 

Attachments

  • img1-lines.JPG
    img1-lines.JPG
    67.8 KB · Views: 73
  • img2-dots.JPG
    img2-dots.JPG
    61.9 KB · Views: 73
  • img3.JPG
    img3.JPG
    96 KB · Views: 65
Last edited:
#volsw_8agg_numbers #https://usethinkscript.com/threads/...atilityswitches-high-point-at-one-time.18137/ #Count multiple studies of VolatilitySwitches high point at one time declare lower; #Assert(length > 0, "'length' must be positive: " + length); def na = double.nan; script volsw { input price2 = close; input length = 0; input agg = AggregationPeriod.TWO_MIN; # def dailyReturn = (price2 - price2[1]) / ( (price2 + price2[1]) / 2 ); def dailyReturn = 1 * (close(period = agg) - close(period = agg)[1]) / ( (close(period = agg) + close(period = agg)[1]) / 2 ); def histVolatility = StDev(dailyReturn, length); def VolatilitySwitch = ( fold i = 0 to length with count do count + if histVolatility[1] <= histVolatility then 1 else 0 ) / length; # plot volsw = dailyReturn; # plot z = histVolatility ; plot volsw = VolatilitySwitch; } input length = 21; input agg1 = AggregationPeriod.TWO_MIN; input agg2 = AggregationPeriod.THREE_MIN; input agg3 = AggregationPeriod.FOUR_MIN; input agg4 = AggregationPeriod.FIVE_MIN; input agg5 = AggregationPeriod.TEN_MIN; input agg6 = AggregationPeriod.FIFTEEN_MIN; input agg7 = AggregationPeriod.TWENTY_MIN; input agg8 = AggregationPeriod.THIRTY_MIN; def vs1 = volsw(close(period = agg1), length, agg1); def vs2 = volsw(close(period = agg2), length, agg2); def vs3 = volsw(close(period = agg3), length, agg3); def vs4 = volsw(close(period = agg4), length, agg4); def vs5 = volsw(close(period = agg5), length, agg5); def vs6 = volsw(close(period = agg6), length, agg6); def vs7 = volsw(close(period = agg7), length, agg7); def vs8 = volsw(close(period = agg8), length, agg8); #plot MidLine = 0.5; def High1 = 1.0; input plot_lines = yes; def m = 2; plot z8 = if plot_lines then (vs8 + (7*m)) else na; plot z7 = if plot_lines then (vs7 + (6*m)) else na; plot z6 = if plot_lines then (vs6 + (5*m)) else na; plot z5 = if plot_lines then (vs5 + (4*m)) else na; plot z4 = if plot_lines then (vs4 + (3*m)) else na; plot z3 = if plot_lines then (vs3 + (2*m)) else na; plot z2 = if plot_lines then (vs2 + (1*m)) else na; plot z1 = if plot_lines then (vs1 + (0*m)) else na; def x=5; # bubble with agg times. shortest time at bottom addchartbubble(plot_lines and (!isnan(close[x+1]) and isnan(close[x])), 0, (agg8/60000) + "\n" + (agg7/60000) + "\n" + (agg6/60000) + "\n" + (agg5/60000) + "\n" + (agg4/60000) + "\n" + (agg3/60000) + "\n" + (agg2/60000) + "\n" + (agg1/60000) + "\n" , color.yellow, yes); def en = !isnan(close); input plot_dots = yes; plot d8 = if plot_dots and en then 7 else na; plot d7 = if plot_dots and en then 6 else na; plot d6 = if plot_dots and en then 5 else na; plot d5 = if plot_dots and en then 4 else na; plot d4 = if plot_dots and en then 3 else na; plot d3 = if plot_dots and en then 2 else na; plot d2 = if plot_dots and en then 1 else na; plot d1 = if plot_dots and en then 0 else na; d8.SetPaintingStrategy(PaintingStrategy.POINTS); d7.SetPaintingStrategy(PaintingStrategy.POINTS); d6.SetPaintingStrategy(PaintingStrategy.POINTS); d5.SetPaintingStrategy(PaintingStrategy.POINTS); d4.SetPaintingStrategy(PaintingStrategy.POINTS); d3.SetPaintingStrategy(PaintingStrategy.POINTS); d2.SetPaintingStrategy(PaintingStrategy.POINTS); d1.SetPaintingStrategy(PaintingStrategy.POINTS); d8.AssignValueColor(if vs8 then color.green else color.red); d7.AssignValueColor(if vs7 then color.green else color.red); d6.AssignValueColor(if vs6 then color.green else color.red); d5.AssignValueColor(if vs5 then color.green else color.red); d4.AssignValueColor(if vs4 then color.green else color.red); d3.AssignValueColor(if vs3 then color.green else color.red); d2.AssignValueColor(if vs2 then color.green else color.red); d1.AssignValueColor(if vs1 then color.green else color.red); def hi1 = vs1 >= High1; def hi2 = vs2 >= High1; def hi3 = vs3 >= High1; def hi4 = vs4 >= High1; def hi5 = vs5 >= High1; def hi6 = vs6 >= High1; def hi7 = vs7 >= High1; def hi8 = vs8 >= High1; AddLabel(yes, (agg1/60000) + " Min VS", if hi1 then createcolor(249,233,167) else color.BLACK); AddLabel(yes, (agg2/60000) + " Min VS", if hi2 then createcolor(249,233,167) else color.BLACK); AddLabel(yes, (agg3/60000) + " Min VS", if hi3 then createcolor(249,233,167) else color.BLACK); AddLabel(yes, (agg4/60000) + " Min VS", if hi4 then createcolor(249,233,167) else color.BLACK); AddLabel(yes, (agg5/60000) + " Min VS", if hi5 then createcolor(249,233,167) else color.BLACK); AddLabel(yes, (agg6/60000) + " Min VS", if hi6 then createcolor(249,233,167) else color.BLACK); AddLabel(yes, (agg7/60000) + " Min VS", if hi7 then createcolor(249,233,167) else color.BLACK); AddLabel(yes, (agg8/60000) + " Min VS", if hi8 then createcolor(249,233,167) else color.BLACK); addchartbubble(0, 0, vs1 + "\n" + vs2 + "\n" + vs3 + "\n" + vs4 + "\n" + vs5 + "\n" + vs6 + "\n" + vs7 + "\n" + vs8 + "\n" , color.yellow, no); #
Thanks for doing this, Halcyonguy. Is there a way to count the bubbles and get an alert when at least 6 of them are green? It doesn't matter which 6.
 
Hi all,
RE: Add down arrow to upper chart based on lower study with ability to change arrow's color

I'm trying to add a down arrow above the candles on my chart whenever the same conditions are met as the label at the bottom of the code. I want to be able to change the color of the arrow. I appreciate any help you can provide.
Ruby:
declare lower;

input length = 21;

def price3 = close(period = aggregationPeriod.THREE_Min);


Assert(length > 0, "'length' must be positive: " + length);

def dailyReturn = (price3 - price3[1]) / ( (price3 + price3[1]) / 2 );
def histVolatility = StDev(dailyReturn, length);

plot VolatilitySwitch = ( fold i = 0 to length with count do count + if histVolatility <= histVolatility then 1 else 0 ) / length;
plot MidLine = 0.5;
plot High1 = 1.0;

def HighSignal = volatilityswitch >= High1;

#VolatilitySwitch.DefineColor("Trending", Color.UPTICK);
#VolatilitySwitch.DefineColor("Mean-Reverting", #Color.DOWNTICK);
#VolatilitySwitch.AssignValueColor(if VolatilitySwitch >= #MidLine then VolatilitySwitch.Color("Mean-Reverting") #else VolatilitySwitch.Color("Trending"));

VolatilitySwitch.DefineColor("On", Color.UPTICK);
VolatilitySwitch.DefineColor("Off", Color.DOWNTICK);
VolatilitySwitch.AssignValueColor(if VolatilitySwitch >= High1 then VolatilitySwitch.Color("On") else VolatilitySwitch.Color("Off"));

AddLabel(yes, "3 Min VS", if volatilityswitch >= High1 then createcolor(249,188,179) else color.BLACK);
 
Last edited by a moderator:
the fold loop is comparing a variable to itself , histVolatility ,
so it will always be 1
do count + if histVolatility <= histVolatility then 1 else 0
original has an offset,
do count + if histVolatility <= histVolatility then 1 else 0


to find just the first occurance of a series, look at the previous bar for a different value and check the current bar.
def HighSignal = volatilityswitch[1] < High and volatilityswitch >= High;

or use crosses above
def HighSignal = volatilityswitch crosses above High;


you are using variable names already assigned to constants. don't do that
plot High = 1.0;#code added
rename it to high1 or something


----------------------

i started over with the original code

make 2nd agg an input for user to change, not just 2 min
can input a minimum limit for the trigger, default is 100%
show a label when VolatilitySwitch * 100 is >= the minimum limit
show a bubble above the high, when VolatilitySwitch * 100 is >= the minimum limit


Code:
# VolatilitySwitch_01

# https://usethinkscript.com/threads/volatilityswitch-from-lower-to-upper-study.18058/

def na = double.nan;
def bn = barnumber();

input price = close;
input length = 21;
#Assert(length > 0, "'length' must be positive: " + length);

def chartagg = getaggregationperiod();
def chartmin = chartagg/(1000*60);

input price_agg = aggregationPeriod.TWO_Min;

# if agg time is < chart time, set the agg time to chart time
def price2;
def aggmin;
if price_agg < chartagg then {
  price2 = close(period = chartagg);
  aggmin = chartagg/(1000*60);
} else {
  price2 = close(period = price_agg);
  aggmin = price_agg/(1000*60);
}

input price_data = {chart_time , default secondary_agg};
# choose data source , 2nd agg data or chart data
def pr;
def mins;
switch(price_data) {
case chart_time:
  pr = close;
  mins = chartmin;
case secondary_agg:
  pr = price2;
  mins = aggmin;
}


def dailyReturn = (price2 - price2[1]) / ( (price2 + price2[1]) / 2 );
def histVolatility = StDev(dailyReturn, length);

def VolatilitySwitch = ( fold i = 0 to length with count do count + if histVolatility[i] <= histVolatility then 1 else 0 ) / length;

def volsw =  VolatilitySwitch * 100;
input min_limit = 100.0;

def firsthi = if bn == 1 then 0
 else if Volsw[1] < min_limit and Volsw >= min_limit then 1
 else 0;


input show_labels = yes;
addlabel(show_labels, "data agg min " + mins, color.light_gray);


AddLabel(
 show_labels and firsthi,
 "VS " + volsw ,
 color.yellow);
# createcolor(249,233,167));


input show_bubble = yes;
addchartbubble(
 show_bubble and firsthi,
 high,
 "vol sw\n" + VolatilitySwitch,
 color.yellow,
 yes);
#

@halcyonguy, I want to thank you again for helping me with this. It is working really well and I learned some things I didn't know.
 
Last edited by a moderator:

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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