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

calculate on bar close multi series strategy

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

    calculate on bar close multi series strategy

    Hi Ninjatraders, I'm having a problem that I'm guessing has a simple solution. I have a strategy with two data series. In a backtest that always calculates on bar close I get my desired effect with this


    Code:
     
                     if (BarsInProgress == 0
    	        && Position.MarketPosition == MarketPosition.Flat)
                {
                     EnterShort(0, 4, "Short");
                     EnterLong(1, 2, "Long");    
                }
    After a close of a bar in the primary series the short happens, after that it waits till the close of a bar in the second series to go long the second series.

    My problem is that testing in real-time the strategy waits till the close of a bar in the primary series and then enters both positions simultaneously.

    I put calculateonbarclose= true in the initialize, and set it to true in the strategy tab settings. Still when live the orders go in simultaneously.
    Last edited by sampras010; 11-14-2017, 06:10 AM.

    #2
    Hello sampras010,

    Thank you for the post.

    When you call the EnterLong on the secondary series, the script will not wait for the secondary series bar to close, all this override does is give you the ability to "point" to the secondary series and say "place an order on this instrument".

    To wait until the close of the bar in the second series, check the BarsInProgress property for BarsInProgress == 1, then enter long.


    Code:
    if (BarsInProgress == 0 && Position.MarketPosition == MarketPosition.Flat) {
    
            EnterShort(0, 4, "Short");
    
    }
    
    if (BarsInProgress == 1) {
    
            EnterLong(1, 2, "Long");
    
    }
    This help guide link describes in detail the advanced order method overrides in detail:


    Please also take a look at this help guide page which explains in detail how multi-time frame/multi-instruments scripts process their data:


    If we may be of any further assistance, please let us know.
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Is there a way with a nested if to enter both orders in the same block of code?

      I'm trying to capture the arb between the closes of each series with a pairs trade. So Ideally there would be a check on the first series to enter long at bar close, and then that would trigger a check on the second series to enter short the next immediate close of the second series.

      I read the section on advanced order handling and multiscript and it appears the orders would still go in at the same exact time unless I called barsinprogress separately for each series like ChrisL suggested, but then I don't see how to get the pairs trade aspect to work. I tried this, or can it be done with on execution?

      Code:
                   if (BarsInProgress == 0)
                  {
                       EnterLong(0, 5, "Long");
      					
      		                     if (BarsInProgress==1)
      				{
      					EnterShort(1, 10, "Short");
      				}
      					
                  }
      Last edited by sampras010; 11-18-2017, 04:06 AM.

      Comment


        #4
        Hello,

        Thanks for the reply.

        Let's say the two series are ES(primary) and CL(secondary) 1 minute.

        If your script is set to calculate OnBarClose, the ES OnBarUpdate() will execute, then right after that your CL OnBarUpdate will be called, that is the way multi-series scripts are processed; Primary first then secondary. You couldn't check

        if (BarsInProgress==1)

        inside of

        if (BarsInProgress==0)

        because the condition would never become true, BarsInProgress will not change for the duration of a OnBarUpdate() call.

        As long as both instrument series have the same time interval, knowing that the order of OnBarUpdate() is ES then CL every time, you should be able to execute your long and short orders the way you want to.

        Possibly it could help you to set up a boolean switch to make sure that you, in fact, entered long in your primary series?

        Code:
        private bool primary_short = false;
        
        if (BarsInProgress == 0 && Position.MarketPosition == MarketPosition.Flat) {
        
                if(<entry condition>){
                EnterShort(0, 4, "Short");
                primary_short = true;
                }
        
        }
        
        if (BarsInProgress == 1) {
        
                if(primary_short){
                    EnterLong(1, 2, "Long");
                }
        
        }
        Please let me know if you have any questions on the multi-timeframe/multi-instrument section of the help guide that I can clear up.
        Chris L.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Jon17, Today, 04:33 PM
        0 responses
        1 view
        0 likes
        Last Post Jon17
        by Jon17
         
        Started by Javierw.ok, Today, 04:12 PM
        0 responses
        4 views
        0 likes
        Last Post Javierw.ok  
        Started by timmbbo, Today, 08:59 AM
        2 responses
        10 views
        0 likes
        Last Post bltdavid  
        Started by alifarahani, Today, 09:40 AM
        6 responses
        41 views
        0 likes
        Last Post alifarahani  
        Started by Waxavi, Today, 02:10 AM
        1 response
        20 views
        0 likes
        Last Post NinjaTrader_LuisH  
        Working...
        X