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

UnManaged ChangeOrder

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

    UnManaged ChangeOrder

    The following code successfully submits unmanaged orders as a loop:

    for (int esuhs = 0, esuhsx = intSizeESES; esuhs < esuhsx; esuhs++)
    {
    if (intSizeESES >= 1)
    {
    myEntryOrderESSHORT = SubmitOrderUnmanaged(1,OrderAction.Sell,OrderType. Limit,1,CurrentDayOHL(Opens[1]).CurrentOpen[0]-14,0, "", "ES SHORT LIMIT"+ esuhs);
    barNumberOfOrderES = CurrentBars[0];
    EStradetoday = true;
    }
    }


    OnExecution the following code successfully creates the associated StopLoss and Profit Targets:


    if (myEntryOrderESSHORT != null && myEntryOrderESSHORT == execution.Order)
    {
    for (int esuhsx = 0, esuhsxx = intSizeESES; esuhsx < esuhsxx; esuhsx++)
    {
    if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
    {
    myStopOrderESSHORT = SubmitOrderUnmanaged(1, OrderAction.BuyToCover, OrderType.StopMarket,1,0,((Math.Max(MinStopESx, (ATR(Closes[1], 5)[1]*10))) + CurrentDayOHL(Opens[1]).CurrentOpen[0]),"oco"+GetAtmStrategyUniqueId()+esuhsx, "ESUHSHORTstop"+esuhsx);
    myTargetOrderESSHORT = SubmitOrderUnmanaged(1, OrderAction.BuyToCover, OrderType.Limit,1,PriorDayOHLC(Closes[1]).PriorClose[0]-20,0,"oco"+ GetAtmStrategyUniqueId()+esuhsx, "ESSHORTtarget"+esuhsx);


    if (execution.Order.OrderState != OrderState.PartFilled)
    {
    myEntryOrderESUHSHORT = null;
    }
    }
    }
    }


    QUESTION: How do employ ChangeOrder in this case to adjust the stoploss myStopOrderESSHORT and profit target myTargetOrderESSHORT levels to new values? Something like this? Do I reference the myStopOrderESSHORT" or can I loop through the "ESSHORTtarget"+esuhsx" signalname? Regards and many thanks.

    for (int esuhsc = 0, esuhscx = intSizeESES; esuhsc < esuhscx; esuhsc++)
    {
    ChangeOrder(myStopOrderESSHORT,myStopOrderESSHORT. Quantity,0,Position.AveragePrice+((Math.Max(Mi nStopESx, (ATR(Closes[1], 5)[1]*10)))));
    ChangeOrder(myTargetOrderESSHORT,myTargetOrderESSH ORT.Quantity, Position.AveragePrice-(CurrentDayOHL(Opens[1]).CurrentOpen[0] - PriorDayOHLC(Closes[1]).PriorClose[0]),0);
    }

    }

    #2
    Hello elliot5,

    For this type of use case you would need to store all of your order objects to individual variables or a collection instead of using 1 variable.

    ChangeOrder needs an Order object to change it, in what you have shown the myStopOrderESSHORT variable is being directly assigned in your loop which would mean you are only saving 1 out of all the orders you submitted. In general you should avoid the old style of direct assignment like you have shown and instead collect the orders from OnOrderUpdate or OnExecutionUpdate.

    For example when submitting the order it would look like the following where you do not use myStopOrderESSHORT:

    Code:
     SubmitOrderUnmanaged(1,OrderAction.Sell,OrderType. Limit,1,CurrentDayOHL(Opens[1]).CurrentOpen[0]-14,0, "", "ES SHORT LIMIT"+ esuhs);
    Later you could monitor OnOrderUpdate to find that order and assign it to its individual variable. If you had a loop of 5 times you would need 5 individual variables.

    Code:
    if(order.Name == "ES SHORT LIMIT[B]1[/B]")
    {
        myEntryOrderESSHORT[B]1[/B] = order; 
    }
    To support a for loop of variable size you would need a collection like a List<Order>. That would be so you can have a variable amount of stored orders. You would otherwise want to avoid the loops and just use X number of lines of code to support the number of orders you wanted:

    Code:
     SubmitOrderUnmanaged(1,OrderAction.Sell,OrderType . Limit,1,CurrentDayOHL(Opens[1]).CurrentOpen[0]-14,0, "", "ES SHORT [B]LIMIT1[/B]");
     SubmitOrderUnmanaged(1,OrderAction.Sell,OrderType . Limit,1,CurrentDayOHL(Opens[1]).CurrentOpen[0]-14,0, "", "ES SHORT [B]LIMIT2[/B]");
     SubmitOrderUnmanaged(1,OrderAction.Sell,OrderType . Limit,1,CurrentDayOHL(Opens[1]).CurrentOpen[0]-14,0, "", "ES SHORT [B]LIMIT3[/B]");
    etc..
    When you wanted to change an order you would pass the order variable to the ChangeOrder method:

    Code:
    ChangeOrder(myEntryOrderESSHORT[B]1 , [/B]Quantity[B], [/B]0[B], [/B]newPrice[B])[/B]
    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Quoting you here:

      Later you could monitor OnOrderUpdate to find that order and assign it to its individual variable. If you had a loop of 5 times you would need 5 individual variables.

      if(order.Name == "ESSHORTLIMIT1")
      {
      myEntryOrderESSHORT1 = order;
      }


      To support a for loop of variable size you would need a collection like a List<Order>.
      That would be so you can have a variable amount of stored orders. You would otherwise want to avoid the loops and just use X number of lines of code to support the number of orders you wanted:



      Response:
      1 - I understand the above but how do I assign the myentryorder + esuh = order? I want to create a loop during onorderupdate to create the order object as in some cases I will have over 70 single lot orders being generated for each entry as I want to break up the size to reduce slippage.
      2 - Also how would I assign OrderList.Add(myentryorder+esuh) upon entering ? Also obviously gives errors.



      I get errors with the following code below that attempts to loop through the creation of the order object.

      protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled,
      double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
      {

      for (int esuhs = 0, esuhsx = SizeES-voladjustES; esuhs < esuhsx; esuhs++)
      {
      if (order.Name == "ESUHSHORTLIMIT"+ esuhs)
      {
      myEntryOrderESUHSHORT + esuh = order; // error.

      //order.Name = order; Does not work.
      //Print(order.Name);
      }
      }
      Last edited by elliot5; 05-11-2021, 06:52 AM.

      Comment


        #4
        Hello elliot5,

        1 - I understand the above but how do I assign the myentryorder + esuh = order? I want to create a loop during onorderupdate to create the order object as in some cases I will have over 70 single lot orders being generated for each entry as I want to break up the size to reduce slippage.
        You would need to find it from OnOrderUpdate by its name. If the order has a 1 appended to the end you need to look for the order with that name and then assign it to a variable or add it to a list. For 70 orders you would have the option of having 70 variables or you can use a List<Order> to store orders.

        2 - Also how would I assign OrderList.Add(myentryorder+esuh) upon entering ? Also obviously gives errors.
        You would need to find it from OnOrderUpdate. This sample is an example of finding an order:

        Code:
        if(order.Name == "ES SHORT LIMIT1")
        {
        
        }
        If you are using loops you can also use a loop here, if the order name matches one of the loop names you could add it to the list.
        Code:
        for (int esuhsx = 0, esuhsxx = intSizeESES; esuhsx < esuhsxx; esuhsx++)
        {
            if(order.Name == "ES SHORT LIMIT" + esuhsxx)
           {
        [B]OrderList.Add(order); [/B]
           }
        }
        Keep in mind you will also likely need to check if the order exists in the list already. If you are not familiar with Lists or how to use them/find items/check if it exists I would suggest using external C# tutorials on Lists for learning those concepts. List<T> is a list of Objects, there are many methods which you can use with lists to find an object by an objects properties. Linq is one way to find items.

        Later when you want to change an order you could locate the order in your List<Order> by its name or other factors, however you intended to know which order to change. Then pass the found order to the Change method. A very simplistic example would be:

        Code:
        Order foundOrder = OrderList.First(); 
        if(foundOrder != null) ChangeOrder(foundOrder, Quantity, 0, newPrice)
        A linq example:

        Code:
        Order foundOrder= OrderList.First(o => o.Name == "ES SHORT LIMIT1");
        if(foundOrder!= null) ChangeOrder(foundOrder, Quantity, 0, newPrice)



        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service

        Comment


          #5
          1- Excellent work. I am still unclear how using an order list I could create the OrderObject - does the list then become the source for the order object name? How are OnExecution Stoploss and Profit Target orders referenced to the original order entry?

          2 - Would adding hundreds of order variables slow ninjatrader in any way in your estimation? Might be easier and more stable than manipulating lists.
          Last edited by elliot5; 05-11-2021, 08:19 AM.

          Comment


            #6
            Hello elliot5,

            1- Excellent work. I am still unclear how using an order list I could create the OrderObject - does the list then become the source for the order object name? How are OnExecution Stoploss and Profit Target orders referenced to the original order entry?
            The list contains Order type objects. The list would then become the source you use to pull any existing order objects from, you would need to determine how you wanted to use that to find the orders that need changed. The Linq example I provided shows how to find an order by a property, its Name. You could use any of the orders properties which you see fit.


            2 - Would adding hundreds of order variables slow NinjaTrader in any way in your estimation? Might be easier and more stable than manipulating lists.
            Not really you are doing the same thing with a list that contains those variables.

            Its really more about how comfortable you are with general C# concepts like Lists and Loops. If you feel comfortable using those concepts you can essentially collapse the amount of code required.

            If you don't feel comfortable with those concepts it may be easier to just hard code what you intended to make. In some cases it can be easier to hard code a bunch of variables and conditions by copying and pasting. That may also make it more easy to debug certain situations. As long as you are comfortable with loops and lists it shouldn't really be a problem to do that.

            I look forward to being of further assistance.
            JesseNinjaTrader Customer Service

            Comment


              #7
              Thank you - excellent analysis and again thank you Jesse for top service. Does the order list clear itself every day or will I have to code that? Regards
              Last edited by elliot5; 05-11-2021, 09:43 AM.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Waxavi, Today, 02:10 AM
              0 responses
              3 views
              0 likes
              Last Post Waxavi
              by Waxavi
               
              Started by TradeForge, Today, 02:09 AM
              0 responses
              9 views
              0 likes
              Last Post TradeForge  
              Started by Waxavi, Today, 02:00 AM
              0 responses
              2 views
              0 likes
              Last Post Waxavi
              by Waxavi
               
              Started by elirion, Today, 01:36 AM
              0 responses
              4 views
              0 likes
              Last Post elirion
              by elirion
               
              Started by gentlebenthebear, Today, 01:30 AM
              0 responses
              4 views
              0 likes
              Last Post gentlebenthebear  
              Working...
              X