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

BasInProgress not allowing stop loss to be placed until bar close

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

    BasInProgress not allowing stop loss to be placed until bar close

    I have the following code layout and for some reason any trade that should be stopped on the first bar isn't until the bars close

    Code:
    protected override void Initialize()
    {
       ...................GENERIC CODE CUT HERE...................
                Add("ES 12-10", PeriodType.Minute, 1);
                Add("YM 12-10", PeriodType.Minute, 1);
                Add("NQ 12-10", PeriodType.Minute, 1);
                SetStopLoss("Long", CalculationMode.Ticks, 16, false);
                   SetProfitTarget("Long", CalculationMode.Ticks, 70);
                SetStopLoss("Short", CalculationMode.Ticks, 15, false);
                   SetProfitTarget("Short", CalculationMode.Ticks, 80);
     }
    
    protected override void OnBarUpdate()
            {    
                if(BarsInProgress == 0){
                    tfTrade();                
                }
                if(BarsInProgress == 1){
                    esTrade();                
                }
                if(BarsInProgress == 2){
                    ymTrade();                
                }
                if(BarsInProgress == 3){
                    nqTrade();    
                   if(something)
                    EnterLong(0, Quantity, "Long");
                    else
                    EnterShort(0, Quantity, "Short");
                }
    Basically the system trades the TF based on data from other markets. Everything functions perfectly except for setting the stop/target

    I noticed that if I don't enter the trade from the last BarsInProgress (in this case 3) that it will enter the trade a bar late. However when doing this the stop doesn't become active for an extra bar. When I do everything from BarsInProgress == 0 there is no problem. However I would like to use this extra market data if possible.

    Am I missing something obvious?

    #2
    Hello shazzmoe,

    Set statements should automatically be submitted upon entry execution. To track this down, can you please let us know the following:
    • Are you doing this in real time or a backtest?
    • What is your CalculateOnBarClose setting?
    • What is the interval used for TF?
    • Do you have a screenshot available showing the entry and stop loss timing you're seeing?
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      shazzmoe, thanks for the post - could you please attach your TraceOrders output for us to review here?

      Also: on which NT release are you working currently? It could be checked under Help > About.

      Thanks,
      BertrandNinjaTrader Customer Service

      Comment


        #4
        1. Yes it is in a backtest. Even if it would function properly in real time I need a way to backtest properly
        2. calconbarclose = true
        3. all instruments used are on 1 minute bars
        4. I attached a screenshot of two examples where it doesn't stop on the first bar and one screen shot of how it works properly after the first bar closes
        Attached Files

        Comment


          #5
          Thanks shazzmoe, would you mind attaching the TraceOrders output as well so we could review the order placement event sequence you get?
          BertrandNinjaTrader Customer Service

          Comment


            #6
            attached is the trace order for a period of time that I know had some major moves (thus stops were more often hit on the first bar)

            9/19/2008 is the first one I see. From looking over all the trace files it seems that it amends the stop to the initialized values after the first bar.

            I've also noticed that a day like 9/30/2008 cancelled the all orders and was stopped out on the first bar at a much larger value. No where in my code is a setstoploss value called outside of the initialized area I've shown.
            Attached Files

            Comment


              #7
              Thanks for the details, shazzmoe. It looks like the one you highlighted is due to a limitation of end of session handling with multiseries scripts. You may see improvement by setting ExitOnClose = false.
              Ryan M.NinjaTrader Customer Service

              Comment


                #8
                ok does this mean there is no known fix for this issue? I haven't tested setting ExitOnClose to false yet as my strategy requires it to properly run.

                Do you think setting a time exit would fix this issue?

                Comment


                  #9
                  Unfortunately there is no fix available. For backtesting, we do not recommend submitting orders to the last session bar if using ExitOnClose = true or TIF = day because of this. Real time trading with COBC = false does not have this limitation.

                  Custom coding your own end of session handling based on a time is a good work-around.
                  Ryan M.NinjaTrader Customer Service

                  Comment


                    #10
                    I don't think this was the issue. For testing I'm now having it exit 5 bars after entering and still get the same issues.

                    The trades don't fire at the markets close but 15 minutes before and will always exit before the sessions end.

                    I do think the issue is somewhere within the order of processing multiple data series in historical data

                    Comment


                      #11
                      I also just discovered a bug with the strategy analyzer

                      If you're connected to the market replay feed and run a test it will not test the proper period of time.
                      In my case i was testing 9/1/2008-10/31/2008 however it ran the test from 9/1/2008-present

                      Comment


                        #12
                        OK - Thanks for the update. Will give it a run here with your updated code snippet. Can you please attach along with a screenshot of the settings used?
                        Ryan M.NinjaTrader Customer Service

                        Comment


                          #13
                          oddly enough I just fixed my issue by placing the entry orders outside of any BarsInProgress statement. I appreciate all the assistance though

                          Or were you wanting screenshots of the incorrect testing dates when paired with the market replay?

                          Comment


                            #14
                            OK, glad you sorted out the original issue. On the new one with backtesting market replay, I'm not seeing the same thing here. Try with SampleMACrossOver strat and let us know if you're seeing the same thing.

                            Does the end date on the summary tab match what you selected in the backtest settings?
                            Ryan M.NinjaTrader Customer Service

                            Comment


                              #15
                              I haven't yet had time to go back in and recreate the issue but I do have one question about adding data series. Currently I would do the following to add the series I want and it will work just fine.

                              Add("ES 06-11", PeriodType.Minute, 1);
                              Add("YM 06-11", PeriodType.Minute, 1);
                              Add("NQ 06-11", PeriodType.Minute, 1);

                              However is there a way to avoid hard coding the data series so that when rollover day comes I don't have to recode the program. Perhaps a way to append a string based on the end portion of the parent data series.

                              EX.
                              if the chart is running "TF 09-11"
                              String Contract = "09-11"

                              Add("ES "+Contract, PeriodType.Minute, 1);

                              -----------------
                              Is something like this possible? Or is it worth my time looking into C# strings?

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by jaybedreamin, Today, 05:56 PM
                              0 responses
                              3 views
                              0 likes
                              Last Post jaybedreamin  
                              Started by DJ888, 04-16-2024, 06:09 PM
                              6 responses
                              18 views
                              0 likes
                              Last Post DJ888
                              by DJ888
                               
                              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
                              6 views
                              0 likes
                              Last Post Javierw.ok  
                              Started by timmbbo, Today, 08:59 AM
                              2 responses
                              10 views
                              0 likes
                              Last Post bltdavid  
                              Working...
                              X