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 Open Position created with a Limit when entering New Order

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

    Close Open Position created with a Limit when entering New Order

    I need to close an open position that was created using a EnterShortLimit() or EnterLongLimit() in order to enter a new position.
    When I use the EnterLong() / EnterShort() when an position is open, it closes the position and opens a new one.

    Is there a simple way to achieve this, without the unmanaged approach?

    I downloaded the example code for the unmanaged approach from: https://ninjatrader.com/support/foru...ry-orders-help
    but I don't now how to use it, because when I code in the OnBarUpdate() method the following:
    if(<condition>)
    SubmitOrder(0, OrderAction.Sell, OrderType.Limit, 1, Close[0], 0, ocoString, "ShortPosition");

    it doesn't work and a get a blank chart and no messages in the log. (see attached UnmanagedOCOBracketExample.cs)

    I am attaching a strategy (ZL Cross.cs) to test the cancellation of the positions created with limits.

    Thanks!
    Attached Files

    #2
    Hello Capablanca,

    To close a long position with a market order in the managed approach call ExitLong(). To close a short position call ExitShort().

    Below are links to the help guide.
    https://ninjatrader.com/support/help...s/exitlong.htm

    If you want to reverse your position, do not call the exit order first. NinjaTrader will automatically close the open position and open a new position when calling an entry order in the opposite direction of the position.

    Also, below is a link to a forum post with helpful information about getting started with NinjaScript.
    https://ninjatrader.com/support/foru...040#post786040
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thank you Chelsea,

      I had tried the ExitLong() but it was in the wrong part of the code. It's working now.

      In my code I am entering two positions:
      EnterShort(0,c1,"Pos1");
      EnterLong(1,c2,"Pos2");

      Can I use the Position.AvgPrice to find the entry price of "Pos1" and "Pos2" independently?
      What would be the correct approach? Because if I save the current price when my orders are submitted it isn't guaranteed it will fill my orders at that exact price.

      I am trying to determine the moment when both positions are positive.


      Comment


        #4
        Hello Capablanca,

        A strategy only has one position. It cannot be long and short at the same time.

        Placing an EnterShort() and an EnterLong() at the same time will conflict with each other. Most likely one of the orders will be cancelled or there will be an overfill.

        No, the Position.AvgPrice will not give each orders .AverageFillPrice independently.
        You will need to get the .AverageFillPrice from each order.
        Chelsea B.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by f.saeidi, Today, 11:02 AM
        1 response
        1 view
        0 likes
        Last Post NinjaTrader_BrandonH  
        Started by geotrades1, Today, 10:02 AM
        4 responses
        11 views
        0 likes
        Last Post geotrades1  
        Started by rajendrasubedi2023, Today, 09:50 AM
        3 responses
        15 views
        0 likes
        Last Post NinjaTrader_BrandonH  
        Started by lorem, Today, 09:18 AM
        2 responses
        11 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by geddyisodin, Today, 05:20 AM
        4 responses
        30 views
        0 likes
        Last Post geddyisodin  
        Working...
        X