• 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.


No announcement yet.

Partner 728x90


Short after TakeProfit opens 2 Positions with IB

  • Filter
  • Time
  • Show
Clear All
new posts

    Short after TakeProfit opens 2 Positions with IB

    Hi guys,
    I need your help in solving my problem with order handling.

    My scenario: If a "take profit" is reached, I want to open a new short position.

    As simple as it sounds, but it always opens 2 shorts and I can't find a mistake. In the backtest everything works fine, so I guess it is the maybe the connection between IB and NT via the gateway?

    Click image for larger version  Name:	fehler.PNG Views:	1 Size:	12.4 KB ID:	1040892
    In my picture there are one target, one close and one RON. Why is the "close" there, it should has been closed already?

    Here is my code:
    protected override void OnOrderUpdate( Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError ) {
                if ( order.Name == "Profit target" && order.OrderState == OrderState.Filled && order.OrderAction == OrderAction.Sell ) {
                        enteredShort = true;
                        EnterShort(0, DefaultQuantity, "RON" );    
                        SetStopLoss( "RON", CalculationMode.Percent, SL/100, false );
                        SetProfitTarget( "RON", CalculationMode.Percent, TP/100);
                        Print("Entered Short");
    Is there anything wrong? By the way: One Position is unmanaged after that entry. The sets just work on the RON. In my picture I closed the wrong position on the second candle.

    A penny for your thoughts!
    Last edited by bennle; 11-26-2018, 08:45 AM.

    Hello bennle,

    Thanks for your post.

    I spot a couple issues with the code written.

    1.) Stop and target orders are being submitted after the entry order. As a best practice, we suggest submitting Set methods when the strategy position is flat before the next entry methods are fired. (NinjaTrader Set methods "prep" NinjaTrader so when the next associated Entry method comes back as filled, then the Profit Target/Stop loss orders will fire.

    2.) Order updates for OrderState.Filled from OnOrderUpdate may be fired before the Execution of the order in OnExecutionUpdate. The strategy may be trying to reverse a position it thinks it has due to the Managed Approach Internal rules because the execution is not seen. We would recommend using OnExecutionUpdate for these purposes.

    If you are using OnOrderUpdate, I would recommend adapting the strategy to handle orders similarly to the SampleOnOrderUpdate strategy which demonstrates using Order objects, OnOrderUpdate and OnExecutionUpdate(). It also shows how Exit methods can be used instead of SetProfitTarget and SetStopLoss. If you have any issues taking the approach outlined in SampleOnOrderUpdate, please let us know.

    SamplePriceModification(demonstrates placing Set methods when flat and updating Stop loss/Profit target) - https://ninjatrader.com/support/help...of_stop_lo.htm

    SampleOnOrderUpdate (demonstrates OnOrderUpdate/OnExecutionUpdate and advanced order handling) - https://ninjatrader.com/support/help...and_onexec.htm

    OnOrderUpdate (See notes) - https://ninjatrader.com/support/help...rderupdate.htm

    OnExecutionUpdate (See notes) - https://ninjatrader.com/support/help...tionupdate.htm

    Managed Approach (Included for internal rules reference) - https://ninjatrader.com/support/help...d_approach.htm

    The help guide information provided is publicly available.

    Please let us know if we can be of further assistance.
    JimNinjaTrader Customer Service


    Latest Posts


    Topics Statistics Last Post
    Started by ATMtrader0001, Today, 01:27 PM
    1 response
    Last Post NinjaTrader_ChelseaB  
    Started by ATMtrader0001, Today, 01:16 PM
    1 response
    1 view
    Last Post NinjaTrader_ChelseaB  
    Started by elnoppo, Today, 01:25 PM
    2 responses
    Last Post NinjaTrader_ChelseaB  
    Started by FabianoTrader, Today, 04:00 PM
    2 responses
    Last Post FabianoTrader  
    Started by FabianoTrader, Today, 03:53 PM
    0 responses
    Last Post FabianoTrader