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

Filling outside of bar price

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

    Filling outside of bar price

    Hello

    I'm currently using this code snippet:


    donchian = DonchianChannel(
    10).Upper[0];

    if (Position.MarketPosition == MarketPosition.Flat)
    {
    EnterLongLimit(donchian +
    1);
    }
    This means, that it should put a limit long order one point above the current upper value of the DonchianChannel using the last 10 bars.
    But now the order gets filled, although the whole bar was about 100 points below the order prize. I'm using the default filling algorithm. How can I get it, that it only gets filled when the limit price gets reached? I've never had this problem before.

    Regards and a happy new year,

    Sepp


    #2
    Sepp, happy new Year to you, too!

    This would fill immediately, as it would be a marketable limit order as you submit at a higher price - were you intending to place a StopLimit order at this level instead?

    BertrandNinjaTrader Customer Service

    Comment


      #3
      Thanks, this helped me alot.

      But now already the next thing: I want to use 2 exit possibilities: one as a trailing stop with 150 Ticks and the second as soon as the price drops below the lower side of the DonchianChannel. I'm using this sort of code:

      if (Position.MarketPosition == MarketPosition.Long)
      {
      ExitLongStopLimit(donchianstop -
      1, donchianstop - 1);
      SetTrailStop(CalculationMode.Ticks,
      150);
      }

      As long as I'm leaving the trailing stop away, everything works fine. But as soon as I implement the trailing stop, the other exit condition gets thrown away and it only exits when the trailing stop gets filled, no matter if the exit would have been earlier when using the Donchian "stop".
      Do you can help me to make this work so that both stops work simultaneously?

      Regards,

      Sepp

      Comment


        #4
        Sepp, you're running into the Internal Order Handling Rules with this approach -



        To make this work, you would need exit with a market order once your Donchian Channel break would trigger the exit.
        BertrandNinjaTrader Customer Service

        Comment


          #5
          After trying different possibilities, I didn't get something to make this work. I guess I have to add something with intrabar stuff, since I'm using 240 min bars. For DonchianChannel(12) I'm using the variable donchianstop and as a second timeframe 5 min bars.


          if
          (Position.MarketPosition == MarketPosition.Long
          && Closes[
          1][0] < donchianstop)
          {
          ExitLong(
          1, "", "");
          }
          Using this, nothing happens and I don't know why. The whole code is implemented in the if(BarsInProgress == 0){...} command, so donchianstop gets calculated based on the 240 min bars.
          The problem has to do something with the "< donchianstop" since if I change it to "> donchianstop", everything works fine.

          I hope you can help me.

          Regards,

          Sepp

          Comment


            #6
            I got it, I only had to use it in if(BarsInProgress == 1){...}.
            Now it works all fine - almost.
            Am I understanding correctly, that during backtest, when the trailing stop and the exit condition both are caused during the same bar, the trailing stop gets preferred, although the exit condition would have caused a lesser loss?

            Regards,

            Sepp

            Comment


              #7
              Great you got it working - correct as NinjaTrader always takes the most conservative approach.
              BertrandNinjaTrader Customer Service

              Comment


                #8
                No, that's the problem. In the 240 min chart, both exit signals were caused on the same bar.
                But on the 5 min bar chart, the exit condition was filled before the trail stop should have been triggered. Therefore, there would have be a smaller loss compared to the exit through the trail stop. That's why I was wondering how this can happen.

                Regards,

                Sepp

                Comment


                  #9
                  Sepp, aare you using the finer granular 5min exit in the correct BarsInProgress so it would trigger on this OnBarUpdate() and not on the 240min?
                  BertrandNinjaTrader Customer Service

                  Comment


                    #10
                    I'm currently using this code snippet, where BarsInProgress == 1 are the 5 min bars.


                    if
                    (BarsInProgress == 1)
                    {
                    if (Position.MarketPosition == MarketPosition.Long
                    && Low[
                    0] < donchianstop)
                    {
                    ExitLong(
                    1, "", "");
                    }
                    SetTrailStop(CalculationMode.Ticks,
                    150);
                    }


                    Hope this helps.

                    Regards,

                    Sepp

                    Comment


                      #11
                      Sepp, but on which timeframe is the Donchian channel value you use to trigger calculated? It this still on the main 240 one? You also check for Low[1] the primary bars objects's low, where the 5 min one would be accessed by Lows[1][0].
                      BertrandNinjaTrader Customer Service

                      Comment


                        #12
                        The donchian Channel value is calculated under BarsInProgress == 1, so with 240min bar data.
                        It also works pretty fine; one time it enters through the exit condition, another time through the trail stop. But two times, there are both exit signal triggered on the same 240 min bar. Both times, the position was closed through the trail stop, although this cause a bigger loss than if the position would've been closed through the exit signal. Additionally, the trail stop was triggered several (5 min) bars later then the exit signal and still was preferred.
                        Do you know, how this can be?

                        Regards,

                        Sepp

                        Comment


                          #13
                          Sepp, if you calculate it on the 240 and use the Low of this chart to trigger and exit it would not trigger nor execute intrabar of the 240 chart - thus the placed trailstop caught this.
                          BertrandNinjaTrader Customer Service

                          Comment


                            #14
                            I don't think this can be, because then it would be like this on every other trade too. But only in this two trades, the trail stop gets executed before the exit condition although this shouldn't be.
                            Anyways, I changed Low[0] to Lows[1][0] - without any results. Everythign stays the same. Here again my code snippet:

                            if (BarsInProgress == 0)
                            {
                            donchianstop = DonchianChannel(12).Lower[0];
                            }

                            if (BarsInProgress == 1)
                            {
                            if (Position.MarketPosition == MarketPosition.Long
                            && Lows[
                            1][0] < donchianstop)
                            {
                            ExitLong(
                            1, "", "");
                            }
                            SetTrailStop(CalculationMode.Ticks,
                            150);
                            }
                            I hope you see something that I didn't see yet.

                            Regards,

                            Sepp

                            Comment


                              #15
                              Sepp, unfortunately not - would recommend to print all associated values to debug step by step how this develops. In case, you weren't using those already, TraceOrders would provide additional insight as well, such as the Trail Stop price.



                              I would also add visuals to see when the conditions you expect to hit are really triggering, sometimes the 'by eye' checks can lead you off track.
                              BertrandNinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by frankthearm, Yesterday, 09:08 AM
                              14 responses
                              47 views
                              0 likes
                              Last Post NinjaTrader_Clayton  
                              Started by gentlebenthebear, Today, 01:30 AM
                              2 responses
                              13 views
                              0 likes
                              Last Post gentlebenthebear  
                              Started by Kaledus, Today, 01:29 PM
                              2 responses
                              8 views
                              0 likes
                              Last Post Kaledus
                              by Kaledus
                               
                              Started by PaulMohn, Today, 12:36 PM
                              2 responses
                              16 views
                              0 likes
                              Last Post PaulMohn  
                              Started by Conceptzx, 10-11-2022, 06:38 AM
                              2 responses
                              56 views
                              0 likes
                              Last Post PhillT
                              by PhillT
                               
                              Working...
                              X