Moving Average Crossovers For ThinkOrSwim

Brother thank you very much. It is rare for someone to go way out of their way... Very appreciative.

There is actually more to modifying the code above for DownTrends... You would actually reverse all of the code, meaning averages should read "is less than" instead of "is greater than" and then "crosses below" instead of "crosses above"... Just in case you didn't already figure that out...
 
There is actually more to modifying the code above for DownTrends... You would actually reverse all of the code, meaning averages should read "is less than" instead of "is greater than" and then "crosses below" instead of "crosses above"... Just in case you didn't already figure that out...
Yes did that without your prompting. haha. Ill be running it all day tomorrow and see how it goes. There is a particular edge when they happen to cross at market open or just before.
 
Good Day all,

I have some code for a EMA cross over. Its Great, how ever I would like the LABEL it to change color. from a neutral YELLOW to
fastMA = GREEN on a buy signal.
slowMA = RED on a sell signal.

I pasted the code bellow:

Thnks.

-------------------------------------------------------------------------------------------------------------------


Code:
#TOS Indicators
#Full tutorial here: https://tosindicators.com/strategy/moving-average-crossover

input longEntries = yes;
input shortEntries = no;

input fastMALength = 8;
input slowMALength = 21;
input averageTypeFast = AverageType.Exponential;
input averageTypeSlow = AverageType.Exponential;

input quantity = 100;

plot fastMA = MovingAverage(averageTypeFast, close, fastMALength);
plot slowMA = MovingAverage(averageTypeSlow, close, slowMALength);

#8 EMA x 21 EMA
AddLabel(yes, fastMALength + (if averageTypeFast == 0 then " Simple" else if averageTypeFast == 1 then " EMA" else if averageTypeFast == 2 then " Weighted" else if averageTypeFast == 3 then " Wilders" else " Hull") + " x " + slowMALength + (if averageTypeSlow == 0 then " Simple" else if averageTypeSlow == 1 then " EMA" else if averageTypeSlow == 2 then " Weighted" else if averageTypeSlow == 3 then " Wilders" else " Hull"), color.yellow);

AddOrder(OrderType.Buy_Tpen, (longEntries) and fastMA crosses above slowMA, close, quantity);
AddOrder(OrderType.Sell_To_Close,fastMA crosses below slowMA, close, quantity);


AddOrder(OrderType.Sell_Tpen, (shortEntries) and fastMA crosses below slowMA, close, quantity);
AddOrder(OrderType.Buy_To_Close, fastMA crosses above slowMA, close, quantity);

#addOrder(OrderType.BUY_AUTO, no);
 
Last edited by a moderator:
@JohnWick4 Try this -

Code:
#TOS Indicators
#Full tutorial here: https://tosindicators.com/strategy/moving-average-crossover

input longEntries = yes;
input shortEntries = no;

input fastMALength = 8;
input slowMALength = 21;
input averageTypeFast = AverageType.Exponential;
input averageTypeSlow = AverageType.Exponential;

input quantity = 100;

plot fastMA = MovingAverage(averageTypeFast, close, fastMALength);
plot slowMA = MovingAverage(averageTypeSlow, close, slowMALength);

#8 EMA x 21 EMA
AddLabel(yes, fastMALength + (if averageTypeFast == 0 then " Simple" else if averageTypeFast == 1 then " EMA" else if averageTypeFast == 2 then " Weighted" else if averageTypeFast == 3 then " Wilders" else " Hull") + " x " + slowMALength + (if averageTypeSlow == 0 then " Simple" else if averageTypeSlow == 1 then " EMA" else if averageTypeSlow == 2 then " Weighted" else if averageTypeSlow == 3 then " Wilders" else " Hull"), if fastMA > slowMA then color.green else color.red);

AddOrder(OrderType.Buy_To_Open, (longEntries) and fastMA crosses above slowMA, close, quantity);
AddOrder(OrderType.Sell_To_Close,fastMA crosses below slowMA, close, quantity);


AddOrder(OrderType.Sell_To_Open, (shortEntries) and fastMA crosses below slowMA, close, quantity);
AddOrder(OrderType.Buy_To_Close, fastMA crosses above slowMA, close, quantity);
 
Good Evening all,

I am working on fine tuning my watch list. I have some code but would prefer that it has black text and color background however I
am not able to solve this yet.

Code:
#TOS Indicators

input longEntries = yes;
input shortEntries = no;

input fastMALength = 8;
input slowMALength = 21;
input averageTypeFast = AverageType.Exponential;
input averageTypeSlow = AverageType.Exponential;

input quantity = 100;

plot fastMA = MovingAverage(averageTypeFast, close, fastMALength);
plot slowMA = MovingAverage(averageTypeSlow, close, slowMALength);

#8 EMA x 21 EMA
AddLabel(yes, fastMALength + (if averageTypeFast == 0 then " Simple" else if averageTypeFast == 1 then " EMA" else if averageTypeFast == 2 then " Weighted" else if averageTypeFast == 3 then " Wilders" else " Hull") + " x " + slowMALength + (if averageTypeSlow == 0 then " Simple" else if averageTypeSlow == 1 then " EMA" else if averageTypeSlow == 2 then " Weighted" else if averageTypeSlow == 3 then " Wilders" else " Hull"), if fastMA > slowMA then color.green else color.red);


# this line is giving me issues. 

# AssignBackgroundColor(if fastMA then color.green else if slowMA then color.red else color.black);
 
Good Evening all,

I am working on fine tuning my watch list. I have some code but would prefer that it has black text and color background however I
am not able to solve this yet.

Code:
#TOS Indicators

input longEntries = yes;
input shortEntries = no;

input fastMALength = 8;
input slowMALength = 21;
input averageTypeFast = AverageType.Exponential;
input averageTypeSlow = AverageType.Exponential;

input quantity = 100;

plot fastMA = MovingAverage(averageTypeFast, close, fastMALength);
plot slowMA = MovingAverage(averageTypeSlow, close, slowMALength);

#8 EMA x 21 EMA
AddLabel(yes, fastMALength + (if averageTypeFast == 0 then " Simple" else if averageTypeFast == 1 then " EMA" else if averageTypeFast == 2 then " Weighted" else if averageTypeFast == 3 then " Wilders" else " Hull") + " x " + slowMALength + (if averageTypeSlow == 0 then " Simple" else if averageTypeSlow == 1 then " EMA" else if averageTypeSlow == 2 then " Weighted" else if averageTypeSlow == 3 then " Wilders" else " Hull"), if fastMA > slowMA then color.green else color.red);


# this line is giving me issues.

# AssignBackgroundColor(if fastMA then color.green else if slowMA then color.red else color.black);
some things to try

the variable conditions in AssignBackgroundColor() aren't true/false.
try AssignBackgroundColor(
if fastMA > slowMA then color.green else if fastMA < slowMA then color.red else color.gray)

for columns, you only want 1 output.
1 plot or 1 addlabel. you have several.
change the plots to def.

the color in addlabel() will be the font color.
the color in AssignBackgroundColor() will be the column cell color.
 
Last edited:
@JohnWick4 Research AssignValueColor() as it allows the contents of a watchlist column to be assigned... Then use AssignBackgroundColor() for the background... This works for Plots... For AddLabel() we cannot change text colors, only the label color...
 
in a column study, you can change the font color.
you set the font color with the color specified in an addlabel or plot.


this is a column study meant to be a programming example, to show 2 ways to change the font color in a column.
in a column, 1 of these is used for output. addlabel is used to display text, and plot is used to display a number.
the color specified will be the font color. i used a variable and random numbers. it can also be done by using specific color names in an if-then.

assignBackgroundColor(color.black) is used to change the background color of the column cell to black.

i made this to test both methods. edit the study and change the value of this variable, to change the output method. (default value is 1)
def use_label = 1
1, to use the addlabel() function to display data, with a font color, based on a random number, 1 to 10
0, to use the plot function to display data, with 3 defined colors, and the rest as yellow


image of both methods, addlabel and plot
E7pQNG0.jpg


Code:
# test_col_font_01

# column study
# this study is meant to be a programming example, to show 2 ways to change the font color in a column
# in a column, addlabel is used to display text, and plot is used to display a number.

# ----------------------
# halcyonguy
# 21-05-23
#  column - test
#  display different font colors
# ---------------------

# change the value of a variable, (0 or 1),
#  to choose the output method used to change the font color

def use_label = 1;

#   set use_label = 1 , to use the addlabel() function to display data,
#    with a font color, based on a random number, 1 to 10
#
#   set use_label = 0 , to use the plot function to display data,
#    with 3 defined colors, and the rest as yellow


# qty of colors
def qty = 9;
# create random numbers, 1 to 10
def c = round((random() * qty) + 1, 0);

# === addlabel =======================
# display test data in a column cell
# a variable in getcolor works with addlabel
addlabel(use_label,  c + " font color #" , getcolor(c));


# === plot =======================
plot z = if !use_label then c else double.nan;
# can define several colors, then reference the desired one
z.DefineColor("one", getcolor(4));
z.DefineColor("two", getcolor(5));
z.DefineColor("three", getcolor(6));
z.DefineColor("four", getcolor(7));
z.AssignValueColor(if !use_label and c == 1 then z.color("one")
else if !use_label and c == 2 then z.color("two")
else if !use_label and c == 3 then z.color("three")
else color.yellow);

# make background color black
assignBackgroundColor(Color.black);


# -- code experiments that didn't work

# this causes an error, dynamic value in getcolor, in defineglobalcolor
#  DefineGlobalColor ("rnd", getcolor(c));

# plot experiments
# these cause an error, dynamic value in getcolor, in plot commands
#  z.setdefaultcolor(getcolor(c));
#  z.DefineColor("two", GetColor(c));
#

column study link
test_col_font_01
http://tos.mx/LmWiHQU
hal_col
 
Last edited:
This is a script for an instant touch of the 21 ma (Not a close) is there a way to add to this 2 standard deviations from the 21 instant touch , Im looking to get alerted when something crosses outside the upper Bollinger Bands and lower Bollinger bands like i said not a close i would like to be alerted real time as its happening . Thanks Hope someone can use this script its great for scalping !


input SMA_Length = 21;

def ma = simpleMovingAvg("length"= Sma_Length);
def triggerLow = open > ma and low <= ma;
def triggerHigh = open < ma and high >= ma;
def MasterTrigger = TriggerHigh and TriggerLow;
plot SmaTouch = triggerHigh or triggerLow;
 
@Buckbull There is no "instant touch", as you describe it; something either crosses or it doesn't... You will get a signal or alert as soon as that happens rather than waiting for a candle/bar to close... In most instances, close serves as last until the candle/bar closes... I'll give your code a test to see how it performs...

You can reference the Bollinger Band Study and test for crossovers of the upper or lower band plots in the same manner as you could a moving average... There are multiple examples here in these forums so use the search feature and you should be able to find them...
 
@Buckbull There is no "instant touch", as you describe it; something either crosses or it doesn't... You will get a signal or alert as soon as that happens rather than waiting for a candle/bar to close... In most instances, close serves as last until the candle/bar closes... I'll give your code a test to see how it performs...

You can reference the Bollinger Band Study and test for crossovers of the upper or lower band plots in the same manner as you could a moving average... There are multiple examples here in these forums so use the search feature and you should be able to find them...
I might have used wrong terminology, as soon as a candle touches the 21 on my script it pops up in my watchlist
 
the studies I looked at in the search is a close above or below, I want to be alerted as it crosses over during real time not waiting for a close, Its a great scalping technique. @BenTen could you possibly point me in the direction please .
 
Crosses will alert immediately even when using close... As I stated in my previous post "In most instances, close serves as last until the candle/bar closes"...
 
Crosses will alert immediately even when using close... As I stated in my previous post "In most instances, close serves as last until the candle/bar closes"...
oh thanks for that I didnt know that , So the arrows appear on the candle as soon as it crosses outside the upper and lower band on the study i originally commented on ?
 
Hey traders...I just stumbled on a cool column that will turn GREEN if the price in your watchlist goes above 21EMA...or whatever EMA number you want to replace it with?

I did not write this so I can't take credit for it....found it on Google and the post was made by "Hahn-Tech, LLC" back in 2015. Since I searched for it here under that name and nothing popped up, I figured I'd share since I got so much good stuff from this site and members. If this custom column exists under another name, I'm sorry for the repeat...



input emalength = 21;

# define the exp avg of then close
def emaValue = ExpAverage (close, emaLength);

# define signal that equals one if the previous bar close is below the
# emaValue while the current bar close is above the emaValue
def priceCrossAboveEMA = close[1] < emaValue [1] and close > emaValue;

# plot a signal line that will equal a value of 1 when the # condition is found, zero if not found
plot scan = priceCrossAboveEMA;

assignBackgroundColor(if Scan >= 1 then color. GREEN else color.BLACK);

scan.assignvaluecolor(if priceCrossAboveEMA >+ 1 then color.BLACK else color.WHITE);
 

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
329 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