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

Exiting positions with different take profits

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

    Exiting positions with different take profits

    Hi,

    I have a strategy that calculates some metrics on each bar and decides to enter a short or a long trade with two contracts. I want to implement a feature that allows me to enter a trade on the metrics calculated on two bars ago. In other words, if on bar number 100 I have a signal, on bar number 101 I submit an order, if the order does not get filled I want to submit an order on bar number 102. However, I am facing an issue based on the way that I submit my orders.

    1. Enter two separate orders with different names so I can control where my take profit is using the "SetProfitTarget" method. I use the following code for this purpose:
    Code:
                        EnterLongLimit(1, limitPrice, orderName);
                        SetStopLoss(orderName, CalculationMode.Ticks, StopLossTickSize, true);
                        SetProfitTarget(orderName, CalculationMode.Ticks, TakeProfitTickSize);
                        
                        EnterLongLimit(1, limitPrice, orderName +" 2");
                        SetStopLoss(orderName +" 2", CalculationMode.Ticks, StopLossTickSize, true);
                        SetProfitTarget(orderName +" 2", CalculationMode.Ticks, TakeProfitTickSize + 5);​
    ISSUE: In this way, when the first bar ends and the order is not filled, in the second bar the order is not submitted again. It is worth mentioning that if I enter only one position everything works as expected. The reason that I want to enter positions separately is to have different take profit for each contract.

    2. Enter one trade with two contracts and set the take profit with "ExitShortStopMarket" method. I use the following code for this purpose:
    Code:
                        EnterLongLimit(2, limitPrice, orderName);
                        SetStopLoss(orderName, CalculationMode.Ticks, StopLossTickSize, true);
    
                        double tp1 = limitPrice + 5 * TickSize;
                        double tp2 = limitPrice + 10 * TickSize;
                        ExitLongStopMarket(1, tp1, " tp1", orderName);
                        ExitLongStopMarket(1, tp2, " tp2", orderName);​​
    ISSUE: In this way, the entry on the second bar from the bar that generates the signal works properly but the exit orders are not submitted/triggered and the position always exits at the stoploss.

    So my questions are:
    • Why aren't the orders submitted on the second bar when they are not filled on the first bar when I enter two separate positions?
    • How can I enter a position with multiple contracts but exit them with different take profits?

    Regards,
    Pouya

    #2
    Hello Pouya,

    Thank you for your post.

    First of all, I see that you are calling SetStopLoss() and SetProfitTarget() after your entry methods, and it is very important that you call the Set() methods before your entry method. It is stated in the help guide as well that, "Since they are submitted upon receiving an execution, the Set method should be called prior to submitting the associated entry order to ensure an initial level is set."Additionally, Set() methods may not be combined with non-market Exit() orders, such as ExitLongStopMarket() or ExitShortStopMarket(). This is because the Managed Approach follows internal order handling rules that prevent both orders from being active at the same time; either the order from the Set() method would be ignored or the Exit() order would be ignored. The rules may be found here:
    https://ninjatrader.com/support/help...rnalOrderHandl ingRulesThatReduceUnwantedPositions
    • Why aren't the orders submitted on the second bar when they are not filled on the first bar when I enter two separate positions?
      • Ultimately, you can get more details about the behavior of orders and why they are or are not submitted as expected by enabling TraceOrders and adding Print() statements to your strategy. This sends information to the NinjaScript Output window that you can use to debug and better understand your strategy's behavior. For more information on using these debugging tools:
      • https://support.ninjatrader.com/s/ar...language=en_US
    • How can I enter a position with multiple contracts but exit them with different take profits?
      • To enter a position with multiple contracts and exit with different stops and/or targets, you must separate the entry orders with different signal names. What you attempted in your first snippet might work if you were to move the SetStopLoss() and SetProfitTarget() methods before your entry methods. We have a reference sample related to scaling out of a position here:
      • https://ninjatrader.com/support/help...a_position.htm
    Please review the provided resources and feel free to reach out with any additional questions or concerns.
    Emily C.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by drnoggin, Today, 12:19 PM
    1 response
    13 views
    0 likes
    Last Post NinjaTrader_Jesse  
    Started by synthhokie, Today, 12:00 PM
    1 response
    16 views
    0 likes
    Last Post NinjaTrader_Jesse  
    Started by moneyexe, Today, 11:22 AM
    1 response
    12 views
    0 likes
    Last Post NinjaTrader_Jesse  
    Started by stevec1824, Today, 11:00 AM
    2 responses
    8 views
    0 likes
    Last Post stevec1824  
    Started by sofortune, Today, 10:05 AM
    2 responses
    14 views
    0 likes
    Last Post sofortune  
    Working...
    X