Example of Code for Moving Average Cross - Changing Colors

13977 33
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.
Remove from Favorite Scripts Add to Favorite Scripts
//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.
Hi, thanks for your script, I'm trying to modify it but I'm getting this error "Add to Chart operation failed, reason: Script could not be translated from: |B|ema10 close, len10 = input"
What am I doing wrong?


study(title="_5 EMA Trend Color", shorttitle="_5_EMA-Trend", overlay=true)

ema10 close, len10 = input(10, minval=1, title="EMA 10")
ema20 close, len20 = input(20, minval=1, title="EMA 20")
ema30 close, len30 = input(30, minval=1, title="EMA 30")
ema144 close, len144 = input(144, minval=1, title="EMA 144")
ema169 close, len169 = input(169, minval=1, title="EMA 169")

emaShort10 = ema(ema10, len10)
emaShort20 = ema(ema20, len20)
emaShort30 = ema(ema30, len30)
emaLong144 = ema(ema144, len144)
emaLong169 = ema(ema169, len169)

spanColor = (emaShort10 and emaShort20 and emaShort30)>=(emaShort144 and emaShort169) ? lime : red

p10 = plot(emaShort10, title="EMA 10", style=line, linewidth=4, color=spanColor)
p20 = plot(emaShort20, title="EMA 20", style=line, linewidth=4, color=spanColor)
p30 = plot(emaShort30, title="EMA 30", style=line, linewidth=4, color=spanColor)

p144 = plot(emaLong144, title="EMA 144", style=circles, linewidth=3, color=spanColor)
p169 = plot(emaLong169, title="EMA 169", style=circles, linewidth=3, color=spanColor)

fill(p10, p20, p30, p144, p169, color=silver, transp=40, title="Fill")
jsudds Sebastian_Lorca
@Sebastian_Lorca, you need add an equal sign between ema10 and close. (i.e. ema10 close -> ema10 = close). The same goes for the first five lines
Hi Chris,
Can you please tell me how I can start learning pine script?
Which editor can I use?
How can I substitute the higher EMA with a simple MA on the script. i.e. EMA 100 with simple MA 100
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!
Hi Chris
is it possibel fo ryou to help me with script for below.
draw line
MOveing average of 8
(low + close )/2
Thank you in advance
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)

+1 Reply
ChrisMoody 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.
Tradac ChrisMoody
@ChrisMoody, Thank you for sharing it!
EN English
EN English (UK)
EN English (IN)
DE Deutsch
FR Français
ES Español
IT Italiano
PL Polski
SV Svenska
TR Türkçe
RU Русский
PT Português
ID Bahasa Indonesia
MS Bahasa Melayu
TH ภาษาไทย
VI Tiếng Việt
JA 日本語
KO 한국어
ZH 简体中文
ZH 繁體中文
AR العربية
HE עברית
Home Stock Screener Forex Screener Crypto Screener Economic Calendar How It Works Chart Features House Rules Moderators Website & Broker Solutions Widgets Stock Charting Library Feature Request Blog & News FAQ Help & Wiki Twitter
Profile Profile Settings Account and Billing My Support Tickets Contact Support Ideas Published Followers Following Private Messages Chat Sign Out