Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

SendMail with Price?

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

    #46
    Hi dsraider,

    Unfortunately, I cannot debug this for you.

    As an alternative, you can work with limit and stop IOrders, which will be accessible directly, instead of SetStopLoss and SetProfitTargets.

    TimNinjaTrader Customer Service

    Comment


      #47
      Tim,

      Just to clarify, so that I don't spend hours and hours chasing my tail, there is a way to use SendMail() with SetStopLoss and SetProfitTarget but I just don't have the right code....correct?

      Dave

      Comment


        #48
        Dave, yes using the Exit methods would be easier here as you do not need to capture IOrders first, as those methods would directly provide those as returns so you can check needed fillstates directly.

        Comment


          #49
          Bertrand!

          Good. I'd much prefer to use SetStopLoss and SetProfitTarget. Is my code way off? I feel like I'm just missing a simple step but can't find the answer anywhere.

          Dave

          Comment


            #50
            Hi Dave, does your code make it to the section where the stops would report filled and a mail should be sent - did you verify this via the prints as Tim outlined in a prior post? If not this would mean you need to debug the way you store the IOrder references and access their unique tokens. I suggest to simplify the code and just test with one timed order realtime, so you verify the basics are working.

            Comment


              #51
              Hello Sir,

              It didn't make it, no . That's the strange thing. The actual orders themselves work perfectly but it just doesn't email and I have them set up together. Happy to try any other suggestions.

              Comment


                #52
                Okay,

                I got the stops and target to both email when filled. Go me. The only problem now is that they don't include PnL. Here's what I have:

                Code:
                protected override void OnBarUpdate()
                { 
                							// Check to make sure there is at least one trade in the collection 
                							if (Performance.RealtimeTrades.Count > 0) 
                							{ 
                								// Get the last completed real-time trade (at index 0) 
                								Trade lastTrade = Performance.AllTrades[Performance.AllTrades.Count - 1]; 
                						
                								if (lastTrade != null) 
                        						Print("The last trade profit is " + lastTrade.ProfitPoints); 
                						
                								
                							} 
                						}	
                
                protected override void OnOrderUpdate(IOrder order)
                if (order.OrderState == OrderState.PendingSubmit)
                			{
                				// Add the "Stop loss" orders to the Stop Loss collection
                				if (order.Name == "Stop loss")
                					stopLossTokens.Add(order.Token);
                				
                				// Add the "Profit target" orders to the Profit Target collection
                				else if (order.Name == "Profit target")
                					profitTargetTokens.Add(order.Token);
                			}
                			
                
                			// Process stop loss orders
                			if (stopLossTokens.Contains(order.Token))
                			{
                				// Check order for terminal state
                				if (order.OrderState == OrderState.Filled)
                				{
                					// Print out information about the order
                					Print(order.ToString());
                					SendMail("", "[email protected]", Instrument.MasterInstrument.Name + " Cross2050 Stop Filled " + order.AvgFillPrice + ": " + lastTrade.ProfitPoints, "");
                					
                				if (order.OrderState == OrderState.Cancelled || order.OrderState == OrderState.Filled || order.OrderState == OrderState.Rejected)	
                					// Remove from collection
                					{
                					stopLossTokens.Remove(order.Token);
                					}
                					
                				}
                				// Print out the current stop loss price
                				else
                					Print("The order name " + order.Name + " stop price is currently " + order.StopPrice);
                			}
                While this prints to the output window correctly (without the SendMail() code), it results in error CS0117: 'double' does not contain a definition for 'ProfitPoints'

                I tried adding a private double variable but just kept getting error messages. Any hints?

                Thanks,
                Dave

                Comment


                  #53
                  Dave, please try defining the lastTrade variable not local to the OnBarUpdate() method, but with global scope in the Variables section.

                  Comment


                    #54
                    Morning Bertrand,

                    The following resulted in an error message:

                    Variables
                    private double lastTrade = Performance.AllTrades[Performance.AllTrades.Count - 1];

                    Am I following you correctly?

                    Comment


                      #55
                      If you don't have any trades then count-1 will be not valid.

                      Comment


                        #56
                        Hi Baruch,

                        Well, I only have SendMail() set up when my stop or target is filled, thereby completing a trade. Do I need to add something other than what I have to let it know it was in one in the first place?

                        Thanks,
                        Dave

                        Comment


                          #57
                          You could just define private Trade lastTrade in Variables and then just use the code you had, the would give the lastTrade variable the wider scope needed to be recognized in your OnOrderUpdate() part.

                          Comment


                            #58
                            Sweet!

                            I will try that as soon as I get home.

                            Thank you!

                            Comment


                              #59
                              Sir,

                              Code:
                              Variables
                              private Trade lastTrade = Performance.AllTrades[Performance.AllTrades.Count - 1];
                              Gave me this:

                              An object is required for the nonstatic field, method, or property "NinjaTrader.Strategy.StrategyBase.Performance.get "

                              Why oh why do you love to torture me?

                              Dave

                              Comment


                                #60
                                Hi Dave, just

                                private Trade lastTrade; in the Variables should do.

                                You can then assign the value later, but this variable has a script wide scope, which you need later in your code.

                                Comment

                                Latest Posts

                                Collapse

                                Topics Statistics Last Post
                                Started by argusthome, 03-08-2026, 10:06 AM
                                0 responses
                                23 views
                                0 likes
                                Last Post argusthome  
                                Started by NabilKhattabi, 03-06-2026, 11:18 AM
                                0 responses
                                20 views
                                0 likes
                                Last Post NabilKhattabi  
                                Started by Deep42, 03-06-2026, 12:28 AM
                                0 responses
                                14 views
                                0 likes
                                Last Post Deep42
                                by Deep42
                                 
                                Started by TheRealMorford, 03-05-2026, 06:15 PM
                                0 responses
                                10 views
                                0 likes
                                Last Post TheRealMorford  
                                Started by Mindset, 02-28-2026, 06:16 AM
                                0 responses
                                41 views
                                0 likes
                                Last Post Mindset
                                by Mindset
                                 
                                Working...
                                X