• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

on execution is triggered multiple times

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

    on execution is triggered multiple times

    I have a addin where I am monitoring the executions and sending emails to myself. however I get 3 instances of onexecution for every execution in the order

    Code:
      protected override void OnStateChange()
    		{
    			if (State == State.SetDefaults)
    			{
    				Description									= @"Enter the description for your new custom Add on here.";
    				Name										= "SureshTradingSystemAddon";
                    Account.AccountStatusUpdate += Account_AccountStatusUpdate;
    				// Find our Sim101 account
            lock (Account.All)
    		{
                 fxcmaccount = Account.All.FirstOrDefault(a => a.Name == "xxxxxxxx" );//"Sim101");// change it to  
                    // Subscribe to account item updates
                    if (fxcmaccount != null)
    
                    {
                      //  account.AccountItemUpdate += OnAccountItemUpdate;
                        fxcmaccount.ExecutionUpdate += Account_ExecutionUpdate;
                    }
    				
    				simaccount = Account.All.FirstOrDefault(a => a.Name == "Sim101" );//"Sim101")
    
                    // Subscribe to account item updates
                    if (simaccount != null)
    
                    {
                      //  account.AccountItemUpdate += OnAccountItemUpdate;
                       simaccount.ExecutionUpdate += Account_ExecutionUpdate;
                    }
    				
    		}
    Code:
      private void Account_ExecutionUpdate(object sender, ExecutionEventArgs e)
            {
    
    
                Print(String.Format("Execution triggered for Order {0}", e.ToString()));
    			 NinjaTrader.Core.Globals.SendMail( "[email protected]", "@boards.trello.com", "trade ", e.ToString(),null) ;
    			NinjaTrader.NinjaScript.Alert.AlertCallback(e.Execution.Instrument, this, DateTime.Now.ToString(), NinjaTrader.Core.Globals.Now, Priority.High, e.ToString(), [email protected]"\sounds\Alert1.wav", new SolidColorBrush(Colors.Blue), new SolidColorBrush(Colors.White), 0);
    			//PostMessage(e.ToString(),true,e.Execution.Instrument);
                 
            }
    Code:
    Email - Message sent successfully
    Email - Message sent successfully
    Email - Message sent successfully
    Execution triggered for Order executionId='a5f198e7e99e45258515e12041678bad' account='Sim101' instrument='AUDUSD' exchange=Default price=0.75562 quantity=1,000 marketPosition=Short operation=Add orderID='e817373212b14fd386c0592b4f54a632' time='1/19/2017 3:12:43 PM' statementDate='2017-01-19'
    Execution triggered for Order executionId='a5f198e7e99e45258515e12041678bad' account='Sim101' instrument='AUDUSD' exchange=Default price=0.75562 quantity=1,000 marketPosition=Short operation=Add orderID='e817373212b14fd386c0592b4f54a632' time='1/19/2017 3:12:43 PM' statementDate='2017-01-19'
    Execution triggered for Order executionId='a5f198e7e99e45258515e12041678bad' account='Sim101' instrument='AUDUSD' exchange=Default price=0.75562 quantity=1,000 marketPosition=Short operation=Add orderID='e817373212b14fd386c0592b4f54a632' time='1/19/2017 3:12:43 PM' statementDate='2017-01-19'

    #2
    Hello junkone, and thank you for your question. While fully debugging your indicator or strategy is beyond the scope of the support we may provide, I did notice that you call this every time SetDefaults is run, and that you do not have a teardown. It is very likely that you are adding multiple handles to simaccount.ExecutionUpdate. Your simulation account object is an object that survives your strategy entirely. I would like to recommend using a static boolean variable, e.g.

    Code:
    private static bool haveAdded;
    protected override void OnStateChange()
    {
      if (State == State.SetDefaults)
      {
        Description                                    = @"Enter the description for your new custom Add on here.";
        Name                                        = "SureshTradingSystemAddon";
        Account.AccountStatusUpdate += Account_AccountStatusUpdate;
        // Find our Sim101 account
        lock (Account.All)
        {
          fxcmaccount = Account.All.FirstOrDefault(a => a.Name == "xxxxxxxx" );//"Sim101");// change it to
          // Subscribe to account item updates
          if (fxcmaccount != null)
    
          {
            //  account.AccountItemUpdate += OnAccountItemUpdate;
            fxcmaccount.ExecutionUpdate += Account_ExecutionUpdate;
          }
    
          simaccount = Account.All.FirstOrDefault(a => a.Name == "Sim101" );//"Sim101")
    
          // Subscribe to account item updates
          if (simaccount != null && ! haveAdded)
          {
            //  account.AccountItemUpdate += OnAccountItemUpdate;
            simaccount.ExecutionUpdate += Account_ExecutionUpdate;
            haveAdded = true;
          }
    
        }
      }
    }
    Please let us know if there is any other way we can help.
    Jessica P.NinjaTrader Customer Service

    Comment


      #3
      how do add'ons get activated. here is a addon that did not get fired after I started ninjatrader. but as soon as I just recompiled the code, it get activated.
      what I mean by actvation means that "when I place a order, it writes to the ooutput1 as specified in the code"

      Code:
      #region Using declarations
      using System;
      using System.Collections.Generic;
      using System.ComponentModel;
      using System.ComponentModel.DataAnnotations;
      using System.Linq;
      using System.Text;
      using System.Threading.Tasks;
      using System.Windows;
      using System.Windows.Input;
      using System.Windows.Media;
      using System.Xml.Serialization;
      using NinjaTrader.Cbi;
      using NinjaTrader.Gui;
      using NinjaTrader.Gui.Chart;
      using NinjaTrader.Gui.SuperDom;
      using NinjaTrader.Gui.Tools;
      using NinjaTrader.Data;
      using NinjaTrader.NinjaScript;
      using NinjaTrader.Core.FloatingPoint;
      using NinjaTrader.Gui.Tools;
      #endregion
      
      //This namespace holds Add ons in this folder and is required. Do not change it. 
      namespace NinjaTrader.NinjaScript.AddOns
      {
      	public class MonitorTrades : NinjaTrader.NinjaScript.AddOnBase
      	{
      		private static bool haveAdded;
      		private Account simaccount,fxcmaccount,ibaccount;
      		
      		protected override void OnStateChange()
      		{
      			if (State == State.SetDefaults)
      			{
      				Description									= @"Enter the description for your new custom Add on here.";
      				Name										= "MonitorTrades";
      			   lock (Account.All)
      		{
            
      				
      				simaccount = Account.All.FirstOrDefault(a => a.Name == "Sim101" );//"Sim101")
      
                      // Subscribe to account item updates
                      if (simaccount != null && !haveAdded)
      
                      {
                        //  account.AccountItemUpdate += OnAccountItemUpdate;
                         simaccount.ExecutionUpdate += Account_ExecutionUpdate;
      					haveAdded=true;
                      }
      				
      		}
      			 
      			}
      			else if (State == State.Configure)
      			{
      			}
      		}
        private void Account_ExecutionUpdate(object sender, ExecutionEventArgs e)
              {
      
      
                  Print(String.Format(DateTime.Now + "Execution triggered for Order {0}", e.ToString()));
      		 
      		
                   
              }
      		protected override void OnWindowCreated(Window window)
      		{
      			
      		}
      
      		protected override void OnWindowDestroyed(Window window)
      		{
      			 if (simaccount != null)
      		{
                  
      			simaccount.ExecutionUpdate -=Account_ExecutionUpdate;
      		}
      			
      		}
      
      	}
      }

      Comment


        #4
        When you first right an add-on and compile it, NinjaTrader must be restarted at least once before it takes effect. From then on you will be able to modify the add-on every compile.
        Jessica P.NinjaTrader Customer Service

        Comment


          #5
          ok, let me clarify my question better,
          For the addon, I have not enabled it on my Ninajtrader menu.
          step 0: I compile the addon
          step1 : The addon works when I execute a order as I can see the print statement on the output window

          step2: I restart ninjatrader
          step3: I execute a order but I don't see the print statements on the output window. is this normal as the addon is not loaded automatically when ninjatrader restarts?
          step4: I compile the addon code, as I can see the print statement on the output window as the compile process probably loaded the addon

          Comment


            #6
            Can you provide the part of the code where you load a NinjaScript object from your add-on to Print from during step 1? This will affect state processing.
            Jessica P.NinjaTrader Customer Service

            Comment


              #7
              here is the entire code printed and its not a lot. i basically get subscribed to the a/c exeuctions and use the print statement on the onExecutionupdate. thats all.
              #region Using declarations
              using System;
              using System.Collections.Generic;
              using System.ComponentModel;
              using System.ComponentModel.DataAnnotations;
              using System.Linq;
              using System.Text;
              using System.Threading.Tasks;
              using System.Windows;
              using System.Windows.Input;
              using System.Windows.Media;
              using System.Xml.Serialization;
              using NinjaTrader.Cbi;
              using NinjaTrader.Gui;
              using NinjaTrader.Gui.Chart;
              using NinjaTrader.Gui.SuperDom;
              using NinjaTrader.Gui.Tools;
              using NinjaTrader.Data;
              using NinjaTrader.NinjaScript;
              using NinjaTrader.Core.FloatingPoint;
              using NinjaTrader.Gui.Tools;
              #endregion

              //This namespace holds Add ons in this folder and is required. Do not change it.
              namespace NinjaTrader.NinjaScript.AddOns
              {
              public class MonitorTrades : NinjaTrader.NinjaScript.AddOnBase
              {
              private static bool haveAdded;
              private Account simaccount,fxcmaccount,ibaccount;

              protected override void OnStateChange()
              {
              if (State == State.SetDefaults)
              {
              Description = @"Enter the description for your new custom Add on here.";
              Name = "MonitorTrades";
              lock (Account.All)
              {


              simaccount = Account.All.FirstOrDefault(a => a.Name == "Sim101" );//"Sim101")

              // Subscribe to account item updates
              if (simaccount != null && !haveAdded)

              {
              // account.AccountItemUpdate += OnAccountItemUpdate;
              simaccount.ExecutionUpdate += Account_ExecutionUpdate;
              haveAdded=true;
              }

              }

              }
              else if (State == State.Configure)
              {
              }
              }
              private void Account_ExecutionUpdate(object sender, ExecutionEventArgs e)
              {


              Print(String.Format(DateTime.Now + "Execution triggered for Order {0}", e.ToString()));



              }
              protected override void OnWindowCreated(Window window)
              {

              }

              protected override void OnWindowDestroyed(Window window)
              {
              if (simaccount != null)
              {

              simaccount.ExecutionUpdate -=Account_ExecutionUpdate;
              }

              }

              }
              }

              Comment


                #8
                Thank you for this information, this makes it much more clear what is occuring. I notice you unsubscribe whenever any window in NinjaTrader is destroyed (for example, any time any chart is closed), while subscribing when your add-on is initialized during OnStateChange. You can take advantage of your current code to better understand how our state processing is occurring; every time you close an open window in your workspace, you will see one fewer line of output.

                I have refactored your code as follows :

                • This code is not unsubscribing every time we close a window any more. Closing e.g. a Time and Sales window which can not place trades should not affect your add-on. Instead, this unsubscribes when the add-on itself terminates
                • Your SetDefaults state now only includes information from your add-on itself to set its default values, rather than information pulled in from other parts of NinjaTrader such as trading accounts
                • Your Configure state now includes information pulled in from other parts of NinjaTrader, such as trading accounts

                This code will initialize your simaccount object exactly once, and unsubscribe exactly once. Note that this also has a built-in check to ensure State.Configure is only reached exactly once in your add-on's lifecycle. Please let us know if there are any other ways we can help.
                Attached Files
                Jessica P.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by priceisking, Today, 12:51 PM
                1 response
                5 views
                0 likes
                Last Post NinjaTrader_PaulH  
                Started by chartish, Today, 10:33 AM
                3 responses
                15 views
                0 likes
                Last Post chartish  
                Started by casabella, Today, 11:51 AM
                1 response
                6 views
                0 likes
                Last Post NinjaTrader_PaulH  
                Started by Johnny, Today, 11:42 AM
                1 response
                13 views
                0 likes
                Last Post NinjaTrader_Jermaine  
                Started by habibalex, Today, 09:03 AM
                2 responses
                13 views
                0 likes
                Last Post sledge
                by sledge
                 
                Working...
                X