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

E-mail Sent Upon Order Execution

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

    E-mail Sent Upon Order Execution

    Hi
    Quick question regarding sending email upon order execution using SendMail() function.
    I had a strategy with 'CalculateOnBarClose=False' where the email was sent immediately upon order execution with the SendMail() function in the 'OnBarUpdate' section; at the same time I sent a note to the Log file.

    However, with a slightly modified strategy & 'CalculateOnBarClose=True', I do not get any email send & I do not get a note sent to the file.

    Is there anything I should be aware of in the way strategies are executed when 'CalculateOnBarClose=True'.

    thx
    David

    #2
    Nothing that would affect SendMail(). Try running your original strategy where SendMail() yielded the results you expect with CalculateOnBarClose = true.
    RayNinjaTrader Customer Service

    Comment


      #3
      Thx Ray. I have done this, but the results are inconsistent, i.e. sometimes an e-mail is sent immediately upon order execution at the open of the new bar & sometimes it doesn't. this has nothing to do with the email sent per se, since in parallel I am sending a note to the log (the note to the log & the email being sent or not sent are consistent with each other).

      If the email is not sent for that particular bar the order was executed, it does send the email (& send a note to the log) the next time a new bar is opened (remember CalculateOnBarClose=True). This is not workable since I am using 30 minute & 60 minute bars.

      The trigger for sending the email & note to the log file is if the MarketPosition (Long, Short or Flar) has changed since the last bar.

      I hope you can help out with this! thx. David

      Comment


        #4
        Please paste your code from OnBarUpdate() into this thread.

        Thanks.
        RayNinjaTrader Customer Service

        Comment


          #5
          Ray - here it is ! thx. David


          protectedoverridevoid OnBarUpdate()
          {
          // Condition set 1

          if (ATR(14)[0] > ATRvalue11
          && Falling(MACD(
          12, 24, 9).Diff)
          && Close[
          0] <= EMA(EMAEntry11)[0]
          && Momentum(EMA(EMAEntry11),
          5)[0] <=0
          && Momentum(EMA(EMAExit11), 5)[0] <0
          && BarsSinceExit() > 1 || BarsSinceExit() == -1)
          {
          EnterShort(DefaultQuantity,
          "Short");
          }
          // Condition set 2
          if (Close[0] >= EMA(EMAEntry11)[0])

          {
          ExitShort(
          "", "Short");
          }
          // Condition set 3
          if (Momentum(EMA(EMAExit11), 5)[0] > 0)

          {
          ExitShort(
          "", "Short");
          }

          if (lastposition != Position.MarketPosition)
          {
          // Position changed, send e-mail confirmation.
          string msg = "";

          if (Position.MarketPosition == MarketPosition.Short)
          {
          msg =
          "YM EMA 1Hour Short BUY -> SL=105; PT=294";
          }

          elseif (Position.MarketPosition == MarketPosition.Flat)
          {
          msg =
          "YM EMA 1Hour Short CLOSE POSITION";
          }

          SendMail(Fromemail, Toemail, msg,
          "");
          Log(msg, NinjaTrader.Cbi.LogLevel.Information);
          }
          lastposition = Position.MarketPosition;

          }

          Comment


            #6
            After looking at your code quickly I figured that you always (!) set lastPosition at the end of your OnBarUpdate method.

            This probably is not intended: you should try to set it right after calling SendMail, since this makes sure, that a mail is sent before lastPosition is changed.

            Note: For bandwidth reasons we are unable to provide in-depth support for coding actual NS strategies.

            Comment


              #7
              Got it ! Great thx. Fix implemented & working robustly now.

              Couple of things that would be useful ->
              1/ more detailed NT strategy programming guide (esp. for dummies like me).
              2/ webinars/seminars on NT strategy programming.

              Any plans for either of these?
              One thing I will say - I think the package you have here is fantastic.

              thx David

              Comment


                #8
                Both is on our ToDo list. Thanks for your suggestion though.

                Comment


                  #9
                  Hi - am trying to send a notification (either to the log file or by email) when a trade exits via a trailing stop --- by tracking a market position change (i.e. from long to flat). CalculateOnBarClose = True (when it is set to false I do not have an issue).

                  The problem is that the exit notification does not get sent until after the close of the bar following the one where the trade actually exited (i.e. too late).

                  I am assuming that the problem is that the OnBarUpdate() is called only on the bar close so the code has no chance to send email until the next bar is closed. When the signal is received to exit the trade the Position.MarketPosition has not yet changed and the next time OnBarUpdate() is called is on the next bar which is too late.

                  What I believe I need to do is send the email when I get the exit signal - however, how do I do this with the trailing stop (until the new version of NT arrives).

                  I have inserted the code below:


                  // Entry Conditions
                  if (Momentum(EMA(EMAEntry10), 1)[0] >= 0
                  && BarsSinceExit() >= 2 || BarsSinceExit() == -1)

                  {
                  EnterLong(DefaultQuantity,
                  "Long");
                  Log(
                  "Enter Long Position (SL = 103, PT = 121). Open Price -> " + Close[0], NinjaTrader.Cbi.LogLevel.Information);
                  SendMail(
                  "[email protected]", "[email protected]", "GET IN", "");
                  lastPosition = Position.MarketPosition;
                  }


                  // All Exit Conditions
                  if (lastPosition != Position.MarketPosition)
                  {
                  if (Position.MarketPosition == MarketPosition.Flat)
                  {
                  Log(
                  "Get Out YM Long. Close Price -> " + Close[0], NinjaTrader.Cbi.LogLevel.Information);
                  SendMail(
                  "[email protected]", "[email protected]", "GET OUT", "");
                  }
                  lastPosition = Position.MarketPosition;
                  }

                  any help greatly appreciated.
                  thx
                  David

                  Comment


                    #10
                    You are correct in your diagnoses that CalculateOnBarClose is what is causing the described behavior. The only way you can work around this for now is with the use of CalculateOnBarClose = false. You can also try moving code into this if statement if you don't want it constantly executing:
                    Code:
                    if (FirstTickOfBar)
                    {
                         // Do something
                    }
                    When NinjaTrader 6.5 comes out what you are trying to do will be easier.
                    Josh P.NinjaTrader Customer Service

                    Comment


                      #11
                      thx Josh - that looks like a promising interim workaround. DG

                      Comment


                        #12
                        Interesting.........

                        1. The following works:
                        If (FirstTickOfBar)
                        {
                        Print("Hello")
                        }

                        2/ The following places orders at any point through the bar, but Prints to the output screen on first tick of bar !?!?! any ideas ?

                        if (FirstTickOfBar)
                        {
                        {
                        // Resets the stop loss to the original value when all positions are closed
                        if (Position.MarketPosition == MarketPosition.Flat)
                        {
                        SetTrailStop(CalculationMode.Ticks, TrailingStop10);
                        Trail =
                        20;
                        highestHigh = High[
                        0];
                        }

                        // If a long position is open, allow for stop loss modification.
                        elseif (Position.MarketPosition == MarketPosition.Long)
                        {
                        // if high of current bar greater than prior high, modify stop loss.
                        if (High[0] > highestHigh)
                        {
                        Trail = Trail - (Factor/
                        49 * (High[0] - highestHigh));

                        SetTrailStop(CalculationMode.Ticks, Trail);
                        Print(Trail);
                        Print(
                        "Hello");
                        highestHigh = High[
                        0];

                        }
                        }
                        }


                        // Entry Conditions
                        if (Momentum(EMA(EMAEntry10), 1)[0] >= 0
                        && BarsSinceExit() >= 2 || BarsSinceExit() == -1)

                        {
                        EnterLong(DefaultQuantity,
                        "Long");
                        Log(
                        "Enter Long Position (SL = 103, PT = 121). Open Price -> " + Close[0], NinjaTrader.Cbi.LogLevel.Information);
                        SendMail(
                        "[email protected]", "[email protected]", "GET IN", "");
                        lastPosition = Position.MarketPosition;
                        }


                        // All Exit Conditions
                        if (lastPosition != Position.MarketPosition)
                        {
                        if (Position.MarketPosition == MarketPosition.Flat)
                        {
                        Log(
                        "Get Out YM Long. Close Price -> " + Close[0], NinjaTrader.Cbi.LogLevel.Information);
                        SendMail(
                        "[email protected]", "[email protected]", "GET OUT", "");
                        }
                        lastPosition = Position.MarketPosition;
                        }

                        }
                        }

                        3/ If I only circle the 'All Exit Conditions' with 'If (FirstTickOfBar)', this section updates on the first tick of bar.

                        As you can see, I would like the whole of '2' to happen on FirstTickOfBar.

                        thx
                        David

                        Comment


                          #13
                          Trailing stop orders are executed in real-time no matter what your CalculateOnBarClose or FirstTickOfBar is set to. There is no way to delay those orders because of the way they are designed. They need to be executed immediately once the stop price has been hit. If this is not the behavior you want I advise you to create your own trailing stop algorithm with the use of ExitLongStop()s. Just keep modifying the stop price on this order as price goes up and eventually you will be stopped out, but keep in mind that if you put this in the FirstTickOfBar segment, the order will only be modified once per bar.
                          Josh P.NinjaTrader Customer Service

                          Comment


                            #14
                            Yes - I should have realised this. thx. DG

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by pmachiraju, 11-01-2023, 04:46 AM
                            8 responses
                            147 views
                            0 likes
                            Last Post rehmans
                            by rehmans
                             
                            Started by mattbsea, Today, 05:44 PM
                            0 responses
                            5 views
                            0 likes
                            Last Post mattbsea  
                            Started by RideMe, 04-07-2024, 04:54 PM
                            6 responses
                            33 views
                            0 likes
                            Last Post RideMe
                            by RideMe
                             
                            Started by tkaboris, Today, 05:13 PM
                            0 responses
                            5 views
                            0 likes
                            Last Post tkaboris  
                            Started by GussJ, 03-04-2020, 03:11 PM
                            16 responses
                            3,282 views
                            0 likes
                            Last Post Leafcutter  
                            Working...
                            X