Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

How to Close All Orders?

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

    How to Close All Orders?

    Hello!

    I'm trying to close all orders with ExitLong() or ExitShort() but it's only closing one of my two orders. It's closing my EnterLong() but it's not closing an additional EnterLongStopMarket(); I have set. What I'm essentially looking for is the equivalent NinjaScript method for the Close Button that's on the UI. Ultimately I might want to open 4 or 5 orders (in the same directions) as scale-in orders as this is a trend following strategy. The goal is to just clean up everything and reset when the trend is over.

    In the Managed Approach docs I see an ExitShort and ExitLong method but that's going to essentially create a stop which is not what I'm looking for. I just want to close everything if I hit a particular condition.

    Here's a little sudo code below to illustrate what I'm up to.

    Code:
    public class Name : Strategy
    {
    
    private EMA EMA100;
    
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    
    // A bunch of vars
    
    }
    else if (State == State.Configure)
    {
    }
    else if (State == State.DataLoaded)
    {
    
    // this get set when EnterLong() or Enter Short() is triggered
    SetProfitTarget("", CalculationMode.Ticks, ProfitTarget);
    
    }
    
    }
    
    }
    
    protected override void OnBarUpdate()
    {
    
    // LONG
    if ( Long Conditions )
    
    {
    
    // Jump into market long
    EnterLong(Convert.ToInt32(NumOfContracts), "");
    
    // Drop a "scale-in" order as well and let that sit until triggered.
    EnterLongStopMarket(0, true, 1, Close[0] + (TickSize * 16), "");.
    
    }
    
    // SHORT
    if ( ShortConditions )
    
    {
    
    // Jump into market short
    EnterShort(Convert.ToInt32(NumOfContracts), "");
    
    // Drop a "scale-in" order as well and let that sit until triggered.
    EnterShortStopMarket(0, true, 1, Close[0] - (TickSize * 16), "");
    
    }
    
    // If the trend is over
    if (Position.MarketPosition == MarketPosition.Long && Close[0] < EMA100[0])
    {
    
    ExitLong(); // close everything
    
    }
    
    // If the trend is over
    if (Position.MarketPosition == MarketPosition.Short && Close[0] > EMA100[0])
    {
    
    ExitShort(); // close everything
    
    }
    
    }
    Attached Files
    Last edited by coopgrafik; 03-22-2021, 05:06 PM. Reason: Just cleaning things up for clarity

    #2
    Hello coopgrafik,

    EnterLong and EnterLongStopMarket are not equal, if EnterLongStopMarket is still working that is not an open Position to exit and is instead a working order which needs cancelled.


    If that order filled and did enter a position then you would need to use Signal Names with your orders. It looks like you are trying to scale from the comments, if so you cannot use "" for all order signal names as that will not allow you to scale out. You can see the following example which demonstrates scaling;


    In the Managed Approach docs I see an ExitShort and ExitLong method but that's going to essentially create a stop which is not what I'm looking for. I just want to close everything if I hit a particular condition.
    The ExitLong and ExitShort methods produce a market order to exit a position. These do no produce stops or targets.

    Please let me know if I may be of additional assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Ack! Still pretty confused.

      When I go to the documentation that you linked—which I have already tried to understand, failed to understand, and thus end up here asking a question where I was linked right back to the same place—I see that there is a CancelOrder() link. I click on that yet it does not give me the documentation for CancelOrder() but instead, it goes to a page about the Cancel() method. That seems off. The Cancel() order seems to take an array and an order object, not a signal name. That's super confusing to me.

      in simpler terms will this work? Should this work?

      // Open
      EnterShortStopMarket(0, true, 1, Close[0] - (TickSize * 16), "Trade Signal Name");

      // Cancel
      CancelOrder("Trade Signal Name");

      Now, I have tried and I am back trying to add a signal name to the CancelOrder method yet it throws out the error, "The best-overloaded method match for 'NinjaTrader.StrategyBase.CancelOrder(NinjaTrader. Cbi.Order)' has some invalid arguments."

      Can you maybe expand a little and provide a dead-simple example as above that will work?

      Thank you!

      P.S. By the way I do not want to scale out of a position I want to scale IN to a position. When that trend is over I want to close everything. the goal is to enter long and then drop a handful of scale-in orders every few points as the trend continues.
      Last edited by coopgrafik; 03-23-2021, 10:40 AM.

      Comment


        #4
        Hello coopgrafik,

        It looks like the link does go to Cancel, I will have that updated. You can also use the search in the help guide for NinjaScript code items like CancelOrder to get the page for that. Here is a direct link:



        in simpler terms will this work? Should this work?
        No, you would need to use the order object like the sample I had linked to. There is not a way to cancel with the signal name.

        Can you maybe expand a little and provide a dead-simple example as above that will work?
        I would suggest downloading the sample from the help guide here: https://ninjatrader.com/support/help...thod_to_ca.htm
        That is a complete sample that shows how the code is used along with storing your orders to variables so they can later be cancelled.


        Please let me know if I may be of additional assistance.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Thanks for your answer.

          Can I use EnterShortStopMarket() and EnterLong() in the same script?

          Right now I seem to only be able to use either or and I'm not sure why.

          Comment


            #6
            Hello coopgrafik,

            You would not be able to enter opposite directions using the managed approach at the same time. To do a entry like that would require unmanaged.





            Please let me know if I may be of additional assistance.
            JesseNinjaTrader Customer Service

            Comment


              #7
              I'm sorry that was a typo.

              Can I use EnterLongStopMarket() and EnterLong() in the same script and have them trigger at the same time?

              Again, right now I seem to only be able to use either or, and I'm not sure why.

              Comment


                #8
                Hello coopgrafik,

                To have multiple entries in the same direction you would need to increase EntriesPerDirection to more than 1 or use unique signal names and then use EntryHandling.Unique.



                Please let me know if I may be of additional assistance.
                JesseNinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by suroot, 04-10-2017, 02:18 AM
                4 responses
                3,019 views
                0 likes
                Last Post truepenny  
                Started by Stanfillirenfro, Today, 07:23 AM
                1 response
                4 views
                0 likes
                Last Post NinjaTrader_Gaby  
                Started by cmtjoancolmenero, Yesterday, 03:58 PM
                2 responses
                22 views
                0 likes
                Last Post cmtjoancolmenero  
                Started by olisav57, Yesterday, 07:39 PM
                1 response
                9 views
                0 likes
                Last Post NinjaTrader_ChelseaB  
                Started by cocoescala, 10-12-2018, 11:02 PM
                7 responses
                944 views
                0 likes
                Last Post Jquiroz1975  
                Working...
                X