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

limit orders, market replay and the ten minute time frame

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

    limit orders, market replay and the ten minute time frame

    I am relatively new to NinjaScript and shorter time frames. I have written some code that buys on limit and sells on limit and have provisionally tested it on Market Replay.

    I am used to Wealth-Script, longer time frames and limit orders where there is no slippage. It troubles me to see that the fills are sometimes outside my limit as defined by the bands of the previous bar. I would very much prefer that the trade was not filled if there were insufficient volume rather than be slightly outside of my limit.

    Is this more likely something I am doing wrong with my code or my misunderstanding of the real life application of limits. Is there a way to minimize or avoid this problem?
    Attached Files

    #2
    Hello johnls101,

    Thanks for your post and welcome to the forums.

    You may wish to review the helpguide here for general reference: http://ninjatrader.com/support/helpG...ime_vs_bac.htm

    To further assist we would need to understand what entry/exit methods you used and what differences you have observed between the orders placed/filled.
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Thank you Paul.

      I understand that there may be differences between live trading and back testing and I have read the part of the help guide that you raised, but I would like to ask one general question without going to code.

      In real time are limit orders truly at the limit or better, or is there latitude on behalf of the broker (in this case IB)? I see it as a really important question because if true, high frequency systems will always be able to beat me to the post and sell it back to me outside of my limit.

      Comment


        #4
        Hello johnls101,

        Thank you for your response.

        The limit orders would be at the price level specified for your order and accepted by the broker and exchange as a valid price point for your order. If you have more questions on resting orders you may wish to speak with your broker.

        Comment


          #5
          Thanks again Patrick. I do appreciate your looking at this.

          I know I am pushing the topic, but I thought that I would delve into this some more as there are some very big concerns that arise in my mind as to the validity of the back test and market replay systems for NinjaTrader.

          When I have analysed systems in Wealth-lab I have assumed that price would have to go beyond the limit before I would accept that a limit would be hit. If this didn't occur, no trade would initiate. It was a conservative assumption, but as you could never guarantee a buy at market touch of a limit, probably a pretty reasonable one. I would much rather have a pessimistic approach than a overly generous one that would delude me into thinking I had a profitable system when I didn't.

          It seems that when I am analysing trades with NinjaTrader, not only are trades occurring at market touch, but limits are not being adhered to. It seems that trades are happening within limits (in the case of buy limits, above the limit) which is crazy. I am entering into trades that should never happen, which on the one hand is overly generous, but on the other hand, the profit margins are less. I am left with the question as to what it all means.

          If limits are not adhered to by brokers, there would be a phenomenal handicap for anyone who trades intraday as the broker not only takes the commission, but buys your limit before your order goes though, and sells the item to you at whatever price he wants outside of your limits. No one would bother to trade.

          You previously asked for code and I have attached a sample of this for which the problem occurs. I have also attached another picture illustrating the problem.

          Cheers

          John


          #region Using declarations
          using System;
          using System.ComponentModel;
          using System.Diagnostics;
          using System.Drawing;
          using System.Drawing.Drawing2D;
          using System.Xml.Serialization;
          using NinjaTrader.Cbi;
          using NinjaTrader.Data;
          using NinjaTrader.Indicator;
          using NinjaTrader.Gui.Chart;
          using NinjaTrader.Strategy;
          #endregion

          namespace NinjaTrader.Strategy
          {
          [Description("Reversional strategy based on Bollinger Bands")]
          publicclass test2016Jan23a : Strategy
          {
          #region Variables
          privateint myInput0 = 1; // Default setting for MyInput0
          #endregion

          protectedoverridevoid Initialize()
          {
          Add(Bollinger(3, 8));
          Add(Bollinger(3, 8));
          CalculateOnBarClose = true;
          }

          protectedoverridevoid OnBarUpdate()
          {
          EnterLongLimit(DefaultQuantity, Bollinger(3, 8).Lower[0], "");

          if(BarsSinceEntry() >= 3)
          ExitLong();
          ExitLongLimit(Bollinger(3, 8).Middle[0], "", "");
          }

          #region Properties
          [Description("")]
          [GridCategory("Parameters")]
          publicint MyInput0
          {
          get { return myInput0; }
          set { myInput0 = Math.Max(1, value); }
          }
          #endregion
          }
          }
          Attached Files

          Comment


            #6
            johnls101,

            Is this backtest in the Strategy Analyzer, Market Replay, or trades on the Sim101 account?

            In backtesting through the Strategy Aanlyzer you can set the slippage amount for the orders.

            Comment


              #7
              Hi Patrick
              I was using market replay in the example I have given. Should slippage apply for limit orders? If volume is insufficient, shouldn't the trade just not be entered into rather than not adhering to the limits in the first place and adding slippage to compensate for this error?
              John

              Comment


                #8
                Hello John,

                Thank you for your response.

                In Market Replay the executions would occur without delay and when the price is touched and when the volume is there to support the trade.

                Are you seeing executions that did not have the needed volume?

                Comment


                  #9
                  Thanks for the reply Patrick.

                  When I mentioned volume I was speaking in a general sense for when trading might involve numerous contracts or shares. However, in this case, only one contract is being traded, and either the trade should happen or not happen, and partial fills are not an issue. I appreciate that on market "touch" there is no guarantee that I will buy my one contract as others may be ahead of me in the cue and I might miss out. This topic is not a big issue for me.

                  Unfortunately my question/concern is still not answered or addressed.

                  What I am seeing is that trades appear to happening on market replay that are outside of the limit specified and this is troubling me. As a hypothetical example, I might be setting a buy limit of $100 or better (i.e. less). However, Ninja trader then seems to often not stick to this limit. For example, it might buy at 106 which means that a trade is happening when I believe it should not have. It seems crazy to me to say that it is OK for Ninjatrader to not adhere to limit prices, but that I should compensate for this error by adding in slippage. I am very happy to be shown that I am doing something wrong here, or that there is something wrong with my understanding, but to me, slippage is something that relates to market orders, not limit orders. It seems that there is something radically wrong with the modelling. I am hoping that you can prove me wrong because is a big flaw if I am not.

                  John

                  Comment


                    #10
                    Originally posted by johnls101 View Post
                    Thanks for the reply Patrick.



                    What I am seeing is that trades appear to happening on market replay that are outside of the limit specified and this is troubling me. As a hypothetical example, I might be setting a buy limit of $100 or better (i.e. less). However, Ninja trader then seems to often not stick to this limit. For example, it might buy at 106 which means that a trade is happening when I believe it should not have. It seems crazy to me to say that it is OK for Ninjatrader to not adhere to limit prices, but that I should compensate for this error by adding in slippage. I am very happy to be shown that I am doing something wrong here, or that there is something wrong with my understanding, but to me, slippage is something that relates to market orders, not limit orders. It seems that there is something radically wrong with the modelling. I am hoping that you can prove me wrong because is a big flaw if I am not.

                    John
                    Post a concrete example and how you are entering the trade.

                    Comment


                      #11
                      Hi Sledge

                      Thanks for replying. I am travelling at the moment and can give a specific day's data to download for market replay in another couple of days to make this completely specific.

                      However, in the meantime I have already provided both sample code and two screen prints illustrating the issue. The lower blue line is the indicator that defines the limit. The first image was a better example, but I didn't show the data box. The more recent image does have a data box and shows the indicator at the bar prior to the buy order setting a limit of 2108.15. (I have lined up the edge of the data box at the bar where I believe the limit is defined). However the "buy" is executed at 2108.25.

                      I should check that my understanding is correct first in relation to timing. When functioning "on bar close" I have assumed that the indicator defining the limit is at the current bar and that the order is then executed at the current bar plus one. Is this correct? The indicator defining the limit would then always be one bar prior to the order.

                      John

                      Comment


                        #12
                        My bad.. missed the original posts.

                        My concern is where you say you have buy limit of 100,but get filled at 106.
                        Post such an example when you get the chance. I don't see that in your earlier posts.
                        Last edited by sledge; 01-30-2016, 03:15 PM.

                        Comment


                          #13
                          Hi Sledge

                          The $100 to $106 was a hypothetical to show direction relative to $100 rather that an absolute amount.

                          The effect of the apparent anomaly with limit orders is no less important in the examples than the $6 above as the outcomes are radically different over time.

                          If my understanding is wrong, could you or someone please clarify how it is wrong?

                          John

                          Comment


                            #14
                            Your hypothetical shouldn't happen.

                            A buy limit of $100 should only fill at =<$100, not over.

                            You mentioned you actually saw this. So post a picture of it.

                            Comment


                              #15
                              Hi Sledge
                              I am glad to hear that you agree that the limits should be adhered to.
                              I have already provided two images illustrating the anomaly but I will reproduce the first of these again for ease of visualisation. This example displays two trades and these are in the S&P 500 with a ten min time frame. The first of these has a buy limit corresponding to the blue indicator the bar before the buy order at ca 2130.0. However the buy was at 2130.25. The second trade had a buy limit of ca 2129.6 but the buy was filled at 2129.75. Although I don't have the exact figures for the buy limits as I didn't print a data box, it is pretty obvious that the fills are well above the blue line of the bar before.
                              Secondly, I have modified the code to make it very simple for the sake of the example and have applied it to a minutely time frame to bring up more trades. I used the Dec 2015 S&P 500 contract and downloaded replay data for the 4th Dec with a range of 2nd Dec to 5th Dec for display.
                              The first three of these examples derive from historical data on 2nd and 3rd Dec. The limit orders are based off the blue line of the bar before the fill. The first of the limits was at the right edge of the data box at 2088.67, but the fill was at 2088.75. The second of these had a limit of 2084.92, but was filled at 2085.0. The last of the three had a limit of 2084.42, but a fill of 2084.50.
                              Two more examples are give using the "prospective" market replay data of 4th Dec 2015 and times are shown in the images. This time the buy limit is in orange and the chart is displayed at the time of the fill. The first had a limit of 2055.67 but was filled at 2055.75. The second had a limit of 2059.38 but was filled at 2059.50.
                              I have reproduced the modified code below. Hopefully there is something obvious I have got wrong with my code.

                              namespace NinjaTrader.Strategy
                              {

                              [Description("Reversional strategy based on Bollinger Bands")]
                              publicclass test2016Jan23a : Strategy
                              {
                              #region Variables
                              privateint myInput0 = 1; // Default setting for MyInput0
                              #endregion

                              protectedoverridevoid Initialize()
                              {
                              Add(Bollinger(2.5, 4));
                              CalculateOnBarClose = true;
                              }


                              protectedoverridevoid OnBarUpdate()
                              {
                              EnterLongLimit(DefaultQuantity, Bollinger(2.5, 4).Lower[0], "buy long");

                              if(BarsSinceEntry() >= 3)
                              ExitLong();
                              }

                              #region Properties
                              [Description("")]
                              [GridCategory("Parameters")]
                              publicint MyInput0
                              {
                              get { return myInput0; }
                              set { myInput0 = Math.Max(1, value); }
                              }
                              #endregion
                              }
                              }
                              Attached Files

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Tim-c, Today, 03:54 AM
                              0 responses
                              3 views
                              0 likes
                              Last Post Tim-c
                              by Tim-c
                               
                              Started by FrancisMorro, Today, 03:24 AM
                              0 responses
                              2 views
                              0 likes
                              Last Post FrancisMorro  
                              Started by Segwin, 05-07-2018, 02:15 PM
                              10 responses
                              1,771 views
                              0 likes
                              Last Post Leafcutter  
                              Started by Rapine Heihei, 04-23-2024, 07:51 PM
                              2 responses
                              31 views
                              0 likes
                              Last Post Max238
                              by Max238
                               
                              Started by Shansen, 08-30-2019, 10:18 PM
                              24 responses
                              945 views
                              0 likes
                              Last Post spwizard  
                              Working...
                              X