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

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


              #21
              Some other questions about OrderEntry.Automated vs OrderEntry.Manual.

              Who is privy to this information and can it be used against my orders?

              Can clearing houses and/or market makers use this information to funnel may trades into different buckets where they may or may not be front run by their own algos?

              What is the cost of lying? Am I in violation of the law or just what CME Globex wants me to do in an ideal world?

              At some point, I'll be far enough along to collect data on whether setting this value one way or the other impacts results. If it does, I'll obviously want to go where the money is, if it's not creating legal exposure for myself or others.

              Thoughts?

              Comment


                #22
                Hello carnitron,

                Thank you for your reply.

                This is a regulatory thing set by and policed by the CME. We've seen cases where a user was using an addon to trade that placed trades automatically for him like a strategy that did not have this flag set to Automatic, and the CME noticed and suspended their account until the user reported they would correct it to reflect the automatic basis for their trades.

                Please let us know if we may be of further assistance to you.
                Kate W.NinjaTrader Customer Service

                Comment


                  #23
                  Ah thank you for that clarification. I certainly don't want to be suspended.

                  Cheers.

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by Rapine Heihei, Today, 08:19 PM
                  1 response
                  8 views
                  0 likes
                  Last Post NinjaTrader_Manfred  
                  Started by Rapine Heihei, Today, 08:25 PM
                  0 responses
                  6 views
                  0 likes
                  Last Post Rapine Heihei  
                  Started by f.saeidi, Today, 08:01 PM
                  1 response
                  9 views
                  0 likes
                  Last Post NinjaTrader_Manfred  
                  Started by Rapine Heihei, Today, 07:51 PM
                  0 responses
                  8 views
                  0 likes
                  Last Post Rapine Heihei  
                  Started by frslvr, 04-11-2024, 07:26 AM
                  5 responses
                  98 views
                  1 like
                  Last Post caryc123  
                  Working...
                  X