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 SalmaTrader, 07-07-2026, 10:26 PM
    0 responses
    45 views
    0 likes
    Last Post SalmaTrader  
    Started by CarlTrading, 07-05-2026, 01:16 PM
    0 responses
    22 views
    0 likes
    Last Post CarlTrading  
    Started by CaptainJack, 06-17-2026, 10:32 AM
    0 responses
    14 views
    0 likes
    Last Post CaptainJack  
    Started by kinfxhk, 06-17-2026, 04:15 AM
    0 responses
    20 views
    0 likes
    Last Post kinfxhk
    by kinfxhk
     
    Started by kinfxhk, 06-17-2026, 04:06 AM
    0 responses
    22 views
    0 likes
    Last Post kinfxhk
    by kinfxhk
     
    Working...
    X