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 this position with unmanaged functions ?

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

    How to close this position with unmanaged functions ?

    Hello

    I have a strategy where I open the following order :

    Code:
    IOrder orderAchat = SubmitOrder(0, OrderAction.Buy, OrderType.Stop, (int)10, 0, 10500, "", "mySignalName");
    When the price of 10500 is reached then the order is getting the status FILLED.

    I try to close it with the following code after some time but that does not work, there is still an opened SELL position after :

    Attempt 1 :
    Code:
    IOrder CloseOrder = SubmitOrder(0, OrderAction.Sell, OrderType.Market, 10, 0, 0, "", "");
    Attempt 2 :
    Code:
    IOrder CloseOrder = SubmitOrder(0, OrderAction.SellShort, OrderType.Market, 10, 0, 0, "", "");
    Thanks

    #2
    Hello investdatasystems,

    Thanks for opening the thread.

    I have modified our Unmanaged Template strategy to take a similar approach to your description, and I am not seeing an issue with positions closing with a sell market order. You may test the example attached and use it for comparisons while debugging your strategy with Prints and using TraceOrders.

    The sell market's can be tested at line 76 and line 104.

    For the thread's reference, debugging information can be found here:

    Debugging - https://ninjatrader.com/support/help...script_cod.htm

    TraceOrders - https://ninjatrader.com/support/help...aceorders2.htm

    Please let me know if we may be of further assistance.
    Attached Files
    JimNinjaTrader Customer Service

    Comment


      #3
      Thank you. I have checked your code but I don't find where is the unmanaged function call to close the position ?

      Comment


        #4
        Hello investdatasystems,

        There is not an unmanaged method to close a position. The position is closed by submitting an order in the opposite direction of the position we are in to flatten the position. Information about the position I.E. Quantity, and MarketPosition, can be read from the Position object. Publicly available documentation is linked below for reference.

        Position - https://ninjatrader.com/support/help...7/position.htm

        At line 76, The position is closed by the sell market order when we are in a long position.

        At line 104 (commented), the position is closed with a sell market order after we see the execution of the long entry order.

        Please let us know if you have any additional questions.
        JimNinjaTrader Customer Service

        Comment


          #5
          Ok I see... That's exactly how I actually proceed... I'll check why this is not working...

          Comment


            #6
            I have just made new tests, and this now works !
            I don't understand why this was not working yesterday... Maybe I was too tired... Anyway I'll reiterate more tests just in case it reproduces !

            Sorry for having made you "waste" your time ! And thank you again !!!

            Comment


              #7
              I'm not analyzing your following code for setting a stoploss :

              Code:
              stopLossLong = SubmitOrder(0, OrderAction.Sell, OrderType.Stop, execution.Order.Filled, 0, execution.Order.AvgFillPrice - 4 * TickSize, "LongExits", "StopLossLong");
              Is this order a "waiting" order that will close the position at a certain level ? Or will it set a stoploss level on a specific order ? (Should I open another thread for that ?)

              Thanks again!!

              Comment


                #8
                In the Unmanaged Approach, we only submit orders and there are not any related Set methods for profit target and stop loss like we have in the Managed Approach.

                Profit Target and Stop Loss orders are typically submitted in the OnExecution method to submit your protective orders once the execution of your entry has been seen. This approach is explained in the SampleOnOrderUpdate strategy. If the Unmanaged Template strategy is unclear, I suggest reviewing the SampleOnOrderUpdate strategy that this strategy is modeled after. The SampleOnOrderUpdate strategy uses the Managed Approach while the Unmanaged Template uses the Unmanaged Approach.

                The SampleOnOrderUpdate strategy can be found below. Associated documentation items on using IOrder objects, OnOrderUpdate and OnExecution are linked there as well. Please be sure to review all of the associated documentation for a better understanding of both these strategies.

                SampleOnOrderUpdate - https://ninjatrader.com/support/help...and_onexec.htm

                Please let us know if you have any specific questions on the example and documentation.
                JimNinjaTrader Customer Service

                Comment


                  #9
                  Ok thank you for the clues.

                  I should add that I use a SellShort order to close a position opened by a Buy Stop Order (OrderAction.Buy + OrderType.Stop).

                  Is that a good way ?

                  Comment


                    #10
                    OrderActions SellShort and Buy are used when entering positions while OrderActions Sell and BuyToCover are used when closing positions. OrderType describes the actual type of order used.

                    Often times these questions have been asked previously on the forums. Depending on how busy we are and how many clients and tickets we need to address, it may take some time for us to return to open inquiries. You may find it convenient to search the forum for such inquiries to avoid waiting for a response from us. I have found one related thread here - https://ninjatrader.com/support/foru...ad.php?t=60274

                    Please let us know if there is anything else we can do to help.
                    Last edited by NinjaTrader_Jim; 06-06-2018, 12:29 PM.
                    JimNinjaTrader Customer Service

                    Comment


                      #11
                      Ok thank you.

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by love2code2trade, Yesterday, 01:45 PM
                      4 responses
                      27 views
                      0 likes
                      Last Post love2code2trade  
                      Started by funk10101, Today, 09:43 PM
                      0 responses
                      7 views
                      0 likes
                      Last Post funk10101  
                      Started by pkefal, 04-11-2024, 07:39 AM
                      11 responses
                      37 views
                      0 likes
                      Last Post jeronymite  
                      Started by bill2023, Yesterday, 08:51 AM
                      8 responses
                      44 views
                      0 likes
                      Last Post bill2023  
                      Started by yertle, Today, 08:38 AM
                      6 responses
                      26 views
                      0 likes
                      Last Post ryjoga
                      by ryjoga
                       
                      Working...
                      X