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

Help with position handling

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

    #16
    Originally posted by sburtt View Post
    Koganam
    i might have find a way to avoid this problem whist keeping CalculateOnBarClose = true;
    please correct me if wrong.

    Running your script what happens is that the limit order is never issued or executed, script below

    Code:
    CalculateOnBarClose = true;
    private void GoShort(string strEntryName)
           {
        ExitLong();
        EnterShortLimit(DefaultQuantity, Close[0], strEntryName);
            }
    running my script, I have a similar, but different issue. I end up doubling my position if executed, because my limit order would work as stop-and-reverse, but this ignores the fact that I would be already flat due to the market order beeing executed too, script below
    Code:
    CalculateOnBarClose = true;
     
      protected override void OnBarUpdate()
    //Enter short
                if (CrossBelow())
                {
                    EnterShortLimit(Close[0] + 0.35* (High[0] - Low[0]));
                }
     
    //Exit long
                if (Position.MarketPosition == MarketPosition.Long
                    && CrossBelow())
                {
                    ExitLong();
                }
    * curious enough if I invert the sequence of the script (//Exit long on top and //Enter long below) my limit order is never issued or executed, as it happens in your script.

    what i think could be a quite good solution is using multi time frame. I am running this strategy on a 240min chart. If I exit my position at market on the primary bar series (240min) and have the limit order running on the secondary bar series (1min) for the entire 240 min bar, I will reduce the risk of doubling up my position to only 1 min (the first minute) rather than 240min, this could be a good trade-off. Below is the script I intend to test:
    Code:
     Add(PeriodType.Minute,1);
    CalculateOnBarClose = true;
     
      protected override void OnBarUpdate()
    if(BarsInProgress == 0)
    {
    //Enter short
                if (CrossBelow())
                {
                    EnterShortLimit([B]1[/B],Close[0] + 0.35* (High[0] - Low[0]));
                }
     
    //Exit long
                if (Position.MarketPosition == MarketPosition.Long
                    && CrossBelow())
                {
                    ExitLong();
                }
    }
    I've back tested this and results are exactly the same as in my original strategy. In back testing it looks like this could be the way to go. Please let me know if you understand what I am trying to say here. Could you tell me if you think this makes sense and could work?
    You show both orders being filtered by the same BarsInProgress. It is hard to see how that is any different from having no bars filter.

    Comment


      #17
      Originally posted by koganam View Post
      You show both orders being filtered by the same BarsInProgress. It is hard to see how that is any different from having no bars filter.
      Your right, thats script is totally incorrect.

      I've been working on this all morning, and anyhow this thing looks impossible to do in the managed approach keeping CalculateOnBarClose = true;

      I see 2 solutions:
      1. moving to the advance order handling (IOrder), but unfortunately my skills are still poor, and I don't find a proper tutorial that explain how this works;
      2. As you've mentioned in your past thread, use CalculateOnBarClose = false; but reading your threads bring me to 2 questions on the code below:

      Code:
      private bool _boolReverseOnOppositeSignal	= false;
      
       CalculateOnBarClose = true.
      	private void GoShort(string strEntryName)
      		{
      		        if (this._boolReverseOnOppositeSignal) ExitLong();
      			EnterShortLimit(DefaultQuantity, Close[0], strEntryName);
                        }
      1. don't you think that the use of CalculateOnBarClose = false is risky, as it could generate more trades than in the case CalculateOnBarClose = true?

      2. what exactly do I need boolean flag for? wouldn't i get the same by using the code without, like below:
      Code:
      Code:
      CalculateOnBarClose = false;
      	private void GoShort(string strEntryName)
      		{
      		        ExitLong();
      			EnterShortLimit(DefaultQuantity, Close[0], strEntryName);
                        }

      Comment


        #18
        Originally posted by koganam
        You are reading more into my query than there is. The boolean flag that I was using in that code had a completely different purpose. My query was on why I could not exit by market and enter by limit on the same bar if COBC was true. With or without the boolean flag, that code was not viable.

        IOW, you are assuming that I was attempting to solve the problem when I made the query of NT support. I was not: I wanted to know why the construct was not working. I was looking to isolate the problem when I made the query: not attempting to solve a problem that I had not at that time properly identified.

        I simply gave you the thread as a reference so that you can see that the issue has been previously discussed, so that you would not spend time going over what was already known. The code in that thread is not a solution to the problem: it was merely a demonstartion of how the problem arose.
        I got your point.
        My understanding so far is that you can only exit by market and enter by limit on the same bar if COBC is set to false, the code below should work fine:

        Code:
                
        protected override void OnBarUpdate()
          {
        	//Enter long
        	if (CrossAbove())
        		{
                                ExitShort();
        			EnterLongLimit(Close[0] - RngFrac * (High[0] - Low[0]));
        		}
        please correct me if wrong

        Comment


          #19
          Originally posted by sburtt View Post
          Your right, thats script is totally incorrect.

          I've been working on this all morning, and anyhow this thing looks impossible to do in the managed approach keeping CalculateOnBarClose = true;

          I see 2 solutions:
          1. moving to the advance order handling (IOrder), but unfortunately my skills are still poor, and I don't find a proper tutorial that explain how this works;
          2. As you've mentioned in your past thread, use CalculateOnBarClose = false; but reading your threads bring me to 2 questions on the code below:

          Code:
          private bool _boolReverseOnOppositeSignal    = false;
           
           CalculateOnBarClose = true.
              private void GoShort(string strEntryName)
                  {
                          if (this._boolReverseOnOppositeSignal) ExitLong();
                      EnterShortLimit(DefaultQuantity, Close[0], strEntryName);
                            }
          1. don't you think that the use of CalculateOnBarClose = false is risky, as it could generate more trades than in the case CalculateOnBarClose = true?

          2. what exactly do I need boolean flag for? wouldn't i get the same by using the code without, like below:
          Code:
          Code:
          CalculateOnBarClose = false;
              private void GoShort(string strEntryName)
                  {
                          ExitLong();
                      EnterShortLimit(DefaultQuantity, Close[0], strEntryName);
                            }
          1. Yes, it is, and one must take proper steps to programatically handle it. One runs the danger of being hopelessly whipsawed if one is not careful, or of being put into a trade mid-bar that turns out not to be a correct signal by the time that the bar closes.

          That having been said, in reality, in Stop&Reverse code, Mar****rders really are not as bad as they at first blush might look: in the aggregate, what you lose on one side, you gain on the other, and vice-versa. Then again, I do understand that you may prefer the solution that you are now exploring, separating the exit from the reversal entry, which is much more precise, and does not depend on statistically nulling out slippage.

          2. You are reading more into my query than there is. The boolean flag that I was using in that code had a completely different purpose. My query was on why I could not exit by market and enter by limit on the same bar if COBC was true. With or without the boolean flag, that code was not viable.

          IOW, you are assuming that I was attempting to solve the problem when I made the query of NT support. I was not: I wanted to know why the construct was not working. I was looking to isolate the problem when I made the query: not attempting to solve a problem that I had not at that time properly identified.

          I simply gave you the thread as a reference so that you can see that the issue has been previously discussed, so that you would not spend time going over what was already known. The code in that thread is not a solution to the problem: it was merely a demonstration of how the problem arose.

          Comment


            #20
            Originally posted by sburtt View Post
            I got your point.
            My understanding so far is that you can only exit by market and enter by limit on the same bar if COBC is set to false, the code below should work fine:

            Code:
             
            protected override void OnBarUpdate()
              {
                //Enter long
                if (CrossAbove())
                    {
                                    ExitShort();
                        EnterLongLimit(Close[0] - RngFrac * (High[0] - Low[0]));
                    }
            please correct me if wrong
            Please read my amended response, and take care of the possibilities/dangers that I outlined, regarding whipsaws and invalid entries by close-of-bar.

            Comment


              #21
              Originally posted by koganam View Post
              Please read my amended response, and take care of the possibilities/dangers that I outlined, regarding whipsaws and invalid entries by close-of-bar.
              Thanks for your help so far. I really appreciated it.
              De facto the indicator I use in my strategy takes as a reference the median point of each bar, rather than the close, this helps reduce noise and very rarely gives false signals, meaning i will try to go live with COBC = false and see how much this differs from my expectations.

              again thanks

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Jonker, Today, 01:19 PM
              0 responses
              1 view
              0 likes
              Last Post Jonker
              by Jonker
               
              Started by futtrader, Today, 01:16 PM
              0 responses
              5 views
              0 likes
              Last Post futtrader  
              Started by Segwin, 05-07-2018, 02:15 PM
              14 responses
              1,791 views
              0 likes
              Last Post aligator  
              Started by Jimmyk, 01-26-2018, 05:19 AM
              6 responses
              838 views
              0 likes
              Last Post emuns
              by emuns
               
              Started by jxs_xrj, 01-12-2020, 09:49 AM
              6 responses
              3,294 views
              1 like
              Last Post jgualdronc  
              Working...
              X