Add alerts to standard deviation study


New member
I have the following code, and I get an error when I use the bottom line. The error comes from the plot LowerLine1 command, but only if I have the alert present using pricetype.XXXX.

The point of the code is to get an alert when the mark crosses below the 1.8 standard deviation but it doesn't seem to be working once the alert is added.

Eventually I want to change it a bit more to become a scanner for stocks crossing below the 1.8 standard deviation, but that's once I get this kink worked out.

input price = close;
input deviation2 = 2.0;
input deviation1  = 1.8;
input fullRange = Yes;
input length = 21;

def regression;
def stdDeviation;
if (fullRange) {
    regression = InertiaAll(price);
    stdDeviation = StDevAll(price);
} else {
    regression = InertiaAll(price, length);
    stdDeviation = StDevAll(price, length);

plot UpperLine = regression + deviation2 * stdDeviation;
plot MiddleLine = regression;
plot LowerLine = regression - deviation2 * stdDeviation;


def regression1;
def stdDeviation1;
if (fullRange) {
    regression1 = InertiaAll(price);
    stdDeviation1 = StDevAll(price);
} else {
    regression1 = InertiaAll(price, length);
    stdDeviation1 = StDevAll(price, length);

plot UpperLine1 = regression1 + deviation1 * stdDeviation1;
plot MiddleLine1 = regression1;
plot LowerLine1 = regression1 - deviation1 * stdDeviation1;
Alert(pricetype.mark < UpperLine1, "BEARISH!", Alert.BAR, Sound.Bell);


New member
Ok, thank you so much.

Side note for why I wanted to use mark was because I was planning on using longer term charts with something like 1day to 1 week aggregation period but I wanted to see about getting alerts on short aggregation periods.

So then I think I would just need something like close( period = AggregationPeriod.Hour) instead, right? Sorry, not at my thinkorswim computer, but I will test it later and let you know how it worked. I appreciate the help Ben.


New member
Hi everyone, I searched on the forum but there was not result for my question. Maybe I could find it here .

Would that be possible to tweak this alert into a scanner ?

I would like to scan stocks with prices BELOW the Lowerline of the "StandardDevChannel" study (see picture below)

I tried but I don't have results so I guess I am not doing the correct settings.

I'm new in the script and tweaking world, don't be rude to me lol. Thank you:)


New member
I believe this can be done. I might try first, changing the length, doing a few value such as 10,20,50,75,100,200. Also, I have noticed, that a lot of std trends hold up much stronger on a long term lengths compared to what you have right now. You have aggregation period as 1 minute, and length of 21 minutes(since aggregation period is how big every 1 length is). That means that is only scanning the past 21 minutes for this alert. If you change to larger aggregation periods and much larger lengths, you might get some results. I would recommend a length of 50x (or up to 200x or more) the aggregation period so that daily channels will be revealed for the past few hours. The length is the number of aggregation periods that it uses to calculate the channel, so if you have an aggregation period of 1 minute, a length of 21 will only see channels that have gone to lower than -2 std in the last 21 minutes, and this trend, from what I have observed is more valid over long time frames, such as, with lengths that are 90-100 days for recent market conditions(outliers when corona virus began completely changed a lot of sectors to be outside of the 2 std channel.I will try this the next day or two and post results as it is something I have been trying to do and for some reason I didn't think about using the built in stdchannel. Thank you for asking.


New member
Also, don't worry about people being rude here. This is one of the most tolerative forums for what you might think are not smart questions. We are all here for the same reason, to learn thinkscript, and help each other get a few more percentages (or more) out of what we have currently. Anybody who treats you bad, ignore, be the bigger person and don't stoop to their level. I got your back if that's the case.


Forgive me if off-topic, but related to standard deviation.

Is there a way to create two labels that show on the chart to represent a 1 and a 2 standard deviation target?

Would base it on Previous Close (I think). If the PC was 10.00, as an example:

[1SD Target: H-10.65 / L-9.35] [2SD Target: H-11.30 / L-9.70] or something along those lines.

Just looking for an easy way to see potential movement during the intraday, as two SD covers like 98% of possible move (usually).



New member
Hi how are you guys? I hope fine and safe. Can you help to set an alert in a standard deviation channel?? I want to set an alert in in the upperline, middle line and lower line, so when the price reach one , TOS just make a sound...

Thanks a lot



New member
Hi, I'm looking for something like this too.. When price reaches the Upper and Lower band of the STDeviation Channel, to send an alert. Any help?

The code in post #1 does not seem to work for me