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

exit open position in state of transition

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

    exit open position in state of transition

    What is the proper way to close or exit and open position in state of transition?

    #2
    Hello luxurious_04,

    Thank you for the post.

    Could you provide more detail on this question? Are you asking about closing historical positions before entering realtime to become flat? If you are currently trying something, are you seeing an error with what you are currently testing?



    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Yes I am trying to close historical positions before entering to realtime.

      Comment


        #4
        Hello luxurious_04,

        The Transition state its self would not be a good place to do this type of logic, but you could do this on the first realtime OnBarUpdate call. If your strategy has entered into a historical position, and you are using the default start behavior the script should be yellow and wait for your exit condition to become true before starting.

        Here is a simple test that would enter into a historical position, and then use the transition state to set a bool in which it can use to exit the position:

        Code:
        private bool pastTransition = false;
        protected override void OnStateChange()
        {
        			
        	else if (State == State.Transition)
        	{
        		pastTransition = true;
        	}
        }
        
        protected override void OnBarUpdate()
        {
        	if(State == State.Historical)
        		EnterLong();
        			
        	if(pastTransition == true)
        	{
        		if(Position.MarketPosition == MarketPosition.Long)ExitLong();
        		pastTransition = false;	
        	}
        	
        }
        This would occur once when the script transitions, on the first realtime bar it would check if there is a position and exit it. This is no different than your existing exit logic except that this specifically runs only when transitioning.

        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_Jesse View Post
          Hello luxurious_04,

          The Transition state its self would not be a good place to do this type of logic, but you could do this on the first realtime OnBarUpdate call. If your strategy has entered into a historical position, and you are using the default start behavior the script should be yellow and wait for your exit condition to become true before starting.

          Here is a simple test that would enter into a historical position, and then use the transition state to set a bool in which it can use to exit the position:

          Code:
          private bool pastTransition = false;
          protected override void OnStateChange()
          {
          			
          	else if (State == State.Transition)
          	{
          		pastTransition = true;
          	}
          }
          
          protected override void OnBarUpdate()
          {
          	if(State == State.Historical)
          		EnterLong();
          			
          	if(pastTransition == true)
          	{
          		if(Position.MarketPosition == MarketPosition.Long)ExitLong();
          		pastTransition = false;	
          	}
          	
          }
          This would occur once when the script transitions, on the first realtime bar it would check if there is a position and exit it. This is no different than your existing exit logic except that this specifically runs only when transitioning.

          I look forward to being of further assistance.

          Hi I’m trying to accomplish the same thing. I tried your code but didn’t work as expected. What start behavior are you suggesting? Basically I want to close historical positions when I turn on my strategy. I tried.

          Code:
          			if(State == State.Historical && CurrentBars[0] <=1 && hd==true)
          			{				Print("Sold");
          
          				ExitLong(Convert.ToInt32(Position.Quantity), "fresh", "Long1");
          				ExitShort(Convert.ToInt32(Position.Quantity), "fresh", "Short1");
          			}
          Hd is a manual bool so if I want to close out historical positions I can ... this isn’t working either. My expectation is that when I turn on the strategy, there are zero positions in the strategy. However, using your method and mine, there is a position in my strategy. I don’t want to wait for the current bar to close. I want to close it out during transition
          Last edited by staycool3_a; 08-31-2018, 05:00 AM.

          Comment


            #6
            Hello staycool3_a,

            Thank you for the post.

            The code I had provided previously was used with the default settings of wait until flat sync account. If you are having difficulties with something you are trying, I would suggest that you create a new thread for that new question and include a sample of the syntax you had used while testing and results you had seen.

            The sample you provided is also not like what I had provided, you have other conditions and two directions of exits without checking for positions.

            I would suggest trying a more simple test as it looks like you may have multiple series in your test as well "CurrentBars[0]". If you are still having difficulties, please create a sample of the syntax you had used and create a new thread for the questions surrounding that syntax.


            I look forward to being of further assistance.
            JesseNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by CortexZenUSA, Today, 12:53 AM
            0 responses
            1 view
            0 likes
            Last Post CortexZenUSA  
            Started by CortexZenUSA, Today, 12:46 AM
            0 responses
            1 view
            0 likes
            Last Post CortexZenUSA  
            Started by usazencortex, Today, 12:43 AM
            0 responses
            5 views
            0 likes
            Last Post usazencortex  
            Started by sidlercom80, 10-28-2023, 08:49 AM
            168 responses
            2,265 views
            0 likes
            Last Post sidlercom80  
            Started by Barry Milan, Yesterday, 10:35 PM
            3 responses
            11 views
            0 likes
            Last Post NinjaTrader_Manfred  
            Working...
            X