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

Signal name

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

    Signal name

    Code:
    Trade tr =  SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 1];
    Is it true that
    Code:
     tr.Entry.Name
    is the signal name?

    Is it true that
    Code:
    SystemPerformance.AllTrades.GetTrades("MyInstrument",tr.Entry.Name,1) == tr;
    Thanks.
    Last edited by AnotherTrader; 08-06-2018, 09:20 AM.

    #2
    Hello AnotherTrader,

    Thank you for your note.

    tr.Entry.Name would be the instrument name, for example NQ for the NQ 09-18 contract or ES for the ES 09-18 contract.

    Please let us know if you need further assistance.
    Alan P.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_AlanP View Post
      tr.Entry.Name would be the instrument name, for example NQ for the NQ 09-18 contract or ES for the ES 09-18 contract.
      Sorry, I don't think that is correct.

      If I run the following custom strategy...

      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.NinjaScript.Indicators;
      using NinjaTrader.NinjaScript.DrawingTools;
      #endregion
      
      //This namespace holds Strategies in this folder and is required. Do not change it. 
      namespace NinjaTrader.NinjaScript.Strategies
      {
      	public class MyCustomStrategy : Strategy
      	{
      		protected override void OnStateChange()
      		{
      			if (State == State.SetDefaults)
      			{
      				this.Name										= "MyCustomStrategy";
      				this.Calculate									= Calculate.OnBarClose;
      				this.IsUnmanaged								= true;
      				this.BarsRequiredToTrade						= 20;
      				this.ClearOutputWindow();
      			}
      		}
      
      		protected override void OnBarUpdate()
      		{
      			if (this.CurrentBar == 25)
      				this.SubmitOrderUnmanaged(0,
      											OrderAction.Buy,
      											OrderType.Market,
      											1,
      											0,
      											0,
      											string.Empty,
      											"Entry-Signal");
      			
      			if (this.CurrentBar == 30)
      				this.SubmitOrderUnmanaged(0,
      											OrderAction.Sell,
      											OrderType.Market,
      											1,
      											0,
      											0,
      											string.Empty,
      											"Entry-Signal");
      			
      			if (this.CurrentBar == 35)
      			{
      				Trade tr =  SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 1];
      				Print(string.Format("bar:\t{0}\ntr.Entry.Name:\t{1}\ntr:\t{2}", this.CurrentBar, tr.Entry.Name, tr.ToString()));
      			}
      		}
      	}
      }

      ... I get the following output:
      Attached Files

      Comment


        #4
        Hello AnotherTrader,

        You are correct, I had some prints mixed up.

        Did you have a question or just wish to confirm this was the entry order name?

        I look forward to your reply.
        Alan P.NinjaTrader Customer Service

        Comment


          #5
          There were two questions in my first post, the first of which is now answered. Thanks.

          The second was ...
          Originally posted by AnotherTrader View Post
          Is it true that
          Code:
          SystemPerformance.AllTrades.GetTrades("MyInstrument",tr.Entry.Name,1) == tr
          i.e. Would tr.Entry.Name be the correct parameter to use for entry signal in the GetTrades method to get back to the trade collection containing just tr?

          Thanks.
          Last edited by AnotherTrader; 08-06-2018, 11:18 AM.

          Comment


            #6
            Hello AnotherTrader,

            GetTrades would be the trade collection while tr would be a trade. You would want to add a index value to the trade collection to test if a certain trade object was equal.




            For example

            Code:
            if (SystemPerformance.AllTrades.Count >1)
            {
            Trade tr =  SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 1];
            if(SystemPerformance.AllTrades.GetTrades("NQ 09-18",tr.Entry.Name,1)[B][0][/B] == tr)
            	Print("Blah Blah Blah");
            else
            Print("no match)");
            }
            I have also attached a example which I suggest testing on a 1 second chart NQ 09-18 chart.

            Please let us know if you need further assistance.
            Attached Files
            Alan P.NinjaTrader Customer Service

            Comment


              #7
              How to use GetTrades()

              When I run 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.NinjaScript.Indicators;
              using NinjaTrader.NinjaScript.DrawingTools;
              #endregion
              
              //This namespace holds Strategies in this folder and is required. Do not change it. 
              namespace NinjaTrader.NinjaScript.Strategies
              {
              	public class MyCustomStrategy : Strategy
              	{	
              		protected override void OnStateChange()
              		{
              			if (State == State.SetDefaults)
              			{
              				this.Name										= "MyCustomStrategy";
              				this.Calculate									= Calculate.OnBarClose;
              				this.IsUnmanaged								= true;
              				this.BarsRequiredToTrade						= 20;
              				this.ClearOutputWindow();
              			}
              		}
              
              		protected override void OnBarUpdate()
              		{
              			if (this.CurrentBar == 25)
              				this.SubmitOrderUnmanaged(0,
              											OrderAction.Buy,
              											OrderType.Market,
              											1,
              											0,
              											0,
              											string.Empty,
              											"Entry_1");
              			
              			if (this.CurrentBar == 35)
              				this.SubmitOrderUnmanaged(0,
              											OrderAction.Sell,
              											OrderType.Market,
              											1,
              											0,
              											0,
              											string.Empty,
              											"Entry_1");
              			
              			if (this.CurrentBar == 45)
              			{
              				foreach(var t in SystemPerformance.AllTrades)
              					Print(SystemPerformance.AllTrades.GetTrades(this.Instrument.FullName, t.Entry.Name, 1)[0].ToString());
              			}
              		}
              	}
              }
              GetTrades() seems to work fine and I get the expected output.

              But if I modify the code to add a trade with the addition of the highlighted stuff below ...

              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.NinjaScript.Indicators;
              using NinjaTrader.NinjaScript.DrawingTools;
              #endregion
              
              //This namespace holds Strategies in this folder and is required. Do not change it. 
              namespace NinjaTrader.NinjaScript.Strategies
              {
              	public class MyCustomStrategy : Strategy
              	{	
              		protected override void OnStateChange()
              		{
              			if (State == State.SetDefaults)
              			{
              				this.Name										= "MyCustomStrategy";
              				this.Calculate									= Calculate.OnBarClose;
              				this.IsUnmanaged								= true;
              				this.BarsRequiredToTrade						= 20;
              				this.ClearOutputWindow();
              			}
              		}
              
              		protected override void OnBarUpdate()
              		{
              			if (this.CurrentBar == 25)
              				this.SubmitOrderUnmanaged(0,
              											OrderAction.Buy,
              											OrderType.Market,
              											1,
              											0,
              											0,
              											string.Empty,
              											"Entry_1");
              			
              			[B]if (this.CurrentBar == 30)
              				this.SubmitOrderUnmanaged(0,
              											OrderAction.Buy,
              											OrderType.Market,
              											1,
              											0,
              											0,
              											string.Empty,
              											"Entry_2");[/B]
              			
              			if (this.CurrentBar == 35)
              				this.SubmitOrderUnmanaged(0,
              											OrderAction.Sell,
              											OrderType.Market,
              											1,
              											0,
              											0,
              											string.Empty,
              											"Entry_1");
              			
              			[B]if (this.CurrentBar == 40)
              				this.SubmitOrderUnmanaged(0,
              											OrderAction.Sell,
              											OrderType.Market,
              											1,
              											0,
              											0,
              											string.Empty,
              											"Entry_2");[/B]
              			if (this.CurrentBar == 45)
              			{
              				foreach(var t in SystemPerformance.AllTrades)
              					Print(SystemPerformance.AllTrades.GetTrades(this.Instrument.FullName, t.Entry.Name, 1)[0].ToString());
              			}
              		}
              	}
              }
              ... I get the error
              Strategy 'MyCustomStrategy': Error on calling 'OnBarUpdate' method on bar 45: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.
              What am I doing wrong here?

              Comment


                #8
                Hello AnotherTrader,

                The added blocks in bold contain the same signal names, "Entry_1" and "Entry_2". If you change these so they are unique, does the issue still occur?

                For example,

                if (this.CurrentBar == 30)
                this.SubmitOrderUnmanaged(0,
                OrderAction.Buy,
                OrderType.Market,
                1,
                0,
                0,
                string.Empty,
                "Entry_2Unique";

                I look forward to your reply.
                Alan P.NinjaTrader Customer Service

                Comment


                  #9
                  Originally posted by NinjaTrader_AlanP View Post
                  Hello AnotherTrader,

                  The added blocks in bold contain the same signal names, "Entry_1" and "Entry_2". If you change these so they are unique, does the issue still occur?

                  For example,

                  if (this.CurrentBar == 30)
                  this.SubmitOrderUnmanaged(0,
                  OrderAction.Buy,
                  OrderType.Market,
                  1,
                  0,
                  0,
                  string.Empty,
                  "Entry_2Unique";

                  I look forward to your reply.
                  I get the same error with the following 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.NinjaScript.Indicators;
                  using NinjaTrader.NinjaScript.DrawingTools;
                  #endregion
                  
                  //This namespace holds Strategies in this folder and is required. Do not change it. 
                  namespace NinjaTrader.NinjaScript.Strategies
                  {
                  	public class MyCustomStrategy : Strategy
                  	{	
                  		protected override void OnStateChange()
                  		{
                  			if (State == State.SetDefaults)
                  			{
                  				this.Name										= "MyCustomStrategy";
                  				this.Calculate									= Calculate.OnBarClose;
                  				this.IsUnmanaged								= true;
                  				this.BarsRequiredToTrade						= 20;
                  				this.ClearOutputWindow();
                  			}
                  		}
                  
                  		protected override void OnBarUpdate()
                  		{
                  			if (this.CurrentBar == 25)
                  				this.SubmitOrderUnmanaged(0,
                  											OrderAction.Buy,
                  											OrderType.Market,
                  											1,
                  											0,
                  											0,
                  											string.Empty,
                  											"Entry_1");
                  			
                  			if (this.CurrentBar == 30)
                  				this.SubmitOrderUnmanaged(0,
                  											OrderAction.Buy,
                  											OrderType.Market,
                  											1,
                  											0,
                  											0,
                  											string.Empty,
                  											[B]"Entry_2Unique"[/B]);
                  			
                  			if (this.CurrentBar == 35)
                  				this.SubmitOrderUnmanaged(0,
                  											OrderAction.Sell,
                  											OrderType.Market,
                  											1,
                  											0,
                  											0,
                  											string.Empty,
                  											"Entry_1");
                  			
                  			if (this.CurrentBar == 40)
                  				this.SubmitOrderUnmanaged(0,
                  											OrderAction.Sell,
                  											OrderType.Market,
                  											1,
                  											0,
                  											0,
                  											string.Empty,
                  										        "Entry_2");
                  			if (this.CurrentBar == 45)
                  			{
                  				foreach(var t in SystemPerformance.AllTrades)
                  					Print(SystemPerformance.AllTrades.GetTrades(this.Instrument.FullName, t.Entry.Name, 1)[0].ToString());
                  			}
                  		}
                  	}
                  }
                  Last edited by AnotherTrader; 08-06-2018, 03:10 PM.

                  Comment


                    #10
                    Hello AnotherTrader,

                    For CurrentBar ==25 and CurrentBar ==35, both orders submitted have the same entry signal name.

                    If you make this entry unique like you did for entry2, do you still see the error?

                    I look forward to your reply.
                    Alan P.NinjaTrader Customer Service

                    Comment


                      #11
                      Originally posted by NinjaTrader_AlanP View Post
                      Hello AnotherTrader,

                      For CurrentBar ==25 and CurrentBar ==35, both orders submitted have the same entry signal name.

                      If you make this entry unique like you did for entry2, do you still see the error?

                      I look forward to your reply.
                      I changed the code as follows, but the error is the same.

                      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.NinjaScript.Indicators;
                      using NinjaTrader.NinjaScript.DrawingTools;
                      #endregion
                      
                      //This namespace holds Strategies in this folder and is required. Do not change it. 
                      namespace NinjaTrader.NinjaScript.Strategies
                      {
                      	public class MyCustomStrategy : Strategy
                      	{	
                      		protected override void OnStateChange()
                      		{
                      			if (State == State.SetDefaults)
                      			{
                      				this.Name										= "MyCustomStrategy";
                      				this.Calculate									= Calculate.OnBarClose;
                      				this.IsUnmanaged								= true;
                      				this.BarsRequiredToTrade						= 20;
                      				this.ClearOutputWindow();
                      			}
                      		}
                      
                      		protected override void OnBarUpdate()
                      		{
                      			if (this.CurrentBar == 25)
                      				this.SubmitOrderUnmanaged(0,
                      											OrderAction.Buy,
                      											OrderType.Market,
                      											1,
                      											0,
                      											0,
                      											string.Empty,
                      											[B]"Entry_1"[/B]);
                      			
                      			if (this.CurrentBar == 30)
                      				this.SubmitOrderUnmanaged(0,
                      											OrderAction.Buy,
                      											OrderType.Market,
                      											1,
                      											0,
                      											0,
                      											string.Empty,
                      											[B]"Entry_2"[/B]);
                      			
                      			if (this.CurrentBar == 35)
                      				this.SubmitOrderUnmanaged(0,
                      											OrderAction.Sell,
                      											OrderType.Market,
                      											1,
                      											0,
                      											0,
                      											string.Empty,
                      											[B]"Entry_1Unique"[/B]);
                      			
                      			if (this.CurrentBar == 40)
                      				this.SubmitOrderUnmanaged(0,
                      											OrderAction.Sell,
                      											OrderType.Market,
                      											1,
                      											0,
                      											0,
                      											string.Empty,
                      											[B]"Entry_2Unique"[/B]);
                      			if (this.CurrentBar == 45)
                      			{
                      				foreach(var t in SystemPerformance.AllTrades)
                      					Print(SystemPerformance.AllTrades.GetTrades(this.Instrument.FullName, t.Entry.Name, 1)[0].ToString());
                      			}
                      		}
                      	}
                      }

                      Comment


                        #12
                        Hello AnotherTrader,

                        If I change the print statement in the currentbar==45 to the following, I can start the strategy with no error,

                        Code:
                        if (this.CurrentBar == 45)
                        {
                        foreach(var t in SystemPerformance.AllTrades)
                        [B]Print(t.Entry.Name.ToString());[/B]
                        }
                        As to why the following is causing an error to be thrown, I would suggest adding prints to discover what is null which is causing the error,

                        Print(SystemPerformance.AllTrades.GetTrades(this.I nstrument.FullName, t.Entry.Name, 1)[0].ToString());

                        How to check for null references,


                        I’ve provided a link to a youtube video which covers an example of using prints to understand behavior:
                        Dive into manipulating C# code from within an unlocked NinjaScript strategy using the NinjaScript Editor.NinjaTrader 7 is an award winning end to end online ...


                        I’ve provided a link covering debugging which you may find helpful.
                        Debugging: http://ninjatrader.com/support/forum...ead.php?t=3418

                        I might suggest using the managed approach as the unmanaged approach is for very experienced programmers,


                        Please let us know if you need further assistance.
                        Alan P.NinjaTrader Customer Service

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by f.saeidi, Yesterday, 08:12 AM
                        3 responses
                        24 views
                        0 likes
                        Last Post NinjaTrader_Jesse  
                        Started by algospoke, Yesterday, 06:40 PM
                        1 response
                        14 views
                        0 likes
                        Last Post NinjaTrader_Jesse  
                        Started by quantismo, Yesterday, 05:13 PM
                        1 response
                        13 views
                        0 likes
                        Last Post NinjaTrader_Gaby  
                        Started by The_Sec, 04-16-2024, 02:29 PM
                        3 responses
                        16 views
                        0 likes
                        Last Post NinjaTrader_ChristopherS  
                        Started by hurleydood, 09-12-2019, 10:45 AM
                        15 responses
                        1,099 views
                        0 likes
                        Last Post Leeroy_Jenkins  
                        Working...
                        X