Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Intrabar orders

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

    Intrabar orders

    Hallo,

    I'm trying to set up following strategy:

    When a condition is met buy at the next bar at Price x, if it is reached.
    Sell at price y within the same bar. Price y is reached in every case because it's the Close price of that bar.

    Price x might be reached or not.
    Using EnterLongStop() doesn't always buy when the condition is met.
    Using EnterLongLimit() also does some entries, when the condition is not met.

    SetProfitTarget() is used to exit.

    What must be changed to get the desired behaviour?

    #2
    Originally posted by Stephan123 View Post
    Hallo,

    I'm trying to set up following strategy:

    When a condition is met buy at the next bar at Price x, if it is reached.
    Sell at price y within the same bar. Price y is reached in every case because it's the Close price of that bar.

    Price x might be reached or not.
    Using EnterLongStop() doesn't always buy when the condition is met.
    Using EnterLongLimit() also does some entries, when the condition is not met.

    SetProfitTarget() is used to exit.

    What must be changed to get the desired behaviour?
    1. CalculateOnBarClose = false
    2. If MarketPosition is not flat, or orders have been made, per the boolean flag in step 4, return without processing.
    3. On FirstTickOfBar, check if the price is higher or lower than your entry price, and reset the boolean flag from step 4.
    4. If higher, use a EnterLongStop(); if lower, use EnterLongLimit() and set a boolean flag to show that orders have been made
    Last edited by koganam; 09-19-2011, 03:32 PM.

    Comment


      #3
      Hello Stephan123,

      What you describe is the general mechanics of order placement in a historical strategy. Conditions are evaluated and if true, associated orders are submitted to the next bar following. Protective orders submitted with Set are applied upon entry execution.
      Ryan M.NinjaTrader Customer Service

      Comment


        #4
        In my strategy the price where the entry has to be done is always above the Open of the entry bar. Therefore I put an EnterLongLimit() order at the evaluation bar. Entry is at the next bar.
        To specify the exit price I use SetProfitTarget and SetStopLoss at the evaluation bar. Exit has to be at the next bar as well. It just doesn't work.
        I have set CalculateOnBarClose=true, because it's a backtest.

        Here is the code snippet:

        Code:
        if (condition ==true)
                                
                            {    
                                double buystop = NextBar().NextOpen[0] + 0.45 * MIN(Range(),2)[1];
                                
                                EnterLongLimit(1, buystop,"entry long");
                                DrawText("My text" + CurrentBar, buystop.ToString(), 0, High[0] + 5 * TickSize, Color.Red);
                                if (NextBar().NextClose[0] - buystop > 0)
                                {
                                double target = (NextBar().NextClose[0] - buystop ) / TickSize;
                                SetProfitTarget(CalculationMode.Ticks,target);    
                                }
                                if (buystop - NextBar().NextClose[0] > 0)
                                {
                                double stoploss = (buystop - NextBar().NextClose[0]) / TickSize;
                                SetStopLoss(CalculationMode.Ticks,stoploss);
                                }
                            }
        What's wrong? I still don't get the clue.

        Comment


          #5
          Originally posted by Stephan123 View Post
          In my strategy the price where the entry has to be done is always above the Open of the entry bar.
          It sounds like you want a stop or stoplimit order here rather than a limit order. A buy limit order placed above the last traded price is marketable and you will be filled every time. A stop order will only fill if trading reaches or exceeds the specified stop price.
          Ryan M.NinjaTrader Customer Service

          Comment


            #6
            Using entrylongstop() gives entries as I want, but there are some bars where entries are supposed to occur but are not occuring instead.

            May be it has to di with stop loss and profit target management.

            What I want to do is: Close the opened position when a target is reached or a
            stop loss price is reached. Stop Loss and TakeProfit are supposed to happen
            at the same bar as the entry.

            Therefore I set them at the same bar as the entrylongstop.
            Since the entry stop price is not always reached there are bars where StopLosss orders and
            take profit orders are generated without an entry taking place.
            They might be hit at a bar where they are not supposed to exist any more.

            How to get arround that?
            Is it possible to "delete" those stop loss/takeprofit orders when they are not needed any more or is there another simple solution?

            Comment


              #7
              The only way to really know what the strategy is doing under the hood is to use TraceOrders output. Monitor this closely to see what messages are reported during cases where you feel a stop order should have been placed or filled.

              Set orders cannot be cancelled, so if you are running into internal order handling rules here, it may require advanced order handling to achieve your order submission goals.

              Please let us know about any TraceOrders messages you are unsure about.
              Ryan M.NinjaTrader Customer Service

              Comment


                #8
                Hallo,

                I tried it like this within OnBarUpdate():

                buystop = NextBar().NextOpen[0] + 0.45 * MIN(Range(),2)[1];
                EnterLongStop(1, buystop,"entry long"); ExitLongLimit(NextBar().NextClose[0],"entrylong");

                The exit order is ignored because the entry is not done
                when the exit order is evaluated.

                This is the output:
                19.03.2011 00:00:00 Entered internal PlaceOrder() method at 19.03.2011 00:00:00: BarsInProgress=0 Action=Sell OrderType=Limit Quantity=0 LimitPrice=1293,1 StopPrice=0 SignalName='' FromEntrySignal='entry long'

                19.03.2011 00:00:00 Ignored PlaceOrder() method: Action=Sell OrderType=Limit Quantity=0 LimitPrice=1293,1 StopPrice=0 SignalName='' FromEntrySignal='entry long' Reason='This was an exit order but no position exists to exit'
                1293,1

                Is there a way to suppress the exit order evaluation untill the entry is done?
                (Both orders have to be done within the same bar)

                Comment


                  #9
                  For this you have to work in the advanced handlers, particularly OnExecution() to monitor for the fill. That way you can submit the exit order as soon as the entry order is filled.

                  If only using OnBarUpdate, it will take one bar before the position is recognized.

                  This sample can help submit protective orders during OnExecution()
                  The OnOrderUpdate() and OnExecution() methods are reserved for experienced programmers. Instead of using Set() methods to submit stop-loss and profit target orders, you can submit and update them manually through the use of IOrder and IExecution objects in the OnOrderUpdate() and OnExecution() methods. The OnOrderUpdate()
                  Ryan M.NinjaTrader Customer Service

                  Comment


                    #10
                    Originally posted by Stephan123 View Post
                    Hallo,

                    I tried it like this within OnBarUpdate():

                    buystop = NextBar().NextOpen[0] + 0.45 * MIN(Range(),2)[1];
                    EnterLongStop(1, buystop,"entry long"); ExitLongLimit(NextBar().NextClose[0],"entrylong");

                    The exit order is ignored because the entry is not done
                    when the exit order is evaluated.

                    This is the output:
                    19.03.2011 00:00:00 Entered internal PlaceOrder() method at 19.03.2011 00:00:00: BarsInProgress=0 Action=Sell OrderType=Limit Quantity=0 LimitPrice=1293,1 StopPrice=0 SignalName='' FromEntrySignal='entry long'

                    19.03.2011 00:00:00 Ignored PlaceOrder() method: Action=Sell OrderType=Limit Quantity=0 LimitPrice=1293,1 StopPrice=0 SignalName='' FromEntrySignal='entry long' Reason='This was an exit order but no position exists to exit'
                    1293,1

                    Is there a way to suppress the exit order evaluation untill the entry is done?
                    (Both orders have to be done within the same bar)
                    In order to use ExitLongLimit(), you must be in a long position already, not just have an order placed. Try using a MarketPosition filter. You may even then have to use an advanced order method, and use OnExecution() and OnOrderUpdate().

                    Comment


                      #11
                      How about the following approach?

                      Place an EnterLongStop at bar 1
                      wait at bar 2 for it's execution using OnExecution()
                      when executed, place at bar 2 an ExitLongStop or ExitLongLimit
                      wait for the fill. - At which bar this fill would happen? Still bar 2 or at bar 3?
                      I would need it at bar 2.

                      Another question:
                      Does TakeProfit and Stop Loss already take place at the bar where it is set
                      when the conditions are met?
                      If the above aproach doesn't work I could use SetTakeProfit and SetStopLoss
                      instead of ExitLongStop or ExitLongLimit in the above snippet.

                      Could it work like that?

                      Comment


                        #12
                        Originally posted by Stephan123 View Post
                        How about the following approach?

                        Place an EnterLongStop at bar 1
                        wait at bar 2 for it's execution using OnExecution()
                        when executed, place at bar 2 an ExitLongStop or ExitLongLimit
                        wait for the fill. - At which bar this fill would happen? Still bar 2 or at bar 3?
                        I would need it at bar 2.

                        Another question:
                        Does TakeProfit and Stop Loss already take place at the bar where it is set
                        when the conditions are met?
                        If the above aproach doesn't work I could use SetTakeProfit and SetStopLoss
                        instead of ExitLongStop or ExitLongLimit in the above snippet.

                        Could it work like that?
                        OnExecution() is a different event and has nothing to do with OnBarUpdate(), so regardless the COBC setting, your protective order could still end up being set on the same bar as the entry order. Your exit will happen whenever the exit price is reached, whichever bar that is on.

                        Yes, the simpler approach is to use SetStopLoss() and SetProfitTarget(), as these put in the OCO protective orders along with the entry order.

                        Comment


                          #13
                          Originally posted by koganam View Post
                          OnExecution() is a different event and has nothing to do with OnBarUpdate(), so regardless the COBC setting, your protective order could still end up being set on the same bar as the entry order. Your exit will happen whenever the exit price is reached, whichever bar that is on.

                          Yes, the simpler approach is to use SetStopLoss() and SetProfitTarget(), as these put in the OCO protective orders along with the entry order.
                          Of course OnExecution() is not the same as OnBarUpdate().
                          What I need to know is if orders can be forced to be executed in a certain order within the
                          same bar beeing in progress. For example, hindering the placement of an exit order until the entry is executed, then forcing the exit still within the same bar as the entry.

                          Comment


                            #14
                            Yes, you can use OnExecution() to submit an exit order as soon as the entry order is filled, and this will be submitted to the same bar. If the exit order is expected to fill, then you can have entry and exit same bar.
                            Ryan M.NinjaTrader Customer Service

                            Comment


                              #15
                              Originally posted by NinjaTrader_RyanM View Post
                              Yes, you can use OnExecution() to submit an exit order as soon as the entry order is filled, and this will be submitted to the same bar. If the exit order is expected to fill, then you can have entry and exit same bar.
                              Great. So this will be the way to go.
                              I asssume this works in backtests with CalculateOnBarClose=true as well. Correct?
                              I will write a custom exit-function within UserDefinedMethods providing the desired functionality.
                              I would call this function from within OnBarUpdate.
                              Therefore I need to know the following:
                              Is it possible to call OnOrderUpdate and OnExecution from within OnBarUpdate?
                              Otherwise I don't see a way to call this userdefined exit-function from within OnBarUpdate.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by rhyminkevin, Today, 04:58 PM
                              3 responses
                              49 views
                              0 likes
                              Last Post Anfedport  
                              Started by iceman2018, Today, 05:07 PM
                              0 responses
                              5 views
                              0 likes
                              Last Post iceman2018  
                              Started by lightsun47, Today, 03:51 PM
                              0 responses
                              7 views
                              0 likes
                              Last Post lightsun47  
                              Started by 00nevest, Today, 02:27 PM
                              1 response
                              14 views
                              0 likes
                              Last Post 00nevest  
                              Started by futtrader, 04-21-2024, 01:50 AM
                              4 responses
                              50 views
                              0 likes
                              Last Post futtrader  
                              Working...
                              X