Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Reversing Position in the same OnBarUpdate()

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

    Reversing Position in the same OnBarUpdate()

    My strategy requires the ability to immediately reverse its position within the same OnBarUpdate.

    Per the Managed Orders documentation I should just be able to call the reversing order method and it should generate two orders, the first to close the existing position
    and the second to open the position in the opposite direction. Like this:

    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;

    if (CurrentBars[0] < 1)
    return;

    if(CurrentBar < BarsRequiredToTrade)
    return;


    // Set 1
    if(//logic for going up here) //going up
    {
    if(Position.MarketPosition == MarketPosition.Flat)
    {
    //this works perfect the first pass through
    SetStopLoss(@"LongEntry", CalculationMode.Ticks, 12, false);
    SetProfitTarget(@"LongEntry", CalculationMode.Ticks, 6);
    EnterLongLimit(Convert.ToInt32(DefaultQuantity), GetCurrentAsk(0), @"LongEntry");
    return;
    }
    else if(Position.MarketPosition == MarketPosition.Short)
    {
    //I know for a fact that I have been short and this fails to reverse my position
    SetStopLoss(@"LongEntry", CalculationMode.Ticks, 12, false);
    SetProfitTarget(@"LongEntry", CalculationMode.Ticks, 6);
    EnterLongLimit(Convert.ToInt32(DefaultQuantity), GetCurrentAsk(0), @"LongEntry");
    return;
    }
    }

    // Set 2
    if(//logic for going down here) //going down
    {

    if(Position.MarketPosition == MarketPosition.Flat)
    {
    //this works perfect the first pass through
    SetStopLoss(@"ShortEntry", CalculationMode.Ticks, 8, false);
    SetProfitTarget(@"ShortEntry", CalculationMode.Ticks, 20);
    EnterShortLimit(Convert.ToInt32(DefaultQuantity), GetCurrentBid(0), @"ShortEntry");
    return;
    }
    else if (Position.MarketPosition == MarketPosition.Long);
    {
    //I know for a fact that I have been long and this fails to reverse my position
    SetStopLoss(@"ShortEntry", CalculationMode.Ticks, 8, false);
    SetProfitTarget(@"ShortEntry", CalculationMode.Ticks, 20);
    EnterShortLimit(Convert.ToInt32(DefaultQuantity), GetCurrentBid(0), @"ShortEntry");
    return;
    }
    }

    }

    What am I doing wrong?​
    Last edited by seanhughes16; 01-26-2023, 04:38 AM.

    #2
    You can't be LONG and SHORT at the same time.
    That strategy sample tries to open a Short position while you are Long, whithout closing the Long position before that.
    If you entered such a position you would end up being LONG and SHORT at the same time.
    You should first close or exit the existing Long or Short position, and then open the new reverse position (Short or Long).
    Last edited by KonstantinosNT; 01-26-2023, 06:04 AM.

    Comment


      #3
      Thank you for that reply.

      The logic will only be EVER true on that bar update for one of the two sets, either the long signal or the short signal.

      The issue at hand is that if the strategy is already in a long position from 2 bars earlier and suddenly the short signal appears, I need to immediately
      reverse my position IN THE SAME bar. When I replace the logic above with ExitLong or ExitShort it will close the position for sure but then waiting for the next signal on some other bar is not what the math says needs to happen. The switch needs to happen right then and there.

      Comment


        #4
        If you have calculate=on.each.tick , then the switch will happen on the next tick, a fraction of a second later

        Comment


          #5
          I think the wait is because you are trying to enter limit order, and the order system has to wait for the price to touch the limit price.. Try enter market order, EnterLong, EnterShort.

          Comment


            #6
            Hello seanhughes16,

            Be sure to enable TraceOrders and provide output from TraceOrders and prints so we can assist with analyzing the behavior.


            If there is a working limit or stop order (meaning the first entry has not yet filled and the position is still flat) any orders working orders placed in the opposite direction will be ignored.


            Further, as you are doing intra-bar stuff, for this to work historically both TickReplay must be enabled and 1-tick intra-bar granularity must be implemented for accurate order fills.
            Chelsea B.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by NullPointStrategies, 03-13-2026, 05:17 AM
            0 responses
            86 views
            0 likes
            Last Post NullPointStrategies  
            Started by argusthome, 03-08-2026, 10:06 AM
            0 responses
            151 views
            0 likes
            Last Post argusthome  
            Started by NabilKhattabi, 03-06-2026, 11:18 AM
            0 responses
            79 views
            0 likes
            Last Post NabilKhattabi  
            Started by Deep42, 03-06-2026, 12:28 AM
            0 responses
            53 views
            0 likes
            Last Post Deep42
            by Deep42
             
            Started by TheRealMorford, 03-05-2026, 06:15 PM
            0 responses
            61 views
            0 likes
            Last Post TheRealMorford  
            Working...
            X