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

CancelOrder when price is moved 3 tick up from the last close[0]

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

    CancelOrder when price is moved 3 tick up from the last close[0]

    Hello,

    Guys need help with how to cancel the EnterLongLimit order when the price has moved 3 ticks up from close[0].
    Close[0] + 3 * TickSize - seems not to work or do I need to use Calculate.OnEachTick instead of OnBarClose?


    Code:
    if (entryOrder == null) {
       EnterLongLimit(0, true, 1, Close[0] - 1 * TickSize, "long entry");
    }              
    else if (Price has moved 3 ticks up from close[0]) {
        CancelOrder(entryOrder);
    }​
    Thanks.
    Last edited by pearu; 01-29-2023, 09:53 AM.

    #2
    Hello pearu,

    Thanks for your post.

    I see that you are calling CancelOrder() on an Order object called 'entryOrder' but you are not assigning the EnterLongLimit() order to the entryOrder Order object.

    See this reference sample, SampleCancelOrder, demonstrating how to use the CancelOrder() method to cancel an order assigned to an Order object.


    If you want to check if the current Close price is 3 ticks higher than the previous Close price, you could create a condition that checks if the current Close price (Close[0]) is greater than the previous Close price + 3 ticks (Close[1] + 3 * TickSize).

    See the help guide pages below for more information.

    Close: https://ninjatrader.com/support/help.../nt8/close.htm
    TickSize: https://ninjatrader.com/support/help...8/ticksize.htm

    ​​​​​​​Please let me know if I may assist further.
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Still didn't get it to work. For some reason, it doesn't put EnterLongLimit in now. It seems that the CancelOrder line of code gets triggered before the EnterLongLimit part.

      entryOrder is set as private Order entryOrder = null; in my code where it should be.

      As I said, it won't put a long limit in. As I remove the cancel order if-statement logic it will put a long limit in. Calculation method is OnBarClose.

      Code:
      protected override void OnBarUpdate()
              {
                  if (CurrentBar < 20) return;
      
                  if (Close[0] > Open[0])
                  {
      
                      if (entryOrder == null) {
                          EnterLongLimit(0, true, 1, Close[0] - 1 * TickSize, "long entry");
                          downMovement = false; // Don't bother with it, it's working and just a boolean that I toggle
                      }
      
                      if (Close[0] - Close[1] >= 2 * TickSize) {
                          CancelOrder(entryOrder);
                      }
                  }
              }
      
              protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled,
                double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
              {
      
                if (order.Name == "long entry")
                    entryOrder = order;
      
                if (entryOrder != null && entryOrder == order)
                {
                    if (entryOrder.OrderState == OrderState.Cancelled)
                    {
                        // Reset myEntryOrder back to null
                        entryOrder = null;
                    }
                }
              }
          }​
      Thanks in advance!
      Last edited by pearu; 01-30-2023, 10:24 AM.

      Comment


        #4
        Hello pearu,

        Thanks for your note.

        You could consider adding a check to see if entryOrder != null in your condition to call CancelOrder() to ensure the entryOrder Order object is not null when your CancelOrder() method is called.

        Ultimately, to understand why the script is behaving as it is, such as placing orders or not placing orders or drawing objects when expected, it is necessary to add prints to the script that print the values used for the logic of the script to understand how the script is evaluating.

        In the strategy add prints (outside of any conditions) that print the values of every variable used in every condition that places an order along with the time of that bar.

        Prints will appear in the NinjaScript Output window (New > NinjaScript Output window).

        Below is a link to a forum post that demonstrates how to use prints to understand behavior.

        https://ninjatrader.com/support/foru...121#post791121

        Let us know if we may assist further.​​
        Brandon H.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by jbays87, Today, 09:46 PM
        0 responses
        2 views
        0 likes
        Last Post jbays87
        by jbays87
         
        Started by popecapllc, 08-09-2023, 07:42 PM
        9 responses
        1,362 views
        0 likes
        Last Post BartMan
        by BartMan
         
        Started by ETFVoyageur, 04-30-2024, 02:04 PM
        11 responses
        101 views
        0 likes
        Last Post ETFVoyageur  
        Started by bubblegum, 03-18-2024, 10:41 AM
        3 responses
        46 views
        0 likes
        Last Post vjsworld  
        Started by JamesK1, Today, 02:48 PM
        1 response
        13 views
        0 likes
        Last Post JamesK1
        by JamesK1
         
        Working...
        X