Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Sendmail Issue

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

    Sendmail Issue

    Hi Guys,

    I have an issue using the SendMail command in my strategy. I have a strategy that works how I want, the only issue is, it does not send an email alert for buy and sell in real time. So, in reading some of the earlier posts, I found that it I needed to have the Historical set to false during real time for the strategy to send an email alert. So, I turned Historical to false in my strategy and it now sends an email.

    The problem is, it now does not follow the same strategy rules with the Historical turned off as it does with it on. With Historical on, the strategy follows my intended rules & matches backtest results. With it off, it can give me a signal that does not match the strategy with Historical on. I guess this makes sense that it may produce different signals since it does not have historical data. But then why would it be a requirement to have Historical = false for this to work if it changes the strategy results? I'm going to review my strategy, but I don't think I understand this...

    Is there a way to get around this or is this a problem with my strategy? Maybe there is something else I'm missing?

    Thanks,

    Lee

    #2
    Hello lee612801,

    Thank you for your inquiry.

    Can you please clarify what you mean by setting Historical to false?

    Are you speaking of running a real-time check?

    Example:
    Code:
    // Only run on real-time data
    if (Historical) 
         return;
    With this check implemented, the strategy will not run its logic against historical bars, which would explain the differences in behavior on how your strategy runs with having this check or not having this check.

    SendMail() only occurs on real-time bars and will not be called on historical bars.

    Can you please provide a sample strategy that demonstrates the behavior you are seeing?
    Zachary G.NinjaTrader Customer Service

    Comment


      #3
      Hi Zac,

      Yes, I used the real time check as you provided here:

      // Only run on real-time data
      if (Historical)
      return;

      So based on what you said, I understand why it would have differences in the buy/sell signals. I guess my simple question is, should the SendMail work in real time if I do not have the if (Historical) return; implemented?

      I don't understand what the SendMail would be good for if you can't use it for a strategy that can run against historic bars? How do I get my strategy that needs historic data to signal properly to Send an email alert in real time?

      Thanks,

      Lee
      Last edited by lee612801; 12-10-2015, 05:46 PM.

      Comment


        #4
        Hello Lee,

        SendMail() will work on a strategy that does process historical bars, but will only ever be called once the strategy is running over live data.

        Can you please provide the condition that you are trying to evaluate in order for your SendMail() to be called? Have you ensured that the condition that you are trying to use SendMail() on is evaluating to true and that the strategy is not evaluating a historical bar when that condition is true?

        Here's an example that you can use to check (change the condition in the print to whatever you are trying to evaluate):
        Code:
        Print("Historical? " + Historical); // will output true if strategy is evaluating a historical bar
        Print("Close[0] > Close[1]?" + (Close[0] > Close[1])); // true if the condition is true; condition below will evaluate
             if (Close[0] > Close[1])
                  SendMail(....);
        Additionally, are there any errors in the log tab appearing if SendMail() is, indeed, being called?
        Zachary G.NinjaTrader Customer Service

        Comment


          #5
          Hi Zac,

          Ok, thanks for the info. So to clarify, I need to ensure that the strategy is not evaluating a historical bar when the condition I want is true. I will try that tonight. If this is true, it means my strategy is evaluating a historic bar at the moment it receives a trade signal, so it won't execute the SendMail command.

          Come to think of it, I believe my strategy is using historic bars when I want it to send the message. When the trade condition is true, and I get a trade signal, its based on the previous day's bar close as I have COBC=true. so I don't see how it would not be using a historic bar in this case. I will have to double check...

          Is there anyway around this? If my strategy runs on historic daily bars, and I need COBC=true for consistent results, is there any other way to get it to execute the SendMail command when I get a trade signal?

          Regards,

          Lee

          Comment


            #6
            Hello Lee,

            Unless the condition occurred while the strategy is running over real-time data, a call to SendMail() will be ignored.

            With CalculateOnBarClose set to true, OnBarUpdate() will be called once a bar closes. If the bar that closes is a real-time bar, and the condition evaluates to true, SendMail() will be called in that condition.

            If the condition is checking over historical bars, but the strategy is currently running on real-time data, the SendMail() will still be called.

            As an example:
            Code:
            // if the close value from 2 bars ago is greater than the close value from 3 bars ago, send an email
            if (Close[2] > Close[3])
                 SendMail(....);
            As long as the condition above is evaluated at the close of a real-time bar, your strategy is running real-time and SendMail() will be called.
            Zachary G.NinjaTrader Customer Service

            Comment


              #7
              Hi Zac,

              I think I see the problem. I am using real-time data, BUT, the condition is triggered from the close of the previous daily bar since I have COBC = true. So it works like this:

              At the start of each current trading day, it evaluates the historic daily bar data and determines if a signal has been triggered. If true, it executes a trade at the beginning of the current trading day, but this current daily real time bar is not closed since COBC = true. So since the real time bar is not closed until the end of the day, it won't execute the SendMail at the beginning of the day when the signal was triggered... Damn...

              Do you know of any reasonable ways around this? Changing COBC = false causes issues with my strategy, but would this now trigger the SendMail at the beginning of the day when the trade is signaled?

              Thanks again,

              Lee

              Comment


                #8
                Hello Lee,

                With CalculateOnBarClose set to false, every new tick that comes in will call OnBarUpdate(). If the condition for the SendMail() to be called is true when the OnBarUpdate() method is called, then the SendMail() will be called as the strategy is currently running over real-time data.

                What you could do is Add() an additional Bars object with a more granular time frame. With a smaller time frame, you would be able to call SendMail() even before your daily bar closes. For instance, you could Add() a 1 tick series to your strategy and have the SendMail() execute once that series calls OnBarUpdate() (which would be on every new tick).

                With this approach, however, you would be unable to remove this added Series from your strategy when no longer needed and so it will continue to process in the background.

                You could use your signal to change a bool variable to true and allow the tick series to check if this bool is true. If it's true, call SendMail(). Just make sure you provide some logic to prevent the SendMail() from continuously occurring.

                As an example:
                Code:
                private bool timeToSendMail = false;
                
                protected override void Initialize()
                {
                     Add(PeriodType.Tick, 1);
                }
                
                protected override void OnBarUpdate()
                {
                     // primary daily bars
                     if (BarsInProgress == 0)
                     {
                          if (/*signal condition*/)              
                               timeToSendMail = true;
                     }
                
                     // secondary tick bars
                     if (BarsInProgress == 1)
                     {
                          if (timeToSendMail)
                          {
                               SendMail(....);
                               timeToSendMail = false; // change to false so the if statement does not evaluate as true again
                          }
                     }
                }
                More information about working with multiple time frames can be found in the help guide here: https://ninjatrader.com/support/help...nstruments.htm
                Zachary G.NinjaTrader Customer Service

                Comment


                  #9
                  Hi Zac,

                  Great idea! I was thinking of using something with multiple bars, but did not know how to go about it. I will give this a try later and see how it works.

                  Thanks again!

                  Lee

                  Comment


                    #10
                    Hi Zac,

                    This should work in Playback just at it would in real time? I tried adding a new bars object to my strategy as you suggested and testing it in Playback. I tried both a Tick and Minute bars object, but I still could not get it to trigger the SendMail. When I used the "Print("Historical? " + Historical);", it kept coming up as true during playback even with the additional bars object. The only way I could get it to work is if I had the "if (Historical) return;" in the strategy, but then I do have not historical data to run off. Is there a way to run one set of bars objects with Historical data and another set of bars objects without?

                    Thanks,

                    Lee

                    Comment


                      #11
                      Hello Lee,

                      Testing a strategy over Market Replay would be similar to testing it live.

                      Can you please provide a sample script that duplicates the behavior that you are seeing so I may test on my end?
                      Zachary G.NinjaTrader Customer Service

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                      0 responses
                      630 views
                      0 likes
                      Last Post Geovanny Suaza  
                      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                      0 responses
                      364 views
                      1 like
                      Last Post Geovanny Suaza  
                      Started by Mindset, 02-09-2026, 11:44 AM
                      0 responses
                      105 views
                      0 likes
                      Last Post Mindset
                      by Mindset
                       
                      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                      0 responses
                      566 views
                      1 like
                      Last Post Geovanny Suaza  
                      Started by RFrosty, 01-28-2026, 06:49 PM
                      0 responses
                      568 views
                      1 like
                      Last Post RFrosty
                      by RFrosty
                       
                      Working...
                      X