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

OnOrderUpdate - Qty

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

    OnOrderUpdate - Qty

    I seem to be in a pickle...

    I am implementing a scale in/out functionality and have discovered I need to handle the case where the user drags a stop on top of another in an effort to merge the two.

    No problem... (except there is) I figured I could detect the move in OnOrderUpdate, which I can when OrderState == ChangePending and update the list of order objects I'm trying to keep track of. That all works. In my list of stop orders, I can find the stop the user is dragging and remove that from the list. Now I just need to update the quantity of the order they dragged the order onto. I do that just fine too. But when I look at the order on the chart, the order flag from ChartTrader tells me there is more orders in the stop/target than should be.

    Catch-22. OK, maybe I don't really need to update my order object in my list - maybe they get updated by the framework - who knows?. So I try that - I comment out the line of code that updates my order object. The stop/target flag from ChartTrader is now correct after I drag one order on top of another but when I check the quantity of the order that I still have in my list, the order is not updated. (I didn't think it would be - just trying everything I can think of.)

    It seems, when I change the order quantity in my list object it adds one extra to the chart object, (because the order that I'm dragging has qty = 1) making that incorrect but at least my order list is correct. In other words, if the qty on the chart trader flag to which I'm dragging is 5, the new qty ends up being 7 instead of 6. When I don't update the quantity, the flag on the ChartTrader is correct, my the orders in my list are not. Here is the code that simply cannot win:

    Code:
    if( order.OrderState == OrderState.ChangePending )
    {
        double draggedStopPrice = order.StopPrice;
        double droppedStopPrice = order.StopPriceChanged;
    
        double draggedTargetPrice = order.LimitPrice;
        double droppedTargetPrice = order.LimitPriceChanged;
    
        // Find the list object (order) with the dragged price and remove that guy
        if( draggedStopPrice > 0 )
        {
            // User moved a stop
            // First update the quantity of the dropped object
            Order stopOrder2Update = StopOrders.Find(o => o.StopPrice == droppedStopPrice);
            if( stopOrder2Update != null )
            {
                //stopOrder2Update.Quantity += order.Quantity;
    
                // Next, remove the order from the list
                Order stopOrder2Change = StopOrders.Find(o => o.StopPrice == draggedStopPrice);
                if (stopOrder2Change != null)
                {
                    StopOrders.Remove(stopOrder2Change);
                }
             }
         }
         else if( draggedTargetPrice > 0 )
         {
             // User moved a target
             // First update the quantity of the dropped object
             Order targetOrder2Update = TargetOrders.Find(o => o.LimitPrice == droppedTargetPrice);
             if (targetOrder2Update != null)
             {
                 //targetOrder2Update.Quantity += order.Quantity;
    
                 // Next, remove the order from the list
                 Order targetOrder2Change = TargetOrders.Find(o => o.LimitPrice == draggedTargetPrice);
                 if (targetOrder2Change != null)
                 {
                     TargetOrders.Remove(targetOrder2Change);
                 }
              }                      
          }
    }
    I'm completely baffled. I do need to update the quantity of the order object in my list but I seem to be damned if I do, damned if I don't. (ChangeOrder isn't the ticket - that puts me into an infinite loop.)

    Any idea what I'm missing? What is the proper way to update a quantity so it doesn't mess with the what ChartTrader thinks it should be?

    Thanks!
    PS. I'm using unmanaged orders.
    Last edited by traderpards; 12-07-2018, 08:30 AM.

    #2
    Hello

    Now I just need to update the quantity of the order they dragged the order onto. I do that just fine too. But when I look at the order on the chart, the order flag from ChartTrader tells me there is more orders in the stop/target than should be.
    I wanted to check, you seem to be removing the order from the list but I dont see where you cancel the order, are you doing this as well? Also does the Order tab show the correct information about the active orders you currently have? If you are just removing the order in your logic, that may explain why the chart still shows more orders than there should be if the order is still active.

    What is the proper way to update a quantity
    You would need to use ChangeOrder, this is the correct way to update or change an order. If using this is putting your script in a loop, you would need to identify why that is the case in your logic. You would likely need to make a more simple test that does not require the List and other logic you have used and only submits/updates orders to further identify the problem in your logic.

    Outside of these observations, I would be unsure based on the information provided. Do you have a test that only submits orders in realtime once to demonstrate the problem? While I wouldn't be able to go over the list logic you have created, I would be happy to review a sample that only demonstrates the extra orders in a more simple way.


    I look forward to being of further assistance.

    JesseNinjaTrader Customer Service

    Comment


      #3
      Thanks Jesse,

      I've been investigating this since you replied. I've even scaled down the scope of what I want to do - I've since realized that I don't need a bunch of targets and stops all over the place since scale in/out is the whole point. So I have modified the project to add to/remove from a single target and stop and it's respective order object. (There is no longer a list.)

      But I'm still having the same problem. I have it so that if I hit the scale in/scale out button a number of times, I will eventually run into the error that you can't change the order if the quantity isn't right. And the reason I get that is because I can't check how many I actually have.

      I attached a sample strategy that I hope this is simple enough. OnBarUpdate is blank. All there is are buttons that enter, scale in and out and exit the whole thing. Scale in/out quantity is 1.

      If you run an output window, I put print statements that demonstrate what TargetOrders.Quanity and StopOrders.Quantity are. The seem to be correct to me. What is wrong when you scale in are the ChartTrader flags that show the quantity for the stops and targets, which are wrong. That's where I can't seem to win for losing.

      If I make it so those flags are correct, my Target/StopOders.Quantity is wrong. If I make it so Target/StopOrders are correct, the flags are wrong.

      Thanks again!
      ScaleTest.cs
      Last edited by traderpards; 12-10-2018, 07:36 AM.

      Comment


        #4
        Is this one a stumper? I thought I would have heard from you by now...

        Comment


          #5
          Hello traderpards,

          I am reviewing this but currently, I do not have a reply I could provide as a solution. Once I have fully reviewed this case and can provide an answer I will reply back here.

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

          Comment


            #6
            Hello traderpards,

            I had some time to further review the sample you created and was able to find some details that I could provide.

            While I was not able to see an error when scaling in I did see the quantity being doubled I.E. 5, 7, 9 etc. I was also seeing an error during the scaling out when you reach one/zero quantity.

            In my testing, I have disregarded your initial posts information and worked only on the questions surrounding this sample, so I have only tried to correct the items I could see and you had described.

            I can see that with the order types being used, you will have some difficulty in certain areas such as dragging the orders to fill, this would not work with the stops in case you drag them above the market.

            I can also see that you would likely need to add some additional error checking to your buttons as the script is driven by the order events but your buttons can allow for actions before receiving feedback from the prior submitted events. This will allow the buttons to place the script in an undesirable state if clicked too quickly.

            In regard to the sample you provided, I have made a revision to your sample which does not double the scale in amount and does not error when reaching 0 quantity during scale out. I also see that the order flags with chart trader accurately match the position of the script.

            One item I did note with your sample is that it places individual targets. After the initial targets of 5, each new target is added separately leaving the existing target of 5 unchanged. Was this intended or were you trying to use ChangeOrder with the targets to update the existing target quantity per the commented out code? In the sample I have provided, I followed what you had commented in your syntax and used ChangeOrder to update the target quantity instead of having many targets. This is one item which you can review to see if this matches your expectation.

            Some of the main items I changed were adding more explicit handling of the scaling in regard to the targets. When using ChangeOrder, you cannot change it to 0 so a condition to change or Cancel the order was needed here. The quantity was eventually getting to an undesirable point in the logic.

            I also changed to Using the scripts Position object and moving some logic to OnExecutionUpdate. I noted that the OnOrderUpdate was being used for a good portion of your logic, this may have come from the initial posts tests where you were moving orders. I would suggest most filled order logic go in OnExecutionUpdate.


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

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by f.saeidi, Yesterday, 12:14 PM
            9 responses
            23 views
            0 likes
            Last Post f.saeidi  
            Started by Tim-c, Today, 03:54 AM
            0 responses
            3 views
            0 likes
            Last Post Tim-c
            by Tim-c
             
            Started by FrancisMorro, Today, 03:24 AM
            0 responses
            3 views
            0 likes
            Last Post FrancisMorro  
            Started by Segwin, 05-07-2018, 02:15 PM
            10 responses
            1,772 views
            0 likes
            Last Post Leafcutter  
            Started by Rapine Heihei, 04-23-2024, 07:51 PM
            2 responses
            31 views
            0 likes
            Last Post Max238
            by Max238
             
            Working...
            X