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 cancel buy order if limited price is jumped

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

    How to cancel buy order if limited price is jumped

    I would like how to compile this condition:

    If actual ask price is bigger than my actual limited buy order price (because it has been a big volatility in the market and the order has not been filled) close long order.

    Thank you very much.

    #2
    Hello ikerbar,

    Thank you for your post.

    You will need to IOrder objects for the orders so that you can call CancelOrder() to cancel the order based on your condition.

    For information on CancelOrder() please visit the following link: http://www.ninjatrader.com/support/h...ancelorder.htm

    For information on IOrder objects please visit the following link: http://www.ninjatrader.com/support/h...nt7/iorder.htm

    Then using GetCurrentAsk() you can check the current ask value against the price of the IOrder object:
    Code:
    			if(GetCurrentAsk(0) >= myOrder.LimitPrice)
    			{
    				CancelOrder(myOrder);
    			}
    For information on GetCurrentAsk() please visit the following link: http://www.ninjatrader.com/support/h...currentask.htm

    Please let me know if I may be of further assistance.

    Comment


      #3
      I have a problem, i dont know how to compile (write the exact word) in order to cancel my running order. The words in red give me compiling errors.

      My compilation for a short order:

      {
      // Condition set 1
      if (ToTime(DateTime.Now) >= ToTime ( 10, 29, 57) && ToTime(DateTime.Now) <= ToTime ( 10, 29, 58))
      {
      EnterShortStopLimit( 1, GetCurrentBid() - 11 * TickSize, GetCurrentBid() - 5 * TickSize, "corto")
      }
      // Condition set 2 "if the price currentbid jump my limitprice and the order has not been filled, cancel the order"
      if (ToTime(DateTime.Now) >= ToTime ( 10, 29, 59) && GetCurrentBid() < ShortStopLimit.LimitPrice && OrderStatus is not Working)
      {
      CancelOrder(ShortStopLimit);
      }
      }
      Last edited by ikerbar; 09-18-2013, 04:02 AM.

      Comment


        #4
        Hello ikerbar,

        Thank you for your post.

        Below you will find how to properly add an IOrder object and call that IOrder object to cancel an order:
        Code:
        [B]#region Variables		
                private IOrder myOrder;
                #endregion[/B]
        		
        		protected override void OnBarUpdate()
        		{
        // Condition set 1
        if (ToTime(DateTime.Now) >= ToTime ( 10, 29, 57) && ToTime(DateTime.Now) <= ToTime ( 10, 29, 58))
        {
        [B]myOrder =[/B] EnterShortStopLimit( 1, GetCurrentBid() - 11 * TickSize, GetCurrentBid() - 5 * TickSize, "corto");
        }
        // Condition set 2 "if the price currentbid jump my limitprice and the order has not been filled, cancel the order"
        if (ToTime(DateTime.Now) >= ToTime ( 10, 29, 59) && ...)
        {
        [B]CancelOrder(myOrder);[/B]
        }
        I recommend reviewing the information on IOrder at the following link: http://www.ninjatrader.com/support/h...nt7/iorder.htm

        Please let me know if I may be of further assistance.

        Comment


          #5
          Thank you very much,
          last question about it (sorry of been a noob)

          if i want to cancel the order if the order hasnt been filled can i write this?

          if (myOrder.OrderState != OrderState.Filled)

          CancelOrder (myOrder)

          Thanks in advance

          Comment


            #6
            Hello ikerbar,

            Thank you for your response.

            That is the correct way to check if the order has not filled.

            Comment


              #7
              Hi again,
              after using my programed strategy, the order didnt cancel when it was not filled. What am i missing? Thanx in advance.

              My compiled strategy:

              #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>
              /// Enter the description of your strategy here
              /// </summary>
              [Description("Enter the description of your strategy here")]
              public class GasBajadaCancel21 : Strategy
              {
              #region Variables
              // Wizard generated variables
              private int ordenes = 1; // Default setting for Ordenes
              private int stop = 20; // Default setting for Stop
              // User defined variables (add any user defined variables below)
              private IOrder myOrder1;
              #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("", CalculationMode.Ticks, 60);
              SetTrailStop("", CalculationMode.Ticks, 20, false);

              CalculateOnBarClose = false;
              }

              /// <summary>
              /// Called on each bar update event (incoming tick)
              /// </summary>
              protected override void OnMarketData(MarketDataEventArgs e)

              {
              // Condition set 1
              if (ToTime(DateTime.Now) >= ToTime ( 16, 29, 57) && ToTime(DateTime.Now) <= ToTime ( 16, 29, 58))
              {
              myOrder1 = EnterShortStopLimit( 1, GetCurrentBid() - 31 * TickSize, GetCurrentBid() - 21 * TickSize, "corto");
              DrawTextFixed("My text (fixed)" + CurrentBar, "Bajada", TextPosition.TopLeft);
              }
              // Condition set 2
              if ((ToTime(DateTime.Now) >= ToTime ( 16, 29, 59)) && (GetCurrentBid() < myOrder1.LimitPrice) && (myOrder1.OrderState != OrderState.Filled))
              {
              CancelOrder(myOrder1);
              }
              }
              #region Properties
              [Description("")]
              [GridCategory("Parameters")]
              public int Ordenes
              {
              get { return ordenes; }
              set { ordenes = Math.Max(1, value); }
              }

              [Description("")]
              [GridCategory("Parameters")]
              public int Stop
              {
              get { return stop; }
              set { stop = Math.Max(1, value); }
              }
              #endregion
              }
              }

              Comment


                #8
                Hello ikerbar,

                Thank you for your post.

                In your code I do not see a check for null on the IOrder object before calling it's order state. Try adding the following:
                Code:
                if ([B]myOrder1 != null[/B] && (ToTime(DateTime.Now) >= ToTime ( 16, 29, 59)) && (GetCurrentBid() < myOrder1.LimitPrice) && (myOrder1.OrderState != OrderState.Filled))
                {
                CancelOrder(myOrder1);
                [B]myOrder1 = null;[/B]
                }

                Comment


                  #9
                  Hello ikerbar,

                  Thank you for your response.

                  Please attach your strategy to your response so I may investigate this matter further. You can find your strategy by going to the following directory on your PC: (My) Documents\NinjaTrader 7\bin\Custom\Strategy > it will be a .cs file.

                  Comment


                    #10
                    Sorry i found the error by myself. I forgot to put the &&
                    Thank you very much for your help!!

                    Comment


                      #11
                      Good evening Patrick,
                      i have done a simulated trade to check it, but i have another problem. The trailing stop doesnt work. The trailing stop continue all the time in the first stop price and doesnt follow the price move with the profit.

                      Comment


                        #12
                        Hello ikerbar,

                        Thank you for your response.

                        What method is used for your trailing stop? Are you using SetTrailStop()? Can you provide the code used for the trailing stop?

                        I look forward to your response.

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by cre8able, Yesterday, 04:22 PM
                        1 response
                        13 views
                        0 likes
                        Last Post NinjaTrader_ChelseaB  
                        Started by stafe, 04-15-2024, 08:34 PM
                        5 responses
                        28 views
                        0 likes
                        Last Post NinjaTrader_ChelseaB  
                        Started by StrongLikeBull, Yesterday, 04:05 PM
                        1 response
                        12 views
                        0 likes
                        Last Post NinjaTrader_Gaby  
                        Started by Mestor, 03-10-2023, 01:50 AM
                        14 responses
                        375 views
                        0 likes
                        Last Post z.franck  
                        Started by molecool, 10-09-2017, 10:48 AM
                        5 responses
                        1,621 views
                        0 likes
                        Last Post trader-ap  
                        Working...
                        X