• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

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 manugarc, Today, 01:15 PM
            0 responses
            7 views
            0 likes
            Last Post manugarc  
            Started by JMont1, Today, 11:42 AM
            0 responses
            5 views
            0 likes
            Last Post JMont1
            by JMont1
             
            Started by Steve L, Yesterday, 10:04 PM
            0 responses
            4 views
            0 likes
            Last Post Steve L
            by Steve L
             
            Started by marianfed, Yesterday, 09:47 PM
            0 responses
            5 views
            0 likes
            Last Post marianfed  
            Started by hir04068, Yesterday, 09:29 PM
            0 responses
            8 views
            0 likes
            Last Post hir04068  
            Working...
            X