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

Stop and Target quantity incorrect on execution

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

    Stop and Target quantity incorrect on execution

    Hello all,

    I am having a problem that occurs intermittently while trading live with interactive brokers using managed orders. Basically, my entry will trigger perfectly at the right time with the correct quantity. The associated stop loss and profit target (assigned OnExecution) are placed at the correct price, but sometimes with the wrong quantity. For example, upon entering the trade I end up Long 10 contracts as desired, but my stop loss and profit target are both only for 4 contracts.

    If I follow the log, in the On Order the quantity is listed correctly as 10, then on execution the quantity changes to 4, and no error is generated. This has occured multiple times, and googling has lead me to things like Race conditions, etc etc. It probably occurs about 1 our of ten times if i run the strategy continuously.

    I have tried it with both By Strategy Position and Per Entry Execution selected, although I hear IB forces it to strategy position anyway.

    I am able to change the quantity manually after the fact, but I'd really love to be able to let this money maker run without me babysitting it.

    I have attached the log window from the most recent time it happened.

    Here is the related code for this specific entry.


    OnBarUpdate()
    // Long Entry
    if (((MarketType == 0) || (MarketType == 1))
    && (other conditions)
    )
    {
    EnterLong(Contracts, @"breaklongOrder");
    }



    OnOrderUpdate()

    if (order.Name == "breaklongOrder")
    {
    breaklongOrder = order;

    if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
    breaklongOrder = null;
    }


    OnExecutionUpdate()

    //Long
    if (breaklongOrder != null && breaklongOrder == execution.Order)
    {
    if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
    {

    SetProfitTarget(@"breaklongOrder", CalculationMode.Ticks, BreakTarget, true);
    SetStopLoss(@"breaklongOrder", CalculationMode.Ticks, BreakStop, false);

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




    Any ideas? Can I prevent this? Or if not, can I use ChangeOrder to change the quantity of the stop and target orders to equal my actual position quantity?

    Could I do something like....

    if (stopOrder.Quantity != longOrder.Quantity)
    {
    ChangeOrder(stopOrder, longOrder.Quantity, 0, Position.AveragePrice - (10 * TickSize));
    }

    For example?

    Thanks so much for your help!
    Attached Files
    Last edited by RobotSyndicate; 02-23-2018, 05:37 PM. Reason: adding code

    #2
    Hello RobotSyndicate,

    In the log, is the stop loss order originally submitted with correct quantity?

    If so, its not the script that is the issue.

    Please send an email to platformsupport [at] ninjatrader [dot] com so that we may review your log and trace files.


    As a heads up, when using Set methods its always best to set these before an order is placed. Once these are set, they cannot be unset (though this is specific to the from entry signal name used).
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hi, thanks for the reply.

      The order quantity is correct in the log/output widow. For example, it will say entryOrder quantity = 20 and that works every single time, but then it says Quantity = 20 for the stop loss and profit target and it only actually places a partial quantity, somewhere between 1 and 20.

      I have tried using Set, or placing orders with advanced managed orders, same result.

      Comment


        #4
        Update: This problem was occuring using Set() methods in state configure, onbarupdate, onorderupdate, or on execution. It also occured in those on update sections using advanced managed orders.

        It looks like ive fixed it by putting the advanced order methods in onpositionupdate and adding an if statement to make sure we have reached a complete fill. Now it seems to place the full stop/target order.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by bortz, 11-06-2023, 08:04 AM
        47 responses
        1,611 views
        0 likes
        Last Post aligator  
        Started by jaybedreamin, Today, 05:56 PM
        0 responses
        9 views
        0 likes
        Last Post jaybedreamin  
        Started by DJ888, 04-16-2024, 06:09 PM
        6 responses
        19 views
        0 likes
        Last Post DJ888
        by DJ888
         
        Started by Jon17, Today, 04:33 PM
        0 responses
        6 views
        0 likes
        Last Post Jon17
        by Jon17
         
        Started by Javierw.ok, Today, 04:12 PM
        0 responses
        22 views
        0 likes
        Last Post Javierw.ok  
        Working...
        X