Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Exit Order Executes Twice

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

    Exit Order Executes Twice

    Hi,

    I used strategy builder to create the strategy below. The issue is it executes the exit order twice. Which causes me to enter a short position and the strategy to be terminated.

    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;

    if (CurrentBars[0] < 1)
    return;

    // Set 1
    if ((Position.MarketPosition == MarketPosition.Flat)
    // Condition group 1
    && ((BarsSinceExitExecution(0, "", 0) == -1)
    || (BarsSinceExitExecution(0, "", 0) > 1))
    && (Close[0] >= High[1])
    && (Times[0][0].TimeOfDay >= TradeWindowStart.TimeOfDay)
    && (Times[0][0].TimeOfDay < TradeWindowEnd.TimeOfDay))
    {
    EnterLong(Convert.ToInt32(DefaultQuantity), "");
    }

    // Set 2
    if (
    // Condition group 1
    ((Low[0] <= Low[1])
    && (Position.MarketPosition == MarketPosition.Long))
    // Condition group 2
    || ((Position.GetUnrealizedProfitLoss(PerformanceUnit .Ticks, Close[0]) >= ProfitTarget)
    && (Position.MarketPosition == MarketPosition.Long)))
    {
    ExitLong(Convert.ToInt32(DefaultQuantity), "", "");
    }

    }​

    #2
    Hello RiskyBuisness,

    Thank you for your post.

    To understand why the script is behaving as it is, such as placing orders or not placing orders (or drawing objects or other actions) when expected, it is necessary to add prints to the script that print the values used for the logic of the script to understand how the script is evaluating.

    In the strategy add prints (outside of any conditions) that print the values of every variable used in every condition that places an order along with the time of that bar.

    This will print to the output window. Run or backtest the script and when the output from the output window appears save this by right-clicking the output window and selecting Save As... -> give the output file a name and save -> then attach the output text file to your reply.

    Output from prints will appear in the NinjaScript Output window.
    NT8: New -> NinjaScript Output

    The prints should include the time of the bar and should print all values from all variables and all hard coded values in all conditions that must evaluate as true for this action to be triggered. It is very helpful to include labels and operators in the print to understand what is being compared in the condition sets.

    Below I am providing a link to videos that demonstrate adding prints to a script to get further information about the behavior of the script.
    NT8 —


    If you are using the Strategy Builder in NinjaTrader 8, you can also build prints in the Actions window under Misc -> Print.
    NT8 Strategy Builder —


    It is also helpful to set TraceOrders to true in State.Configure.
    TraceOrders will output to the NinjaScript Output window a message when orders are being submitted, ignored, cancelled, or rejected.
    These tools will let you know what happens to the order.
    TraceOrders - https://ninjatrader.com/support/help...raceorders.htm

    I'm also including a link to a forum post with further suggestions on debugging a script.


    Save the output from the output window to a text file. Let me know if you need assistance creating a print or enabling TraceOrders.

    I am happy to assist with analyzing the output from prints and TraceOrders.​

    Comment


      #3
      Attached is the text document from exit conditions. I had to place it in a zip file to be able to attach it.

      Here is a brief rundown of my exit strategy:
      1) Current Market Position is equal to Long AND the low of the current candle is less than or equal to the low of the previous candle

      OR

      2) Current Market Position is equal to Long AND my unrealized profit is greater than or equal to "Profit Target (30 ticks)

      I will say I'm using higher time frame (30min+ candles) so these conditions may occur during the same bar,

      Also, I'm calculating on "Each Tick" and I'm only trading between the hours 10am-1pm EST.
      Attached Files
      Last edited by RiskyBuisness; 07-07-2024, 04:16 PM.

      Comment


        #4
        Hello RiskyBuisness,

        Thank you for your response.

        Have you enabled TraceOrders? I'm not seeing the information from TraceOrders in this output, so it's missing the information of when the orders are actually being submitted.

        Additionally, the prints a bit vague. For example,

        "Current Market Position must be equal to Long"

        Since we are trying to figure out why your exit conditions are evaluating to true and re-submitting an exit order, it would be more helpful to actually print out these values. For example,

        Print(Times[0][0] + " MarketPosition: " + Position.MarketPosition + " == Long ");

        Also, if there are two separate conditions for entry, please make sure there are two separate print statements (1 for each condition, each condition should be 1 line above the respective exit condition).

        Can you also provide which date/time stamp we are looking at? For what timestamp is the exit order being submitted twice?

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        111 views
        0 likes
        Last Post argusthome  
        Started by NabilKhattabi, 03-06-2026, 11:18 AM
        0 responses
        59 views
        0 likes
        Last Post NabilKhattabi  
        Started by Deep42, 03-06-2026, 12:28 AM
        0 responses
        38 views
        0 likes
        Last Post Deep42
        by Deep42
         
        Started by TheRealMorford, 03-05-2026, 06:15 PM
        0 responses
        42 views
        0 likes
        Last Post TheRealMorford  
        Started by Mindset, 02-28-2026, 06:16 AM
        0 responses
        78 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Working...
        X