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

Minimal example for BarsSinceEntry + MTF?

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

    Minimal example for BarsSinceEntry + MTF?

    Hello,

    I'm not having much luck getting BarsSinceEntry to work with MTF.

    Would it be possible for someone to provide a minimal example of something that would get you out of a long position after 5 bars (using BarsSinceEntry) in a strategy that had more than one time frame enabled?

    Thank you

    #2
    Hello tickaway,

    I have attached the requested script. Please let us know if there are any other ways we can help.
    Attached Files
    Jessica P.NinjaTrader Customer Service

    Comment


      #3
      Thanks, but still confused a bit here.

      What are the fields for ?... EnterLong(A, B, "C");

      EnterLong(1, 1, "entered on 150 tick");


      here is an example of my OnBarUpdate code that will not compile:

      Code:
      		
      		if (Position.MarketPosition == MarketPosition.Long){
      			if ((BarsInProgress == 0) && (BarsSinceEntry(1, "test", 0) >= 5) )
      				{
      				Print("BarsSinceEntry : " + BarsSinceEntry(1, "test", 0));
      				ExitLong(1, 1, "test");	
      				}
      		
      		}	
      			
      		         
      			//Create a lot of long entries.... if time is *:13 or *:28
      			if (Time[0].Minute == 13 || Time[0].Minute == 28)
      			{					
      					EnterLong (1, 1, "test");							
      			}

      Basically in this example above, just trying to get in Long every on every :14 and :28 of the hour, and get out after 5 bars (5 minutes - since the primary series at index[0] is 1min) have past. In my actual code I do some things with other timeframes, so this is why BarsSinceEntry required the extra fields I guess.
      Last edited by tickaway; 07-18-2016, 10:22 AM.

      Comment


        #4
        I am happy to clarify further.

        Originally posted by tickaway View Post
        What are the fields for ?... EnterLong(A, B, "C");

        EnterLong(1, 1, "entered on 150 tick");
        Here is an excerpt from the Help Guide documentation on EnterLong which explains what each of these are for. I will annotate

        Originally posted by http://ninjatrader.com/support/helpGuides/nt7/enterlong.htm
        The following method variation is for experienced programmers who fully understand Advanced Order Handling concepts.

        EnterLong(int barsInProgressIndex (e.g. A), int quantity (e.g. B), string signalName (e.g. C))


        barsInProgressIndex: The index of the Bars object the order is to be submitted against. See the BarsInProgress property. This determines what instrument the order is submitted for.

        quantity: Entry order quantity.

        signalName: User defined signal name identifying the order generated. Max 50 characters.
        Originally posted by tickaway View Post
        here is an example of my OnBarUpdate code that will not compile:

        Code:
        [FONT=Courier New]         // *snip*[/FONT]
        Here is a quote from the help guide page for ExitLong

        Originally posted by http://ninjatrader.com/support/helpGuides/nt7/exitlong.htm
        The following method variation is for experienced programmers who fully understand Advanced Order Handling concepts.

        ExitLong(int barsInProgressIndex, int quantity, string signalName, string fromEntrySignal)

        barsInProgressIndex: The index of the Bars object the order is to be submitted against. See the BarsInProgress property. This determines what instrument the order is submitted for.

        quantity: Entry order quantity.



        signalName: User defined signal name identifying the order generated. Max 50 characters.

        fromEntrySignal: The entry signal name. This ties the exit to the entry and exits the position quantity represented by the actual entry.
        Given that, then, I was able to get this code sample to compile :

        Code:
        [FONT=Courier New]        if (Position.MarketPosition == MarketPosition.Long){
                    if ( (BarsInProgress == 0) && (BarsSinceEntry(1, "test", 0) >= 5) )
                        {
                        Print("BarsSinceEntry : " + BarsSinceEntry(1, "test", 0));
                        ExitLong(1, 1, "test exit", "test");    
                        }
                
                }    
                    
                         
                    //Create a lot of long entries.... if time is *:13 or *:28
                    if (Time[0].Minute == 13 || Time[0].Minute == 28)
                    {                    
                            EnterLong (1, 1, "test");                            
                    }
                }[/FONT]
        Please let us know if there are any other ways we can help.
        Jessica P.NinjaTrader Customer Service

        Comment


          #5
          I'm actually doing everything on the index0. So My EnterLong looks like:

          Code:
          EnterLong (0, 1, "test");
          The rest I've made to look like this... but doesn't seem to work, and console always reports the Position Start/End in as "True" -- which doesn't seem right since it is supposed to be exiting if both of those instructions are ran.

          Code:
          	if (Position.MarketPosition == MarketPosition.Long){
                      	if ( (BarsInProgress == 0) && (BarsSinceEntry(1, "test", 0) >= 1) )
                          {
                          Print("BarsSinceEntry : " + BarsSinceEntry(1, "test", 0));
                          Print("Position Start : " + (Position.MarketPosition == MarketPosition.Long));
          				ExitLong(0, 1, "test exit", "test");    
          				Print("Position End : " + (Position.MarketPosition == MarketPosition.Long));
                          }
                  
                  	}

          Comment


            #6
            PS: I changed it to get out after 1 bar just for testing.

            Comment


              #7
              Hello tickaway,

              I believe you are encountering a situation where you are calling ExitLong and EnterLong simultaneously. I would like to ask, are there any errors being reported in the log section of the control center?

              I also highly recommend adding a print statement after checking to see if you are in a long position, but before checking to see if you are on the first data series with at least 1 bar since entry.
              Jessica P.NinjaTrader Customer Service

              Comment


                #8
                Originally posted by NinjaTrader_JessicaP View Post
                Hello tickaway,

                I believe you are encountering a situation where you are calling ExitLong and EnterLong simultaneously. I would like to ask, are there any errors being reported in the log section of the control center?

                I also highly recommend adding a print statement after checking to see if you are in a long position, but before checking to see if you are on the first data series with at least 1 bar since entry.
                I used your example and have got it to work a little better. Yes, going to add Print statements everywhere to try to test and see exactly what is going on. Thank you for the assistance. Order of operations gets a little confusing with MTF

                Comment


                  #9
                  @NinjaTrader_JessicaP :

                  For whatever reason, it's backwards. I am FLAT when I should be long, and visa versa.

                  Here's my whole test OnBarUpdate() block....

                  Code:
                  protected override void OnBarUpdate()
                          {		
                  			if (CurrentBar < 14)
                  			    {return;}
                  		
                  			{
                  			
                  				// START
                  				
                  				if (BarsInProgress == 0 && Position.MarketPosition == MarketPosition.Long){
                  				Print (Time[0] + " I have a position, it is: **" + Position.MarketPosition + "** In position for: " + BarsSinceEntry(1, "test",0) + " Bars.");
                  					if ( (BarsInProgress == 0) && (BarsSinceEntry(1, "test", 0) >= 5) )
                  					{
                  					Print (" ");
                  					Print ("--- START EXITING POSITION ---");
                  					Print(Time[0] + " ***** Enough bars have passed. BarsSinceEntry : " + BarsSinceEntry(1, "test", 0));
                  					Print (Time[0] + " About to exit. My current position is: " + Position.MarketPosition);
                  					ExitLong(1, 1, "test exit", "test");
                  					Print (Time[0] + " I should  now be FLAT. My current position is: " + Position.MarketPosition);
                  					Print ("--- END EXITING POSITION ------");
                  					Print (" ");
                  					}
                          
                          		}    
                              
                                   
                  				// Look for entry point at 13 minutes after the hour...
                  				if (Time[0].Minute == 13)
                  				{	
                  					// Go long if the last bar on the 1min TF was green...
                  					if (BarsInProgress == 0 && Close[1] > Open[1])	
                  					{                    
                  						Print (" ");
                  						Print ("--- START GOING LONG ---");
                  						Print (Time[0] + " Going LONG....");
                  						EnterLong (1, 1, "test");
                  						Print (Time[0] + " I should now be LONG, but I am: " + Position.MarketPosition);
                  						Print ("--- END GOING LONG-------");
                  						Print (" ");
                  					}
                          		}
                  				// END
                  			}	
                  
                  			
                  	
                  		}

                  Comment


                    #10
                    Nothing in error log about this either.... not sure why it is backwards.

                    Comment


                      #11
                      Hello tickaway,

                      Your Position property will not update until you are actually in a position. The method EnterLong() will not immediately put you in a position. This is an advanced topic, but I believe adding the following code to your strategy and studying the output will make it clear exactly what is happening.

                      Code:
                      [FONT=Courier New]protected override void OnOrderUpdate(IOrder order)
                      {
                          Print("At time " + Time[0] + ", this is the current order state : " + order);
                      }[/FONT]
                      Briefly and directly, however, the reason your output seems backward is because your code to ExitLong only occurs when you are in a Long position. ExitLong and EnterLong are what are known as "non-blocking method calls", which mean that they simply start a process and do not wait to see if it has completed. This is analagous to placing an order at a drive-thru at a fast food restaurant and then driving your car forward to the window immediately, rather than waiting for a confirmation over the loudspeaker before driving forward. The expected value of Position.MarketPosition, then, both before and after your call to ExitLong, is MarketPosition.Long, since you have only asked to begin the process of exiting; you haven't actually exited the position yet.

                      Please let us know if there are any other ways we may help.
                      Jessica P.NinjaTrader Customer Service

                      Comment


                        #12
                        I'll have to think about that a bit & test with some Print statements as you suggest to understand how it works. But thanks, that's helpful.

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by junkone, Today, 11:37 AM
                        1 response
                        7 views
                        0 likes
                        Last Post NinjaTrader_ChelseaB  
                        Started by frankthearm, Yesterday, 09:08 AM
                        11 responses
                        41 views
                        0 likes
                        Last Post frankthearm  
                        Started by quantismo, 04-17-2024, 05:13 PM
                        5 responses
                        35 views
                        0 likes
                        Last Post NinjaTrader_Gaby  
                        Started by proptrade13, Today, 11:06 AM
                        1 response
                        6 views
                        0 likes
                        Last Post NinjaTrader_Clayton  
                        Started by love2code2trade, 04-17-2024, 01:45 PM
                        4 responses
                        34 views
                        0 likes
                        Last Post love2code2trade  
                        Working...
                        X