Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Check order on a price level

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

    Check order on a price level

    Hi,

    How do I set up a condition to check whether there are any working orders on a price level?

    #2
    Hello er111222,
    You can assign an object while submitting the order and from there you can check the order state of the order. For example:

    Code:
    //submit order
    IOrder ord = EnterLongLimit(Close[0] - 10 * TickSize);
    
    //check the order state
    if (ord != null && ord.OrderState == OrderState.Working)
    {
       // do stuff
    }
    JoydeepNinjaTrader Customer Service

    Comment


      #3
      I am using unmanaged order management and my strategy put many orders around levels. I need to check whether there are any order on a price level given only the price.

      Comment


        #4
        Hello er111222,
        IOrder will work for UnManaged orders too. In case of multiple orders you can create a generic list of IObjects and store the values there.

        in variable
        Code:
        List<IObject> listOrd = new List<IOrder>();
        in OnBarUpdate
        Code:
        IOrder ord = SubmitOrder(0, OrderAction.Buy, OrderType.Limit, 1, Close[0] - TickSize * 10, 0, "", "Buy Limit");
        
        listOrd.Add(ord);
        
        //loop through the list to get the values
        Please let me know if I can assist you any further.
        JoydeepNinjaTrader Customer Service

        Comment


          #5
          I am not a programmer, so do get a bit into detail if possible.
          I tried the following, is it correct?

          In variables:
          Code:
          List<IOrder> listOrd = new List<IOrder>();
                          private IOrder es_0 = null;
          		private IOrder es_1 = null;
          		private IOrder es_2 = null;
          		private IOrder es_3 = null;
          Code:
          protected override void OnStartUp()
          		{
          double level_0 = 1234
          double level_1 = 1235
          double level_2 = 1236
          double level_3 = 1237
          
          private void Es_0()
          {es_0 = SubmitOrder(0, OrderAction.Sell, OrderType.Limit, DefaultQuantity, level_0, 0, "", "es_0");}
          private void Es_1()
          {es_1 = SubmitOrder(0, OrderAction.Sell, OrderType.Limit, DefaultQuantity, level_1, 0, "", "es_1");}
          private void Es_2()
          {es_2 = SubmitOrder(0, OrderAction.Sell, OrderType.Limit, DefaultQuantity, level_2, 0, "", "es_2");}
          private void Es_3()
          {es_3 = SubmitOrder(0, OrderAction.Sell, OrderType.Limit, DefaultQuantity, level_3, 0, "", "es_3");}
          
          listOrd.Add(es_0, level_0);
          listOrd.Add(es_1, level_1);
          listOrd.Add(es_2, level_2);
          listOrd.Add(es_3, level_3);
          }
          How to get say IOrder es_2 when given the price of level_2? Should I set another class for (es_2, level_2) and do something like:
          Code:
          private class TheOrder : IEquatable<TheOrder>
          {
           private TheOrder(order, level)
              {
               this.Order = order;
               this.Level = level;
              }
              
              private IOrder Order {get; set;}
              private double Level {get; set;}
          }
          or do something like this:
          Code:
          public class MultiMap<V>
          		{	
          			Dictionary<double, List<V>> _dictionary =
          	        new Dictionary<double, List<V>>();
          			
          			public void Add(double key, V value)
          			{
          				orderId.Add(level_0, es_0);
          				orderId.Add(level_1, es_1);
          				orderId.Add(level_2, es_2);
          				orderId.Add(level_3, es_3);

          As I am not a programmer, I have searched and read a lot trying to solve the issue but a bit confuse as to which is the most simplest application for this situation?

          Comment


            #6
            Hello er111222,
            I will simply loop through the list. For example
            Code:
            foreach (IOrder o in listOrd)
            {
            	if (o != null && o = es_1)
            	{
            		//do stuff
            	}
            }

            Please let me know if I can assist you any further.
            Last edited by NinjaTrader_Joydeep; 06-13-2012, 05:45 AM.
            JoydeepNinjaTrader Customer Service

            Comment


              #7
              The question is I don't know which IOrders are sitting on the level, each level has 2 different IOrders, (for example: es_1 and ts_1), all I know is the price of the level. I need to find the IOrders and check whether they are working or not, then do something.

              Comment


                #8
                Hello er111222,
                You can compare the IOrder directly.

                From help files
                To check for equality you can compare IOrder objects directly



                In your code the listOrd is a list of IOrder and not a list of the private class TheOrder.
                Code:
                List<IOrder> listOrd = new List<IOrder>();
                Anyway, had it been a list of TheOrder, then also you can directly compare the order objects.

                Please let me know if I can assist you any further.
                JoydeepNinjaTrader Customer Service

                Comment


                  #9
                  May be you have answered my question but I still don't get it, my question is "How to get say IOrder es_2 when given the price of level_2?" where is "level_2" in the solution?

                  I have spent the weekend trying to work this out but still can not compile, done a test code in random levels using another class to make 1 key with 2 values in dictionary and TryGetValue to get the working orders (es_2 and ts_2 on level_2). Please correct my code as follows:

                  Code:
                  #region Using declarations
                  using System;
                  using System.ComponentModel;
                  using System.Collections.Generic;
                  using System.Diagnostics;
                  using System.Drawing;
                  using System.Drawing.Drawing2D;
                  using System.Xml.Serialization;
                  using NinjaTrader.Cbi;
                  using NinjaTrader.Data;
                  using NinjaTrader.Indicator;
                  using NinjaTrader.Gui.Chart;
                  using NinjaTrader.Strategy;
                  #endregion
                  
                  namespace NinjaTrader.Strategy
                  {
                      [Description("Enter the description of your strategy here")]
                  	
                      public class dictionaryTest : Strategy
                      {
                          #region Variables
                  		private bool _initialized = false;
                  		private double levelGap = 8;
                  		private double SRHigh = 0;
                          private double SRLow = 0;
                  		
                          private double level_0 = 0;
                  		private double level_1 = 0;
                  		private double level_2 = 0;
                  		private double level_3 = 0;
                  		private double level_4 = 0;
                  		private double level_5 = 0;
                  		
                  		private IOrder shortR1        = null;
                  		private IOrder shortR11       = null;
                  		private IOrder longS11        = null;
                  		private IOrder longS1         = null;
                  		
                  List<IOrderValue>>();
                  		private IOrder Order1 = null;
                  		private IOrder Order11 = null;
                  		
                  		private IOrder ts_0 = null; 
                  		private IOrder ts_1 = null;  
                  		private IOrder ts_2 = null;
                  		private IOrder tl_3 = null;
                  		private IOrder tl_4 = null;
                  		private IOrder tl_5 = null;
                  		
                  		private IOrder es_0 = null;    //exit short
                  		private IOrder es_1 = null;
                  		private IOrder es_2 = null;
                  		private IOrder el_3 = null;
                  		private IOrder el_4 = null;
                  		private IOrder el_5 = null;
                  		
                  		List<double>listSR = new List<double>();
                  	
                          #endregion
                  
                          protected override void Initialize()
                          {
                              Unmanaged = true;
                  			DefaultQuantity = 1;
                  			ConnectionLossHandling = ConnectionLossHandling.Recalculate;
                              DisconnectDelaySeconds = 5; // Disconnect has to be at least 5 seconds
                  			ExitOnClose = true;
                              ExitOnCloseSeconds = 60; // Triggers the exit on close function 30 seconds prior to session end
                  			IncludeCommission = true;
                  			TraceOrders = true;
                  			BarsRequired = 1;
                  			CalculateOnBarClose = false;
                  			
                          }
                  		
                  		private void Es_0()
                  		{es_0 = SubmitOrder(0, OrderAction.Sell, OrderType.Limit, DefaultQuantity, level_0, 0, "", "es_0");}
                  		private void Es_1()
                  		{es_1 = SubmitOrder(0, OrderAction.Sell, OrderType.Limit, DefaultQuantity, level_1, 0, "", "es_1");}
                  		private void Es_2()
                  		{es_2 = SubmitOrder(0, OrderAction.Sell, OrderType.Limit, DefaultQuantity, level_2, 0, "", "es_2");}
                  		private void Es_3()
                  		{el_3 = SubmitOrder(0, OrderAction.Sell, OrderType.Limit, DefaultQuantity, level_3, 0, "", "el_3");}
                  		private void Es_4()
                  		{el_4 = SubmitOrder(0, OrderAction.Sell, OrderType.Limit, DefaultQuantity, level_4, 0, "", "el_4");}
                  		private void Es_5()
                  		{el_5 = SubmitOrder(0, OrderAction.Sell, OrderType.Limit, DefaultQuantity, level_5, 0, "", "el_5");}
                  		
                  		private void Ts_0()
                  		{ts_0 = SubmitOrder(0, OrderAction.Sell, OrderType.Limit, DefaultQuantity, level_0, 0, "", "ts_0");}
                  		private void Ts_1()
                  		{ts_1 = SubmitOrder(0, OrderAction.Sell, OrderType.Limit, DefaultQuantity, level_1, 0, "", "ts_1");}
                  		private void Ts_2()
                  		{ts_2 = SubmitOrder(0, OrderAction.Sell, OrderType.Limit, DefaultQuantity, level_2, 0, "", "ts_2");}
                  		private void Ts_3()
                  		{tl_3 = SubmitOrder(0, OrderAction.Sell, OrderType.Limit, DefaultQuantity, level_3, 0, "", "tl_3");}
                  		private void Ts_4()
                  		{tl_4 = SubmitOrder(0, OrderAction.Sell, OrderType.Limit, DefaultQuantity, level_4, 0, "", "tl_4");}
                  		private void Ts_5()
                  		{tl_5 = SubmitOrder(0, OrderAction.Sell, OrderType.Limit, DefaultQuantity, level_5, 0, "", "tl_5");}
                  		
                  
                          protected override void OnStartUp()
                  			{
                  				//random levels
                  					double firstGap = Math.Round(levelGap/2);
                  					double level_0 = Close[0] + firstGap * TickSize;
                  					double level_1 = level_0 + levelGap * TickSize;
                  					double level_2 = level_1 + levelGap * TickSize;
                  					double level_3 = level_0 - levelGap * TickSize;
                  					double level_4 = level_3 - levelGap * TickSize;
                  					double level_5 = level_4 - levelGap * TickSize;
                  				
                  				listSR.Add(level_0);
                  				listSR.Add(level_1);
                  				listSR.Add(level_2);
                  				listSR.Add(level_3);
                  				listSR.Add(level_4);
                  				listSR.Add(level_5);
                  			}
                  		
                  		protected void FindSR()
                  			{ 
                  				listSR.Sort();
                  				for (int x = 0; x < listSR.Count; x++)
                  				{
                  					if (listSR[x] > Close[0])
                  						{
                  							SRHigh = listSR[x];
                  							if (x != 0) SRLow = listSR[x - 1];
                  							break;
                  						}
                  					if (listSR[x] > Close[0]) SRLow = 0;
                  					if (listSR[x] < Close[0]) SRHigh = 0;
                  				}
                  			}
                  		
                  		public class Orders
                  		{
                  			public IOrder Order1 { get; set; }
                  			public IOrder Order11 { get; set; }
                  		}
                  
                  		protected void Dictionary()
                  		{
                              Dictionary<double, Orders> dict = new Dictionary<double, Orders>();
                              dict.Add(level_0, new Orders { Order1 = es_0, Order11 = ts_0 });
                              dict.Add(level_1, new Orders { Order1 = es_1, Order11 = ts_1 });
                  			dict.Add(level_2, new Orders { Order1 = es_2, Order11 = ts_2 });
                              dict.Add(level_3, new Orders { Order1 = el_3, Order11 = tl_3 });
                  			dict.Add(level_4, new Orders { Order1 = el_4, Order11 = tl_4 });
                              dict.Add(level_5, new Orders { Order1 = el_5, Order11 = tl_5 });
                  			
                  				if (dict.TryGetValue(level_2, out Orders(Order1)))
                  				{
                                                   shortR1 = Order1
                  				}
                          }
                  		
                          protected override void OnBarUpdate()
                  		{
                  			if (!_initialized)
                  				_initialized = true;
                  			if (BarsInProgress != 0)  return;
                  			if (CurrentBar < 0)      return;
                  			
                  			FindSR();
                  			
                  			Dictionary();
                  		}	
                  			
                          #region Properties
                          #endregion
                      }
                  }
                  The following is the other code I have tried, just some ideas if needed otherwise ignore it:

                  // Dictionary<double, Orders> order = new Dictionary<double, Orders>();
                  // order.Add(level_0, es_0, ts_0);
                  // order.Add(level, new Record("a", "b", true, true, 1, "c"));

                  // public class order : Dictionary<double, IOrderValue>
                  // {
                  // Dictionary<double, IOrderValue> order = new Dictionary<double, IOrderValue>();
                  //// order.Add(level_0, (Order1=es_0, Order11=ts_0));
                  // order.Add(level_0, new IOrderValue(Order1, Order11) (es_1, ts_1));
                  // order.Add(level_0, new IOrderValue{Order1 = es_0, Order11=ts_0 });
                  // order.Add
                  // }

                  // protected void FindOrders()
                  // {
                  // var orders = new List<KeyValuePair<IOrder, IOrder>>();
                  // orders.Add(new KeyValuePair<IOrder, IOrder>(es_0, ts_0));
                  // orders.Add(new KeyValuePair<IOrder, IOrder>(es_1, ts_1));
                  // orders.Add(new KeyValuePair<IOrder, IOrder>(es_2, ts_2));
                  // orders.Add(new KeyValuePair<IOrder, IOrder>(es_3, ts_3));
                  // orders.Add(new KeyValuePair<IOrder, IOrder>(es_4, ts_4));
                  // orders.Add(new KeyValuePair<IOrder, IOrder>(es_5, ts_5));
                  // Dictionary<double, orders> dic = new Dictionary<double, orders>();
                  // }
                  // public struct MyValue
                  // {
                  // public IOrder es_0;
                  // public IOrder ts_0;
                  // }
                  // public class MyDictionary : Dictionary<double, MyValue>
                  // {
                  // public void Add(double key, IOrder value1, IOrder value2)
                  // {
                  // MyValue val;
                  // val.Value1 = value1;
                  // val.Value2 = value2;
                  // this.Add(key, val);
                  // }
                  // }
                  // Dictionary<double, MyValue> dict = new Dictionary<double, MyValue>();
                  // dict.Add(level_0, new
                  // public class IOrders
                  // {
                  // Orders<KeyValuePair<IOrder, IOrder>> orders = new Orders<KeyValuePair<IOrder, IOrder>>();
                  // orders.Add(new KeyValuePair(IOrder, IOrder)(es_0, ts_0));
                  // orders.
                  // }

                  Comment


                    #10
                    Hello er111222,
                    To compare the IOrder with an price please refer to the below code
                    Code:
                    //if filled
                    if (es_2.OrderState == OrderState.Filled || es_2.OrderState == OrderState.PartFilled)
                    {
                    	if (es_2.AvgFillPrice == level_2)
                    	{
                    		//do something
                    	}
                    }
                    else //if not filled compare it with the limit price
                    {
                    	
                    	if (es_2.LimitPrice == level_2)
                    	{
                    		//do something
                    	}
                    }
                    Here es_2 is the IOrder object and the level_2 is the price.
                    I have compared the price with the Avgerage Fill Price (if order is filled/partfilled) or with the limit price (if not filled)

                    Your code has too many generic C# elements which is unfortunately beyond what we could support.
                    JoydeepNinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                    0 responses
                    648 views
                    0 likes
                    Last Post Geovanny Suaza  
                    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                    0 responses
                    369 views
                    1 like
                    Last Post Geovanny Suaza  
                    Started by Mindset, 02-09-2026, 11:44 AM
                    0 responses
                    108 views
                    0 likes
                    Last Post Mindset
                    by Mindset
                     
                    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                    0 responses
                    572 views
                    1 like
                    Last Post Geovanny Suaza  
                    Started by RFrosty, 01-28-2026, 06:49 PM
                    0 responses
                    574 views
                    1 like
                    Last Post RFrosty
                    by RFrosty
                     
                    Working...
                    X