Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strategy closes all positions instead of just the runner

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

    Strategy closes all positions instead of just the runner

    I have a script that should only close my runner, but it closes all positions at once. Does something look off here? Appreciate your help!


    //Close the runner on 13 and 9 cross
    if (b_4 != null && b_4.OrderState == OrderState.Filled && (b_3 == null || b_3.OrderState != OrderState.Filled))
    {
    if (ema_9[1] < ema_13[1])
    {
    //Print("Close all on 4th runner buy");
    ExitLong();
    b_1 = null;
    b_2 = null;
    b_3 = null;
    b_4 = null;

    take_1 = null;
    take_2 = null;
    take_3 = null;
    take_4 = null;

    }
    }
    if (s_4 != null && s_4.OrderState == OrderState.Filled && (s_3 == null || s_3.OrderState != OrderState.Filled))
    {
    if (ema_9[1] > ema_13[1])
    {
    //Print("Close all on 4th runner sell");
    ExitShort();
    s_1 = null;
    s_2 = null;
    s_3 = null;
    s_4 = null;

    take_1 = null;
    take_2 = null;
    take_3 = null;
    take_4 = null;

    #2
    Hello koop1975,

    Thanks for your post.

    I do not see anything specifically wrong in the code you shared. If a script is not behaving as expected, debugging prints should be added to the script to understand how it is behaving.

    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.

    Prints will appear in the NinjaScript Output window (New > NinjaScript Output window).

    Below is a link to a forum post that demonstrates how to use prints to understand behavior.

    https://ninjatrader.com/support/foru...121#post791121

    Let us know if we may assist further.​​
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
    0 responses
    560 views
    0 likes
    Last Post Geovanny Suaza  
    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
    0 responses
    325 views
    1 like
    Last Post Geovanny Suaza  
    Started by Mindset, 02-09-2026, 11:44 AM
    0 responses
    101 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
    0 responses
    547 views
    1 like
    Last Post Geovanny Suaza  
    Started by RFrosty, 01-28-2026, 06:49 PM
    0 responses
    547 views
    1 like
    Last Post RFrosty
    by RFrosty
     
    Working...
    X