How to code alerts of certain indicators?


0
0

I’ve seen all of your videos, and first off I want to say thank you!! I’ve just started in the market, not fully established yet, but getting very close especially with your help setting TOS up. I promise once I acquire capital you’ll see my thanks in contribution. Before my question a side question, is there a way to attach other sounds instead of using the TOS standard sounds? With so many alerts its hard to differentiate each sound when they’re all similar..

As for my question: What is the normal code (one I could use on others as well) used to make alerts sound off and show up. I know the basics of coding, so if you can easily attempt to inform me for future reference how to set it for others also great, if not that’s okay too. I’ve attempted to just copy and paste to use on the indicator I’m trying to use but it’s in error. I want it to alert me when the bar comes into immediate contact with a parabolic bar. The indicator is a parabolic custom as pasted:

input direction = {default “Long”};

input accelerationFactor = 0.02;
input accelerationLimit = 0.2;

assert(accelerationFactor > 0, “‘acceleration factor’ must be positive: ” + accelerationFactor);
assert(accelerationLimit >= accelerationFactor, “‘acceleration limit’ (” + accelerationLimit + “) must be greater than or equal to ‘acceleration factor’ (” + accelerationFactor + “)”);

def state = {default init, long, short};
def extreme;
def SAR;
def acc;

switch (state[1]) {
case init:
state = state.long;
acc = accelerationFactor;
extreme = high;
SAR = low;
case short:
if (SAR[1] < high)
then {
state = state.long;
acc = accelerationFactor;
extreme = high;
SAR = extreme[1];
} else {
state = state.short;
if (low < extreme[1])
then {
acc = min(acc[1] + accelerationFactor, accelerationLimit);
extreme = low;
} else {
acc = acc[1];
extreme = extreme[1];
}
SAR = max(max(high, high[1]), SAR[1] + acc * (extreme – SAR[1]));
}
case long:
if (SAR[1] > low)
then {
state = state.short;
acc = accelerationFactor;
extreme = low;
SAR = extreme[1];
} else {
state = state.long;
if (high > extreme[1])
then {
acc = min(acc[1] + accelerationFactor, accelerationLimit);
extreme = high;
} else {
acc = acc[1];
extreme = extreme[1];
}
SAR = min(min(low, low[1]), SAR[1] + acc * (extreme – SAR[1]));
}
}

plot parSAR = SAR;
parSAR.SetPaintingStrategy(PaintingStrategy.POINTS);
parSAR.SetDefaultColor(GetColor(5));

# Alerts:
#
def alerttrigger= if direction[1]<=0 and longswitch then 1
else if direction[1]>=0 and shortswitch then 1 else 0;
input alerttext=”Alert Text”;
# BLOCK CODE BELOW
input UseAlerts = {false, default true};
input AlertType = {default “BAR”, “ONCE”, “TICK”};
def at=AlertType;
input AlertSound = {“Bell”, “Chimes”, default “Ding”, “NoSound”, “Ring”};
alert(alerttrigger AND UseAlerts, alerttext, if at==1 then Alert.ONCE else if at==2 then Alert.TICK else Alert.BAR, AlertSound);

Marked as spam
Posted by (Questions: 2, Answers: 1)
Asked on March 11, 2017 2:07 pm
971 views
0
Private answer

Sorry the answer to your first question is that for chart based alerts we can only use the sounds provided by the Thinkorswim platform.

For my solution, I assumed you are using the standard built in study named, ParabolicSAR. It is really great that you provided your code, working or not. This really helps viewers to understand what you were trying to accomplish. Rather than try to fix the code you have provided I decided it would be much easier to understand if I simply provided the lines of code you would add to the built in version.

Before posting the solution I will mention a few things to help you understand where you went astray. The alert condition, must be derived from a true/false condition. And it must be derived from elements that are dynamic (think of plots as being dynamic elements on the chart). You were trying to use an input for your alert condition.

From your code:

if direction[1]<=0 and longswitch then 1 else if direction[1]>=0 and shortswitch then 1 else 0;

You see where you typed “direction[1]”? That is an input. It’s only possible value is “Long” based on your code. You are testing if it is less than or equal to zero. This can never occur.

The other item I will mention is that I see you went to great lengths to provide user adjustable inputs for alert type and alert sound, among others. This is redundant as these adjustments are already provided within the Edit Studies screen.

So here it is. Just four lines of code. Just copy the code from the standard built in study and add these four lines to provide alerts. If you really want to learn how to work with alerts, I suggest you try working with a much less complex indicator than this one. There is just too much going on in this code for the novice to absorb.

Don’t forget to up-vote any answers that best solve your question! Post in the comments below if you need further explanation or adjustments.

def alertLongParSAR = state[1] == state.short and state == state.long;
def alertShortParSAR = state[1] == state.long and state == state.short;
Alert(alertLongParSAR, "PSAR Long", Alert.BAR, Sound.RING);
Alert(alertShortParSAR, "PSAR Short", Alert.BAR, Sound.RING);

 

Marked as spam
Posted by (Questions: 37, Answers: 4087)
Answered on March 11, 2017 11:05 pm
0
Pete - I took the original PSAR code and added your four lines. How do I edit the condition to trigger the alert? I select long position "is true" and I keep getting a "rec usage is not allowed in this context." What am I doing wrong? I'm trying to couple this alert with the Ichi T/K and cloud strategy entries (that do work). Thanks!
( at August 8, 2019 8:29 am)
0
This message can only appear of you are trying to apply this code to a Study Alert or a Conditional order. In which case this is impossible because recursion is not permitted in Study Alerts or Conditional Orders and there is no work around.
( at August 8, 2019 10:26 am)