Ideas Scripts Chart Stock Screener Forex Signal Finder Economic Calendar How It Works Chart Features House Rules Moderators For the WEB Widgets Stock Charting Library Feature Request FAQ Help & Wiki Twitter

# Example of Code for Moving Average Cross - Changing Colors

4812 29
Famous 7 Time World Trading Champion Chuck Hughes found the 50 and 100 EMA to be the best Signal for a Change in Trend. Through extensive back-testing he found these EMA’s to give the earliest signal that also resulted in a Long-Term Change in Trend.

Dotted Line represents Long-Term EMA . The 100 EMA in this example.
Solid line represents the Short-Term EMA . The 50 EMA in this example.

If Short-Term EMA is ABOVE Long-Term EMA ...Color = Green.
If Short-Term EMA is BELOW Long-Term EMA ...Color = Red.
```//Created by user ChrisMoody
//Long EMA = Dots, Short EMA = Line
//If Short EMA is ABOVE Long EMA = Lime Color, If Short EMA is BELOW Long EMA = Red Color
study(title="_CM_Double EMA Trend Color", shorttitle="_CM-2EMA-Trend", overlay=true)
src = close, len = input(50, minval=1, title="Short EMA")
src2 = close, len2 = input(100, minval=1, title="Long EMA")
emaShort = ema(src, len)
emaLong = ema(src2, len2)

spanColor = emaShort>=emaLong ? lime : red

p1 = plot(emaShort, title="EMA Short", style=line, linewidth=4, color=spanColor)
p2 = plot(emaLong, title="EMA Long", style=circles, linewidth=3, color=spanColor)

fill(p1, p2, color=silver, transp=40, title="Fill")

//If you do not want the Fill gradient between the EMA's follow the steps below.

//Erase the p1 = in line 12 and 13 so plot is at the far left
//Erase line 15 or put two forward slashes infront of the word fill, just like the two forward slashes at the beginning of this line.```
Code is Below:

//Created by user ChrisMoody
//Long EMA = Dots, Short EMA = Line
//If Short EMA is ABOVE Long EMA = Lime Color, If Short EMA is BELOW Long EMA = Red Color
study(title="_CM_Double EMA Trend Color", shorttitle="_CM-2EMA-Trend", overlay=true)
src = close, len = input(50, minval=1, title="Short EMA")
src2 = close, len2 = input(100, minval=1, title="Long EMA")
emaShort = ema(src, len)
emaLong = ema(src2, len2)

spanColor = emaShort>=emaLong ? lime : red

p1 = plot(emaShort, title="EMA Short", style=line, linewidth=4, color=spanColor)
p2 = plot(emaLong, title="EMA Long", style=circles, linewidth=3, color=spanColor)

fill(p1, p2, color=silver, transp=40, title="Fill")

//If you do not want the Fill gradient between the EMA's follow the steps below.

//Erase the p1 = in line 12 and 13 so plot is at the far left
//Erase line 15 or put two forward slashes in front of the word fill, just like the two forward slashes at the beginning of this line.
Hi, i have this error

line 8:35 no viable alternative at input 'close'
iamthekey
I'll check in about 15 minutes...it may be the way I posted the code.
Make sure line 4 above that starts with EMA is actually on line 3. Or put a // in front of the EMA

LINE 6 should be a continuation of line 6

Being that line 1 is the first // I have listed above
iamthekey
Sorry the comment I just made didn't make sense...I was looking at chart on a iPad and it wrapped the text.

I just copied the code and pasted it in a new indicator and it worked perfect....You can also make the chary yours and then you will have access to the indicator on the chart and you can save it to your indicators....
Ah, sorry i will try. I do not know what i doing wrong.
hi

Sorry for what platform is that code ?

Thanks
ivansavic0
Sorry I may not understand your question. It's for the TradingView platform. Any charts posted on this site are TradingView charts. Let me know if that doesn't answer your question.
Thanks for posting this Chris. I was able to get one of my own scripts that wouldn't work to work by seeing this. Do you have a link to a "how to" on working with this script??
also do you know if there is a way to change the color of each span in this particular indicator you have posted....one green and red to correspond with the change in color of the averages?
Kazonomics
Right now you can't change the color of each Span...feature will be coming soon.

I don't know of any printed examples...I emailed tech support and they helped me out. Below are two other charts I posted with the same feature. You'll see that the last few lines of code are pretty much the same in order to create the change in colors...

Either post your code here so I can take a look at what you did or Post your chart and send me a link...

These two posts have more examples

Saw you talk about this on someones chart, do you know if this pair does well on lower aggs, or just daily?
TheLark
Or, better yet, has Chuck done any research into lower aggs? I wonder if he has research to backup his daily claims as well, I know hes not a champ trader just because he follows a couple moving averages
TheLark
I've seen a bunch of his videos....Every one he uses a Daily chart with those tow EMA's and Keltner Channels. He Takes the top 5 sectors in a up trend and takes the top performing ETF's and Stocks within those sectors(Opposite for downtrend). When price drops to lower Keltner channel he's buying Options. He has specific rules regarding the strike price which I would have to look up. He also owns stocks and ETF's and sells calls when he thinks they are coming off the top Band of the Keltner Channels....but he makes his big money buying long calls and short puts in downtrends....he also sells Naked puts and spreads.

His system is very basic...he trades in direction of trend on Daily time frames, and trades the strongest/weakest sectors based on trend. For years he did a bunch of interviews and spoke at events without charging and gave his system away. You can find a lot of videos online and I've probably seen 30+ videos and he's always been 100% consistent in explaining his methodology... Now he sells his "everything course" for 2K or 3K...I've considered buying but I've seen so many of his videos I really don't think there is much more to it. The funny thing is you can get his "everything course for a few thousand but he has an alert service where he tells you when to buy and sell etc. Broken down into 3 or 4 services for 6K each...and doesn't teach anything...just an alert service...where if you pay for his "Everything" course the alerts are included to help you learn for less that 20% of what people pay just for his alerts.

He also does weekly options now but that course is only a few hundred bucks. I'm sure I'll get it at some point but I've been selling weekly options for a long time...and as long as you can read a daily chart and your selling premium outside of any expected move it's not difficult...it's actually the easiest trading I've ever done.
ChrisMoody
Oh and as far as his daily claims....he did a webinar this last Monday and he always shows his live accounts...he's transparent.

Run a google search for Chuck Hughes Trading on Google...filter out all the ones where he's selling his system on platforms like MetaStock...and look for videos that are like 3 years old or older...a lot of the recent ones he holds back since he's gone in to the educational business. Also he's done a lot of recent ones for the TradeMonster Options platform...those he covers some good stuff...but it's the same stuff he covered years ago...TradeMonster is who he did the Seminar with on Monday and I'm 99% sure he covered his options rules on that one, he showed how he uses Keltner Channels, but for some reason I think that's the first webinar I've seen where he doesn't mention the moving averages...but I tend to watch his older videos mostly...
ChrisMoody
Thanks for the reply Chris, I'll take a look when I have some time :)
ChrisMoody
Amazing comment and thank you very much for this script! Cheers!
bergaler
No Problem man....
It would seem something has gone wrong right now in the pine script engine....Did you notice the color change in this indicator has totally stopped working? Not sure if there is something on our end we need to do to remedy this.
Kazonomics
Hey...Sorry don't have time to look in to it right now...but try this code...This doesn't have the bells and whistles of the later moving averages I've created...but I think it is Version 2 of the code above...PM me if you have any issues...but I verified the code below works on my end

// Created By User ChrisMoody
// 2 EMA's Color based on Fast EMA being Above or Below Slow EMA
//Default Plot is 50EMA and 100EMA using Chuck Hughes Method

study(title = "CM-DoubleEMA-TrendColor", shorttitle="CM-2-EMA-TrendColor", overlay=true)

src = close, len = input(50, minval=1, title="Fast EMA")
src2 = close, len2 = input(100, minval=1, title="Slow EMA")

emaFast = ema(src, len)
emaSlow = ema(src2, len2)

plot_color = emaFast >= emaSlow ? lime : emaFast < emaSlow ? red : yellow

p1 = plot(emaFast, title="emaFast", style=line, linewidth=4, color = plot_color)
p2 = plot(emaSlow, title="emaSlow", style=line, linewidth=4, color = plot_color)

fill(p1, p2, color=white, transp=40, title="Fill")
ChrisMoody
Hey Chris,

The problem seems to be with "circles". You change it to "cross" or even "line", things will work normally.

Some of the users reported the same problem to me this morning, looks like a recent TV PineEngine update has regressed.
LazyBear
Good Point...I found the same issue in a code this morning...
Can anyone help me please! I built an indicator that has uses only 1 line! But it has 2 colors!
I want to get an alert when line color changes (Example: Line turns green = "Buy", line turns red = "Sell"

linecolor2=direction2 < 0 ? red : direction2 > 0 ? green : red
Can i use this on any time frame and any expiry
Hi Chris
is it possibel fo ryou to help me with script for below.
draw line
MOveing average of 8
parameter
(low + close )/2
anilpune
No special features. You can input length...defaults to 8. Simple Moving Average based on (Low + Close) /2

Here is a chart with it plotted:

We used to not be able to paste code in here...I'll try....if it doesn't come out right I'll post a download link to the code in a basic Text File.

//Created by Chris Moody for User Request fo anilpune
//Uses Low + Close devided /2 for data source with a default 8 Simple Moving Average.
study(title="CM_Custom Moving Average L+C/2", shorttitle="CM_MA LC/2", overlay=true)
len = input(8, minval=1, title="Length")

ma = sma((low + close) * .5, len)
plot(ma, title="MA Low+Close / 2", style=line, linewidth=6, color = yellow)

ChrisMoody
Looks like the code plotted correctly. Just a basic coding rule...any time you want to /2 it's better to use * .5 ... Multiplying is more efficient than dividing.

If that isn't what you wanted let me know.
Hi, thanks for the indicator example. How can I add two fill colors: green fill between the moving averages when trend is up, and red fill between ma's when trend is down? thanks!
How can I substitute the higher EMA with a simple MA on the script. i.e. EMA 100 with simple MA 100
Hi Chris,
Can you please tell me how I can start learning pine script?
Which editor can I use?
Ideas Scripts Chart
United States
United Kingdom
India
España
France
Italia
Polska
Brasil
Россия
Türkiye
Indonesia

한국
Home Stock Screener Forex Signal Finder Economic Calendar How It Works Chart Features House Rules Moderators For the WEB Widgets Stock Charting Library Priority Support Feature Request Blog & News FAQ Help & Wiki Twitter