EMA Script written from scratch without using the MovingAverage Function?

jonesjb

New member
VIP
What would be the code for Exponential Moving Average if written from scratch without the common MovingAverage Function?

I need this for some MTF code I'm trying to write that will work on Mobile. I tried searching but couldn't find it. Can someone please help me? I'll post my final script once I finish it (assuming I can get this).
 
@jonesjb I can think of two options off the top of my head... The first would be to use the ExpAverage() Function... The second would be to reference the MovAvgExponential Study... Alternatively, if you are a glutton for punishment, you could Google "Exponential Moving Average formula" and attempt to reinvent the wheel from scratch...

Hope this information is what you were looking for...
Thank you, yes I did the latter... I came up with two methods (which get pretty close):

###Method 1 EMA

Def price_0 = close;
Def price_1 = close[1];
Def price_2 = close[2];
Def price_3 = close[3];
Def price_4 = close[4];
Def price_5 = close[5];

Def Mult = 2 / (5 + 1);
Def BK_mult = 1 - mult;

Def Smooth_0 = mult;
Def Smooth_1 = BK_mult;
Def Smooth_2 = BK_mult * BK_mult;
Def Smooth_3 = BK_mult * BK_mult * BK_mult;
Def Smooth_4 = BK_mult * BK_mult * BK_mult * BK_mult;

Def EMA_Top = price_0 + (price_1 * Smooth_1) + (price_2 * Smooth_2) + (price_3 * Smooth_3)+ (price_4 * Smooth_4);
Def EMA_Bottom = 1 + Smooth_1 + Smooth_2 + Smooth_3 + Smooth_4;

Plot EMA = EMA_Top / EMA_Bottom;



###Method 2 EMA

Def price_0 = close;
Def price_1 = close[1];
Def price_2 = close[2];
Def price_3 = close[3];
Def price_4 = close[4];
Def price_5 = close[5];

Def Mult = 2 / (5 + 1);
Def BK_mult = 1 - mult;

Def SMA = (price_1 + price_1 + price_3 + price_4 + price_5)/5;
plot EMA = (Price_0 * Mult) + (SMA * BK_mult);
 
From a clock cycle efficiency standpoint, using the ExpAverage() Function would be the fastest method as it doesn't require that the parser evaluate as much Thinkscript code - which comes from my 40 years of bit-fiddling for code optimization dating back to the early days of PC programming... Function calls are always the fastest method...
 

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

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
358 Online
Create Post

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