Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

BarsSinceExit - avoid multiple closing trades

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

    BarsSinceExit - avoid multiple closing trades

    Dear all,

    I have the current setup:
    - Open simultaneously 3 positions (S1, S2, S3), based on a channel ("Box").
    - If price goes against me and crosses the middle of the box, I close one position at the time. If it happens three times, all positions are closed.

    Initially, I used a "counter", set at 0 when no trades are closed, then 1 if S1 is closed, 2 if S2 is closed but it doesnt work with an object based code.
    I am now using the BarsSinceExit but I am still failing.

    Please find below the code extract and also attached a screenshot that shows that S1 and S2 are closed simultaneously and not one after the other.

    // Partial close of short position S1 & then S2
    if ((Close[1] < midboxSize) && (Close[0] >= midboxSize) &&
    (Position.MarketPosition == MarketPosition.Short))
    // && (tradeShClosingCounter == 0))
    {
    ExitShort(
    "S1");
    //tradeShClosingCounter = tradeShClosingCounter + 1;
    }
    if ((Close[1] < midboxSize) && (Close[0] >= midboxSize) && (BarsSinceExit(0,"S1",0) > 2) &&
    (Position.MarketPosition == MarketPosition.Short))
    // && (tradeShClosingCounter == 1))
    {
    ExitShort(
    "S2");
    //tradeShClosingCounter = tradeShClosingCounter + 1;
    }

    Thank you,
    Best

    Nicolas
    Attached Files

    #2
    The code you have here is triggering both exits at the same time since bars since entry is greater than 2 for both situations.

    Your initial thought of using a counter should work as long as you are resetting it properly after all three trades exit.

    You may want to add Print() statements throughout your code to ensure the tracking variable is being set as expected.
    LanceNinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    110 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
    37 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    41 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