Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strategy orders at Tick

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

    Strategy orders at Tick

    Is there a way stop and limit orders are executed at the moment they appear on the market on a strategy backtesting? Currently they just execute at the end of a candle, living strange situations as entry and sell on the same down bar on a long position....

    #2
    Hello zaico, and thank you for your question.

    If you change your script's CalculateOnBarClose setting to false in your Initialize section, your OnBarUpdate routine will run once a tick instead of once a bar. I believe this is the setting you were looking for. For any actions you'd prefer to only take once a bar in this mode, we make available the IsFirstTickOfBar boolean.

    If you would prefer to take action even more often than upon each fully formed tick, we also expose the method OnMarketData, documented here,



    That will execute the instant a new Last, Bid, or Ask price comes in. Please let us know if there are any other ways we can help.
    Jessica P.NinjaTrader Customer Service

    Comment


      #3
      that's not what I meant. I want stop and limit after beeing set, to be checked at every tick. Just as if it was life. Otherwise, things like this happen:
      Click image for larger version

Name:	Captura de pantalla de 2016-12-12 20:41:31.png
Views:	1
Size:	4.8 KB
ID:	881407

      Comment


        #4
        If you would like to modify orders every tick, you may set up a 1-tick data series and modify orders within this series. I have attached a script in which the profit target and stop loss are kept 5 ticks on either side of a 14 period SMA every single tick. This script is being provided as an educational aid only and is not intended for live trading.
        Attached Files
        Last edited by NinjaTrader_JessicaP; 12-12-2016, 02:05 PM.
        Jessica P.NinjaTrader Customer Service

        Comment


          #5
          thanks, but that's not exactly what I asked. My orders are not modified, are set and static since the moment I set my limit buy for a long position.

          In my strategy I have a condition, and when It appears I set a limit stop and profit target. Let's say profit and stop are 10 points both on each side. I can't do that with what you show since my limits are static.

          Comment


            #6
            The advice I gave first, setting CalculateOnBarClose to false, allows you to take every action on each tick except for modifying orders.

            The advice I gave second fills that gap, setting up a 1-tick data series, allowing you to modify orders.

            If neither of these pieces of advice will cover your use case, would it be possible for you to provide a stripped-down work-in-progress code sample for us to review? I will be happy to either answer questions regarding this code sample or, if a small educational sample would be more appropriate, modify it and return it. Please note that while our staff can not perform development work we are happy to provide demonstrative code samples to the public.
            Jessica P.NinjaTrader Customer Service

            Comment


              #7
              Code:
                      protected override void Initialize()
                      {
              			CalculateOnBarClose = false;
              			ExitOnClose = true;
              			IncludeCommission = true;
                      }
              		
                      protected override void OnBarUpdate()
                      {
              		if (pattern found){
              
              						SetStopLoss("", CalculationMode.Ticks, 10, false);
              						SetProfitTarget("", CalculationMode.Ticks, 10);
              						EnterLongLimit(0, true, 1, MIN(Low, 3)[0], "Long Entry");
              					}
              				
              		}
              As you see is a very simple code. The only thing I want is that both stop and profit target area check every tick.

              Comment


                #8
                The following changes will accomplish your goals.

                Code:
                [FONT=Courier New]
                       protected override void Initialize()
                        {
                            CalculateOnBarClose = false;
                            ExitOnClose = true;
                            IncludeCommission = true;[B]
                            Add(PeriodType.Tick, 1);
                            SetProfitTarget(CalculationMode.Ticks, 10);
                            SetStopLoss(CalculationMode.Ticks, 10);[/B]
                        }
                        
                        protected override void OnBarUpdate()
                        {[B]
                            if (BarsInProgress != 1)
                            {
                               return;
                            }[/B]
                        if (pattern found){
                                        EnterLongLimit(0, true, 1, MIN(Low, 3)[0], "Long Entry");
                                    }
                                
                        }[/FONT]
                Jessica P.NinjaTrader Customer Service

                Comment


                  #9
                  Thanks. But something is not right. Now the condition is never accomplished.

                  This is how the code is now:

                  Code:
                       protected override void Initialize()
                          {
                              CalculateOnBarClose = false;
                              ExitOnClose = true;
                              IncludeCommission = true;
                              Add(PeriodType.Tick, 1);
                              SetProfitTarget(CalculationMode.Ticks, 10);
                              SetStopLoss(CalculationMode.Ticks, 10);
                          }
                  
                          /// <summary>
                          /// Called on each bar update event (incoming tick)
                          /// </summary>
                          protected override void OnBarUpdate()
                          {
                          
                              if (BarsInProgress != 1)
                              {
                  				return;
                              }
                         		 if (Close[0] == 11212){//example
                                          EnterLongLimit(0, true, 1, MIN(Low, 3)[0], "Long Entry");
                  			Print("Time: " + Time[0]+"       Stochastic: K" + Stochastics(7,14,3).K[0]+"   D:"+Stochastics(7,14,3).D[0]); //to check if ever arrives here
                  
                                      }
                                  
                          }
                  Last edited by zaico; 12-13-2016, 05:02 AM.

                  Comment


                    #10
                    While directly debugging user code is beyond the scope of the services we can provide, I can provide some hints that can help you get started debugging your code. There are two potential reasons why your condition is not met.

                    I will lead off with the least likely reason. One thing that could occur here is that our example condition is never met. It is better to use ranges when using double precision values. I would like to recommend the following as a replacement for your example clause.

                    Code:
                    [FONT=Courier New]if (11211.75 <= Close[0] && Close[0] <= 11212.25)[/FONT]
                    If a quarter of a point is too broad a tolerance range you can use the Market Replay connection and trial and error to find the right value for your strategy to subtract from, and add to, 11212 .

                    If you would like to test the rest of your code outside of this condition, I would like to instead suggest replacing this by this first. Once you are sure the code outside this condition is working as expected you can then use your own entry condition with confidence in the rest of the code.

                    Code:
                    [FONT=Courier New]if (Position.MarketPosition == MarketPosition.Flat)[/FONT]
                    Second I would like to mention what I believe is preventing your code from placing trades. If you ran the above condition, you probably noticed that code near EnterLong was being reached, but EnterLongLimit was not placing trades. This lets us know that what is occurring has to be occurring with the EnterLongLimit call itself. To discover what is occurring, we can turn to the documentation for EnterLongLimit.

                    Originally posted by http://ninjatrader.com/support/helpGuides/nt7/enterlonglimit.htm
                    Syntax

                    EnterLongLimit(double limitPrice)
                    EnterLongLimit(double limitPrice, string signalName)

                    EnterLongLimit(int quantity, double limitPrice)

                    EnterLongLimit(int quantity, double limitPrice, string signalName)



                    The following method variation is for experienced programmers who fully understand Advanced Order Handling concepts.



                    EnterLongLimit(int barsInProgressIndex, bool liveUntilCancelled, int quantity, double limitPrice, string signalName)
                    You have chosen to use the last form in your code. I would like to recommend first experimenting with one of the simpler forms to see if it places trades. Try doing this when BarsInProgress == 0, and BarsInProgress == 1.

                    If you have gone through the above steps, you now have a thorough understanding as far as why your code is not placing trades in this way. This is the solution you should arrive at :

                    Code:
                    [FONT=Courier New]EnterLongLimit([B]1[/B], true, 1, MIN(Low, 3)[0], "Long Entry");[/FONT]
                    That said, since this form is restricted to using a signal, I would strongly encourage you to use one of the simpler forms of EnterLongLimit without a signal name, and to ensure outside this call that your BarsInProgress == 1. Otherwise, you will have to refactor your SetProfitTarget and SetStopLoss methods so that they use this same FromEntrySignal. Since this involves a design decision, implementing this must be left up to you.
                    Jessica P.NinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by pechtri, 06-22-2023, 02:31 AM
                    9 responses
                    122 views
                    0 likes
                    Last Post NinjaTrader_ChelseaB  
                    Started by frankthearm, 04-18-2024, 09:08 AM
                    16 responses
                    66 views
                    0 likes
                    Last Post NinjaTrader_Clayton  
                    Started by habeebft, Today, 01:18 PM
                    1 response
                    5 views
                    0 likes
                    Last Post NinjaTrader_ChelseaB  
                    Started by benmarkal, Today, 12:52 PM
                    2 responses
                    15 views
                    0 likes
                    Last Post benmarkal  
                    Started by f.saeidi, Today, 01:38 PM
                    1 response
                    9 views
                    0 likes
                    Last Post NinjaTrader_BrandonH  
                    Working...
                    X