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

Looking to add an alert

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

    Looking to add an alert

    Hello, I'm looking to add a simple alert (audible) for the below MACD crossover indicator. Can someone help me with this? All I need is a sound when the MACD crosses up or down.

    Thanks
    Jim

    Also, I tried to change the 12 and 26 EMA to and SMA but I received and error when compiling. Do you not simply change all instances of fastEma to fastSma?

    {
    /// <summary>
    /// The MACDBarColor (Moving Average Convergence/Divergence) is a trend following momentum indicator that shows the relationship between two moving averages of prices.
    /// </summary>
    [Description("The MACDBarColor (Moving Average Convergence/Divergence) is a trend following momentum indicator that shows the relationship between two moving averages of prices.")]
    public class MACDBarColor : Indicator
    {
    #region Variables
    private int fast = 12;
    private int slow = 26;
    private int smooth = 9;
    private DataSeries fastEma;
    private DataSeries slowEma;

    private bool above = true;
    private Color aboveColor = Color.Green;
    private Color belowColor = Color.Red;
    #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.Green, "Macd"));
    Add(new Plot(Color.DarkViolet, "Avg"));
    Add(new Plot(new Pen(Color.Navy, 5), PlotStyle.Bar, "Diff"));

    Add(new Line(Color.DarkGray, 0, "Zero line"));

    fastEma = new DataSeries(this);
    slowEma = new DataSeries(this);
    }

    /// <summary>
    /// Calculates the indicator value(s) at the current index.
    /// </summary>
    protected override void OnBarUpdate()
    {
    if (CurrentBar == 0)
    {
    fastEma.Set(Input[0]);
    slowEma.Set(Input[0]);
    Value.Set(0);
    Avg.Set(0);
    Diff.Set(0);
    }
    else
    {
    fastEma.Set((2.0 / (1 + Fast)) * Input[0] + (1 - (2.0 / (1 + Fast))) * fastEma[1]);
    slowEma.Set((2.0 / (1 + Slow)) * Input[0] + (1 - (2.0 / (1 + Slow))) * slowEma[1]);

    double macd = fastSma[0] - slowDma[0];
    double macdAvg = (2.0 / (1 + Smooth)) * macd + (1 - (2.0 / (1 + Smooth))) * Avg[1];

    Value.Set(macd);
    Avg.Set(macdAvg);
    if(CrossAbove(Value, Avg, 1))
    above = true;
    else if (CrossBelow(Value, Avg, 1))
    above = false;

    if(above)
    BarColor = AboveColor;
    else
    BarColor = BelowColor;

    Diff.Set(macd - macdAvg);
    }


    }

    #region Properties
    /// <summary>
    /// </summary>
    [Browsable(false)]
    [XmlIgnore()]
    public DataSeries Avg
    {
    get { return Values[1]; }
    }

    /// <summary>
    /// </summary>
    [Browsable(false)]
    [XmlIgnore()]
    public DataSeries Default
    {
    get { return Values[0]; }
    }

    /// <summary>
    /// </summary>
    [Browsable(false)]
    [XmlIgnore()]
    public DataSeries Diff
    {
    get { return Values[2]; }
    }
    // Create our user definable color input
    [XmlIgnore()]
    [Description("Color for painted region")]
    [GridCategory("Parameters")]
    public Color AboveColor
    {
    get { return aboveColor; }
    set { aboveColor = value; }
    }

    // Serialize our Color object
    [Browsable(false)]
    public string AboveColorSerialize
    {
    get { return NinjaTrader.Gui.Design.SerializableColor.ToString( aboveColor); }
    set { aboveColor = NinjaTrader.Gui.Design.SerializableColor.FromStrin g(value); }
    }

    [XmlIgnore()]
    [Description("Color for painted region")]
    [GridCategory("Parameters")]
    public Color BelowColor
    {
    get { return belowColor; }
    set { belowColor = value; }
    }

    [Browsable(false)]
    public string BelowColorSerialize
    {
    get { return NinjaTrader.Gui.Design.SerializableColor.ToString( belowColor); }
    set { belowColor = NinjaTrader.Gui.Design.SerializableColor.FromStrin g(value); }
    }

    /// <summary>
    /// </summary>
    [Description("Number of bars for fast EMA")]
    [GridCategory("Parameters")]
    public int Fast
    {
    get { return fast; }
    set { fast = Math.Max(1, value); }
    }

    /// <summary>
    /// </summary>
    [Description("Number of bars for slow EMA")]
    [GridCategory("Parameters")]
    public int Slow
    {
    get { return slow; }
    set { slow = Math.Max(1, value); }
    }

    /// <summary>
    /// </summary>
    [Description("Number of bars for smoothing")]
    [GridCategory("Parameters")]
    public int Smooth
    {
    get { return smooth; }
    set { smooth = Math.Max(1, value); }
    }
    #endregion
    }
    }

    #2
    Hello morpheous455,

    Thank you for writing in. You have errors in the code you provided such as "fastSma" and "fastDma" which are not declared variables. Please make sure you have debugged your code prior to beginning the changes I discuss below.


    To change the EMA to SMA, the simplest way is to leave all the variable names the same, and just to change the logic. Please locate the following section of code:
    Code:
    fastEma.Set((2.0 / (1 + Fast)) * Input[0] + (1 - (2.0 / (1 + Fast))) * fastEma[1]);
    slowEma.Set((2.0 / (1 + Slow)) * Input[0] + (1 - (2.0 / (1 + Slow))) * slowEma[1]);
    and change it to something like the following:
    Code:
    fastEma.Set(SMA(Fast)[0]);
    slowEma.Set(SMA(Slow)[0]);
    To create the alert, you would use something like the following:
    Code:
    if(CrossAbove(Value, Avg, 1))
    {
    above = true;
    Alert("CrossAboveAlert", NinjaTrader.Cbi.Priority.High, "Crossed Above", "Alert1.wav", 1, Color.Black, Color.Yellow);
    }
    else if (CrossBelow(Value, Avg, 1))
    {
    above = false;
    Alert("CrossBelowAlert", NinjaTrader.Cbi.Priority.High, "Crossed Below", "Alert1.wav", 1, Color.Black, Color.Yellow);
    }
    For more information on working with alerts, please see our help guide here: http://ninjatrader.com/support/helpG...lightsub=alert

    Please let me know if I may be of further assistance.
    Michael M.NinjaTrader Quality Assurance

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by algospoke, Yesterday, 06:40 PM
    2 responses
    21 views
    0 likes
    Last Post algospoke  
    Started by ghoul, Today, 06:02 PM
    3 responses
    14 views
    0 likes
    Last Post NinjaTrader_Manfred  
    Started by jeronymite, 04-12-2024, 04:26 PM
    3 responses
    45 views
    0 likes
    Last Post jeronymite  
    Started by Barry Milan, Yesterday, 10:35 PM
    7 responses
    21 views
    0 likes
    Last Post NinjaTrader_Manfred  
    Started by AttiM, 02-14-2024, 05:20 PM
    10 responses
    181 views
    0 likes
    Last Post jeronymite  
    Working...
    X