Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Initial Trade for Strategy with "bars since exit" function

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

    Initial Trade for Strategy with "bars since exit" function

    I have created a strategy where the buy signal is required to wait X number of Renko bars since last exit before making a purchase. The problem is that there has been no initial trade. I think manually entering the first trade could work but I do not want to do this as there would not be a way to backtest in that case. What I would like is for an identical buy signal, minus the bars since last exit to be created but to only run once at the start of the strategy, but I have no idea how to do this, please help.

    #2
    Hello Chris001,

    Thank you for your post and welcome to the NinjaTrader Support Forum!

    The following is an example of how to use a bool to allow the initial trade:
    Code:
            #region Variables		
            private bool firstTrade = true;
            #endregion
    
            
            protected override void Initialize()
            {
    			
            }
    		
    		protected override void OnBarUpdate()
    		{	
    			// is it the first trade?
    			if(firstTrade)
    			{
    				EnterLong();
    				// ensure this only occurs once by setting
    				// the bool to false.
    				firstTrade = false;
    			}
    			
    			// basic exit condition, use you own exit of course.
    			if(Position.MarketPosition == MarketPosition.Long)
    				ExitLong();
    			
    			if(BarsSinceExit() > 2)
    			{
    				// place your new trades.
    			}
    		}

    Comment


      #3
      Hi Patrick,

      Thank you very much. Are the conditions I already have in the code what the first trade uses, or do I need to enter those conditions elsewhere? My exit is a trailing stop created in Strategy Wizard, and already appears under "protected override void Initialize()", I don't need to place that trailing stop as my exit elsewhere do I? I do not know what the false means in the trailing stop below

      SetTrailStop("", CalculationMode.Ticks, 4, false);

      Please let me know if this stitch in attempt seems correct.

      protected override void Initialize()
      {
      // copy and paste what is already under this part of my strategy
      }

      protected override void OnBarUpdate()
      {
      if(firstTrade)

      //Do I merge the if statement for my Long condition to this if statement?
      {
      EnterLong();
      firstTrade = false;

      }
      //trailing stop already under: protected override void Initialize()


      if(firstTrade)

      //Do I merge the if statement for my Short condition to this if statement?
      {
      EnterShort();
      firstTrade = false;
      }
      //trailing stop already under: protected override void Initialize()

      {
      // the rest of my strategy. just copy and paste long condition and
      // short condition here
      // already contains BarsSinceExit() > 3 for long and short conditions
      //should it still appear as an if statement before the conditions ?
      }
      }

      Thanks.

      Comment


        #4
        The indents disappeared but they are identical to the template you provided.

        Comment


          #5
          Hello Chris001,

          Thank you for your response.

          Your understanding is correct, if you would like I can provide the full implementation to you if you can provide me with the strategy file. If you wish to provide this it will be located in the following directory on your PC: (My) Documents\NinjaTrader 7\bin\Custom\Strategy --> the file will be a .cs file.

          Comment


            #6
            Thanks. I think I should be able to find the time to try this tomorrow or the day after, I will let you know if I run into difficulties.

            Comment


              #7
              I have attached the strategy as a text file. When I try to compile I get the error:

              The name 'firstTrade' does not exist in the current context

              The error code is CS0103. I have removed the conditions and descriptions in the code because I do not feel comfortable posting what I worked hard on for everyone to see, hopefully you can still work with this. I know the conditions worked before trying to add the firstTrade and adding:

              BarsSinceExit() >= 3

              at the end of condition set 3 and 4.

              Thanks
              Attached Files

              Comment


                #8
                Just updated the start of the code, I forgot to include: private bool firstTrade=true;. I still get the error message plus another one: Expected class, delegate, enum, interface, or struct. The error code is CS1518

                #region Using declarations
                using System;
                using System.ComponentModel;
                using System.Diagnostics;
                using System.Drawing;
                using System.Drawing.Drawing2D;
                using System.Xml.Serialization;
                using NinjaTrader.Cbi;
                using NinjaTrader.Data;
                using NinjaTrader.Indicator;
                using NinjaTrader.Gui.Chart;
                using NinjaTrader.Strategy;
                private bool firstTrade=true;
                #endregion

                Comment


                  #9
                  I think it is working fine now. private bool firstTrade=true; was under:
                  #region Using declarations
                  When i put it under:
                  #region Variables

                  it seems to work properly

                  Thanks

                  Comment


                    #10
                    Hi Chris, when you want it in a short way you can use one line:

                    if (BarsSinceExit() > zBars || BarsSinceExit() == -1)

                    Regards

                    chris

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by Tim-c, Today, 03:54 AM
                    0 responses
                    3 views
                    0 likes
                    Last Post Tim-c
                    by Tim-c
                     
                    Started by FrancisMorro, Today, 03:24 AM
                    0 responses
                    2 views
                    0 likes
                    Last Post FrancisMorro  
                    Started by Segwin, 05-07-2018, 02:15 PM
                    10 responses
                    1,771 views
                    0 likes
                    Last Post Leafcutter  
                    Started by Rapine Heihei, 04-23-2024, 07:51 PM
                    2 responses
                    31 views
                    0 likes
                    Last Post Max238
                    by Max238
                     
                    Started by Shansen, 08-30-2019, 10:18 PM
                    24 responses
                    945 views
                    0 likes
                    Last Post spwizard  
                    Working...
                    X