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 CarlTrading, 03-31-2026, 09:41 PM
    1 response
    128 views
    1 like
    Last Post NinjaTrader_ChelseaB  
    Started by CarlTrading, 04-01-2026, 02:41 AM
    0 responses
    73 views
    1 like
    Last Post CarlTrading  
    Started by CaptainJack, 03-31-2026, 11:44 PM
    0 responses
    116 views
    2 likes
    Last Post CaptainJack  
    Started by CarlTrading, 03-30-2026, 11:51 AM
    0 responses
    109 views
    1 like
    Last Post CarlTrading  
    Started by CarlTrading, 03-30-2026, 11:48 AM
    0 responses
    88 views
    0 likes
    Last Post CarlTrading  
    Working...
    X