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 charlesugo_1, 05-26-2026, 05:03 PM
    0 responses
    51 views
    0 likes
    Last Post charlesugo_1  
    Started by DannyP96, 05-18-2026, 02:38 PM
    1 response
    142 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Started by CarlTrading, 05-11-2026, 05:56 AM
    0 responses
    160 views
    0 likes
    Last Post CarlTrading  
    Started by CarlTrading, 05-10-2026, 08:12 PM
    0 responses
    96 views
    0 likes
    Last Post CarlTrading  
    Started by Hwop38, 05-04-2026, 07:02 PM
    0 responses
    275 views
    0 likes
    Last Post Hwop38
    by Hwop38
     
    Working...
    X