NexusFi: Find Your Edge


Home Menu

 





Ema2 Function in NinjaScript?


Discussion in NinjaTrader

Updated
    1. trending_up 4,779 views
    2. thumb_up 2 thanks given
    3. group 1 followers
    1. forum 3 posts
    2. attach_file 2 attachments




 
Search this Thread
  #1 (permalink)
 
srgtroy's Avatar
 srgtroy 
Los Angeles, California Republic
Legendary  R.I.P. 1965-2023 
 
Experience: None
Platform: Sierra Chart
Broker: CQG
Trading: ES
Posts: 1,928 since Jan 2011
Thanks Given: 1,375
Thanks Received: 3,723

In Thinkscript, there is a function called Ema2. Here is its description:

Ema2(IDataHolder data, int prefetch, double smoothing factor);

Description

Returns the Exponential Moving Average (EMA) of data with smoothing factor. The prefetch parameter controls the number of historical data points used to initialize EMA for the first bar.

Example

input additionalBars = 0;
plot ExpAvg = Ema2(close, additionalBars, 0.2);

The code plots the exponential average of a security Close price with a smoothing factor of 0.2. Note that studies using ema2 fetch a necessary number of additional bars for correct initialization, so adding more initialization data by increasing additionalBars input has little impact on the study.


Is there anything like this in NinjaScript?

If not, how would I go about getting the value of Ema2(Close, 5, 0.2) for instance?


Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
SEC Chairman Calls for New Golden Age of SEC-CFTC Regula …
Traders Hideout
The May 31 Binary: 60% Trump Declares Iran Ops Over, Onl …
Prediction Markets & Event Contracts
GDP Day: The First Economic Reckoning -- Pahlavi at 6.55 …
Prediction Markets & Event Contracts
CME Raises Energy Futures Margins After Iran-War Volatil …
Commodities
CME Group Fee Schedule Changes Hit All Four Exchanges -- …
Traders Hideout
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Sober Journey With S&P
24 thanks
2026 Jlab journal
10 thanks
Algo automated / semi-automated trading anyone?
6 thanks
Lady Vols Primer: Trading Volatility Journal
6 thanks
Trying to learn Volume and price action correlation
5 thanks
  #3 (permalink)
 
Fat Tails's Avatar
 Fat Tails 
Berlin, Europe
Market Wizard
 
Experience: Advanced
Platform: NinjaTrader
Broker: Interactive Brokers
Trading: Futures & Stocks
Posts: 9,887 since Mar 2010
Thanks Given: 4,242
Thanks Received: 27,114


I do not know ThinkScript, but what you describe can easily be achieved in NinjaScript.

Smoothing factor:

Usually you use an integer period to calculate an exponential moving average. If n is the integer period, this translates into a smoothing factor k = 2/(n+1). To obtain the smoothing factor 0.2, you would need to select a period of 9, as k = 2/(n+1) = 2/(9+1) = 2/10 = 0.2

Of course it is possible to change the formula of the EMA to use a smoothing factor instead of the period.

Prefetch:

NinjaTrader uses the variable BarsRequired to cut off the unstable period during the initialization of an indicator. You can simply declare a variable prefetch, and allocate the selected value to BarsRequired.

I have modified the default EMA to catch a prefetch value and to use a smoothing constant instead of a period.


Attached Thumbnails
Click image for larger version

Name:	ES 09-11 (15 Min)  27_06_2011.jpg
Views:	236
Size:	87.2 KB
ID:	42168  
Attached Files
Elite Membership required to download: EMA2.zip
Reply With Quote
Thanked by:
  #4 (permalink)
 
srgtroy's Avatar
 srgtroy 
Los Angeles, California Republic
Legendary  R.I.P. 1965-2023 
 
Experience: None
Platform: Sierra Chart
Broker: CQG
Trading: ES
Posts: 1,928 since Jan 2011
Thanks Given: 1,375
Thanks Received: 3,723

May the Force be with you, always!


Started this thread Reply With Quote




Last Updated on June 29, 2011


© 2026 NexusFi®, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Downloads - Top
no new posts