How to Alert on Change of color

With this code below - how can I sound/send an Alert based on whether MA.Color is Up or Down

Thank you for your guidance

plot MA = average;
MA.SetPaintingStrategy(PaintingStrategy.LINE_VS_POINTS);
MA.SetLineWeight(2);

MA.DefineColor("Up", Color.DARK_GREEN);
MA.DefineColor("Down", Color.RED);
MA.DefineColor("Flat", Color.GRAY);

MA.AssignValueColor(if MA[0] > MA[1] then MA.Color("Up") else if MA[0] <
MA[1] then MA.Color("Down") else MA.Color("Flat"));
 
@MatthewTherrien You would use the same logic that the MA.AssignValueColor() conditional logic code is using to trigger your alerts...
I appreciate you replying but as I don't code - I don't know how to write the correct syntax -

I fumbled around with if Alert(MA.Color("Up"), text = "Buy HA Signal", sound = Sound.Bell, "alert type" = Alert.BAR);

but that is a wrong guess at the syntax

Thank you for your help - it is appreciated
 

rad14733

Well-known member
VIP
I appreciate you replying but as I don't code - I don't know how to write the correct syntax -

I fumbled around with if Alert(MA.Color("Up"), text = "Buy HA Signal", sound = Sound.Bell, "alert type" = Alert.BAR);

but that is a wrong guess at the syntax

Thank you for your help - it is appreciated

The only way to learn is to keep trying... That's how we all learn everything we do in life... This is a single line of code, not an entire script so you can do this... ;) (y) Have you referenced the Alert() function in the Thinkscript Learning Center...??? There are also more than a few examples of the implementation of Alert() throughout these forums for reference...
 

rad14733

Well-known member
VIP
Yes I did check the the Alert() function in the Thinkscript Learning Center - I still can't work it out - I am 70 and not as sharp as I once was :)

But thank you anyway - I will just do without Alerts

@MatthewTherrien I hear you... I'll be 61 this summer myself... Still sharp but need to realize my physical age... Nursing an aching back today from harvesting firewood yesterday... I've been coding since 1984 so I'm used to figuring things out across multiple languages... Show me your latest efforts and I'll get you going and then maybe things will start to click for you... We've got to keep at this stuff to stay sharp...
 
@MatthewTherrien I hear you... I'll be 61 this summer myself... Still sharp but need to realize my physical age... Nursing an aching back today from harvesting firewood yesterday... I've been coding since 1984 so I'm used to figuring things out across multiple languages... Show me your latest efforts and I'll get you going and then maybe things will start to click for you... We've got to keep at this stuff to stay sharp...
Yep bad back too :) I stay sharp by being a very active and heavy TOS Options Trader - trading mostly SPX and now some /mnq /mes on Tradovate

The best I could work out is - which Thinkscript tells me is wrong:

Alert(MA.Color("Up"), text = "Buy HA Signal", sound = Sound.Bell, "alert type" = Alert.BAR);
Alert(MA.Color("Down", text = "HA Momentum_Down", sound = Sound.Bell, "alert type" = Alert.BAR);

Thanks again
 
Code:
##############################################
#
#     Calculate and Plot Moving Average With Colors
#     by Martin Begley
#
##############################################

input length = 30;
input price = close;
input AverageType = {Default Simple, Exponential, Weighted, Wilders, Hull};

def average;
switch (AverageType) {
case Simple:
    average = Average(price, length);
case Exponential:
    average = ExpAverage(price, length);
case Weighted:
    average = wma(price, length);
case Wilders:
    average = WildersAverage(price, length);
case Hull:
    average = HullMovingAvg(price, length);
}

plot MA = average;
MA.SetPaintingStrategy(PaintingStrategy.LINE_VS_POINTS);
MA.SetLineWeight(2);

MA.DefineColor("Up", Color.DARK_GREEN);
MA.DefineColor("Down", Color.RED);
MA.DefineColor("Flat", Color.GRAY);

MA.AssignValueColor(if MA[0] > MA[1] then MA.Color("Up") else if MA[0] <
MA[1] then MA.Color("Down") else MA.Color("Flat"));

# My Attempt At Alerts

Alert(MA.Color("Up"), text = "Buy HA Signal", sound = Sound.Bell, "alert type" = Alert.BAR);
Alert(MA.Color("Down", text = "HA Momentum_Down", sound = Sound.Bell, "alert type" = Alert.BAR);
 

rad14733

Well-known member
VIP
@MatthewTherrien The following code should work as you desire... Notice how I used just the conditional logic as the first parameter, the text to display as the second, the alert type as the third, and the sound as the fourth... Keeping the parameters in proper order negates the need to include the parameter descriptor... Also, the use of [0] for variables and prices is redundant as when omitted, the [0] index is implicit... That said, the use won't present problems... Let me know how this performs for you... Good luck...

Ruby:
##############################################
#
# Calculate and Plot Moving Average With Colors
# by Martin Begley
#
##############################################

input length = 30;
input price = close;
input AverageType = {Default Simple, Exponential, Weighted, Wilders, Hull};

def average;
switch (AverageType) {
case Simple:
average = Average(price, length);
case Exponential:
average = ExpAverage(price, length);
case Weighted:
average = wma(price, length);
case Wilders:
average = WildersAverage(price, length);
case Hull:
average = HullMovingAvg(price, length);
}

plot MA = average;
MA.SetPaintingStrategy(PaintingStrategy.LINE_VS_POINTS);
MA.SetLineWeight(2);

MA.DefineColor("Up", Color.DARK_GREEN);
MA.DefineColor("Down", Color.RED);
MA.DefineColor("Flat", Color.GRAY);

MA.AssignValueColor(if MA[0] > MA[1] then MA.Color("Up") else if MA[0] <
MA[1] then MA.Color("Down") else MA.Color("Flat"));

# My Attempt At Alerts

Alert(MA > MA[1], "Buy HA Signal", Alert.BAR, Sound.Bell);
Alert(MA < MA[1], "HA Momentum_Down", Alert.BAR, Sound.Bell);
 
@MatthewTherrien The following code should work as you desire... Notice how I used just the conditional logic as the first parameter, the text to display as the second, the alert type as the third, and the sound as the fourth... Keeping the parameters in proper order negates the need to include the parameter descriptor... Let me know how this performs for you... Good luck...

Ruby:
##############################################
#
# Calculate and Plot Moving Average With Colors
# by Martin Begley
#
##############################################

input length = 30;
input price = close;
input AverageType = {Default Simple, Exponential, Weighted, Wilders, Hull};

def average;
switch (AverageType) {
case Simple:
average = Average(price, length);
case Exponential:
average = ExpAverage(price, length);
case Weighted:
average = wma(price, length);
case Wilders:
average = WildersAverage(price, length);
case Hull:
average = HullMovingAvg(price, length);
}

plot MA = average;
MA.SetPaintingStrategy(PaintingStrategy.LINE_VS_POINTS);
MA.SetLineWeight(2);

MA.DefineColor("Up", Color.DARK_GREEN);
MA.DefineColor("Down", Color.RED);
MA.DefineColor("Flat", Color.GRAY);

MA.AssignValueColor(if MA[0] > MA[1] then MA.Color("Up") else if MA[0] <
MA[1] then MA.Color("Down") else MA.Color("Flat"));

# My Attempt At Alerts

Alert(MA[0] > MA[1], "Buy HA Signal", Alert.BAR, Sound.Bell);
Alert(MA[0] < MA[1], "HA Momentum_Down", Alert.BAR, Sound.Bell);
Thank you so much - and I can see how it works now - thanks to your explanation - so I did learn something after all :)
 
Can I ask you for one further lesson?

I would like to display an Up Arrow if MA.Color is Up and Down Arrow for the reverse

I have looked at the Thinkorswim documentation for SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP); but I can't work out how to use this with those MA variables

I have an example that does what I want with another script:

Code:
plot Bullish = BullishChange;
Bullish.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
Bullish.SetDefaultColor(Color.LIGHT_GRAY);
Bullish.SetLineWeight(2);

plot Bearish = BearishChange;
Bearish.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
Bearish.SetDefaultColor(Color.ORANGE);
Bearish.SetLineWeight(2);
#Bullish.SetStyle(Curve.SHORT_DASH);
#Bullish.AssignValueColor(Color.LIGHT_GRAY);

But again, I can't see how to do it with the code you modified for the alerts

BTW I thought it right to acknowledge you in the study in case I share it with my Trading Group

# Alerts Thanks to rad14733 from UseThinkscript

Thank you again for your patience and help
 

rad14733

Well-known member
VIP
@MatthewTherrien I added corrected crossovers as well as inputs to enable/disable both the alerts and crossovers... You should have plenty to learn from in the added code...

Ruby:
##############################################
#
# Calculate and Plot Moving Average With Colors
# by Martin Begley
#
##############################################

input length = 30;
input price = close;
input AverageType = {Default Simple, Exponential, Weighted, Wilders, Hull};
input showAlerts = yes;
input showCrossovers = yes;

def average;
switch (AverageType) {
case Simple:
average = Average(price, length);
case Exponential:
average = ExpAverage(price, length);
case Weighted:
average = wma(price, length);
case Wilders:
average = WildersAverage(price, length);
case Hull:
average = HullMovingAvg(price, length);
}

plot MA = average;
MA.SetPaintingStrategy(PaintingStrategy.LINE_VS_POINTS);
MA.SetLineWeight(2);

MA.DefineColor("Up", Color.DARK_GREEN);
MA.DefineColor("Down", Color.RED);
MA.DefineColor("Flat", Color.GRAY);

MA.AssignValueColor(if MA[0] > MA[1] then MA.Color("Up") else if MA[0] <
MA[1] then MA.Color("Down") else MA.Color("Flat"));

# Alerts

Alert(showAlerts and MA[0] > MA[1], "Buy HA Signal", Alert.BAR, Sound.Bell);
Alert(showAlerts and MA[0] < MA[1], "HA Momentum_Down", Alert.BAR, Sound.Bell);

# Crossovers

plot Bullish = if showCrossovers and price crosses above MA then 1 else double.NaN;
Bullish.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
Bullish.SetDefaultColor(Color.LIGHT_GRAY);
Bullish.SetLineWeight(2);

plot Bearish = if showCrossovers and price crosses below MA then 1 else Double.NaN;
Bearish.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
Bearish.SetDefaultColor(Color.ORANGE);
Bearish.SetLineWeight(2);

# END
 
@MatthewTherrien I added corrected crossovers as well as inputs to enable/disable both the alerts and crossovers... You should have plenty to learn from in the added code...

Ruby:
##############################################
#
# Calculate and Plot Moving Average With Colors
# by Martin Begley
#
##############################################

input length = 30;
input price = close;
input AverageType = {Default Simple, Exponential, Weighted, Wilders, Hull};
input showAlerts = yes;
input showCrossovers = yes;

def average;
switch (AverageType) {
case Simple:
average = Average(price, length);
case Exponential:
average = ExpAverage(price, length);
case Weighted:
average = wma(price, length);
case Wilders:
average = WildersAverage(price, length);
case Hull:
average = HullMovingAvg(price, length);
}

plot MA = average;
MA.SetPaintingStrategy(PaintingStrategy.LINE_VS_POINTS);
MA.SetLineWeight(2);

MA.DefineColor("Up", Color.DARK_GREEN);
MA.DefineColor("Down", Color.RED);
MA.DefineColor("Flat", Color.GRAY);

MA.AssignValueColor(if MA[0] > MA[1] then MA.Color("Up") else if MA[0] <
MA[1] then MA.Color("Down") else MA.Color("Flat"));

# Alerts

Alert(showAlerts and MA[0] > MA[1], "Buy HA Signal", Alert.BAR, Sound.Bell);
Alert(showAlerts and MA[0] < MA[1], "HA Momentum_Down", Alert.BAR, Sound.Bell);

# Crossovers

plot Bullish = if showCrossovers and price crosses above MA then 1 else double.NaN;
Bullish.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
Bullish.SetDefaultColor(Color.LIGHT_GRAY);
Bullish.SetLineWeight(2);

plot Bearish = if showCrossovers and price crosses below MA then 1 else Double.NaN;
Bearish.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
Bearish.SetDefaultColor(Color.ORANGE);
Bearish.SetLineWeight(2);

# END
That is totally brilliant - you are a real Gent :) and I will definitely study and digest your enhancements. If I can do something for you in return -please let me know

Matthew
 

Similar threads

Top