Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Absolute Value EMA Indicator

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Absolute Value EMA Indicator

    Hi guys. Looking for a fairly simple function. Tried to do this myself but am not a coder. Spent about 30 minutes on this and am positive the solution is likely 30 seconds away in the hands of someone that can actually code.

    All I'm looking for is the absolute value of whatever the input is. I am using a custom oscillator which ranges from +100 to -100, and I want to do a moving average of the absolute values of the indicator using the indicator as an input. Fortunately, Ninjatrader provides that solution out of the box since I can change the input of the EMA.

    I've included the indicator below in case someone can help with this. If this really is more complex than 30 seconds, can someone point me to the right place on how to tackle this?


    //
    // Copyright (C) 2006, NinjaTrader LLC <www.ninjatrader.com>.
    // NinjaTrader reserves the right to modify or overwrite this NinjaScript component with each release.
    //

    #region Using declarations
    using System;
    using System.Diagnostics;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.ComponentModel;
    using System.Xml.Serialization;
    using NinjaTrader.Data;
    using NinjaTrader.Gui.Chart;
    #endregion

    // This namespace holds all indicators and is required. Do not change it.
    namespace NinjaTrader.Indicator
    {
    /// <summary>
    /// Exponential Moving Average. The Exponential Moving Average is an indicator that shows the average value of a security's price over a period of time. When calculating a moving average. The EMA applies more weight to recent prices than the SMA.
    /// </summary>
    [Description("The Exponential Moving Average is an indicator that shows the average value of a security's price over a period of time. When calculating a moving average. The EMA applies more weight to recent prices than the SMA.")]
    public class EMA : Indicator
    {
    #region Variables
    private int period = 14;
    #endregion

    /// <summary>
    /// This method is used to configure the indicator and is called once before any bar data is loaded.
    /// </summary>
    protected override void Initialize()
    {
    Add(new Plot(Color.Orange, "EMA"));

    Overlay = true;
    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    Value.Set(CurrentBar == 0 ? Input[0] : Input[0] * (2.0 / (1 + Period)) + (1 - (2.0 / (1 + Period))) * Value[1]);
    }

    #region Properties
    /// <summary>
    /// </summary>
    [Description("Numbers of bars used for calculations")]
    [GridCategory("Parameters")]
    public int Period
    {
    get { return period; }
    set { period = Math.Max(1, value); }
    }
    #endregion
    }
    }

    #region NinjaScript generated code. Neither change nor remove.
    // This namespace holds all indicators and is required. Do not change it.
    namespace NinjaTrader.Indicator
    {
    public partial class Indicator : IndicatorBase
    {
    private EMA[] cacheEMA = null;

    private static EMA checkEMA = new EMA();

    /// <summary>
    /// The Exponential Moving Average is an indicator that shows the average value of a security's price over a period of time. When calculating a moving average. The EMA applies more weight to recent prices than the SMA.
    /// </summary>
    /// <returns></returns>
    public EMA EMA(int period)
    {
    return EMA(Input, period);
    }

    /// <summary>
    /// The Exponential Moving Average is an indicator that shows the average value of a security's price over a period of time. When calculating a moving average. The EMA applies more weight to recent prices than the SMA.
    /// </summary>
    /// <returns></returns>
    public EMA EMA(Data.IDataSeries input, int period)
    {
    if (cacheEMA != null)
    for (int idx = 0; idx < cacheEMA.Length; idx++)
    if (cacheEMA[idx].Period == period && cacheEMA[idx].EqualsInput(input))
    return cacheEMA[idx];

    lock (checkEMA)
    {
    checkEMA.Period = period;
    period = checkEMA.Period;

    if (cacheEMA != null)
    for (int idx = 0; idx < cacheEMA.Length; idx++)
    if (cacheEMA[idx].Period == period && cacheEMA[idx].EqualsInput(input))
    return cacheEMA[idx];

    EMA indicator = new EMA();
    indicator.BarsRequired = BarsRequired;
    indicator.CalculateOnBarClose = CalculateOnBarClose;
    #if NT7
    indicator.ForceMaximumBarsLookBack256 = ForceMaximumBarsLookBack256;
    indicator.MaximumBarsLookBack = MaximumBarsLookBack;
    #endif
    indicator.Input = input;
    indicator.Period = period;
    Indicators.Add(indicator);
    indicator.SetUp();

    EMA[] tmp = new EMA[cacheEMA == null ? 1 : cacheEMA.Length + 1];
    if (cacheEMA != null)
    cacheEMA.CopyTo(tmp, 0);
    tmp[tmp.Length - 1] = indicator;
    cacheEMA = tmp;
    return indicator;
    }
    }
    }
    }

    // This namespace holds all market analyzer column definitions and is required. Do not change it.
    namespace NinjaTrader.MarketAnalyzer
    {
    public partial class Column : ColumnBase
    {
    /// <summary>
    /// The Exponential Moving Average is an indicator that shows the average value of a security's price over a period of time. When calculating a moving average. The EMA applies more weight to recent prices than the SMA.
    /// </summary>
    /// <returns></returns>
    [Gui.Design.WizardCondition("Indicator")]
    public Indicator.EMA EMA(int period)
    {
    return _indicator.EMA(Input, period);
    }

    /// <summary>
    /// The Exponential Moving Average is an indicator that shows the average value of a security's price over a period of time. When calculating a moving average. The EMA applies more weight to recent prices than the SMA.
    /// </summary>
    /// <returns></returns>
    public Indicator.EMA EMA(Data.IDataSeries input, int period)
    {
    return _indicator.EMA(input, period);
    }
    }
    }

    // This namespace holds all strategies and is required. Do not change it.
    namespace NinjaTrader.Strategy
    {
    public partial class Strategy : StrategyBase
    {
    /// <summary>
    /// The Exponential Moving Average is an indicator that shows the average value of a security's price over a period of time. When calculating a moving average. The EMA applies more weight to recent prices than the SMA.
    /// </summary>
    /// <returns></returns>
    [Gui.Design.WizardCondition("Indicator")]
    public Indicator.EMA EMA(int period)
    {
    return _indicator.EMA(Input, period);
    }

    /// <summary>
    /// The Exponential Moving Average is an indicator that shows the average value of a security's price over a period of time. When calculating a moving average. The EMA applies more weight to recent prices than the SMA.
    /// </summary>
    /// <returns></returns>
    public Indicator.EMA EMA(Data.IDataSeries input, int period)
    {
    if (InInitialize && input == null)
    throw new ArgumentException("You only can access an indicator with the default input/bar series from within the 'Initialize()' method");

    return _indicator.EMA(input, period);
    }
    }
    }
    #endregion


    #2
    Hello kidpwrtrader,

    Thank you for the post.

    I wanted to make sure I understood the question right, do you mean the mathematical absolute for example :

    Abs(-19.69) = 19.69?

    If so you just need to use Math.Abs on the return value:

    Code:
    double emaVal = CurrentBar == 0 ? Input[0] : Input[0] * (2.0 / (1 + Period)) + (1 - (2.0 / (1 + Period))) * Value[1];
    Value.Set(Math.Abs(emaVal));
    That will leave the original values as they were and only plot the absolute, if you mean to instead calculate based on the absolute you probably want something more like this:


    Code:
    double absInput = Math.Abs(Input[0]);
    double emaVal = CurrentBar == 0 ? absInput : absInput * (2.0 / (1 + Period)) + (1 - (2.0 / (1 + Period))) * Value[1];
    Value.Set(emaVal);
    [/CODE]


    I look forward to being of further assistance.

    JesseNinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by TheTradingMantis, 01-19-2023, 02:05 AM
    41 responses
    906 views
    0 likes
    Last Post jmagaia
    by jmagaia
     
    Started by _Zero_, 04-10-2020, 03:21 PM
    144 responses
    7,891 views
    6 likes
    Last Post NinjaTrader_ChelseaB  
    Started by Richozzy38, Yesterday, 01:06 PM
    7 responses
    27 views
    0 likes
    Last Post Richozzy38  
    Started by swjake, Today, 12:04 PM
    5 responses
    15 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Started by forrestang, Today, 01:41 PM
    1 response
    7 views
    0 likes
    Last Post NinjaTrader_Erick  
    Working...
    X