Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

SendMail with Price?

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

    #16
    In OnBarUpdate()
    Code:
    if (CrossAbove(EMA(20), EMA(50), 1))
    {
         entryOrder = EnterLong(DefaultQuantity, "2050 Cross");
         DrawArrowUp(...);
         DrawTriangleUp(...);
    }
    In OnOrderUpdate()
    Code:
    protected override void OnOrderUpdate(IOrder order)
    {
         if (entryOrder != null && entryOrder.Token == order.Token)
         {
              if (order.OrderState == OrderState.Filled)
                   SendMail(...);
         }
    }
    Josh P.NinjaTrader Customer Service

    Comment


      #17
      Josh wins! Okay, next February, drinks are on me. Sorry, Bertrand, but Josh is now my favorite.

      Will give this a whirl when I get home.

      Thanks, Josh. I really appreciate it.

      Dave

      Comment


        #18
        Okay, here's where I'm at...

        Entry:
        if (entryOrder1 != null && entryOrder1.Token == order.Token)
        {
        if (entryOrder1.OrderState == OrderState.Filled)
        SendMail("", "[email protected]", "ES Long " + Position.AvgPrice, "");
        }
        ---> Still gives me ES Long 0.

        Breakeven works perfectly.

        Exit:
        if (exitOrder1 != null && exitOrder1.Token == order.Token)
        {
        if (exitOrder1.OrderState == OrderState.Filled)
        SendMail("", "[email protected]", "ES Long Exit " + (Close[0]), "");
        ---> Usually right but is sometimes off by a tick.

        For my trail, though, I'm at a loss. Here's my trail code:

        case MarketPosition.Long:
        // Once the price is greater than entry price+ breakEvenTicks ticks, set stop loss to breakeven
        if (Close[0] == Position.AvgPrice + breakEvenTicks * TickSize && previousPrice == 0)
        {
        initialBreakEven = Position.AvgPrice + plusBreakEven * TickSize;
        SetStopLoss("2050 Cross", CalculationMode.Price, initialBreakEven, false);
        previousPrice = Position.AvgPrice;
        PrintWithTimeStamp("previousPrice = "+previousPrice);
        PrintWithTimeStamp("newPrice = "+previousPrice);
        PlaySound(@"C:\Program Files\NinjaTrader 6.5\sounds\AutoTrail.wav");
        SendMail("", "[email protected]", "ES Stop to Even", "");
        }
        // Once at breakeven wait till trailProfitTrigger is reached before advancing stoploss by trailStepTicks size step
        else if (previousPrice != 0 ////StopLoss is at breakeven
        && Close[0] > previousPrice + trailProfitTrigger * TickSize)
        {
        newPrice = previousPrice + trailStepTicks * TickSize;
        SetStopLoss("2050 Cross", CalculationMode.Price, newPrice, false);
        previousPrice = newPrice;
        PrintWithTimeStamp("previousPrice = "+previousPrice);
        PrintWithTimeStamp("newPrice = "+previousPrice);
        PlaySound(@"C:\Program Files\NinjaTrader 6.5\sounds\AutoTrail.wav");
        }
        }

        While I'm getting one email when my stop moves to breakeven, I'm getting two emails for entry and exit. I'd like to take those down to one. Also, if my stop or target is taken out instead of my exit, how do I go about grabbing that price and sending it off in an email?

        Thanks,
        Dave

        Comment


          #19
          Hello,

          Maybe Josh can help you more, but have you considered putting in a bool flag that stops the next email and resets on another criteria?

          if(!myFlag)
          {
          //do something
          myFlag = true;
          //send your email here
          }


          if(...some other criteria...)
          {
          myFlag = false;
          }
          DenNinjaTrader Customer Service

          Comment


            #20
            Hi Ben,

            I haven't, no, but since it's supposed to email only when the order is filled (a one-time occurrence), I don't think there's anything wrong with my code. Also, I'm currently looking into IExecution as I'd prefer to just have it email me when a trade is taken or an execution is made, rather than code for five different possibilities. In fact, I put the following together but no email was sent:

            Code:
            protected override void OnExecution(IExecution execution)
            			{		
            				if (entryOrder1 != null && entryOrder1.Token == execution.Order.Token) 
            					SendMail("", "[email protected]", "ES " + execution, "");
            			}
            Is there anything I can do with this to simplify things?

            Thanks,
            Dave

            Comment


              #21
              Hello,

              I'm not sure. Did you use a Print() in the IExecution to see if it is being called at all? Also, try putting brackets around your email and print inside of that to see if that is being called.
              DenNinjaTrader Customer Service

              Comment


                #22
                Well, I found part of the problem. According to Ninja Help, it should be "execution.Price", not just "executon". Still, it neither emailed nor printed. Strange. Anyway, is there no way to just have it email filled orders, no matter what they may be?

                Thanks,
                Dave

                Comment


                  #23
                  Dave,

                  If you want to send emails for filled orders you shouldn't do it from OnExecution(). Instead try using OnOrderUpdate(). The only if-statement you need from there is if (order.OrderState == OrderState.Filled).
                  Josh P.NinjaTrader Customer Service

                  Comment


                    #24
                    Hi Josh,

                    Fair enough. Can you give me a hint on how to email if my target or stop is filled? I'm guessing the code for that is different. Also, is there just no way to simply email whatever the last filled order was in general, thereby eliminating all the different SendMail() snippets?

                    Thanks.

                    Comment


                      #25
                      If you want to know if a target or a stop is filled you need to filter the OnOrderUpdate() for the IOrder objects of the target and of the stop. When those objects return a filled state then you know the target or the stop was filled. If you want a general email for any order that is filled then just use the if-statement in my prior email instead of any filtering of IOrder objects.
                      Josh P.NinjaTrader Customer Service

                      Comment


                        #26
                        Josh,

                        So:

                        if (order.OrderState == OrderState.Filled).
                        SendMail(...)

                        will work? And will it intuitively know what the order was (i.e. Long Entry, Stop Filled, etc) or is that why you have to filter through IOrder?

                        Thanks,
                        Dave

                        Comment


                          #27
                          Dave,

                          Code:
                          protected override void OnOrderUpdate(IOrder order)
                          {
                               if (order.OrderState == OrderState.Filled)
                                    SendMail(....whatever message you want...);
                          }
                          From the email you send you can check the order.Name property to get the signal name. If you name it something that actually has Long or Short in the name then it will let you know if it is long or short in your email.
                          Josh P.NinjaTrader Customer Service

                          Comment


                            #28
                            Josh,

                            Code:
                            if (entryOrder1 != null && entryOrder1.Token == order.Token)
                                 			{
                            				if (Position.MarketPosition != MarketPosition.Flat)	
                                      			{
                            					if (entryOrder1.OrderState == OrderState.Filled)
                            					SendMail("", "[email protected]", "ES Long " + Position.AvgPrice, "");
                            					}
                                 			}
                            &&

                            Code:
                            if (entryOrder1 != null && entryOrder1.Token == order.Token)
                                 			{
                            				if (entryOrder1.OrderState == OrderState.Filled)
                                      			{
                            					if (Position.MarketPosition != MarketPosition.Flat)	
                            					SendMail("", "[email protected]", "ES Long " + Position.AvgPrice, "");
                            					}
                                 			}
                            both result in no email. Also, any reason why I'm getting two for entries and exits? If the entry only happens once, this should only send one email, right? I'm a little hazy on Ben's code but since this doesn't seem logical to me, I thought I'd ask.

                            Thanks.

                            Comment


                              #29
                              dsraider,

                              You will get as many emails as however many times your code reaches the SendMail() line. If you get no emails with the code below you will need to check your if-statements. Please use Print() and debug your if-statements to see which parts are true and which parts aren't.

                              Also, you cannot check for Position.MarketPosition inside of OnOrderUpdate(). Your order will be updated before your position is updated. Meaning, your order will fill and you would be in OnOrderUpdate() before Position.MarketPosition would report you actually being long or short.
                              Josh P.NinjaTrader Customer Service

                              Comment


                                #30
                                Fair enough. I only added it as you suggested I do so a few posts back, in order to have SendMail() register the price. Maybe I put it in the wrong spot.

                                About the double email, though. I don't follow you. My code says to send an email if the order is filled. If the order is only filled once, I don't understand why I'm getting two emails. For example, this only sends one:

                                Code:
                                if (Close[0] == Position.AvgPrice + breakEvenTicks * TickSize && previousPrice == 0)
                                {
                                	initialBreakEven = Position.AvgPrice + plusBreakEven * TickSize; 
                                        SendMail("", "[email protected]", "ES Stop to Even", "");
                                }
                                I know the code is slightly different but both are based on a single occurrence of something happening and I don't see one works and the other doesn't.

                                Thanks,
                                Dave

                                Comment

                                Latest Posts

                                Collapse

                                Topics Statistics Last Post
                                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                                0 responses
                                672 views
                                0 likes
                                Last Post Geovanny Suaza  
                                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                                0 responses
                                379 views
                                1 like
                                Last Post Geovanny Suaza  
                                Started by Mindset, 02-09-2026, 11:44 AM
                                0 responses
                                111 views
                                0 likes
                                Last Post Mindset
                                by Mindset
                                 
                                Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                                0 responses
                                575 views
                                1 like
                                Last Post Geovanny Suaza  
                                Started by RFrosty, 01-28-2026, 06:49 PM
                                0 responses
                                582 views
                                1 like
                                Last Post RFrosty
                                by RFrosty
                                 
                                Working...
                                X