Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Possible to run synchronous onbarupdate()?

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

    Possible to run synchronous onbarupdate()?

    I have an exit, and entry of position happening when a certain condition is met using onbarupdate().

    else if (Close[0] < emaLow)
    {

    if (Position.MarketPosition == MarketPosition.Long)
    {
    ExitLong("Below EMA", "");

    }


    EnterShortPositionsIfAllowed();

    }

    EnterShortPositionsIfAllowed() checks that marketposition.flat is true. ​But because the exit and entry code is basically executed at the same time, marketposition always returns not flat. If i don't check for marketposition.flat, it will double my entry in the other direction due to exiting the position, and at the same time reversing the position in the EnterShortPositionsIfAllowed method.

    I know that i could get it to work by simply allowing it to reverse my position and not callign ExitLong, but for analysis of my trades i really want to rename that exit. Is there anyway to run the statements in sequence, waiting for one to complete before executing the other?
    Last edited by sofortune; 08-30-2023, 01:48 AM.

    #2
    Hello sofortune,

    Thank you for your post.

    Because you are using the Managed Approach, Entry() methods in the opposite direction reverse the position automatically. As you said, if you use an exit as well as an entry in the opposite direction you could end up with an unexpected quantity/double your entry in the other direction. The only way to prevent the EnterShort() from submitting a Close order and an entry would be to only submit it if the Position.MarketPosition is flat.

    To circumvent this limitation of the Managed Approach, you could consider using the Unmanaged Approach if you require to have your own exit name and submit the reversal entry without having a close position order submitted as well. The Unmanaged Approach offers additional flexibility at the cost of sacrificing some of the convenience and the order handling rules in place with the Managed Approach. For more information about both approaches, please see the following help guide sections:Please let us know if we may be of further assistance.

    Comment


      #3
      Yes I tried that but then it doesn't take an entry since it essentially executes the functions at the same time, so it doesn't recognize that it's flat yet.


      ​​​​​​So there's no way to use something like await to wait until the exit method is complete first?

      Comment


        #4
        Hello sofortune,

        Thank you for your reply.

        Using the Managed approach, an entry() method will check if there is an open position when it is submitted. If you call the entry in the same pass of OnBarUpdate() as the exit, OnPositionUpdate() will not have run yet and NinjaTrader will not know that the position was closed. You could consider using a bool so that the platform waits for the next pass of OnBarUpdate(), or even consider using OnExecutionUpdate() since the position object is driven by Executions, to toggle the bool once the exit is filled to submit the entry. This will mean OnPositionUpdate() runs before the next pass of OnBarUpdate() and the position will be seen as flat by the time the entry is submitted.

        You could test out something like the following to see if it meets your needs:
        Code:
        if (reverseEntry == true)
        {
        EnterShort();
        // reset the bool back to false
        reverseEntry = false;
        }
        
        if (Close[0] < emaLow && Position.MarketPosition == MarketPosition.Long)
        {
        ExitLong("Below EMA", "");
        // this will set the bool to true, so the first condition above will be met during the next pass of OnBarUpdate()
        reverseEntry = true;
        }
        Or if you'd like to work with OnExcutionUpdate() I suggest reviewing the following reference sample that demonstrates working with Order objects and checking when they are executed in OnExecutionUpdate():


        Please let us know if we may be of further assistance.

        Comment


          #5
          Thanks Emily. This would take the entry on the next bar then right?

          Yeah the reason i use managed orders is because i need to be able to change the limits during the trade.

          Comment


            #6
            Originally posted by sofortune View Post
            Thanks Emily. This would take the entry on the next bar then right?

            Yeah the reason i use managed orders is because i need to be able to change the limits during the trade.
            Ultimately, you could test it out to see when the next entry occurs. It would be submitted on the next pass of OnBarUpdate(), which would be the next bar if you are using Calculate.OnBarClose.

            Limits may be changed when using any approach. The ChangeOrder() method applies to both Managed and Unmanaged strategies:


            Please let us know if we may be of further assistance.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by NullPointStrategies, Today, 05:17 AM
            0 responses
            52 views
            0 likes
            Last Post NullPointStrategies  
            Started by argusthome, 03-08-2026, 10:06 AM
            0 responses
            130 views
            0 likes
            Last Post argusthome  
            Started by NabilKhattabi, 03-06-2026, 11:18 AM
            0 responses
            70 views
            0 likes
            Last Post NabilKhattabi  
            Started by Deep42, 03-06-2026, 12:28 AM
            0 responses
            43 views
            0 likes
            Last Post Deep42
            by Deep42
             
            Started by TheRealMorford, 03-05-2026, 06:15 PM
            0 responses
            48 views
            0 likes
            Last Post TheRealMorford  
            Working...
            X