Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

{NinjaScript} NT8 - Unmanaged Approach - Close All Opened Long Trades.

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

    {NinjaScript} NT8 - Unmanaged Approach - Close All Opened Long Trades.

    Dear All, hope you are fine.

    I have a script that only opens long trades in a unmanaged approach.
    I save all long orders in a collection. I get the total size opened like this:

    int size = 0;

    foreach(Order o in orders)
    {
    if (o.OrderState==OrderState.Filled)
    {
    size+=o.Quantity;
    }
    }

    And I open a short hoping to exit all long trades:

    string oco;
    if (State == State.Historical)
    oco = DateTime.Now.ToString() + CurrentBar;
    else
    oco = GetAtmStrategyUniqueId();

    SubmitOrderUnmanaged(0, OrderAction.Sell, OrderType.Market, size,0,0,oco, "ExitAllLongs");

    But the long trades doesnt close till they touch theirs take profit or stop loss, and now I have a market short opened.

    What im doing wrong?
    Cold you give me some sample code showing how to exit opened trades in a unmanaged approach?

    Thank you!

    #2
    Hello fscabrera03,

    Thank you for the post.

    From the given details I couldn't say what may be happening in your script, you would likely need to add prints to see how the logic is equating in the use case.

    I can say that what you provided would not be enough to exit the position. You would need to first cancel the associated targets and then observe that they were cancelled from OnOrderUpdate. After they had been cancelled you could submit an exit. Technically the exit should work despite the targets being open still which is why you need to cancel them.

    Here is a very simple example of entering an order with targets and then exiting the position. This does not close the targets, this simply shows that the position can be entered and exited with SubmitOrderUnmanaged.

    Code:
    protected override void OnBarUpdate()
    {
        if(State == State.Realtime)
       {
          if(Position.MarketPosition == MarketPosition.Flat)
          {
             string oco = GetAtmStrategyUniqueId();
             SubmitOrderUnmanaged(0, OrderAction.Buy, OrderType.Market, 1,0,0,"", "EnterLong");
    
             SubmitOrderUnmanaged(0, OrderAction.Sell, OrderType.StopMarket, 1,0, Close[0] - 10 * TickSize,oco, "LongStop");
             SubmitOrderUnmanaged(0, OrderAction.Sell, OrderType.Limit, 1,Close[0] + 10 * TickSize,0,oco, "LongProfit");
          } else {
             SubmitOrderUnmanaged(0, OrderAction.Sell, OrderType.Market, 1,0,0,"", "ExitAllLongs");
          }
    
       }
    }
    You would normally need to use the order events from OnOrderUpdate or OnExecutionUpdate to submit targets rather than submitting them right after the entry like shown here, this is simply for the purpose of demonstration to confirm the exit works.

    I look forward to being of further assistance.

    Comment


      #3
      Thank you,
      Im having trouble with this. How can I add the Order object of the targets (take profit or stop loss) on a list collection before they are touched?
      Some sample code?
      Thank you

      Comment


        #4
        Hello fscabrera03,

        I don't have any samples using Lists, you can search online for C# examples of using Lists if you are having trouble with using a list in some way. To use order objects or find them you can see the following sample: https://ninjatrader.com/support/help..._and_profi.htm

        I look forward to being of further assistance.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        66 views
        0 likes
        Last Post argusthome  
        Started by NabilKhattabi, 03-06-2026, 11:18 AM
        0 responses
        41 views
        0 likes
        Last Post NabilKhattabi  
        Started by Deep42, 03-06-2026, 12:28 AM
        0 responses
        24 views
        0 likes
        Last Post Deep42
        by Deep42
         
        Started by TheRealMorford, 03-05-2026, 06:15 PM
        0 responses
        27 views
        0 likes
        Last Post TheRealMorford  
        Started by Mindset, 02-28-2026, 06:16 AM
        0 responses
        53 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Working...
        X