Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

multi instrument exit after x bars problem

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

    multi instrument exit after x bars problem

    Hi,
    I have 2 orders, 1 long, one short, and wish to hold each for a different number of bars. I can't get the short trade to exit. Can you have a look?

    else if (State == State.Configure)
    {
    AddDataSeries("ES 12-22", Data.BarsPeriodType.Minute, 15, Data.MarketDataType.Last);
    }
    }

    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;

    if (CurrentBars[0] < 1)
    return;

    // Set 1
    if (
    ToTime(Time[0]) == FirstBarCloseTime

    && (Close[0] > Open[0]))
    {
    EnterLong(0, 1, @"nqLong");
    EnterShort(1, 1, @"esShort");
    }

    // Set 2
    if (BarsSinceEntryExecution(0, "", 0) >= BarsTofHoldNq)
    {
    ExitLong(@"nqLong");
    }

    // Set 3
    if (BarsSinceEntryExecution(0, "", 0) >= BarsToHoldEs)
    {
    ExitShort(@"esShort");
    }

    }​
    Last edited by trader3000a; 11-19-2022, 08:31 AM.

    #2
    Hello trader3000a,

    Thanks for your post.

    If the Exit method to exit the short order is not triggering, this likely means the condition to call ExitShort() is not becoming true.

    In the code you shared, you are checking if the number of bars that have passed on the primary series since the entry execution is greater than or equal to BarsToHoldEs. If you want to check the BarsSinceEntryExecution on the added series, you would need to pass in a barsInProgress argument of 1. And, specify the signalName argument of the short Entry order in your BarsSinceEntryExecution condition.

    BarsSinceEntryExecution(int barsInProgressIndex, string signalName, int entryExecutionsAgo)

    From the BarsSinceEntryExecution help guide: When working with a multi-series strategy the BarsSinceEntryExecution() will return you the elapsed bars as determined by the first Bars object for the instrument specified by the barsInProgressIndex.

    Also, I see that you are only making sure there are enough bars processing for the primary data series. You also need to add a CurrentBars check for the added series to make sure the added series has enough bars loaded.

    See this help guide page about CurrentBars and sample code: https://ninjatrader.com/support/help...urrentbars.htm

    Note that when using BarsSinceEntryExecution you should also add a condition that checks '|| BarsSinceEntryExecution() == -1)' as seen in the help guide.

    See this help guide page about BarsSinceEntryExecution and sample code: https://ninjatrader.com/support/help...yexecution.htm

    Please also review this help guide about working with Multi-Timeframe/Multi-Instrument NinjaScripts: https://ninjatrader.com/support/help...nstruments.htm

    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 NullPointStrategies, 03-13-2026, 05:17 AM
    0 responses
    86 views
    0 likes
    Last Post NullPointStrategies  
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    151 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    79 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    52 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    59 views
    0 likes
    Last Post TheRealMorford  
    Working...
    X