Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Ninjascript Intra-bar trades

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

    Ninjascript Intra-bar trades

    Hi friends,

    I been searching Google on how to enter a trade as soon as my conditions are satisfied.
    Basically, I'm entering a trade as soon as there has been a retrace by x amount of pips. So in order for me to get the current price, I used Close[0]. I'm not sure if that means the close after the bar is completed or as the bar is happening. I also have the Calculate set to Calculate.OnEachTick. I also looked at CalculateOnBarClose, which seems to be different and applicable only to live accounts. I asked this question previously and a staff was nice enough to tell me to set CalculateOnBarClose to false when you get NJ8, but I get a compile error.

    Thanks
    Last edited by BernWillChris; 08-03-2015, 09:49 AM.

    #2
    Hello BernWillChris,

    Thanks for your post.

    Some background. In NT7 you could choose CalculateOnBarClose = true or CalculateOnBarClose= false. When set to true your code only executes once at the end of the bar. When set to false, your code would execute on every tick.

    IN NT8, there are now three choices, Calculate.OnBarClose, Calculate.OnEachTick or Calculate.PriceChange.

    Calculate.OnBarClose will operate exactly like NT7 CalculateOnBarClose = true.

    Calculate.OnEachTick will operate exactly like NT7 CalculateOnBarClose = false.

    Calculate.OnPriceChange has been added as something between those so that any indicators that need to recalculate on price change can do so without being asked to recalculate on each tick, so a slightly less CPU intensive alternative.

    Here is the helpguide clarification:
    Definition: Determines how often OnBarUpdate() is called for each bar. OnBarClose means once at the close of the bar. OnEachTick means on every single tick. OnPriceChange means once for each price change. If there were two ticks in a row with the same price, the second tick would not trigger OnBarUpdate(). This can improve performance if calculations are only needed when new values are possible.

    When scripts are running on historical data, OnBarUpdate() is only called on the close of each historical bar even if this property is set to .OnEachTick or .OnPriceChange. This is due to the fact that with a historical data set, only the Open, High, Low, Close, Volume, Time of the bar is known and not each tick that made up the bar.

    To answer your question, for an intrabar order you would want to use Calculate.OnEachTick. In this case, Close[0] would be the current (last) price as the bar is forming.
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Thanks!! That was very insightful. What I thought changed from NJ7 to NJ8 is the ability to place trades without the need of creating an inner bar object. If I heard wrong, what changes did NJ8 made to improve from NJ7 and what would be the best route? Does it remain the same way as NJ7?

      Thanks

      Comment


        #4
        Hello BernWillChris,

        Thanks for your reply.

        In the case of live trading or market replay you have access to ticks. I suspect what you are referring to is historical data in backtesting.

        In NT7 when backtesting, only the finished bars Open, High, Low, close, Volume and Time of the bar was available. If you had a strategy based on 5 minute bars, the ticks that made up the bar were not available and any order would not be placed until the open of the next bar. To improve back testing, users had to add "tick" data series to their strategies to enable them to be able to enter orders intrabar.

        In NT8 there are two significant changes:
        1) Standard Fill Resolution breaks bars in to three virtual bars to simulate the direction of the price which was used to form the bar
        2) High Resolution mode automatically adds a secondary data series as the additional resolution used for fills, without needing to custom program

        Here is a link to the "what's new " section of the NT8 helpguide
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          Okay!!

          I'm currently trying to run it on the simulator, but for some reason, I'm seeing the trades on the chart, but it isn't updating the account balance. I will troubleshoot this for a bit. I will soon look into doing this with granularity for NJ8 soon. The thing is that when I tried it on NJ7, it wasn't what I thought it was. I was thinking that the sample code would allow me to enter as soon as condition is satisfied, but I was wrong. Here is a prior thread that I started a while back with NJ7, with Michael giving me info:



          He said the sample code (the sample code located in this forum for executing granularity) is for intra-bar fill, not intra-bar calculations. What I think I got from that is this sample would allow me to enter a trade before completion of bar, but it wouldn't allow me to do that because there is no intra-bar calculations???

          Thanks

          Comment


            #6
            Hello BernWillChris,

            Thanks for your reply.

            In NT7 backtesting, your strategy is executed only once per bar at the end of the bar regardless of CalculateOnBarClose = true or false because historical bars (such as 5 minute bars) do not contain the ticks that made them. When you add a tick data series in NT7 to your strategy this gives you many more OnBarUpdate events in the same 5 minute bar time frame with which your strategy can execute its code (intrabar processing and order placement).

            In NT8, as mentioned previously, you have improved standard fill resolution where you will get 3 OnBarUpdate events per (5 minute in this example) bar. In addition you can run at a higher resolution mode without needing to change your strategy to add a data series, giving you many more OnBarUpdate events.
            Paul H.NinjaTrader Customer Service

            Comment


              #7
              Okay Paul,

              This is my last question for this topic, I promise . I was able to take a look at the settings and see the Order Fill Resolution option. What I think this option should let me do is this. For an example, if I have a strategy that allows me to place trades on moving averages crossover as soon as that happens, and the chart time frame is hourly, I can go to Order Fill Resolution and click on High and then pick Tick with value set to 1. This would allow my strategy to see all of the inner values within those hourly bars on the chart; this would ultimately allow me to get into trades around 12:37, 3:42, and a bunch of other times instead of it being strictly at the top of the hour.

              That is my understanding of this feature and I thought that sample code for NJ7 would allowed this programmatically, but I was wrong. I was told that it was for intra-bar fills, not intra-bar calculations, which for some crazy reason, it doesn't make a lot of sense to me. When he said intra-bar fill, this has me to believe that my order will get fill(enter a trade) within the smaller timeframes of the hourly bar. When he said intra-bar calculations, this leads me to believe that he is referring to the actual values of the moving averages within the hourly bars. So wouldn't you have to have both, the calculations to know if the conditions were satisfied and the intrabar fill to enter the trade as soon as conditions are satisfied to get into the trade. Sorry that I'm not getting this earlier, but this is an important concept to master in order to program with no bugs.

              Comment


                #8
                Hello BernWillChris,

                Thanks for your reply.

                Using 1 hours bars.

                For live trading, if you use Calculate.OnBarClose then the strategy will only execute at the end of the one hour bar and at that point would evaluate for any crossover conditions and if there are crossovers, orders would be placed. I do not believe this is what you want.

                For live trading, if you use Calculate.OnEachTick then the strategy will be executed on each tick and at that point would evaluate for any crossover conditions and if there are crossovers, orders would be placed. I believe this is what you want.

                For backtesting, if you enable tick replay, then the strategy will be executed on each tick and at that point would evaluate for any crossover conditions and if there are crossovers, orders would be placed. I believe this is what you want.

                In a general answer to your questions, you can have a multi-time frame strategy and with that you can code it so that the order decision making can be done on one time frame whilst the order placement can be made on the other. The reasons for this are as many as there are traders, so in the end it just depends on what you want to do and we've tried to provide a platform that offers the flexibility to meet the many different needs.
                Paul H.NinjaTrader Customer Service

                Comment


                  #9
                  Okay Paul,

                  This is what I did thus far. I went to Tools >> Options >> Market Data and I enabled the Tick Replay feature. I then proceed to Strategy Analyzer and clicked enabled Tick Replay and also picked Order FIll Resolution to High and set the smallest increments to 1 Tick. Finally, I went ahead and run the back-tester within a time span that I'm sure should be in the system and after the completion of the back-test, nothing happens. When I ran this code in a simulator, everything was working-- so I don't think it is the code.

                  Thanks

                  Comment


                    #10
                    Hello BernWillChris,

                    Thanks for your post.

                    So that I can recreate on my end, what instrument and time period did you run the back test over?

                    If you use the SampleMaCrossover strategy do you see the same results?
                    Paul H.NinjaTrader Customer Service

                    Comment


                      #11
                      Okay,

                      I was using Playback Connection for data source.

                      EUR/USD, 5 mins, and I used the SampleMaCrossover strategy. When I did that, it ran, but every trade was executed in increments of 5 mins.

                      I used the period from July 27 til Aug 3

                      Comment


                        #12
                        Help Please On Getting This Back-tester To Work. Image attached.

                        Okay,

                        I been unsuccessful for the past week or two trying to get the back-tester to work as the way that I believe it is suppose to. I did everything from previous post and still no success. I believe the best thing for me to do now is to attached an image. What the image doesn't show is the fact that I made sure that I downloaded the data for that week and the connection; data is downloaded for that week and my connection is Playback Connection.



                        Thanks a million if anyone can get this to work for me. This is an invaluable tool for me to create profitable strategies.

                        Comment


                          #13
                          The Playback connection does not work for the Strategy Analyzer. Is this the only data source you have access to minute data with?
                          MatthewNinjaTrader Product Management

                          Comment


                            #14
                            I have FXCM connection and everything else that comes at default: Kinetick, Playback Connection, and SImulated Data Feed. I thought that the Playback Connection would be the appropriate because that is where the simulator is ran from.

                            Comment


                              #15
                              Connect to FXCM, then run your test again. The Simulator is used trading real-time data. To test using the Strategy Analyzer backtest account, you'll need a market data connection like FXCM to test on that data.

                              The Playback Connection would be used for replaying data and simulating live trades.
                              MatthewNinjaTrader Product Management

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by terofs, Yesterday, 04:18 PM
                              1 response
                              21 views
                              0 likes
                              Last Post terofs
                              by terofs
                               
                              Started by CommonWhale, Today, 09:55 AM
                              1 response
                              3 views
                              0 likes
                              Last Post NinjaTrader_Erick  
                              Started by Gerik, Today, 09:40 AM
                              2 responses
                              7 views
                              0 likes
                              Last Post Gerik
                              by Gerik
                               
                              Started by RookieTrader, Today, 09:37 AM
                              2 responses
                              12 views
                              0 likes
                              Last Post RookieTrader  
                              Started by alifarahani, Today, 09:40 AM
                              1 response
                              7 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Working...
                              X