Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to REVERSE a position & order programatically in a custom trading indicator?

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

    How to REVERSE a position & order programatically in a custom trading indicator?

    I'm able to CLOSE a trade position/order with the following code, in a custom trading indicator (NOT STRATEGY);

    myAccount.Flatten(new [] { Instrument });

    However. I can't seem to find an equivalent coding to REVERSE a trade position/order. I shall appreciate some code snippets example to guide me.

    Regards.

    omololu

    #2
    Hi omololu, thanks for posting. You can submit orders to the Account object with the CreateOrder() and Submit() methods. To reverse a position, one would need to make a script that look for the exit order to fill in OnExecutionUpdate, then submits an order in the opposite direction using CreateOrder() and Submit().






    Kind regards,
    -ChrisL

    Comment


      #3
      Originally posted by NinjaTrader_ChrisL View Post
      Hi omololu, thanks for posting ... one would need to make a script that look for the exit order to fill in OnExecutionUpdate, then submits an order in the opposite direction using CreateOrder() and Submit().

      Kind regards,
      -ChrisL
      Hi Chris,

      Thanks for your very quick reply.

      Yes, I'm aware of the above links that you have provided, and have previously read through all of them ... they don't contain example code snippets that specifically address my issue. My custom indicator already has the "OnOrderUpdate", "OnExecutionUpdate" and "OnPositionUpdate" sections.

      My challenge is the code snippets or "script that look for the exit order to fill in OnExecutionUpdate".​ Could you please share some code snippets that do just this ?

      Regards.

      omololu

      Comment


        #4
        Hi omololu, I found an existing example that adds buttons to the chart through an indicator. The buttons submit orders using an Account object, so I made an addition to this that will reverse the existing position (See use case 4). It will 1. Detect the existing position, 2. Submit an order to close that position, 3. Wait for that order to fill in OnExeUpdate, and 4. submit an order in the opposite direction in OnExeUpdate.

        Kind regards,
        -ChrisL
        Attached Files

        Comment


          #5
          Originally posted by NinjaTrader_ChrisL View Post
          Hi omololu, I found an existing example that adds buttons to the chart through an indicator. The buttons submit orders using an Account object, so I made an addition to this that will reverse the existing position (See use case 4). It will 1. Detect the existing position, 2. Submit an order to close that position, 3. Wait for that order to fill in OnExeUpdate, and 4. submit an order in the opposite direction in OnExeUpdate.

          Kind regards,
          -ChrisL
          Thanks Chris,

          I tested the script "as-is" and noted that the script made provision for ONLY Long entries ... yes, it reverses Long entries. So, I used the ChartTrader panel to enter a Short entry and discovered that the script would NOT reverse a Short entry. I additionally want to be able to reverse entries attached to ATMs. Could you please include provision for Short entries and also ATMs ?

          Best Regards.

          omololu

          Comment


            #6
            Hi omololu, thanks for the follow up. Reversing a short on this example works for me, see the short clip:



            Unfortunately, the standard ATM orders will not be usable with this example since they do not get the order closing events from Chart Trader. One would need to add onto this example to submit protective orders in OnExecutionUpdate. There is a related strategy example doing this here:

            Comment


              #7
              Originally posted by NinjaTrader_ChrisL View Post
              There is a related strategy example doing this here:

              https://ninjatrader.com/support/help...and_onexec.htm
              Hi Chris,

              Thanks for your reply.

              The above example is for Strategy ... my custom script is an Indicator. So, are the codes in the example Strategy applicable to Indicator ?

              omololu

              Comment


                #8
                Hi omololu, Unfortunately I do not have an indicator that does this currently. You will need to use this example as boilerplate/a general example of what to do here. You will not be able to use Exit or Enter orders in the Indicator, so any time those orders are used in this example, use Account.CreateOrder/Account.Submit.

                Kind regards,
                -ChrisL

                Comment

                Latest Posts

                Collapse

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