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

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, 01: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, 01: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)
            			{
            			    [COLOR="Red"]Realtime = true[/COLOR];
            			}
            			else if (State == State.Terminated)
            			{
            				[COLOR="red"]if ( Realtime == true )[/COLOR]
            				{
            					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 GwFutures1988, Today, 02:48 PM
                1 response
                3 views
                0 likes
                Last Post NinjaTrader_Clayton  
                Started by ScottWalsh, 04-16-2024, 04:29 PM
                6 responses
                27 views
                0 likes
                Last Post ScottWalsh  
                Started by frankthearm, Today, 09:08 AM
                10 responses
                36 views
                0 likes
                Last Post frankthearm  
                Started by mmenigma, Today, 02:22 PM
                1 response
                3 views
                0 likes
                Last Post NinjaTrader_Jesse  
                Started by NRITV, Today, 01:15 PM
                2 responses
                9 views
                0 likes
                Last Post NRITV
                by NRITV
                 
                Working...
                X