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

Market execution location

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

    Market execution location

    Dear Sir or Madam,

    I am trying this strategy for quite long, and the issue that I need to understand is, that If I programmed the strategy to enter at the close of the bar, the strategy always executes the trade a tick after the close price (maybe Spread). so in each complete trade Buy/Sell, I have a loss of 2 ticks because of the market spread/late execution ...

    How to come over this issue and manage to open a trade at the exact location of the Close[0]. ?

    Actually, I tried to switch the straight entries to Entries limit a tick bellow or Up depending on the type so I will manage to open the trade at the exact location,
    However I faced an error in my strategy maybe because I didn't cancel the unfilled trade...
    My questions
    1- can you suggest me any solution to overcome this issue?
    2- Do you think Entries limit is the only partial solution, since there is this risk that the market takes off without hitting my entry limit and executing order?
    3- if yes. Can you advise me on how cancelling a pending order after 2 bars, so if my market moved 2 bars without executing the entry limit. I want it to be cancelled to it will not affect lated entries.
    Please, note my code is over 500 lines, and all on protected override void OnBarUpdate().

    Note, I hope there with be an alternative solution without using entry limit.
    Thank you .

    #2
    Hello Yassine.Chetouani,

    Thanks for your post.

    The only way to ensure that you fill at a specified price is to use a limit order which may or may not fill. Market orders will always fill but at a price convenient to the market. You would need to decide which is most important to you (fill or price) and code in that manner.

    If you are using the managed approach, orders that are placed on a bar are automatically canceled if not filled and if not resubmitted and if not using the advanced order handling methods.

    Are you running your strategy with CalculateOnbarClose = true or false?
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Hi Paul,

      First of all, I want to thank you for your fast reply.
      to explain you the situation well, I have this long code with different entries. I have being testing is for one week now, and believe me the two ticks difference between the Buy/sell switched my cumulative profit. imagine (2 ticks x 2000 trades).
      So with reference to your last message, I want to inform you that I run it on CalculateOnbarClose = true, without the bar close set to true and with RSI and other indicators it causes several entries to be executed within the same bar everytime the market shake.

      So, let's focus on the limit entries as the only and probable solution.
      Can I have any advise on how to overcome the error of unfilled pending entries. in othjer words, how to cancel the limit order just after 1-2 bars from its entry pending location
      I saw on some postes that protected override void OnBarUpdate() is not working and I have to pass to Iorder entries. so should I transfer the all code, is there any example on how to do it for short and long position case.
      Thank you Paul
      Last edited by Yassine.Chetouani; 08-01-2017, 08:26 AM.

      Comment


        #4
        Hello Yassine.Chetouani,

        Thanks for your reply.

        If you are running with your strategy using CalculateOnBarClose = true then this means your strategy is not running on real time data but instead making decisions and entries based on historical data.
        If you use limit orders, placed at the Close[0] of the bar, the current realtime price likely will not be at that price when the order is actually placed and in fact may produce an order error.

        If you run your strategy using CalculateOnBarClose = false you can compare the current price, which will be Close[0], to where you want to place the limit order and thus (through your logic) prevent your strategy from placing the entry at the incorrect price.

        To clarify, CalculateOnBarClose=true means that the index position of Close[0] points to the latest closed bars close price and the forming bar (current price) is unknown to the strategy. CalculateOnBarClose = false changes the index Close[0] to point to the current price on the forming bar. This can be confusing but close[0] is the actual price in this mode. Close[1] points to the latest completed bars' close price. So as you can see the index shifts forward when using CalculateOnBarClose = false.

        If you are placing limit orders that are close to current price then you pretty much have to use CalculateOnbarClose = false in order to prevent entry order errors by having your strategy logic compare the current price to the entry price of the limit order.

        You can have your code run in both modes and this would be the recommendation to keep you from having multiple entries. The way to do this, in NT7, is to segment your code that you want to run once per bar by using if (FirstTickOfBar) { code in here executed once per bar} code out here executed on every tick. reference: http://ninjatrader.com/support/helpG...onbarclose.htm and http://ninjatrader.com/support/helpG...ttickofbar.htm

        By segmenting your code you can have your order entry section using the real time data to ensure success of placing your limit orders by verifying the current price to the entry you wish to make and adjusting as needed.

        We have a reference that shows how to using the code running in both modes here: http://ninjatrader.com/support/forum...ad.php?t=19387

        You are welcome to review all of the reference samples here that may assist you on this and other needs in the future, here: http://ninjatrader.com/support/forum...splay.php?f=30
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          Dear Mr.Paul,
          I want to thank you so much for your instructive response. It was a bit confusing for me so I needed to analyze your answer carefully and re-read it several times. Still I have some question that I will describe them better with the example below.
          Let’s consider this code below which look like my actual one in a simple way..
          protected override void Initialize()
          CalculateOnBarClose = true;

          protected override void OnBarUpdate()
          {
          If (Condition1 && condition2(bools)

          && Close[0] > Open [0]
          && (Stochastics_Color(3, 50, 3).K[0] >= 80)
          && (CrossAbove(EMA(10), EMA(20), 1)))
          {
          EnterShort(Quantity2, "S2");
          }


          So to put what you said into practice, the new Code become:

          protected override void Initialize()
          CalculateOnBarClose = false;

          protected override void OnBarUpdate()
          {
          if (FirstTickOfBar)
          {


          If (Condition1 && condition2 (bools)
          && Close[1] > Open [1] // Do we need to change the conditions also or should we keep them the same
          && (Stochastics_Color(20, 20, 20).K[1] >= 80)
          && (CrossAbove(EMA(10), EMA(20), 2))) // should I take the cross one bar before?
          {
          EnterShortLimit(0, true, Quantity2 , Close[1], "S2"); // I believe this one is right.
          }


          So here are my questions:
          1- Was the change I made correct?
          2- If Yes, I want your confirmation of my well understanding that I need to take all the IF conditions one bar before since it shifted one bar forwards after CalculateOnBarClose = false;
          3- Please confirm me that Entershortlimit is correct.
          4- My interesting question is, since it is entrylimit, there is this probability that the order may not be executed if the market takes off. In this case if the order keeps pending and unfilled it may cause an error problem. Shouldn’t we add something to cancel it if not executed after X bars,
          5- Or is it for sure executed no matter what in my condition? In other words, this entry is the same as CalculateOnBarClose = true; with more accurate entries matching the close(0)

          Again excuse my need of confirmation since my code is more than 700 lines and any change at this level will require a considerable effort, so I hope to be right 100% before any action.

          Thank you again Paul .

          Comment


            #6
            Hello Yassine.Chetouani,

            Thanks for your reply.

            I think you are on the right approach and given the length of your code, I would advise that you create just a simple functioning test script to help clarify which is what it looks like you are doing and would use that with live data testing to solidify the understanding.

            It looks like you have your references correct. The only thing I can suggest is that where you place your entry order, that it be in the tick processing part. for example:

            if (FirstTickOfBar) // processed once per bar
            {
            if (your conditions to enter)
            {
            enteranorder = true; // enteranorder is a bool used to say we are ready to enter order
            }
            }
            // tick processing begins here, on each tick

            if (enteranorder)
            {
            if (Close[0] < Close[1]) // if current price is below last bar close
            {
            EnterShortLimit(...Close[1]...); //place the order at last close
            enteranorder = false; // set false
            }
            else
            EnterShortLimit(... ???...) // you need to determine where you want to alternatively place the order if the current price is above Close[1], or perhaps you simply do not place an order.
            enteranorder = false; // set false
            }

            In the above example where the entershortlimit() is, you can save the CurrentBar to an int variable and can test if you are flat and two bars have passed like:

            if (Position.MarketPosition == MarketPosition.Flat && CurrentBar - savedBar >= 2)
            {
            CancelOrder(...)
            }

            These are a lot of details that you will need to work out as these are beyond the level of support we provide and are really up to the coder to resolve, I just wanted to provide you with some concepts to move your forward. The best advice here is to code up a short simple test and see how it works. Use your debugging techniques to help you understand what is happening through print statements if needed.
            Paul H.NinjaTrader Customer Service

            Comment


              #7
              Dear Paul,
              I did what you suggested on just a small part of the code.
              However I have an issue here. kindly see the pictures attached.
              Somehow the entry point do miss the location I need to open a trade in.

              It seems that the strategy sees the code below and compare the close of the bars even after using (FirstTickOfBar) and CalculateOnBarClose = false; normally the firstTickOfBar should consider every tick within the bar and be able to enter during bar progress
              Please tell me what I am missing here.
              kindly note that there is no problem with my conditions since I can draw arrows and used them with straight entries before.

              By the way I tested the strategy on replay market where the bars are moving tick by tick in front of my eyes but somehow the strategy does not execute the trades in the required location.
              How you can help me to solve this issue. Thank you

              The code is below:
              protected override void Initialize()
              CalculateOnBarClose = false;

              protected override void OnBarUpdate()
              {
              if (FirstTickOfBar) // processed once per bar
              {
              // ...For Long positions

              if ( my conditions to enter )

              {
              DrawArrowUp("My up arrow" + CurrentBar, false, 0, Low[0] - (1 * TickSize), Color.LimeGreen);
              condition13 = false;
              condition14 = true;

              enterLongOrder = true;
              enterShortOrder = false;
              savedLongBar = CurrentBar;
              }

              if ((enterLongOrder) && (Close[0] <= Close[1]- (1 * TickSize)))

              {

              EnterLongLimit(0, true, Quantity2 , Close[1], "L2"); //place the order at last close
              enterShortOrder = false; // set false
              enterLongOrder = false; // set false
              }
              }
              }
              Attached Files

              Comment


                #8
                Hello Yassine.Chetouani,

                Thanks for your reply.

                It looks as if all of your code is contained within the FirstTickofBar {} Which means that the code will execute only once per bar which is no different than CalculateOnbarClose = true.

                Please review my previous example provided and note the remark // tick processing begins here, on each tick As the code below that remark would execute on each tick.

                Also, you should study the working example from http://ninjatrader.com/support/forum...ad.php?t=19387

                Please note that while we can assist with questions we cannot provide coding or debugging services. Alternatively, we can also provide references to 3rd party coders who can provide the coding service if you wish.
                Paul H.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by xiinteractive, 04-09-2024, 08:08 AM
                3 responses
                11 views
                0 likes
                Last Post NinjaTrader_Erick  
                Started by Johnny Santiago, 10-11-2019, 09:21 AM
                95 responses
                6,193 views
                0 likes
                Last Post xiinteractive  
                Started by Irukandji, Today, 09:34 AM
                1 response
                3 views
                0 likes
                Last Post NinjaTrader_Clayton  
                Started by RubenCazorla, Today, 09:07 AM
                1 response
                5 views
                0 likes
                Last Post RubenCazorla  
                Started by TraderBCL, Today, 04:38 AM
                3 responses
                25 views
                0 likes
                Last Post NinjaTrader_Jesse  
                Working...
                X