Alert using TOS Daily SMA


0
0

Sir,

I have been looking for a way to utilize an alert from the DailySMA within TOs, without having to set it once triggered, or changing betweren symbols. I have complied the code below to try within the study (portion from TOS), but not getting the alerts/ sounds. Can this be edited to work?

 

input price = FundamentalType.CLOSE;
input aggregationPeriod = AggregationPeriod.DAY;
input length = 20;
input displace = 0;
input showOnlyLastPeriod = no;

plot DailySMA;

if showOnlyLastPeriod and !IsNaN(close(period = aggregationPeriod)[-1]) {
DailySMA = Double.NaN;
} else {
DailySMA = Average(fundamental(price, period = aggregationPeriod)[-displace], length);
}

DailySMA.SetDefaultColor(GetColor(1));
DailySMA.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

plot UpSignal = price crosses above DailySMA;
plot DownSignal = price crosses below DailySMA;

Alert(UpSignal, “Price crossing above”, Alert.BAR, Sound.Chimes);
Alert(DownSignal, “Price crossing below”, Alert.BAR, Sound.Bell);

Marked as spam
Posted by (Questions: 1, Answers: 1)
Asked on February 2, 2022 7:04 am
136 views
0
Private answer

Sorry but the code you provided had some serious flaws that could not be corrected without radically overhauling the entire thing. So I generated my own solution from scratch. And it actually took me less time to build from scratch than trying to fix the code you provided.

If you are interested in learning I will point out one flaw that was preventing your code from working:

plot UpSignal = price crosses above DailySMA;
plot DownSignal = price crosses below DailySMA;

Those two lines should be as follows:

plot UpSignal = Fundamental(price, period = aggregationPeriod) crosses above DailySMA;
plot DownSignal = Fundamental(price, period = aggregationPeriod) crosses below DailySMA;

But that only works if you set the chart to a daily time frame. When you set it to an intraday time frame it creates even more problems than your original solution. The only way to do this properly, so that it works on intraday time frames as well as daily time frame, is to use recursive variables.

Here is the solution that works as you intended. However it is missing a few features. Such as it does not have an input to change the price type and it does not include any way to displace the moving average or force it to only display on the last period.

input maLengthOne = 20;
input maTypeOne = AverageType.SIMPLE;
input timeFrameOne = AggregationPeriod.DAY;
plot maOne = MovingAverage(maTypeOne, close(period = timeFrameOne), maLengthOne);
rec trackUpSignal = if close[1] < maOne[1] and close > maOne then 1 else 0;
rec trackDownSignal = if close[1] > maOne[1] and close < maOne then 1 else 0;
plot upSignal = trackUpSignal and !trackUpSignal[1];
upSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
upSignal.SetDefaultColor(Color.CYAN);
upSignal.SetLineWeight(3);
plot downSignal = trackDownSignal and !trackDownSignal[1];
downSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
downSignal.SetDefaultColor(Color.MAGENTA);
downSignal.SetLineWeight(3);
Alert(upSignal, "Price crossing above", Alert.BAR, Sound.RING);
Alert(downSignal, "Price crossing below", Alert.BAR, Sound.RING);

Marked as spam
Posted by (Questions: 37, Answers: 4084)
Answered on February 2, 2022 8:55 am
0
Thank you for the help on the code. I would love to understands more on what I had done wrong. I kind of pieced two together that I found in TOS and online, so I understand why it was wrong. I have put in the code, and will let you know Monday on how it works out! Your site and help is greatly appreciated!
( at February 4, 2022 9:11 am)