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

Close Order Being Cancelled

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

    Close Order Being Cancelled

    Alright so I have a strategy that has a button. This button is supposed to Close all current positions for the current instrument and current account selected.

    When it is not in DLL form it works flawlessly.

    For some reason though when I convert to DLL it is cancelling my Close Order even though it is being submitted after the cancelallorders function is called.

    I would just use the Flatten function but that disables the strategy.

    Here is the code below. Let me know if I am doing something wrong, Thanks!

    """
    if(button.Name == "Close"){
    currAccount.CancelAllOrders(Instrument);
    Position pos = new Position();
    foreach(Position p in currAccount.Positions){
    if(p.Instrument == Instrument)
    pos = p;
    }
    if(pos.MarketPosition == MarketPosition.Long){
    CloseOrder = currAccount.CreateOrder(Instrument, OrderAction.Sell, OrderType.Market, OrderEntry.Manual, TimeInForce.Gtc, pos.Quantity, 0, 0, null, "Close", Core.Globals.MaxDate, null);
    currAccount.Submit(new[] {CloseOrder});
    }
    if(pos.MarketPosition == MarketPosition.Short){
    CloseOrder = currAccount.CreateOrder(Instrument, OrderAction.BuyToCover, OrderType.Market, OrderEntry.Manual, TimeInForce.Gtc, pos.Quantity, 0, 0, null, "Close", Core.Globals.MaxDate, null);
    currAccount.Submit(new[] {CloseOrder});

    }
    if(StopOrder != null){
    currAccount.Cancel(new [] {StopOrder});
    StopOrder = null;
    }
    }​
    """

    #2
    Hello Vikuno1,

    This type of code would not be expected to be used in a strategy so that may be part of the problem. Using the account directly is reserved for other types like indicators or addons.

    Do you see the same problem if you use the correct methods to close strategy positions or orders?

    To close a strategy position you need to submit strategy exits. To flatten an instrument the strategy would need to close only its own positions and orders using the managed or unmanaged approach methods. Using anything else from the addon framework would not be supported and may not work in all use cases.



    JesseNinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Jesse View Post
      Hello Vikuno1,

      This type of code would not be expected to be used in a strategy so that may be part of the problem. Using the account directly is reserved for other types like indicators or addons.

      Do you see the same problem if you use the correct methods to close strategy positions or orders?

      To close a strategy position you need to submit strategy exits. To flatten an instrument the strategy would need to close only its own positions and orders using the managed or unmanaged approach methods. Using anything else from the addon framework would not be supported and may not work in all use cases.


      So it is a bit of a special case. It is a tool that is placed in strategy form so that it is easy for users. I haven't had any issues in the past with this and on every other computer I have tested on it works like a charm. It is just for some reason on my server it cancels that close order even tho it is called before the order is submitted.

      Even weirder that when I just use the CS form not the DLL it works like a charm as well. Any ideas? Can't use strategy positions because the users need to be able to close the position manually, if they close manually and I am using strategy methods then the strategy will keep track of the strategy position and it causes some back offsets.

      Had thought about using a time wait but not sure if that would work. I'll test that real quick

      Comment


        #4
        Hello Vikuno1,

        You can use buttons in strategies but you still need to use the correct methods to submit orders or close positions. The addon framework is not the correct approach to use with strategies. What you have shown is not correct for strategies.

        The only way our support can assist with a strategy is if you are following the strategy basics by using the managed or unmanaged approach in your strategy. If you have addon methods being used like you have shown we would not expect that to work right. We would have to have you try with the strategy methods instead because that is the only use case that is expected to work with strategies.

        JesseNinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_Jesse View Post
          Hello Vikuno1,

          You can use buttons in strategies but you still need to use the correct methods to submit orders or close positions. The addon framework is not the correct approach to use with strategies. What you have shown is not correct for strategies.

          The only way our support can assist with a strategy is if you are following the strategy basics by using the managed or unmanaged approach in your strategy. If you have addon methods being used like you have shown we would not expect that to work right. We would have to have you try with the strategy methods instead because that is the only use case that is expected to work with strategies.
          Ok so a couple of questions.

          Is the AddOn function the "CancelAllOrders" function?
          If so then I would have to switch to using the "CancelOrder" function, but I can only cancel orders that I have saved. Is the strategy capable with catching the manual orders that are submitted by the user without using these account methods that are only supposed to be used in non strategies?
          If I switch to an AddOn do AddOns have the capability of putting buttons in the chart trader?

          Lastly you had mentioned that they are made for indicators and addons. If I switch this code over to an indicator do you think that would fix it and if not then would you be able to help me?

          Comment


            #6
            Hello Vikuno1,

            An easy way to identify addon methods is to use the help guide, if the left side menu shows addon then you are in the wrong section.

            CancelAllOrders is not a strategy function.

            Strategies need to submit orders like ExitLong to exit a position. To cancel an order you need to use the CancelOrder method and supply the strategy order you saved from OnOrderUpdate.



            Strategies are not intended to observe any other orders or positions besides the ones they create. Manual orders or positions are not supported in strategies.

            Addons can manipulate a chart and add buttons and also observe manually placed orders or positions. You would then use the addon help guide section to find an account and submit orders or use other addon features. That would be used from an addon or indicator and not a strategy.

            I wouldn't be able to say if using an indicator or addon would work for whatever you are trying to do but the code you are using in the strategy is not intended for a strategy so I would expect that specific use case to not work.

            JesseNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Salahinho99, Today, 04:13 AM
            0 responses
            9 views
            0 likes
            Last Post Salahinho99  
            Started by junkone, 04-28-2024, 02:19 PM
            8 responses
            92 views
            1 like
            Last Post brucerobinson  
            Started by mkouitra, 10-23-2021, 04:40 PM
            17 responses
            1,965 views
            0 likes
            Last Post NinjaTrader_Jason  
            Started by Vietanhnguyen2hotmailcom, 05-03-2024, 10:29 AM
            4 responses
            30 views
            0 likes
            Last Post Vietanhnguyen2hotmailcom  
            Started by PhillT, 04-19-2024, 02:16 PM
            4 responses
            39 views
            0 likes
            Last Post PhillT
            by PhillT
             
            Working...
            X