Adding a Background Flash with an Alert

becker908

New member
I am an options day trader and one of the key indicators I use is a 9ema cross of 50ma on a five minute chart. I have ThinkScript that builds a cloud between the moving averages and sends an alert and draws a vertical line on the chart when the cross happens. I want the chart to flash - change background color - when the alert fires because I have a large screen with a dozen imbedded charts and I'd like to have a visual alert as well as the audible ring. The ThinkScript works like a charm except the AssignBackgroundColor command doesn't work on the upward cross. As I understand AssignBackgroundColor (and it works on the downward cross) it should fire and hold the new color for one bar cycle (five minutes in this case). Here's the code...

#----- EMA/SMA CROSSOVERS MAX ONLY
#-----MRB 210530

# Purpose: Display Cloud for fast moving average (9 periods) vs slow moving average (50 periods) and provide alerts for moving average crosses. Alert with vertical lines, audible alerts, and screen flash one bar after fast MA crosses slow MA on 5min chart.

#----- Define Variables
def price = close;
def fastL = 9;
def slowL = 50;
plot fastAvg = MovingAverage(AverageType.EXPONENTIAL, close, fastL);
plot slowAvg = MovingAverage(AverageType.SIMPLE, close, slowL);
def isCall = Crosses(fastAvg[1], slowAvg[1], CrossingDirection.ABOVE); #<---9MA crosses 50MA forming GreenCloud, Call buy signal
def isPut = Crosses(fastAvg[1], slowAvg[1], CrossingDirection.BELOW); #<---9MA crosses 50MA forming RedCloud, Put buy signal

#----- Define Look&Feel for moving averages
slowAvg.SetPaintingStrategy(PaintingStrategy.LINE); #<---Define L/F for M50
slowAvg.SetLineWeight(4);
fastAvg.SetLineWeight(2);

slowAvg.DefineColor("Up", Color.LIME); #<---Trend UP = Green, trend DOWN = RED, trend FLAT = WHITE
slowAvg.DefineColor("Down", Color.RED);
slowAvg.DefineColor("Flat", Color.WHITE);

slowAvg.AssignValueColor(if slowAvg[1] > slowAvg[3] * 1.00008 then slowAvg.Color("Up") else if slowAvg[1] <
(slowAvg[3] * 0.99992) then slowAvg.Color("Down") else slowAvg.Color("Flat")); #<---Define color values for slow average based on trend
fastAvg.AssignValueColor(Color.CYAN); #Define look for fast average
AddCloud(fastAvg, slowAvg, Color.LIGHT_GREEN, Color.LIGHT_RED); #<---Define cloud

fastAvg.HideBubble();
slowAvg.HideBubble();

#----- Vertical Lines for MAX CALL / PUT Alerts display Alerts
AddVerticalLine(isCall, "", Color.GREEN, Curve.LONG_DASH); #<---Alerts for Call Buy
Alert(Crosses(fastAvg[1], slowAvg[1], CrossingDirection.ABOVE), "MAX CALL", Alert.BAR, Sound.Ring);
AssignBackgroundColor(if isCall then Color.LIGHT_GREEN else Color.BLACK); <--- THIS DOES NOT WORK

AddVerticalLine(isPut, "", Color.RED, Curve.LONG_DASH); #<---Alerts for Put Buy
Alert(Crosses(fastAvg[1], slowAvg[1], CrossingDirection.BELOW), "MAX PUT", Alert.BAR, Sound.Ring);
AssignBackgroundColor(if isPut then Color.LIGHT_RED else Color.BLACK); <--- THIS DOES WORK

#----- END CODE

The highlighted two lines of script are the commands in question. I've also substituted "Crosses(fastAvg[1], slowAvg[1], CrossingDirection[**],"*",Alert.BAR, Sound.Ring)" for the "isCall" and "isPut" and that doesn't work either.

Thank you in advance for any help on this, it's driving me crazy. :cool:
 
Last edited:
@becker908 Where did you come up with the assumption that AssignBackgroundColor() only holds for one bar...??? The change is based on the conditional criteria you decide to use and the color remains until you change it again with different conditional criteria... There should be several example of MA crossovers changing background color here in the forums... I know I've posted one or two examples myself...

Also, there is no flashing feature within Thinkorswim...

crosses above and crosses below are the two conditions that work best for color changes...
 
@rad14733 The AssignBackgroundColor() in this case only works for one bar because it is based on a conditional criteria for only one bar. When the next bar paints the criteria result changes.

I'm referring to what happens to the chart as a "flash" only because when the 9ema crosses the 50ma to down, the chart background changes to red. That works. When it crosses to up, the background does not change.
 
See if the following works better than Crosses(), which is rarely used in scripts...

Ruby:
AssignBackgroundColor(if fastAvg crosses above slowAvg then Color.LIGHT_GREEN else if fastAvg crosses below slowAvg then Color.RED else Color.BLACK);
 
may 31 , pulses , test file

here is a lower test study, backcolorflash_test_00_lower, to test changing the background color.
after iscall or isput is true, 3 true pulses ( current bar, 3rd bar, 5th bar) will be generated over 6 bars, to trigger a change in background color, 3 times. the default quantity of bars for pulses, a master enable signal, is 7.
there is an input, to show the test data pulses, 3 pulses and a master enable signal.

if this is used, all other studies with a backgroundcolor function should be disabled with 'show study' unchecked

observations i have seen using AssignBackgroundColor().
1. if there is more than 1 enabled study with AssignBackgroundColor(), the study closest to the top of the list, will control the backcolor.
2. there should be just 1 AssignBackgroundColor() function in a study. if you need to switch between several colors, create a if-then-else to pick the desired color, in one function.
3. if there are no conditions true to pick a color, use color.current, to show the default background.

test data
since the market is closed now, i haven't seen this change the color 3 times. but i was able to find several stocks that are triggered right now.

sym: BIIB
time: 5 minutes
after hours: on
triggered by iscall, screen is light green

sym: CHGG
time: 15 minutes
after hours: on
triggered by isput, screen is light red

sym: CMG
time: 2 minutes
after hours: on
triggered by isput, screen is light red

this test study is a copy of the original, with the original AssignBackgroundColor() functions and plots, disabled.

Python:
# backcolorflash_test_00_lower

# 21-05-30
# halcyonguy
# test backcolor, pulses

# https://usethinkscript.com/threads/adding-a-background-flash-with-an-alert.6741/#post-65218

#----- EMA/SMA CROSSOVERS MAX ONLY
#-----MRB 210530

# Purpose: Display Cloud for fast moving average (9 periods) vs slow moving average (50 periods) and provide alerts for moving average crosses. Alert with vertical lines, audible alerts, and screen flash one bar after fast MA crosses slow MA on 5min chart.


declare lower;

#----- Define Variables
def price = close;
def fastL = 9;
def slowL = 50;
#plot fastAvg = MovingAverage(AverageType.EXPONENTIAL, close, fastL);
#plot slowAvg = MovingAverage(AverageType.SIMPLE, close, slowL);
def fastAvg = MovingAverage(AverageType.EXPONENTIAL, close, fastL);
def slowAvg = MovingAverage(AverageType.SIMPLE, close, slowL);

def isCall = Crosses(fastAvg[1], slowAvg[1], CrossingDirection.ABOVE); #<---9MA crosses 50MA forming GreenCloud, Call buy signal
def isPut = Crosses(fastAvg[1], slowAvg[1], CrossingDirection.BELOW); #<---9MA crosses 50MA forming RedCloud, Put buy signal

#plot z = 0;
#plot c = iscall * 0.5;
#plot p = -isput * -0.5;

#----- Define Look&Feel for moving averages
#slowAvg.SetPaintingStrategy(PaintingStrategy.LINE); #<---Define L/F for M50
#slowAvg.SetLineWeight(4);
#fastAvg.SetLineWeight(2);

#slowAvg.DefineColor("Up", Color.LIME); #<---Trend UP = Green, trend DOWN = RED, trend FLAT = WHITE
#slowAvg.DefineColor("Down", Color.RED);
#slowAvg.DefineColor("Flat", Color.WHITE);

#slowAvg.AssignValueColor(if slowAvg[1] > slowAvg[3] * 1.00008 then slowAvg.Color("Up") else if slowAvg[1] < (slowAvg[3] * 0.99992) then slowAvg.Color("Down") else slowAvg.Color("Flat")); #<---Define color values for slow average based on trend
#fastAvg.AssignValueColor(Color.CYAN); #Define look for fast average

#AddCloud(fastAvg, slowAvg, Color.LIGHT_GREEN, Color.LIGHT_RED); #<---Define cloud

#fastAvg.HideBubble();
#slowAvg.HideBubble();

#----- Vertical Lines for MAX CALL / PUT Alerts display Alerts
#AddVerticalLine(isCall, "", Color.GREEN, Curve.LONG_DASH); #<---Alerts for Call Buy
AddVerticalLine(isCall, "", Color.cyan, Curve.LONG_DASH); #<---Alerts for Call Buy
Alert(Crosses(fastAvg[1], slowAvg[1], CrossingDirection.ABOVE), "MAX CALL", Alert.BAR, Sound.Ring);

#AssignBackgroundColor(if isCall then Color.LIGHT_GREEN else Color.BLACK);  #<---

#AddVerticalLine(isPut, "", Color.RED, Curve.LONG_DASH); #<---Alerts for Put Buy
AddVerticalLine(isPut, "", Color.cyan, Curve.LONG_DASH); #<---Alerts for Put Buy
Alert(Crosses(fastAvg[1], slowAvg[1], CrossingDirection.BELOW), "MAX PUT", Alert.BAR, Sound.Ring);
#AssignBackgroundColor(if isPut then Color.LIGHT_RED else Color.BLACK);

#----- END CODE


# new stuff

# notes about AssignBackgroundColor() useage
# if there is more than 1 enabled study with AssignBackgroundColor(), the study closest to the top of the list, will control the backcolor.
#  should be just 1 AssignBackgroundColor() function in a study.
 
# calc a series of 1 bar pulses
#  for iscall and isput
#  determine master on time (qty of bars)

def na = double.nan;
def bn = barnumber();
input bars_to_pulse = 7;

def triggerup = if (iscall and !iscall[1]) then bn else triggerup[1];
# add 2 , to use even, to find 1st bar
def cntup = ((bn - triggerup) + 2);
def evenup = if rounddown( cntup/2, 0 ) == cntup/2 then 1 else 0;
def master_up = if (cntup > bars_to_pulse) then 0 else 1;
def pulsesup2 = (master_up and evenup);

def triggerdwn = if (isput and !isput[1]) then bn else triggerdwn[1];
# add 2 , to use even, to find 1st bar
def cntdwn = ((bn - triggerdwn) + 2);
def evendwn = if rounddown( cntdwn/2, 0 ) == cntdwn/2 then 1 else 0;
def master_dwn = if (cntdwn > bars_to_pulse) then 0 else 1;
def pulsesdwn2 = (master_dwn and evendwn);

AssignBackgroundColor( if pulsesup2 then Color.LIGHT_GREEN
 else if pulsesdwn2 then Color.LIGHT_RED
 else Color.current);

# test data -------------------------------
#addchartbubble(yes,0.5, trigger + "\n" + cnt1 , color.yellow, no);
input show_pulses = no;

plot pulsesup = if show_pulses then pulsesup2 else na;
plot mup = if show_pulses then (master_up * 0.7) else na;

plot pulsesdwn = if show_pulses then (pulsesdwn2 * -1) else na;
plot mdwn = if show_pulses then (master_dwn * -0.7) else na;
plot z = 0;
#
---study

test data, image of trigger pulses
QaWjXyZ.jpg

hal_pulse
 
Last edited:

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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