• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

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.
        https://ninjatrader.com/support/help...n-us/order.htm
        Chelsea B.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by DTSSTS, Today, 10:14 PM
        0 responses
        2 views
        0 likes
        Last Post DTSSTS
        by DTSSTS
         
        Started by babouin77, Today, 08:56 PM
        0 responses
        4 views
        0 likes
        Last Post babouin77  
        Started by JTizz, Today, 04:56 PM
        0 responses
        17 views
        0 likes
        Last Post JTizz
        by JTizz
         
        Started by Dan_D_Trader, Today, 03:29 PM
        0 responses
        4 views
        0 likes
        Last Post Dan_D_Trader  
        Started by Bsmckm, Today, 02:02 PM
        0 responses
        13 views
        0 likes
        Last Post Bsmckm
        by Bsmckm
         
        Working...
        X