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

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(), NinjaTrader.Core.Globals.InstallDir+@"\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:
    [FONT=Courier New]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;
          }
    
        }
      }
    }[/FONT]
    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 stafe, 04-15-2024, 08:34 PM
                7 responses
                31 views
                0 likes
                Last Post NinjaTrader_ChelseaB  
                Started by adeelshahzad, Today, 03:54 AM
                4 responses
                30 views
                0 likes
                Last Post adeelshahzad  
                Started by merzo, 06-25-2023, 02:19 AM
                10 responses
                823 views
                1 like
                Last Post NinjaTrader_ChristopherJ  
                Started by frankthearm, Today, 09:08 AM
                5 responses
                17 views
                0 likes
                Last Post NinjaTrader_Clayton  
                Started by jeronymite, 04-12-2024, 04:26 PM
                3 responses
                43 views
                0 likes
                Last Post jeronymite  
                Working...
                X