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

Historical state or CurrentBar issue - indicator run amok

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

    Historical state or CurrentBar issue - indicator run amok

    I'm trying ot create an indicator that looks at the latest 4 bars of a chart and send me and send me an email if a particular condition occurs.I set it to use Calculate.OnBarClose but when I apply it to a chart and switch the IsVisible to on, it sends a flood on email and crashes NT. My guess is it is processing more bars than it should. I've tried changing the value of "CurrentBar", tried using "IsFirstTickOfBar", all to no avail. I'm attaching a snippet of some of the relevant code. I believe the resolution will involve State === historical but I'm not sure where it would go and the how it is worded correctly.

    Any help would be appreciated.!​
    Attached Files

    #2
    Hello sobertradingpartner,

    Thank you for your post.

    I also suspect that it is attempting to send a flood of emails when it runs through the historical data upon starting the indicator.

    I recommend adding logic to either prevent the indicator from processing in historical (you can add this at the top of OnBarUpdate()):

    Code:
    if (State == State.Historical)
    return;
    Or adding a check in the condition that would cause the email to be sent that makes sure the indicator is running in real-time.

    Code:
    if (State == State.Realtime)
    // send email


    Please let me know if you have any further questions.
    Gaby V.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Gaby View Post
      I also suspect that it is attempting to send a flood of emails when it runs through the historical data upon starting the indicator.
      Are you sure?

      The SendMail documentation says,

      "This method can only be called once the State has reached State.Realtime.
      Calls to this method in any other State will be silently ignored​..."

      So, according to the help guide, calls to SendMail in State.Historical should
      not be causing the problem.

      Just my 2˘.

      Comment


        #4
        Originally posted by NinjaTrader_Gaby View Post
        Hello sobertradingpartner,

        Thank you for your post.

        I also suspect that it is attempting to send a flood of emails when it runs through the historical data upon starting the indicator.

        I recommend adding logic to either prevent the indicator from processing in historical (you can add this at the top of OnBarUpdate()):

        Code:
        if (State == State.Historical)
        return;
        Or adding a check in the condition that would cause the email to be sent that makes sure the indicator is running in real-time.

        Code:
        if (State == State.Realtime)
        // send email


        Please let me know if you have any further questions.
        That worked perfectly, thank you! Incidentally, I ended up using both - added "Historical" just after OnBarUpdate and "Realtime" to the logic that sends the email. Does it matter what the "CurrentBar" settings is?

        Comment


          #5
          Originally posted by sobertradingpartner View Post
          I'm trying ot create an indicator that looks at the latest 4 bars of a chart and send me and send me an email if a particular condition occurs.I set it to use Calculate.OnBarClose but when I apply it to a chart and switch the IsVisible to on, it sends a flood on email and crashes NT. My guess is it is processing more bars than it should. I've tried changing the value of "CurrentBar", tried using "IsFirstTickOfBar", all to no avail. I'm attaching a snippet of some of the relevant code. I believe the resolution will involve State === historical but I'm not sure where it would go and the how it is worded correctly.

          Any help would be appreciated.!​
          First off, let's consider this code,

          Code:
          if (CurrentBar < -2)
              return;
          Why the negative number? That's wrong.

          I see the highest BarsAgo index used is 3,
          so that guard code should actually be,

          Code:
          if (CurrentBar < 3)
              return;
          Do you understand why?

          -=o=-

          Try changing your code to this,

          IsSuspendedWhileInactive = false;

          See if that makes a difference.

          -=o=-

          You'll need to debug your code.

          Instead of calling SendMail, use Print instead,
          then isolate why that Print is being called so
          much using other Print statements.

          //SendMail(....);
          Print("Bar=" + CurrentBar + " Calling SendMail now");


          If you need more help than that, we'd probably
          need to see more code, esp the code that is
          calling SendMail.

          Last edited by bltdavid; 02-16-2024, 09:53 AM.

          Comment


            #6
            Originally posted by bltdavid View Post

            First off, let's consider this code,

            Code:
            if (CurrentBar < -2)
            return;
            Why the negative number? That's wrong.

            I see the highest BarsAgo index used is 3,
            so that guard code should actually be,

            Code:
            if (CurrentBar < 3)
            return;
            Do you understand why?

            -=o=-

            Try changing your code to this,

            IsSuspendedWhileInactive = false;

            See if that makes a difference.

            -=o=-

            You'll need to debug your code.

            Instead of calling SendMail, use Print instead,
            then isolate why that Print is being called so
            much using other Print statements.

            //SendMail(....);
            Print("Bar=" + CurrentBar + " Calling SendMail now");


            If you need more help than that, we'd probably
            need to see more code, esp the code that is
            calling SendMail.

            Thank you. I've read, but I guess not correctly understood the CurrentBar usage. Some said -1 some said 1. The use case examples weren't very helpful. I appreciate your clarifying that. My point was to make sure all the bars loaded, then run off the last four bars (0-3). As that part of code is working now it was as I suspected, it was running off the historical data.
            Last edited by sobertradingpartner; 02-16-2024, 10:20 AM.

            Comment


              #7
              Originally posted by sobertradingpartner View Post
              AS that part of code is working now it was as I suspected, it was running off the historical data.
              But, SendMail is supposed to do nothing when called during State.Historical.

              You should confirm that, with something like this,

              SendMail(..., "Testing SendMail",
              "Bar=" + CurrentBar + " State=" + State + " Calling SendMail");


              Are you using the SendMail API?

              Comment


                #8
                Originally posted by sobertradingpartner View Post
                Thank you. I've read, but I guess not correctly understood the CurrentBar usage. Some said -1 some said 1. The use case examples weren't very helpful. I appreciate your clarifying that.
                You're welcome!

                More good reading here, here, and here.



                Comment


                  #9
                  Originally posted by bltdavid View Post

                  But, SendMail is supposed to do nothing when called during State.Historical.

                  You should confirm that, with something like this,

                  SendMail(..., "Testing SendMail",
                  "Bar=" + CurrentBar + " State=" + State + " Calling SendMail");


                  Are you using the SendMail API?
                  I'm using an added "SendMailChart" function as shown in the attachment.
                  Attached Files

                  Comment


                    #10
                    Originally posted by sobertradingpartner View Post
                    I'm using an added "SendMailChart" function as shown in the attachment.
                    Ah-hah!

                    Thanks, that explains it.



                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by lightsun47, Today, 03:51 PM
                    0 responses
                    2 views
                    0 likes
                    Last Post lightsun47  
                    Started by 00nevest, Today, 02:27 PM
                    1 response
                    8 views
                    0 likes
                    Last Post 00nevest  
                    Started by futtrader, 04-21-2024, 01:50 AM
                    4 responses
                    41 views
                    0 likes
                    Last Post futtrader  
                    Started by Option Whisperer, Today, 09:55 AM
                    1 response
                    13 views
                    0 likes
                    Last Post bltdavid  
                    Started by port119, Today, 02:43 PM
                    0 responses
                    8 views
                    0 likes
                    Last Post port119
                    by port119
                     
                    Working...
                    X