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 judysamnt7, 03-13-2023, 09:11 AM
    4 responses
    59 views
    0 likes
    Last Post DynamicTest  
    Started by ScottWalsh, Today, 06:52 PM
    4 responses
    36 views
    0 likes
    Last Post ScottWalsh  
    Started by olisav57, Today, 07:39 PM
    0 responses
    7 views
    0 likes
    Last Post olisav57  
    Started by trilliantrader, Today, 03:01 PM
    2 responses
    21 views
    0 likes
    Last Post helpwanted  
    Started by cre8able, Today, 07:24 PM
    0 responses
    10 views
    0 likes
    Last Post cre8able  
    Working...
    X