• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

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, 07: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; Yesterday, 06:36 AM.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Orden33, Today, 10:09 AM
      2 responses
      7 views
      0 likes
      Last Post Orden33
      by Orden33
       
      Started by martyn73, Today, 10:18 AM
      4 responses
      11 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Started by silverm3170, Today, 10:03 AM
      3 responses
      4 views
      0 likes
      Last Post NinjaTrader_Jesse  
      Started by diff24rac, 04-04-2018, 05:32 AM
      6 responses
      481 views
      0 likes
      Last Post fx.practic  
      Started by gma17, Today, 08:58 AM
      3 responses
      11 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Working...
      X