Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Managing Entries and Exits separately

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

    Managing Entries and Exits separately

    I have a strategy that has 2 situations as described below.

    Situation 1: a stop and reverse is appropriate
    Situation 1: an exit occurs on one bar and an occurs a few bars later.

    I want to manage ALL entries and exits separately so I have some flexibility.

    My strategy works fine on Situation 2 where an exit occurs on one bar and an entry occurs a few bars later.

    Unfortunately, on situation 1 where a stop a reverse occurs, I don't get an exit of the previous position and instead end up with 2 contracts going in the reverse direction. As an example: Let's say I am long 1 contract when a stop and reverse situation occurs. I end up short 2 contracts instead of an exit of 1 from the long position and an entry of 1 contract for the short position.

    This is the code... Notice that my exit conditions are exactly opposite to the entry conditions. This code is a simplified version of my "real" strategy but coded this to make sure of what I was doing. It behaves the exact same way my real strategy does. CalculateOnBarClose is set to true.

    Based on my research of the documentation I expected the following to occur.
    1. Open of bar #2 skips the ExitShort and ExitLong because there is no open position.
    2. Evaluate open of bar #2 for an entry and enter either long or short. Let's say it entered a long with 1 contract.
    3. Bar #3 is evaluated for an exit and let's say an exit condition is met so ExitLong removes 1 contract causing the long position to exit.
    4. Continuing at open of Bar #3 to evaluate conditions for an entry. Conditions are met and so a short position is entered with 1 contract.

    Following is what actually happens or so it seems.
    1. First bar skips the ExitShort and ExitLong because there is no open position.
    2. Evaluate at the open of bar #2 for an entry and enter either long or short. Let's say it entered a long with 1 contract.
    3. This is where the difference occurs...Bar #3 is evaluated for an exit and let's say an exit condition is met so the exit block is fired but no exit occurs or at least it isn't apparent that an exit occurs.
    4. Continuing on Bar #3 to evaluate conditions for an entry. Conditions are met and so a short position is entered with 2 contracts.

    Seems that I must be misunderstanding what ExitLong and ExitShort are doing.

    protected override void OnBarUpdate()
    {
    //***** Evaluate for an exit ******* //
    if (CrossAbove(SMA(Fast), SMA(Slow), 1))
    ExitShort();
    else if (CrossBelow(SMA(Fast), SMA(Slow), 1))
    ExitLong();

    //***** Evaluate for an entry ******* //
    if (CrossAbove(SMA(Fast), SMA(Slow), 1))
    EnterLong();
    else if (CrossBelow(SMA(Fast), SMA(Slow), 1))
    EnterShort();
    }

    Please beat me over the head and shoulders if need be to make me understand.

    I'm not providing screen shots until you tell me you need them because I'm thinking you will be able to tell me what I'm doing wrong just by looking at this.

    Thanks,
    Mike Winfrey

    #2
    Hi Mike Winfrey,

    Thanks for your post.

    You experience synchronization problems because I believe your 'On starting a real-time strategy' setting is set to 'Immediately submit live working historical orders'. You can find it under Tools > Options > Strategies > NinjaScript.

    Please set this to 'Wait until Flat before executing live'.

    You can also add TraceOrders = True to your Initialize() and open up the Output window from the Control Center (under Tools) to see which orders are being placed by your strategy.

    Also, please review this link - http://www.ninjatrader-support.com/H...tPosition.html

    Comment


      #3
      Thanks Bertrand...unfortunately that setting was already set to that. See my attachment.

      I reviewed the document you referred me to and that doesn't seem to be the situation.

      Just curious though. Are my assumptions correct about what should happen...that the exitlong will exit any long position and then continuing in the code the entershort will enter a new short position?
      Attached Files

      Comment


        #4
        Hi Mike Winfrey,

        the real problem with simple crossover strategies is - they are never really flat.

        ExitLong() and ExitShort() will exit, if there is a position to exit from, otherwise they are ignored.

        To manage your Entry and Exit orders separately you could attach a unique signal name to both entry and exit ordes.

        For your code, it should not matter, the net result will be the same. You can compare easily by coding two strategies, one pure reversal and the other as you posted and compare the two signalwise then.

        Comment


          #5
          That's what i thought Bertrand which is why I figured that having a separate ExitLong prior to the EnterShort would be the ticket. I inserted TraceOrders = true; and ran it for a total of 6 reversals...here's what I got. I am including a file with the trace. I ended up with 6 contracts on without any removal of any contracts and I think you will see that in the trace. At least that's the way I interpret it.
          Attached Files

          Comment


            #6
            looks like i may need to use the OnOrderUpdate() method to make sure I get the results I want. I'll explore that. Please provide any comments you think might be useful.

            Comment


              #7
              You need to use IOrders if you are trying to manage everything. You cannot submit EnterLong right after you do ExitShort. You need to wait for confirmation that you have indeed successfully closed out your short before you can enter long. This needs to be done in either OnOrderUpdate or OnExecution.
              Josh P.NinjaTrader Customer Service

              Comment


                #8
                i understand...I'm researching all that now and at least that part seems to be very clear. I think that may be the only way I can reliably confirm order placement and then take action based on that instead of the onbarupdate method. Am I correct in this belief?

                Comment


                  #9
                  That is correct.
                  Josh P.NinjaTrader Customer Service

                  Comment


                    #10
                    Outstanding...thank you...you guys may make a smart fella out of me after all. I keep hoping you guys won't give up on me.

                    Comment


                      #11
                      Not a problem. If you have any other questions feel free to post them on the forums.
                      Josh P.NinjaTrader Customer Service

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                      0 responses
                      576 views
                      0 likes
                      Last Post Geovanny Suaza  
                      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                      0 responses
                      334 views
                      1 like
                      Last Post Geovanny Suaza  
                      Started by Mindset, 02-09-2026, 11:44 AM
                      0 responses
                      101 views
                      0 likes
                      Last Post Mindset
                      by Mindset
                       
                      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                      0 responses
                      553 views
                      1 like
                      Last Post Geovanny Suaza  
                      Started by RFrosty, 01-28-2026, 06:49 PM
                      0 responses
                      551 views
                      1 like
                      Last Post RFrosty
                      by RFrosty
                       
                      Working...
                      X