• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Little issue using SendMail() to know when a Strategy is disabled

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

    Little issue using SendMail() to know when a Strategy is disabled

    Since I like to know if my Strategy was disabled, I use the email sharing service; so I put this basic instruction:

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    ........
    }
    else if (State == State.Configure)
    {
    ........
    }
    else if (State == State.Terminated)
    {
    SendMail ("[email protected]", "STRATEGY DISABLED ! ", " Review " );
    }
    }
    Whenever I set the Strategy from the Tab "Strategy", a "light error, non-disruptive" pops up in the log:
    Strategy 'PRS': Error on calling 'OnStateChange' method: Object reference not set to an instance of an object.

    It seems NOT to have serious consequences of any kind, it even works as thought but since I don't like to see errors, I'd like to know if perhaps there's some restriction to use sendmail() from state.Terminated, if so, please indicate me another way to send the alert.

    Thanks

    #2
    Hello pstrusi,

    Thanks for opening the thread.

    I am not sure if you are receiving a null reference exception elsewhere in OnStateChange(), but you should not have issue sending an email from State.Terminated.

    I would suggest to add some additional code to control your email so it is triggered after State.Terminated happens after the strategy has reached State.Realtime or State.Historical, since State.Terminated is reached after you open a strategy dialog window. This is outlined in the documentation page linked below.

    NinjaScript Lifecycle - https://ninjatrader.com/support/help...fecycle_of.htm
    JimNinjaTrader Customer Service

    Comment


      #3
      Thanks Jim, excellent suggestion!

      In fact, I can confirm to you that was the cause of the error in the log. With a few lines checking first the state was the solution.

      Regards!
      Last edited by pstrusi; 09-28-2017, 12:00 PM.

      Comment


        #4
        Originally posted by pstrusi View Post
        Thanks Jim, excellent suggestion!

        In fact, I can confirm to you that was the cause of the error in the log. With a few lines checking first the state was the solution.

        Regards!
        Dear pstrusi,

        I face the same issue. Would you mind to share how you did rewrite the code?

        Comment


          #5
          No problem, here you can see an easy workaround.
          Previously declare an Integer variable called "a" in this example, then it follows like this:

          Code:
          protected override void OnStateChange()
          		{
          			if (State == State.SetDefaults)
          			{
          				.......				
          			}
          			else if (State == State.Configure)
          			{
          				.......
          			}
          			else if (State == State.Realtime)
          			{
          			    a=1;
          			}
          			else if (State == State.Terminated)
          			{
          				if ( a > 0 )
          				{
          					SendMail ("[email protected]", "STRATEGY DISABLED ! ", " Review " );
          				}
          			}
          		}
          Last edited by pstrusi; 11-06-2017, 12:10 PM.

          Comment


            #6
            Originally posted by pstrusi View Post
            No problem, here you can see an easy workaround.
            Previously declare an Integer variable called "a" in this example, then it follows like this:

            private bool Realtime = false;

            Code:
            protected override void OnStateChange()
            		{
            			if (State == State.SetDefaults)
            			{
            				.......			     		
            			}
            			else if (State == State.Configure)
            			{
            				.......
            			}
            			else if (State == State.Realtime)
            			{
            			    Realtime = true;
            			}
            			else if (State == State.Terminated)
            			{
            				if ( Realtime == true )
            				{
            					SendMail ("[email protected]", "STRATEGY DISABLED ! ", " Review " );
            				}
            			}
            		}
            Would it also work if I use a bool? Like the above?

            Comment


              #7
              Originally posted by sagetrade View Post
              Would it also work if I use a bool? Like the above?
              I don't see why not, Just make sure that variable's value does not change unwillingly in the code. That was just a simple example to do it but you can create a more refined one with more functions.

              Comment


                #8
                Thank you. Works well!

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by lavalampmj, Today, 05:25 PM
                0 responses
                3 views
                0 likes
                Last Post lavalampmj  
                Started by Antares66, Today, 05:19 PM
                0 responses
                2 views
                0 likes
                Last Post Antares66  
                Started by jmneto, Today, 05:18 PM
                0 responses
                4 views
                0 likes
                Last Post jmneto
                by jmneto
                 
                Started by Bobbyj78, Today, 02:21 PM
                2 responses
                14 views
                0 likes
                Last Post bltdavid  
                Started by EnveousColnago, Today, 02:10 PM
                1 response
                8 views
                0 likes
                Last Post NinjaTrader_Ariana  
                Working...
                X