Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Proper Use of OrderEntry

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

    #16
    Checking in again regarding the issue. This is a concern as I am using this API in situations where it would be classified as an automated execution. I'd rather not have to go and write my own Flatten code.

    Comment


      #17
      Hello ntbone,

      If you are placing trades in your AddOn that qualify as automated, and you want to ensure they are marked as automated, I suggest using Account.CreateOrder instead of Account.Flatten.

      Please see the example attached.
      Attached Files
      JimNinjaTrader Customer Service

      Comment


        #18
        Thanks.

        In the attached code you iterate through each of the account positions and assign the newly created order to myFlattenOrder. This will result in only one order when you submit. The structure of the code doesn't handle if multiple positions are open. It creates the orders but because myFlattenOrder only points to the last order created its the only order that will be submitted.


        Code:
        Order myFlattenOrder = null;
        
        foreach (Position pos in account.Positions)
        {
        	if (pos.Instrument == Instrument)
        	{
                // Only the last order created here will end up being submitted.
        		if (pos.MarketPosition == MarketPosition.Long)
        			myFlattenOrder = account.CreateOrder(Instrument, OrderAction.Sell, OrderType.Market, OrderEntry.Automated, TimeInForce.Gtc, pos.Quantity, 0, 0, "", "FlattenPosition "+DateTime.Now.ToString(), DateTime.Now, null);
        		if (pos.MarketPosition == MarketPosition.Short)
        			myFlattenOrder = account.CreateOrder(Instrument, OrderAction.BuyToCover, OrderType.Market, OrderEntry.Automated, TimeInForce.Gtc, pos.Quantity, 0, 0, "", "FlattenPosition "+DateTime.Now.ToString(), DateTime.Now, null);
        	}
        }
        
        if (myFlattenOrder != null)
        	account.Submit(new[] { myFlattenOrder });
        If the API for Flatten had a parameter to specify the OrderEntry parameter it would look like this.

        Code:
        namespace NinjaTrader.Cbi
        {
            public static class AccountExtensions
            {
                public static void Flatten(this Account account, ICollection<Instrument> instruments, OrderEntry orderEntry)
                {
                    var orders = new List<Order>();
        
                    foreach(var position in account.Positions)
                    {
                        if(instruments.Contains(position.Instrument))
                        {
                            if(position.MarketPosition == MarketPosition.Long)
                            {
                                orders.Add(CreateFlattenOrder(account, position, OrderAction.Sell));
                            }
        
                            if(position.MarketPosition == MarketPosition.Short)
                            {
                                orders.Add(CreateFlattenOrder(account, position, OrderAction.BuyToCover));
                            }
                        }
                    }
        
                    if(orders.Count > 0)
                    {
                        account.Submit(orders);
                    }
                }
        
                private static Order CreateFlattenOrder(Account account, Position position, OrderAction action)
                {
                    return account.CreateOrder(
                        position.Instrument,
                        action,
                        OrderType.Market,
                        OrderEntry.Automated,
                        TimeInForce.Gtc,
                        position.Quantity,
                        0, 0,
                        string.Empty,
                        "FlattenPosition " + DateTime.Now.ToString(),
                        DateTime.Now,
                        null);
                }
            }
        }
        I am not sure what use case scenario, other then perhaps creating a button clicked by the user called Flatten, in which the Account.Flatten API is useable by scripts.

        Comment


          #19
          Hello ntbone,

          The example describes how you can flatten a position with an order created with Account.CreateOrder so you can set the OrderEntry property and achieve your goal to mark the order as an automated order. I am simply showing how you can flatten a position using Account.CreateOrder and Account.Submit since Account.Flatten does not have an overload that has an EntryOrder parameter.

          If you want to mark an order as OrderEntry.Automated, this would be the way.

          Instrument is relative to the indicator that is running the code and it is not intended to be a drop in for your AddOn. If you want to close positions on other instruments that are handled in your AddOn, you would need to create separate checks to see if pos.Instrument matches any of the instruments managed in your AddOn.

          As a rough example:

          Code:
          foreach (Position pos in account.Positions)
          {
             if (pos.Instrument == InstrumentA)
             {
                Order myFlattenOrderA = null;
          
                if (pos.MarketPosition == MarketPosition.Long)
                   myFlattenOrderA = account.CreateOrder(Instrument, OrderAction.Sell, OrderType.Market, OrderEntry.Automated, TimeInForce.Gtc, pos.Quantity, 0, 0, "", "FlattenPosition "+DateTime.Now.ToString(), DateTime.Now, null);
                if (pos.MarketPosition == MarketPosition.Short)
                   myFlattenOrderA = account.CreateOrder(Instrument, OrderAction.BuyToCover, OrderType.Market, OrderEntry.Automated, TimeInForce.Gtc, pos.Quantity, 0, 0, "", "FlattenPosition "+DateTime.Now.ToString(), DateTime.Now, null);
          
                account.Submit(new[] { myFlattenOrderA });
             }
          
             if (pos.Instrument == InstrumentB)
             {
                Order myFlattenOrderB = null;
          
                if (pos.MarketPosition == MarketPosition.Long)
                   myFlattenOrderB = account.CreateOrder(Instrument, OrderAction.Sell, OrderType.Market, OrderEntry.Automated, TimeInForce.Gtc, pos.Quantity, 0, 0, "", "FlattenPosition "+DateTime.Now.ToString(), DateTime.Now, null);
                if (pos.MarketPosition == MarketPosition.Short)
                   myFlattenOrderB = account.CreateOrder(Instrument, OrderAction.BuyToCover, OrderType.Market, OrderEntry.Automated, TimeInForce.Gtc, pos.Quantity, 0, 0, "", "FlattenPosition "+DateTime.Now.ToString(), DateTime.Now, null);
          
                account.Submit(new[] { myFlattenOrderB });
             }
          }
          Please let us know if you have any additional questions.
          JimNinjaTrader Customer Service

          Comment


            #20
            I was clarifying for those who might later come across.

            If you look at my code, it takes in a collection of instruments just like the Flatten official API, and then checks each position to see if that position is in the instrument list, and if so then adds the newly created order to the collection. Once all the positions for the account have been iterated, it then takes the collection of orders created and submits them. My extension API should execute similar to Account.Flatten but allows one to specify the OrderAction.

            The point I was making with the code you provided, is that your code iterated through the positions and created multiple orders, but only saved the last created order for submission. I suppose this is ok since an account can only have one open position per instrument.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by n8dogg59, Today, 07:58 AM
            0 responses
            5 views
            0 likes
            Last Post n8dogg59  
            Started by CoffeeOutlaw, Yesterday, 07:33 PM
            1 response
            12 views
            0 likes
            Last Post NinjaTrader_Kate  
            Started by localappleseed, Yesterday, 08:16 PM
            1 response
            11 views
            0 likes
            Last Post NinjaTrader_Jesse  
            Started by aslane, Today, 06:57 AM
            1 response
            11 views
            0 likes
            Last Post NinjaTrader_Kate  
            Started by MisterGee, Yesterday, 09:27 PM
            2 responses
            18 views
            0 likes
            Last Post MisterGee  
            Working...
            X