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

Continuous email alerts

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

    Continuous email alerts

    Hi I want to send an email when a trade is taken but I am receiving hundreds of emails when the executions takes place.




    #region Variables

    private bool trigger = true;
    private double askPrice = 0;
    private double bidPrice = 0;

    #endregion

    protected override void OnMarketData(MarketDataEventArgs e)
    {
    if (e.MarketDataType == MarketDataType.Ask)
    askPrice = e.Price;
    else if (e.MarketDataType == MarketDataType.Bid)
    bidPrice = e.Price;
    else if (e.MarketDataType == MarketDataType.Last)



    // Sample Condition set 1
    if (Position.MarketPosition == MarketPosition.Flat
    && Close[0] > High[1]
    && trigger == true)

    {
    EnterLong(DefaultQuantity, "L1");
    SendMail(emailFrom, emailTo, "Long Entry " +Instrument.FullName +" @ "+bidPrice," "+hash);
    trigger = false;
    }




    if (Position.MarketPosition == MarketPosition.Flat)
    {
    trigger = true;
    }

    }


    #2
    Hello brucelevy, and thank you for your question.

    Since you included this in your OnMarketData section, the block of code you specified is going to get hit many times between the first time you call EnterLong, and when your Position is updated. Market Data comes in from your data feed provider with every price change, and it takes some time to talk to your data feed provider, for them to talk to the trade desk, and then for them to relay information from the trade desk. Hundreds to thousands of price changes can occur during this window. EnterLong has some protections against this kind of "signal bounce" but SendMail has no such protections, so you will need to add these yourself.

    I see we attempted to do so, but the above explains why this code is not sufficient :

    Code:
    [FONT=Courier New]
    if (Position.MarketPosition == MarketPosition.Flat)
    {
        trigger = true;     
    }[/FONT]
    Please let us know if there are any other ways we can help.
    Last edited by NinjaTrader_JessicaP; 10-20-2016, 11:39 AM.
    Jessica P.NinjaTrader Customer Service

    Comment


      #3
      I am now also getting this error message probably due to so many emails being sent..

      Comment


        #4
        Thanks, so something like this would be sufficient?


        if (Position.MarketPosition == MarketPosition.Long)
        {
        SendMail(emailFrom, emailTo, "Long Entry " +Instrument.FullName +" @ "+bidPrice," "+hash);
        trigger = false;

        }
        if (Position.MarketPosition == MarketPosition.Short)
        {
        SendMail(emailFrom, emailTo, "Short Entry " +Instrument.FullName +" @ "+askPrice," "+hash);
        trigger = false;

        }
        else if (Position.MarketPosition == MarketPosition.Flat)
        {
        trigger = true;

        }

        Comment


          #5
          No, Position.MarketPosition can not be used here in that way, because it will not update in time. If you would prefer to include this check, you will want to move the check where you set trigger = true to OnBarUpdate instead.

          I had assumed there was a reason why you wanted to restrict things to OnMarketData, but this will resolve your query.

          Code:
          [FONT=Courier New]protected override void OnBarUpdate()
          {
              if (Position.MarketPosition == MarketPosition.Flat)
              {
                  trigger = true;
              }
          }[/FONT]
          I also noticed in your OnMarketData code that you are setting, but not using, trigger. You will need this change as well :

          Code:
          [FONT=Courier New]if (Position.MarketPosition == MarketPosition.Long [B]&& trigger[/B])
          {
              SendMail(emailFrom, emailTo, "Long Entry " +Instrument.FullName +" @ "+bidPrice," "+hash);
              trigger = false;
               
          }
          if (Position.MarketPosition == MarketPosition.Short [B]&& trigger[/B])
          {
              SendMail(emailFrom, emailTo, "Short Entry " +Instrument.FullName +" @ "+askPrice," "+hash);
              trigger = false;
               
          }[/FONT]
          Last edited by NinjaTrader_JessicaP; 10-20-2016, 11:42 AM.
          Jessica P.NinjaTrader Customer Service

          Comment


            #6
            Thanks,

            Also I can't seem to get an email sent out I am getting this error.. I'm assuming its my email provider?

            Comment


              #7
              While that is likely the case, net_io_connectionclosed is a general class of error and does not tell us anything specific. However, as the typical causes of this status are things like mismatched ports and passwords, and your code successfully sent e-mails before this appeared, I can not think of another reason why this would occur.
              Jessica P.NinjaTrader Customer Service

              Comment


                #8
                Thanks Jessica, it seems to be working correctly now.

                How would I apply the same check to an indicator if there are no long, short, flat positions to check for as a condition?


                if (CustomTradingActivityIndicator >= 20
                && trigger == true)
                {
                SendMail(emailFrom, emailTo, "High Activity " +Instrument.FullName+ " "+hash,"");
                trigger = false;
                }

                Comment


                  #9
                  If you would like an indicator to track a strategy's positions, you will want to use global scope variables.

                  For NinjaTrader 7, this is very easy. We provide the files (My) Documents\NinjaTrader 7\bin\Custom\Indicator\UserDefinedMethods.cs and (My) Documents\NinjaTrader 7\bin\Custom\Strategy\UserDefinedMethods.cs . You can store global variables that will be included for all Indicators in this file. Make sure they are part of the Indicator class. For example,

                  Code:
                  [FONT=Courier New]    partial class Indicator
                      {
                          /// <summary>all the plots from all the indicators</summary>
                          public static Plot[][] allThePlots;
                      }
                  [FONT=Arial][/FONT][/FONT]


                  NinjaTrader 8 will not have a similar file. I am including a publicly available link from the MSDN C# documentation which will guide you toward putting together a global scope file in NT8.

                  The C# namespace alias qualifier `::` is used to access a member of an aliased namespace. The `::` operator is often used with the `global` alias, an alias for the global namespace


                  Whichever way you choose, you will want to add some logic surrounding your global objects.


                  With static classes in the NinjaTrader.Strategy namespace, you can also expose the output of a GetAccountValue call. I am providing documentation on this strategy method.



                  Monitoring account values can give you an indication that a trade was placed.
                  Jessica P.NinjaTrader Customer Service

                  Comment


                    #10
                    Well I just want an indicator to send out an email when a condition is met but not to repeat the email.

                    This below still sends out a lot of emails when triggered:



                    if (CustomTradingActivityIndicator >= 20
                    && trigger == true)
                    {
                    SendMail(emailFrom, emailTo, "High Activity " +Instrument.FullName+ " "+hash,"");
                    trigger = false;
                    }

                    Comment


                      #11
                      Thank you for this additional information, I believe I understand better what our goals are. So that I can ensure I am on the same page as you, can you tell me what our use case is for calling this during OnMarketData ?
                      Jessica P.NinjaTrader Customer Service

                      Comment


                        #12
                        I have taken it out of OnMarketData and put the main code in OnMarketUpdate.

                        Comment


                          #13
                          Hello brucelevy,

                          That I know of, there is no OnMarketUpdate method. There are OnMarketData and OnMarketDepth.

                          Can you clarify what you are trying to achieve?
                          Chelsea B.NinjaTrader Customer Service

                          Comment


                            #14
                            My mistake I meant to say OnBarUpdate().

                            I would like to send an email when the indicator goes above a numerical reading of 20.

                            Apparently I a missing something here as I get a lot of emails.

                            if (CustomTradingActivityIndicator >= 20
                            && trigger == true)
                            {
                            SendMail(emailFrom, emailTo, "High Activity " +Instrument.FullName+ " "+hash,"");
                            trigger = false;
                            }

                            Comment


                              #15
                              Hello brucelevy,

                              Currently, the code you have would trigger anytime the method it is called in is triggered.

                              If you have the code in OnMarketData, this would send an email for every market data event (meaning every tick and every change in ask and bid data) anytime the condition is true.
                              This could mean quite a bit of emails.

                              If your code is in OnBarUpdate and Calculate on bar close (COBC) is True, and there are no added data series, this would send an email once per bar any time the condition is true.
                              If COBC is false, this would send an email every tick when the condition is true.
                              If there is a secondary series added, this would send an email for every bar update of the primary and the secondary series.

                              As a suggestion, what you could do is use a variable to track the last state of the indicator, and only send an email when the indicator doesn't match the last value saved to the indicator. This way it would only send an email when the value changes.

                              Code:
                              private int lastIndicatorValue = 0;
                              protected override void OnBarUpdate()
                              {
                                      if (lastIndicatorValue != MyCustomIndicator()[0] && MyCustomIndicator()[0] >= 20)
                                      {
                                      // send email
                                      }
                                      lastIndicatorValue = MyCustomIndicator()[0];
                              }
                              Chelsea B.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Rapine Heihei, Today, 07:51 PM
                              0 responses
                              3 views
                              0 likes
                              Last Post Rapine Heihei  
                              Started by frslvr, 04-11-2024, 07:26 AM
                              5 responses
                              96 views
                              1 like
                              Last Post caryc123  
                              Started by algospoke, 04-17-2024, 06:40 PM
                              6 responses
                              49 views
                              0 likes
                              Last Post algospoke  
                              Started by arvidvanstaey, Today, 02:19 PM
                              4 responses
                              11 views
                              0 likes
                              Last Post arvidvanstaey  
                              Started by samish18, 04-17-2024, 08:57 AM
                              16 responses
                              61 views
                              0 likes
                              Last Post samish18  
                              Working...
                              X