• 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

Short after TakeProfit opens 2 Positions with IB

Collapse
X
  • 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:
    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, 09:45 AM.

    #2
    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

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by psquare, Today, 03:11 PM
    4 responses
    16 views
    0 likes
    Last Post psquare
    by psquare
     
    Started by ballboy11, Today, 03:20 PM
    1 response
    12 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Started by tonynt, Today, 02:18 PM
    1 response
    10 views
    0 likes
    Last Post NinjaTrader_JoshG  
    Started by denalek2419, Today, 01:46 PM
    3 responses
    12 views
    0 likes
    Last Post NinjaTrader_ChristopherJ  
    Started by REI140205, Today, 12:58 PM
    1 response
    11 views
    0 likes
    Last Post NinjaTrader_Jim  
    Working...
    X