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

Email Price Alert Indicator

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

    Email Price Alert Indicator

    I need some help adding a simple check so that emails are only sent once when price is touched. I've tried applying some methods but can't seem to get it to work.

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

    // This namespace holds all indicators and is required. Do not change it.
    namespace NinjaTrader.Indicator
    {
    /// <summary>
    /// Sends an email when a specified price is reached.
    /// </summary>
    [Description("Sends an email when a specified price is reached.")]
    public class EmailPriceAlert : Indicator
    {
    #region Variables
    // Wizard generated variables
    private double priceLevel = 1; // Default setting for PriceLevel
    private string toEmail = " "; // Default setting for ToEmail
    private string fromEmail = " "; // Default setting for FromEmail
    // User defined variables (add any user defined variables below)
    #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.FromKnownColor(KnownColor.Orange), PlotStyle.HLine, "PriceAlert"));
    Overlay = true;
    CalculateOnBarClose = false;
    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    if(CurrentBar < 1)
    return;

    PriceAlert.Set(PriceLevel);

    if(Close[0] == PriceLevel)
    {


    SendMail(fromEmail, toEmail, "Price Alert", "Price level was hit at: " + PriceLevel);
    }

    }

    #region Properties
    [Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
    [XmlIgnore()] // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
    public DataSeries PriceAlert
    {
    get { return Values[0]; }
    }

    [Description("")]
    [GridCategory("Parameters")]
    public double PriceLevel
    {
    get { return priceLevel; }
    set { priceLevel = Math.Max(1, value); }
    }

    [Description("")]
    [GridCategory("Parameters")]
    public string ToEmail
    {
    get { return toEmail; }
    set { toEmail = value; }
    }

    [Description("")]
    [GridCategory("Parameters")]
    public string FromEmail
    {
    get { return fromEmail; }
    set { fromEmail = value; }
    }
    #endregion
    }
    }

    #2
    Hello brucelevy,

    Thanks for your post.

    As mentioned in this thread http://ninjatrader.com/support/forum...ad.php?t=85165 You would use a bool to prevent multiple e-mails. You will also need to be able to reset that bool when you are ready for further e-mail.

    Code:
    public class EmailPriceAlert : Indicator
    {
    #region Variables
    // Wizard generated variables
    private double priceLevel = 1; // Default setting for PriceLevel
    private string toEmail = " "; // Default setting for ToEmail
    private string fromEmail = " "; // Default setting for FromEmail
    [COLOR="Blue"]private bool doitonce = true;  // for e-mail[/COLOR]
    // User defined variables (add any user defined variables below)
    #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.FromKnownColor(KnownColor.Orange), PlotStyle.HLine, "PriceAlert"));
    Overlay = true;
    CalculateOnBarClose = false;
    }
    
    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    if(CurrentBar < 1)
    return;
    
    PriceAlert.Set(PriceLevel);
    
    if(Close[0] == PriceLevel[COLOR="blue"] && doitonce[/COLOR])
    {
    SendMail(fromEmail, toEmail, "Price Alert", "Price level was hit at: " + PriceLevel);
    [COLOR="blue"]doitonce = false; // set so only send one e-mail[/COLOR]
    }
    
    [COLOR="blue"]if (your condition to reset the e-mail)
    {
    doitonce = true; // re-enable for e-mail
    }[/COLOR]
    
    }
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Thanks that worked perfectly.

      Comment


        #4
        How could this be applied to a trailing stop loss order?

        I am trying to send an email every time the stop is moved up behind price.

        Comment


          #5
          Hello brucelevy,

          Thanks for your post.

          You are likely to generate a lot of e-mails depending on the number of ticks on your trailing stop so it may not be wise to do so.

          Assuming you are using the setTrailStop() method, one way that this could be done is to test for the name "Trail stop" in the OnOrderUpdate() method .

          For example:

          Code:
          protected override void OnOrderUpdate(IOrder order)	
          		{
          			if (order.Name == "Trail stop")
          			{
          				
          				if (trailprice != order.StopPrice)  // test for new price
          				{
          					trailprice = order.StopPrice;
          					DrawDot ("test"+DateTime.Now, false, 0, trailprice, 
          Color.Red);
                                                 SendMail(....);
          				}
          			}
          
          		}
          (Note: trailprice is a double that i declared in region variables.
          Paul H.NinjaTrader Customer Service

          Comment


            #6
            Hi

            Just wondering about retrieving data from Tools Misc Email settings the default email sender info.
            Is there a way to get the info to save putting it in to each indicator. I thought maybe leaving Blank would call the default. Obviously a recipient email is required just wanting to save the user from headaches.

            private string fromEmail = " "; // Default setting for FromEmail

            Any help appreciated.
            Thanks Raef

            Comment


              #7
              Hello raefon72,

              Thanks for your post.

              While this would not be supported you can try:

              Print (NinjaTrader.Cbi.Globals.MailUser.ToString());
              Paul H.NinjaTrader Customer Service

              Comment


                #8
                Hi,

                It compiled ok but didnt call the default email address. I got a log error "system format exception"

                if (zzemail == true){SendMail(NinjaTrader.Cbi.Globals.MailUser.To String(),emailaddress, "Trade Alert ", "Bear Pin "+ Instrument.FullName+" "+ Close[0]);}

                Thanks Raef
                Last edited by raefon72; 07-09-2018, 08:50 PM. Reason: addition

                Comment


                  #9
                  Hello raefon72,

                  Thanks for your reply.

                  I had no issues here using:

                  if (!Historical && doitonce)
                  {
                  SendMail(NinjaTrader.Cbi.Globals.MailUser.ToString (), NinjaTrader.Cbi.Globals.MailUser.ToString(), "trade test", Close[0].ToString());
                  doitonce = false;
                  }


                  I also tried changing the "To:" to a direct e-mail address which also worked.

                  I notice that you have To String instead of ToString.
                  Paul H.NinjaTrader Customer Service

                  Comment


                    #10
                    Hi,

                    Thanks Very much it worked a charm once I removed the space.
                    This removes the need for double entries on the indicator.
                    Thanks Raef (;

                    Comment


                      #11
                      Hi Guys,

                      I'm having a bit of trouble retrieving the current charts Time period to add to the email.

                      + PeriodType.Minute.ToString()

                      if (zzemail == true){SendMail(NinjaTrader.Cbi.Globals.MailUser.To String (), zemailaddress, "Trade Alert ", "Bear Pin "+ Instrument.FullName+ " "+ Close[0]+ PeriodType.Minute.ToString());}

                      The email is sending the word minute but not the Int vale.

                      Bull Pin $AUDUSD 0.7379Minute

                      Any help Appreciated.
                      Thanks Raef

                      Comment


                        #12
                        Hello raefon72,

                        Thanks for your post.

                        In this case you would want to use the BarsPeriod.Value. Please see the helpguide and note the example: https://ninjatrader.com/support/help...barsperiod.htm
                        Paul H.NinjaTrader Customer Service

                        Comment


                          #13
                          Thanks.
                          I tried bars in progress and a few others but BarsPeriods. worked a treat.

                          I used 2 types.

                          " Chart Type "+BarsPeriods[0].BasePeriodType+" " +BarsPeriod.Value

                          Bear Pin $AUDUSD 0.7419 Chart Type Minute 1

                          thanks again.
                          raef
                          Last edited by raefon72; 07-22-2018, 03:42 PM. Reason: grammer

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by kujista, Today, 06:23 AM
                          0 responses
                          1 view
                          0 likes
                          Last Post kujista
                          by kujista
                           
                          Started by traderqz, Yesterday, 04:32 PM
                          1 response
                          10 views
                          0 likes
                          Last Post NinjaTrader_Gaby  
                          Started by f.saeidi, Today, 05:56 AM
                          1 response
                          4 views
                          0 likes
                          Last Post Jltarrau  
                          Started by Jltarrau, Today, 05:57 AM
                          0 responses
                          4 views
                          0 likes
                          Last Post Jltarrau  
                          Started by Stanfillirenfro, Yesterday, 09:19 AM
                          7 responses
                          53 views
                          0 likes
                          Last Post NinjaTrader_Gaby  
                          Working...
                          X