Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Code to exit opposite position first (if any)/cancel orders (if any) before Lng/Sht.

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

    Code to exit opposite position first (if any)/cancel orders (if any) before Lng/Sht.

    Hi:

    Sample:

    protected override void OnBarUpdate()
    {
    if (Position.MarketPosition == MarketPosition.Flat)

    if (CrossAbove(SMA(Fast), SMA(Slow), 1))
    m_EnterLong=true;

    if (CrossBelow(SMA(Fast), SMA(Slow), 1))
    m_EnterShort=true;

    How can I do this? Assuming I am already in the market in the opposite direction currently:

    1. IF I AM CURRENTLY SHORT-If conditions are meet for a Long, before entering the Long, how can I make it exit all current shorts FIRST and then CANCEL any associated orders FIRST BEFORE executing the m_EnterLong=true; in this above code?

    Can you please put a sample in so I can test? Vica Versa for IF I AM CURRENTLY LONG.

    I have the check market position is Flat above the beginning of the conditions to be sure it always is checking that there is a flat condition after various things happen. Is there a better place to put it?

    Thanks a lot!



    Greg

    #2
    Hello birdog,

    Thanks for your note.

    You are wanting to change from a long position to a short position when this condition is true, is this correct?

    If so, NinjaTrader will automatically handle exiting your current position, cancelling any associated stop losses and profit targets and then enter your new position if you are to call an enter order while already in a position.

    Is this automatic handling not working correctly for you?

    Also, I have noticed in your code that the check for the position being flat does not have brackets around the following code. When this is the case, only the first line of code will be affected by the preceeding condition. This means that m_Enterlong=true; can only execute when flat, however, m_EnterShort=true; will execute anytime its crossover condition is true whether or not you are flat.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_ChelseaB View Post
      Hello birdog,

      Thanks for your note.

      You are wanting to change from a long position to a short position when this condition is true, is this correct?

      If so, NinjaTrader will automatically handle exiting your current position, cancelling any associated stop losses and profit targets and then enter your new position if you are to call an enter order while already in a position.

      Is this automatic handling not working correctly for you?

      Also, I have noticed in your code that the check for the position being flat does not have brackets around the following code. When this is the case, only the first line of code will be affected by the preceding condition. This means that m_Enterlong=true; can only execute when flat, however, m_EnterShort=true; will execute anytime its crossover condition is true whether or not you are flat.
      I just did not include it above:

      It is this (is that what you mean) in the actual code:

      protected override void OnBarUpdate()
      {
      if (Position.MarketPosition == MarketPosition.Flat)

      if (CrossAbove(SMA(Fast), SMA(Slow), 1))
      m_EnterLong=true;

      if (CrossBelow(SMA(Fast), SMA(Slow), 1))
      m_EnterShort=true;
      }

      Correct it sometimes works but sometimes I get "Over Fill" conditions and the strategy shuts down. I am trying to prevent that.

      Please let me know if you have other insight.

      Comment


        #4
        Hello birdog,

        Thanks for your note.

        Overfills generally happen when an order is cancelled that has already begin filling or has filled.

        Are you cancelling orders in your script?

        As I mentioned NinjaTrader is able to reverse your position automatically by calling an enter order command in the opposite direction.
        Chelsea B.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        637 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        366 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        107 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        569 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        572 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X