Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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.​
    Brandon H.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by wuannetraam, Today, 12:22 AM
    0 responses
    9 views
    0 likes
    Last Post wuannetraam  
    Started by TGOG1, Yesterday, 03:02 PM
    2 responses
    22 views
    0 likes
    Last Post TGOG1
    by TGOG1
     
    Started by AlgoDreamer, Yesterday, 06:12 PM
    0 responses
    8 views
    0 likes
    Last Post AlgoDreamer  
    Started by Rafaelseki, Yesterday, 03:36 PM
    0 responses
    9 views
    0 likes
    Last Post Rafaelseki  
    Started by rdlans, Yesterday, 05:42 AM
    2 responses
    26 views
    0 likes
    Last Post rdlans
    by rdlans
     
    Working...
    X