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

how to avoir reintering in the market on the same candle

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

    how to avoir reintering in the market on the same candle

    Hello,

    I have a problem as when backtesting my strategy, in some circumstancies If my position is flattened and conditions are still true a new order is submitted while I would like to avoir reintering on the same candle.
    Please help me

    #2
    Hello Kamala,

    Thanks for your post and welcome to the NinjaTrader forums!

    You can use the method BarsSinceExitExecution() as part of your entry conditions.

    Please see the helpguide link for example of use: https://ninjatrader.com/support/help...texecution.htm
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      follow up

      Originally posted by NinjaTrader_Paul View Post
      Hello Kamala,

      Thanks for your post and welcome to the NinjaTrader forums!

      You can use the method BarsSinceExitExecution() as part of your entry conditions.

      Please see the helpguide link for example of use: https://ninjatrader.com/support/help...texecution.htm
      Thank you for your reply , but when I try to use the wizard using barsince exit it prevents from generating trades, it looks like if I enter the code manually it seems ok...
      Also I would like to know if i enter a condition and the condition is not achieved on the next bar, how long is this condition remaining alive?
      Thank you

      Comment


        #4
        Hello Kamala,

        Thanks for your reply.

        From the helpguide it advises that "An int value that represents a number of bars. A value of -1 will be returned if a previous exit does not exist."

        Which means you need to check for a -1 or 1, please see the example in the previous helpguidelink, i've partially copied here: if ((BarsSinceExitExecution() > 10 || BarsSinceExitExecution() == -1) && The statement is checking for a -1 or >10 bars. the check for -1 is for the case where no previous trades have occurred and that alone would prevent any trades if you did not check for -1.

        In the strategy builder you would need to create a condition group that is set to "If any" and then create two conditions one for -1 and the other for 1. The condition group would then be used in the "if all" of the entry conditions.

        An example of a strategy builder condition group can be found in the section "How to create time filters" in the conditions builder section of the helpguide: https://ninjatrader.com/support/help...on_builder.htm

        If by condition you mean entry order, if the order is not filled it is automatically canceled on the next bar (unless your code resubmits on the next bar).
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          dowloading long period of data

          Thank you very much it all works now
          The problem I have now is downloading a chart of the dax on a very long period of time, I have a problem because the chart does not provide me with the most liquid expiry which result in an inaccurate backtest...
          Please can you help me...
          Best regards

          Comment


            #6
            Hello Kamala,

            Thanks for your reply.

            I'm not sure I understand, can you clarify with some specifics of what you have tried, what you see and how you have determined, " most liquid expiry"?
            Paul H.NinjaTrader Customer Service

            Comment


              #7
              It s ok I think I fixed it
              I have another question regarding stop order
              When I place buy stop order at the previous close level, in the backtest it is sometimes triggered and sometimes not, could you help me please

              Comment


                #8
                Hello Kamala,

                Thanks for your reply.

                Please post some examples along with your code for review. If you prefer not to publicly post your strategy, please feel free to write into PlatformSDupport[at]Ninjatrader[dot]com. Mark your e-mail atten:Paul and include your strategy source code file, screenshots of the strategy analyzer settings applied, screenshots that identify the instrument time frame and time of the order in question so I can replicate.
                Paul H.NinjaTrader Customer Service

                Comment


                  #9
                  PLease find attached an example and the code

                  namespace NinjaTrader.NinjaScript.Strategies
                  {
                  public class Trend : Strategy
                  {
                  private HMA HMA1;
                  private SMA SMA1;
                  private SMA SMA2;

                  protected override void OnStateChange()
                  {
                  if (State == State.SetDefaults)
                  {
                  Description = @"Enter the description for your new custom Strategy here.";
                  Name = "Trend";
                  Calculate = Calculate.OnPriceChange;
                  EntriesPerDirection = 1;
                  EntryHandling = EntryHandling.AllEntries;
                  IsExitOnSessionCloseStrategy = true;
                  ExitOnSessionCloseSeconds = 260;
                  IsFillLimitOnTouch = true;
                  MaximumBarsLookBack = MaximumBarsLookBack.Infinite;
                  OrderFillResolution = OrderFillResolution.High;
                  OrderFillResolutionType = BarsPeriodType.Second;
                  OrderFillResolutionValue = 15;
                  Slippage = 3;
                  StartBehavior = StartBehavior.WaitUntilFlat;
                  TimeInForce = TimeInForce.Day;
                  TraceOrders = true;
                  RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
                  StopTargetHandling = StopTargetHandling.PerEntryExecution;
                  BarsRequiredToTrade = 20;
                  // Disable this property for performance gains in Strategy Analyzer optimizations
                  // See the Help Guide for additional information
                  IsInstantiatedOnEachOptimizationIteration = true;
                  Contrats_échange = 180;
                  Profit1 = 80;
                  Stop1 = 150;
                  Bar_avantréentré = 0;
                  Longuer_MMS = 60;
                  Longuer_MML = 150;
                  }
                  else if (State == State.Configure)
                  {
                  SetProfitTarget("", CalculationMode.Ticks, Profit1);
                  SetStopLoss("", CalculationMode.Ticks, Stop1, false);
                  }
                  else if (State == State.DataLoaded)
                  {
                  HMA1 = HMA(Convert.ToInt32(Longuer_MMS));
                  HMA1.Plots[0].Brush = Brushes.Crimson;
                  AddChartIndicator(HMA1);
                  SMA1 = SMA(Convert.ToInt32(Longuer_MML));
                  SMA1.Plots[0].Brush = Brushes.Chartreuse;
                  AddChartIndicator(SMA1);
                  SMA2 = SMA(Convert.ToInt32(Longuer_MML));
                  SMA2.Plots[0].Brush = Brushes.Lime;
                  AddChartIndicator(SMA2);
                  }
                  }

                  protected override void OnBarUpdate()
                  {
                  if (CurrentBars[0] < 1)
                  return;

                  // Set 1
                  if (
                  // Condition group 1
                  ((BarsSinceExitExecution() == -1)
                  || (BarsSinceExitExecution() >= Bar_avantréentré))
                  && (HMA1[1] < HMA1[0])
                  && (SMA1[1] < SMA2[0]))
                  {
                  EnterLongStopMarket(Convert.ToInt32(DefaultQuantit y), High[1], "");
                  }

                  }

                  #region Properties
                  [NinjaScriptProperty]
                  [Range(10, int.MaxValue)]
                  [Display(ResourceType = typeof(Custom.Resource), Name="Contrats_échange", Order=1, GroupName="NinjaScriptStrategyParameters")]
                  public int Contrats_échange
                  { get; set; }

                  [NinjaScriptProperty]
                  [Range(5, int.MaxValue)]
                  [Display(ResourceType = typeof(Custom.Resource), Name="Profit1", Order=2, GroupName="NinjaScriptStrategyParameters")]
                  public int Profit1
                  { get; set; }

                  [NinjaScriptProperty]
                  [Range(5, int.MaxValue)]
                  [Display(ResourceType = typeof(Custom.Resource), Name="Stop1", Order=3, GroupName="NinjaScriptStrategyParameters")]
                  public int Stop1
                  { get; set; }

                  [NinjaScriptProperty]
                  [Range(0, int.MaxValue)]
                  [Display(ResourceType = typeof(Custom.Resource), Name="Bar_avantréentré", Order=4, GroupName="NinjaScriptStrategyParameters")]
                  public int Bar_avantréentré
                  { get; set; }

                  [NinjaScriptProperty]
                  [Range(20, int.MaxValue)]
                  [Display(ResourceType = typeof(Custom.Resource), Name="Longuer_MMS", Order=5, GroupName="NinjaScriptStrategyParameters")]
                  public int Longuer_MMS
                  { get; set; }

                  [NinjaScriptProperty]
                  [Range(100, int.MaxValue)]
                  [Display(ResourceType = typeof(Custom.Resource), Name="Longuer_MML", Order=6, GroupName="NinjaScriptStrategyParameters")]
                  public int Longuer_MML
                  { get; set; }
                  #endregion

                  }
                  }
                  Attached Files

                  Comment


                    #10
                    Hello Kamala,

                    Thanks for your reply.

                    Please post a screenshot of your strategy analyzer settings so I can replicate. I need to see what instrument and time frame is being used.
                    Paul H.NinjaTrader Customer Service

                    Comment


                      #11
                      Here it is
                      Attached Files

                      Comment


                        #12
                        Hello Kamala,

                        Thanks for your reply.

                        In the case of why did the September 7th bar not fill, the reason is because the conditions to place the order were true on the September 6th bar, the order was placed the following day based on the High of the September 5th bar which was 12197.5. The Setptember 7th bars low was 12247.5 which is well above the EnterLongStopMarket and the order was ignored. A longstoporder must be placed higher than the current price

                        In the other case where it did fill, it filled because the price was below the High[1] of the prior bar to the bar where the conditions to place the order were true.

                        In summary, in backtesting you are using historical data and as such the calculate mode is OnbarClose meaning that the strategy code is not executed until the end of the bar and any orders submitted would not be filled until the following day/bar.

                        To help you see this, you may want to have your strategy draw something or perhaps set the bar color or background color when your entry conditions are true. Once you see this then you can see where the entry was conditions were true and can see that the bar before was the High[1] that was used on the bar after the entry bar.
                        Paul H.NinjaTrader Customer Service

                        Comment


                          #13
                          Thank you very much for your clear answers!

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by DJ888, 04-16-2024, 06:09 PM
                          6 responses
                          18 views
                          0 likes
                          Last Post DJ888
                          by DJ888
                           
                          Started by Jon17, Today, 04:33 PM
                          0 responses
                          1 view
                          0 likes
                          Last Post Jon17
                          by Jon17
                           
                          Started by Javierw.ok, Today, 04:12 PM
                          0 responses
                          6 views
                          0 likes
                          Last Post Javierw.ok  
                          Started by timmbbo, Today, 08:59 AM
                          2 responses
                          10 views
                          0 likes
                          Last Post bltdavid  
                          Started by alifarahani, Today, 09:40 AM
                          6 responses
                          41 views
                          0 likes
                          Last Post alifarahani  
                          Working...
                          X