Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

cancel order

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

    #16
    chancehero, since you're already working in CalculateOnBarClose = false I would suggest you CancelOrder() directly in your OnBarUpdate then, you can access GetCurrentBid, GetCurrentAsk and Last (Close[0]) there, too.

    Comment


      #17
      !!! yeah !

      Allright so it now owrks, the cancel part after 2 ticks. now my next problem was the calculateonbarclose = false. when I get a order, then as long as the bar is not over it keeps buying in as it sell. I was thinking of adding a true/false statement, such that it begins as false, enter long, turn true, but only resets when the bar is ended. would that work? or it there a very easy command I missed.

      JP


      PHP Code:
      #region Using declarations
      using System;
      using System.ComponentModel;
      using System.Diagnostics;
      using System.Drawing;
      using System.Drawing.Drawing2D;
      using System.Xml.Serialization;
      using NinjaTrader.Cbi;
      using NinjaTrader.Data;
      using NinjaTrader.Indicator;
      using NinjaTrader.Gui.Chart;
      using NinjaTrader.Strategy;
      #endregion
      
      // This namespace holds all strategies and is required. Do not change it.
      namespace NinjaTrader.Strategy
      {
          /// <summary>
          /// sell after 2 ticks above order limit
          /// </summary>
          [Description("sell after 2 ticks above order limit")]
          public class test6 : Strategy
          {
              #region Variables
              private IOrder myEntryOrder = null;
              private int barNumberOfOrder = 0;
              private double price1 = 0.00;
              #endregion
      
              /// <summary>
              /// This method is used to configure the strategy and is called once before any strategy method is called.
              /// </summary>
              protected override void Initialize()
              {
                  SetProfitTarget("LongA1",CalculationMode.Ticks, 1);
                  SetStopLoss("LongA1",CalculationMode.Ticks, 10,false);
                  TraceOrders = true;
                  CalculateOnBarClose = false;
              }
      
              /// <summary>
              /// Called on each bar update event (incoming tick)
              /// </summary>
              protected override void OnBarUpdate()
              {
                  if (CurrentBar < 1)
                      return;
                  if(Position.MarketPosition == MarketPosition.Flat)
                  {
                      if(myEntryOrder == null && Close[0] > Open[1])
                      {
                          myEntryOrder = EnterLongLimit(0,true,1,Close[0] - 1 * TickSize ,"LongA1");
                          barNumberOfOrder = CurrentBar;
                          price1 = Close[0]-1*TickSize;
                          Print("Wanting to go long at" + Close[0]);
                      }
                      else if (myEntryOrder != null && CurrentBar > barNumberOfOrder)
                      {
                          ExitLong("LongA1");
                          CancelOrder(myEntryOrder);
                          myEntryOrder = null;
                          price1 = 0.00;
                          Print("order closed after 2 bar");
                      }
                      else if (myEntryOrder != null && GetCurrentBid() > price1 + 2 * TickSize)
                      {
                          CancelOrder(myEntryOrder);
                          myEntryOrder = null;
                          price1 = 0.00;
                          Print("XXXXXXXXXXXXXX"+ GetCurrentBid() + "cancel +2 ticks");
                      }    
                  }
              }
              #region Properties
              
              #endregion
          }
      } 
      

      Comment


        #18
        Great job - you can just add a bool flag for this and include it in your conditions when you submit the order first time and reset it on the FirstTickOfBar then to allow for a new entry.

        Comment


          #19
          Something like this?

          Hi ok, sorry for the later reply. So tried it but this is what I have, still I get the loop of orders flooding in. Help.

          Here is my flag bit:
          PHP Code:
          #region Using declarations
          using System;
          using System.ComponentModel;
          using System.Diagnostics;
          using System.Drawing;
          using System.Drawing.Drawing2D;
          using System.Xml.Serialization;
          using NinjaTrader.Cbi;
          using NinjaTrader.Data;
          using NinjaTrader.Indicator;
          using NinjaTrader.Gui.Chart;
          using NinjaTrader.Strategy;
          #endregion
          
          // This namespace holds all strategies and is required. Do not change it.
          namespace NinjaTrader.Strategy
          {
              /// <summary>
              /// sell after 2 ticks above order limit
              /// </summary>
              [Description("sell after 2 ticks above order limit")]
              public class test6 : Strategy
              {
                  #region Variables
                  private IOrder myEntryOrder = null;
                  private int barNumberOfOrder = 0;
                  private double price1 = 0.00;
                  private bool bar1 = false;
                  private bool FirstTickOfBar = false;
                  #endregion
          
                  /// <summary>
                  /// This method is used to configure the strategy and is called once before any strategy method is called.
                  /// </summary>
                  protected override void Initialize()
                  {
                      SetProfitTarget("LongA1",CalculationMode.Ticks, 1);
                      SetStopLoss("LongA1",CalculationMode.Ticks, 10,false);
                      TraceOrders = true;
                      CalculateOnBarClose = false;
                  }
          
                  /// <summary>
                  /// Called on each bar update event (incoming tick)
                  /// </summary>
                  protected override void OnBarUpdate()
                  {
                      if (CurrentBar < 1)
                          return;
                      
                      if(Position.MarketPosition == MarketPosition.Flat)
                      {
                          if(bar1 == false && myEntryOrder == null && Close[0] > Open[0])
                          {
                              myEntryOrder = EnterLongLimit(0,true,1,Close[0] - 1 * TickSize ,"LongA1");
                              barNumberOfOrder = CurrentBar;
                              price1 = Close[0]-1*TickSize;
                              bar1 = true;
                              Print("Wanting to go long at" + Close[0]);
                          }
                          else if (myEntryOrder != null && CurrentBar > barNumberOfOrder)
                          {
                              ExitLong("LongA1");
                              CancelOrder(myEntryOrder);
                              myEntryOrder = null;
                              price1 = 0.00;
                              Print("order closed after 2 bar");
                          }
                          else if (myEntryOrder != null && GetCurrentBid() > price1 + 2 * TickSize)
                          {
                              CancelOrder(myEntryOrder);
                              myEntryOrder = null;
                              price1 = 0.00;
                              Print("XXXXXXXXXXXXXX"+ GetCurrentBid() + "cancel +2 ticks");
                          }    
                          else if (FirstTickOfBar == true)
                              bar1 = false;
                      }
                  }
                  #region Properties
                  
                  #endregion
              }
          } 
          

          Comment


            #20
            No, more like this - FirstTickOfBar is a reserverd property / keyword in NT - a pseudo code of how to attack it below -

            if (yourTrigger && okToTrade = true)
            Enter()...
            ok ToTrade = false;

            if (FirstTickOfBar)
            okToTrade = true;

            Comment


              #21
              got it

              I believe this makes more sense now.

              PHP Code:
              #region Using declarations
              using System;
              using System.ComponentModel;
              using System.Diagnostics;
              using System.Drawing;
              using System.Drawing.Drawing2D;
              using System.Xml.Serialization;
              using NinjaTrader.Cbi;
              using NinjaTrader.Data;
              using NinjaTrader.Indicator;
              using NinjaTrader.Gui.Chart;
              using NinjaTrader.Strategy;
              #endregion
              
              // This namespace holds all strategies and is required. Do not change it.
              namespace NinjaTrader.Strategy
              {
                  /// <summary>
                  /// sell after 2 ticks above order limit
                  /// </summary>
                  [Description("sell after 2 ticks above order limit")]
                  public class test6 : Strategy
                  {
                      #region Variables
                      private IOrder myEntryOrder = null;
                      private int barNumberOfOrder = 0;
                      private double price1 = 0.00;
                      private bool OkToTrade = true;
                      #endregion
              
                      /// <summary>
                      /// This method is used to configure the strategy and is called once before any strategy method is called.
                      /// </summary>
                      protected override void Initialize()
                      {
                          SetProfitTarget("LongA1",CalculationMode.Ticks, 1);
                          SetStopLoss("LongA1",CalculationMode.Ticks, 10,false);
                          TraceOrders = true;
                          CalculateOnBarClose = false;
                      }
              
                      /// <summary>
                      /// Called on each bar update event (incoming tick)
                      /// </summary>
                      protected override void OnBarUpdate()
                      {
                          if (CurrentBar < 1)
                              return;
                          
                          if(Position.MarketPosition == MarketPosition.Flat)
                          {
                              if(OkToTrade == true && myEntryOrder == null && Close[0] > Open[0])
                              {
                                  myEntryOrder = EnterLongLimit(0,true,1,Close[0] - 1 * TickSize ,"LongA1");
                                  barNumberOfOrder = CurrentBar;
                                  price1 = Close[0]-1*TickSize;
                                  OkToTrade = false;
                                  Print("Wanting to go long at" + Close[0]);
                              }
                              if (myEntryOrder != null && CurrentBar > barNumberOfOrder)
                              {
                                  ExitLong("LongA1");
                                  CancelOrder(myEntryOrder);
                                  myEntryOrder = null;
                                  price1 = 0.00;
                                  Print("order closed after 2 bar");
                              }
                              if (myEntryOrder != null && GetCurrentBid() > price1 + 2 * TickSize)
                              {
                                  CancelOrder(myEntryOrder);
                                  myEntryOrder = null;
                                  price1 = 0.00;
                                  Print("XXXXXXXXXXXXXX"+ GetCurrentBid() + "cancel +2 ticks");
                              }    
                          }
                              if (FirstTickOfBar)
                                  OkToTrade = true;
                          
                      }
                      #region Properties
                      
                      #endregion
                  }
              } 
              

              The next question I have now is the following:

              If I have 2 similar strategy sunning at the same time:

              (1) they cannot work together, correct?
              (2) if I have a limit of 1 order max, and one strat is filled and going long 1 , then the second strat will not enter if I am long 1.


              thank you

              Comment


                #22
                chancehero,

                1. Correct, strategies do not interact with each other.
                2. No, the strategies are completely independent of each other. The EntriesPerDirection is on a per strategy basis and not on a per account basis.
                Josh P.NinjaTrader Customer Service

                Comment


                  #23
                  would it be possible to set a limit of positions per account?

                  so what I am doing is making use of the time manager , to set specific trading times for a strategy, but a lot of the time the time frames overlap but I do not want to have multiple contracts. I guess I could explicitly map all the time frame I have in the strategy itself such that I would not need to use the session manager. but that looks like alot of work.

                  JP

                  Comment


                    #24
                    Unfortunately you can't set a limit per account used, however you could maintain open positions across strategies for example in a text file to keep track and then stop taking new entries if you're at the max #.

                    Comment


                      #25
                      ok, if I try to do this complete time business, you will be able to give some advice, as it feels like its going to get confusing.

                      JP

                      Comment


                        #26
                        I'm not exactly sure what you refer to by 'complete time business' but for sure I'll try lending you a hand here...

                        Comment


                          #27
                          Hello,

                          Thanks for your note,

                          To do this you would need to add a secondary bar series to the indicator or strategy, which would be a 30 minute bar series as you would need to add more granularity to see deeper into the bar.

                          Please see the following sample and information:

                          You can submit orders to different Bars objects. This allows you the flexibility of submitting orders to different timeframes. Like in live trading, taking entry conditions from a 5min chart means executing your order as soon as possible instead of waiting until the next 5min bar starts building. You can achieve this by


                          Also,



                          Let me know if I can be of further assistance.
                          BrettNinjaTrader Product Management

                          Comment


                            #28
                            Hello,

                            As this is not how NinjaTrader is setup to create bars.

                            Each minute bar will always close on the hour, you cannot change the time that the bars open or close. You can only change the closing time by changing the bar series period. Down from a 60 minute to a 30 minute is essentially doing the same thing, it is changing the bars close time and thus its next open time.

                            This is simply how you do it in NinjaTrader.

                            If you want this indicator to run only in realtime since NinjaTrader reciving the ticks live you could store a value at any time and use that in our calculations live, however when your backtesting or wanting the strategy to run correctly on historical data the bars input OHLC of the bar is the only thing that is known, you cannot look intraday or instraminute inside a 60 minute chart. You must add granularity with a secondary bar series.

                            If you want to be able to take a value from any time you could add a 1 minute series and then use a time filter to specify which value out of the 1 minute series you want.

                            However this will lengthen historical processing time and backtesting sime since there are 60 more OHLC data points for each hour. Vs 1 OHLC data point for each hour, this is the reason this is setup in this fashion.

                            Let me know if I can be of further assistance.
                            BrettNinjaTrader Product Management

                            Comment


                              #29
                              ok I think I get it

                              ok I think I get it. is there any reference sample to saving values?

                              1) let say, I load up my chart,

                              I ask because I believe I know what you are saying but I am still unsure as to how I should proceed.

                              Jp


                              I will do some coding and place it on the post next week.
                              Last edited by chancehero; 04-04-2013, 09:51 AM.

                              Comment


                                #30
                                Hello,

                                How I would tackle this is only do the comparison once at the time your ready to start the trade.

                                You will need a few time filters for this.

                                Quick question though are you planning on running Calculater On Bar Close True or False?

                                I look forward to assisting you further.
                                BrettNinjaTrader Product Management

                                Comment

                                Latest Posts

                                Collapse

                                Topics Statistics Last Post
                                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                                0 responses
                                675 views
                                0 likes
                                Last Post Geovanny Suaza  
                                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                                0 responses
                                379 views
                                1 like
                                Last Post Geovanny Suaza  
                                Started by Mindset, 02-09-2026, 11:44 AM
                                0 responses
                                111 views
                                0 likes
                                Last Post Mindset
                                by Mindset
                                 
                                Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                                0 responses
                                578 views
                                1 like
                                Last Post Geovanny Suaza  
                                Started by RFrosty, 01-28-2026, 06:49 PM
                                0 responses
                                584 views
                                1 like
                                Last Post RFrosty
                                by RFrosty
                                 
                                Working...
                                X