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

Strategy enters at bar close instead of next bar open

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

    Strategy enters at bar close instead of next bar open

    Hello,

    Someone please help! I've been trying to solve this for a few weeks and no luck.
    I have an automated strategy that works fine with minute charts. When I set "calculate on bar close" the strategy will make all needed calculations and if there's setup it will enter on next bar open. Perfect.

    However when I change to Range 4 chart my strategy will enter on the current bar close!!! And the difference is significant (sometimes you could have a few minutes difference in close and open times for two adjacent range 4 bars).

    Please!!! How to make my strategy WAIT and enter on the next bar?

    By the way in demo mode it will enter on the next bar as it supposed to be! What could be the problem? Obviously I have "calculate on bar close" selected.

    Please help.

    #2
    Hello,

    Thank you for the post.

    I would be unsure of the results without being able to run the strategy and see what you are. Would you be able to provide a sample for testing?

    One item I was not sure of based on the information, are you currently using any secondary series in the script or only a single series?

    Also is this Backtest or realtime you are referring to?

    In general a single series script should execute its logic on each bar close with CalculateOnBarClose set to true.


    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Hi Jesse,

      Thank you very much for your reply! Being not a programmer myself it is very hard for me to understand your questions. However, may I paste my simple code for illustration of my problem? In regards to your last question, backtests work just fine - the trade is entered on the next bar open; however, in realtime the trade is entered on the close of the current bar or even in the middle of it. It will not wait until next bar open as I want it to. This problem occur when I use range charts. With minute charts as I noticed both realtime and backtest work fine. Please help. The problem is with range charts! But I need to use range charts for my trading. Thank you in advance!

      I attach the code below in txt format.

      Originally posted by NinjaTrader_Jesse View Post
      Hello,

      Thank you for the post.

      I would be unsure of the results without being able to run the strategy and see what you are. Would you be able to provide a sample for testing?

      One item I was not sure of based on the information, are you currently using any secondary series in the script or only a single series?

      Also is this Backtest or realtime you are referring to?

      In general a single series script should execute its logic on each bar close with CalculateOnBarClose set to true.


      I look forward to being of further assistance.
      Attached Files

      Comment


        #4
        Hello,

        Thank you for the reply.

        What you are experiencing sounds like the differences you would see between Backtesting and Realtime data.

        In general during a backtest, all logic is evaluated on the bar close, any executions that were evaluated during that close would be listed on the next bar. In realtime, the execution should be listed on the bar that closes. You can find more about the differences between these two modes here:

        In realtime, if you are using CalculateOnBarClose = true, you should still be seeing the executions on the bar close but you mentioned you are seeing intrabar trades. I do not see this being possible with the logic in the file provided while using CalculateOnBarClose = true. Can you provide an image of the difference you are seeing?

        For a trade to be entered on the bar open instead of the close, you would need to specifically program the strategy to do that. Generally the First tick of the bar is used in this case to place a trade on the first tick of the bar. That would assume you are using CaclualteOnBarClose = false though and would ultimately work differently than the Backtest would a backtest only uses CalculateOnBarClose = true.

        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Hi Jesse,

          Thank you for your help! Following your advice I have included if (FirstTickOfBar) and changed all entry conditions to [1] instead of [0].

          Now when I run backtest I see that all trades actually happen at the open of the 2nd bar after signal (instead of next after signal) but I believe this is not an error, is just because backtest will use COBC at all times, but in my live trading it should execute just as I want it on the open of the next after signal bar?

          Jesse, just one question please should I list if (FirstTickofBar) before each of my entry conditions? (I have several entry conditions, or just stating it once will be sufficient and it will be assumed that all entry conditions will follow the same logic?


          Originally posted by NinjaTrader_Jesse View Post
          Hello,

          Thank you for the reply.

          What you are experiencing sounds like the differences you would see between Backtesting and Realtime data.

          In general during a backtest, all logic is evaluated on the bar close, any executions that were evaluated during that close would be listed on the next bar. In realtime, the execution should be listed on the bar that closes. You can find more about the differences between these two modes here:

          In realtime, if you are using CalculateOnBarClose = true, you should still be seeing the executions on the bar close but you mentioned you are seeing intrabar trades. I do not see this being possible with the logic in the file provided while using CalculateOnBarClose = true. Can you provide an image of the difference you are seeing?

          For a trade to be entered on the bar open instead of the close, you would need to specifically program the strategy to do that. Generally the First tick of the bar is used in this case to place a trade on the first tick of the bar. That would assume you are using CaclualteOnBarClose = false though and would ultimately work differently than the Backtest would a backtest only uses CalculateOnBarClose = true.

          I look forward to being of further assistance.

          Comment


            #6
            Hello,

            It would depend on the logic you are using if you should include the first tick of bar condition. What i mean is that if the overall condition becomes true before the first tick of the bar, the entry would not be submitted. Then if the overall condition is no longer true during the first tick of the bar it would not be submitted.

            This would likely require that you test whatever logic you end up with to determine if it is or is not working how you want it to. I wouldn't be able to say what it would do in realtime at this point with the modifications. In general if you are checking for the First tick of the bar, you could execute orders on the First tick of the bar rather than the Close of the bar which is what you had intended to do.

            I would suggest running this in realtime on the sim account or use the MarketReplay to test what the realtime executions are like. If they are working correctly then you have solved it, otherwise if it is not working per expectation please let me know and we can continue to work through the problem.

            I look forward to being of further assistance.
            JesseNinjaTrader Customer Service

            Comment


              #7
              Hi Jesse,

              Well now my strategy will just shoot 3 separate orders at the first tick of bar. So it enters first trade correctly, but it suppose to wait for other conditions to occur before entering. These conditions could never be on the same bar. So now I got another problem which looks even worse to me.

              What I am struggling with is that in real-time simulation mode the strategy without even twicking it works just perfect. It enters on the next bar after signals (range chart). However when I run it on live it will enter on the close of the current bar. And that could be many minutes before actual entry.

              You suggest to run it in market replay. I would love to try that, but is there market replay with range charts?

              I am not getting anywhere with this now. Can I somehow limit the number of possible trades per bar?



              Originally posted by NinjaTrader_Jesse View Post
              Hello,

              It would depend on the logic you are using if you should include the first tick of bar condition. What i mean is that if the overall condition becomes true before the first tick of the bar, the entry would not be submitted. Then if the overall condition is no longer true during the first tick of the bar it would not be submitted.

              This would likely require that you test whatever logic you end up with to determine if it is or is not working how you want it to. I wouldn't be able to say what it would do in realtime at this point with the modifications. In general if you are checking for the First tick of the bar, you could execute orders on the First tick of the bar rather than the Close of the bar which is what you had intended to do.

              I would suggest running this in realtime on the sim account or use the MarketReplay to test what the realtime executions are like. If they are working correctly then you have solved it, otherwise if it is not working per expectation please let me know and we can continue to work through the problem.

              I look forward to being of further assistance.

              Comment


                #8
                Hello,

                Thank you for the reply

                Originally posted by SNIP07 View Post
                Hi Jesse,

                Well now my strategy will just shoot 3 separate orders at the first tick of bar. So it enters first trade correctly, but it suppose to wait for other conditions to occur before entering. These conditions could never be on the same bar. So now I got another problem which looks even worse to me.
                If the conditions are happening at all, that would mean they had equated true somehow. To address why this is happening you would need to use Print statements with the conditions you are using now. In some way the condition controlling the second and third order would have needed to become true in order to submit the orders.

                What I am struggling with is that in real-time simulation mode the strategy without even twicking it works just perfect. It enters on the next bar after signals (range chart). However when I run it on live it will enter on the close of the current bar. And that could be many minutes before actual entry.
                I am not sure I understand this statement, are you comparing a Live account to a Sim account here or are we still talking about backtest vs realtime still?

                You suggest to run it in market replay. I would love to try that, but is there market replay with range charts?
                Yes you can run tick based charts in MarketReplay. If you are using an instrument you can not download data for, you can also enable recording of live data for market replay in the Tools -> Options -> Data tab.

                I look forward to being of further assistance.
                JesseNinjaTrader Customer Service

                Comment


                  #9
                  Hi Jesse,

                  By following your advice it seems like I got it all fixed and working now! Thank you very much!



                  Originally posted by NinjaTrader_Jesse View Post
                  Hello,

                  Thank you for the reply



                  If the conditions are happening at all, that would mean they had equated true somehow. To address why this is happening you would need to use Print statements with the conditions you are using now. In some way the condition controlling the second and third order would have needed to become true in order to submit the orders.



                  I am not sure I understand this statement, are you comparing a Live account to a Sim account here or are we still talking about backtest vs realtime still?



                  Yes you can run tick based charts in MarketReplay. If you are using an instrument you can not download data for, you can also enable recording of live data for market replay in the Tools -> Options -> Data tab.

                  I look forward to being of further assistance.
                  Last edited by SNIP07; 06-15-2016, 08:54 PM.

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by manitshah915, Today, 12:59 PM
                  1 response
                  3 views
                  0 likes
                  Last Post NinjaTrader_Erick  
                  Started by ursavent, Today, 12:54 PM
                  1 response
                  3 views
                  0 likes
                  Last Post NinjaTrader_Jesse  
                  Started by cre8able, Today, 01:01 PM
                  1 response
                  4 views
                  0 likes
                  Last Post NinjaTrader_ChelseaB  
                  Started by Mizzouman1, Today, 07:35 AM
                  3 responses
                  17 views
                  0 likes
                  Last Post NinjaTrader_Gaby  
                  Started by RubenCazorla, Today, 09:07 AM
                  2 responses
                  13 views
                  0 likes
                  Last Post NinjaTrader_ChelseaB  
                  Working...
                  X