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

{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.
    JesseNinjaTrader Customer Service

    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.
        JesseNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Brevo, Today, 01:45 AM
        0 responses
        3 views
        0 likes
        Last Post Brevo
        by Brevo
         
        Started by aussugardefender, Today, 01:07 AM
        0 responses
        3 views
        0 likes
        Last Post aussugardefender  
        Started by pvincent, 06-23-2022, 12:53 PM
        14 responses
        239 views
        0 likes
        Last Post Nyman
        by Nyman
         
        Started by TraderG23, 12-08-2023, 07:56 AM
        9 responses
        384 views
        1 like
        Last Post Gavini
        by Gavini
         
        Started by oviejo, Today, 12:28 AM
        0 responses
        6 views
        0 likes
        Last Post oviejo
        by oviejo
         
        Working...
        X