Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

CancelOrder() by ticks

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

    CancelOrder() by ticks

    Hi,

    I'm new to trading priciples and still a bit confused about all the order handling options. Basically, I would like to cancel a stop order when the price moves too far away in the opposite direction and I would like for this to happen by incoming tick and not just on the close of a bar. I wonder what's the best way to do it and if there is already a method that does what I require. Here's how I would do it:

    Say, on the close of a 15min bar my conditions apply and I place a liveUntilCancelled EnterLongStopLimit order with stop and limit higher than the current High. If the Order is filled, SetProfitTarget and SetStopLoss take care of the Exit and, if I understand correctly, all these methods are handling my orders independently from the 15min clocking of OnBarUpdate(). Now, if the price breaks the current Low I would like to cancel everything, independently from the 15min timing as well.

    Do I simply set "CalculateOnBarClose = false" until either the price reaches the Low and I CancelOrder() or the entry order is filled, and then I set "CalculateOnBarClose = true" again to continue as normal? Or is there a more CPU-friendly solution for this, e.g. through one of the order handling methods?

    #2
    Hello Greg814,

    Calculation for tick by tick or at the end of the bar close is controlled with CalculateOnBarClose property.

    You would set this to false to calculate tick by tick.

    You can use the principles from this reference sample to separate some logic based on every tick and other logic based on bar close.

    The Set statements(profit target and stop loss) aren't applied until entry execution so your entry is the only order you would have to issue CancelOrder() commands to.

    This reference sample can help with CancelOrder and the needed IOrder object structure for this.
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      Hi,

      the reference sample for separating tick and bar logic (http://www.ninjatrader.com/support/f...ad.php?t=19387) is quite interesting, but it makes backtesting difficult. That's why I would like to ask again if there is anything wrong with manipulating "CalculateOnBarClose". If not I could switch between incoming tick and bar data when trading live and use 1min bar data granularity (https://"http://www.ninjatrader.com/...ead.php?t=6652) instead of ticks when backtesting.

      The idea is something like that:
      Code:
      protected override void Initialize()
      {
         Add(PeriodType.Minute, 1);     //for intrabar backtesting
         CalculateOnBarClose = true;   //start in bars mode
      }
      protected override void OnStartUp()
      {
         IOrder activeOrder = null;
      }
      protected override void OnBarUpdate()
      {
         // don't worry about 1min bars when live
         if(!Historical && BarsInProgress == 1) return;
      
         //  check for a trade only on primary bars object and only if no active order
         if(BarsInProgress == 0 && activeOrder != null && trade conditions)
         {
            activeOrder = EnterLongStop(1,entrythreshold,"long");
            SetStopLoss("long",...,StopLoss,...);
            SetProfitTarget(...);
            //  switch to tick mode if live
            if(!Historical) CalculateOnBarClose = false;
         }
      
         // when order active check for price movement in tick mode (1min bars in backtest)
         if(activeOrder != null)
         {
            // cancel order if StopLoss is reached before entrythreshold
            if(Close[0] <= StopLoss && activeOrder.OrderState != OrderState.Working)
            {
                CancelOrder(activeOrder);
                activeOrder = null;
                // switch back to bars mode
                CalculateOnBarClose = true;
            }
            // if trade exits through StopLoss or ProfitTarget
            if(barsSinceExit(1, "long", 0) > 0) 
            {
                activeOrder[ii] = null;
                CalculateOnBarClose = true;
            }
         }
      }
      Happy to hear if this is possible or if manipulating "CalculateOnBarClose" causes trouble.
      If it is possible it would make a nice reference sample.

      Cheers..

      Comment


        #4
        CalculateOnBarClose is set initially and is not meant to be changed at runtime.

        You may consider enclosing code meant for backtesting when Historical.


        if (Historical)
        {
        //run backtesting logic here
        }

        else
        {
        //real time logic here.
        }
        Ryan M.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by judysamnt7, 03-13-2023, 09:11 AM
        4 responses
        59 views
        0 likes
        Last Post DynamicTest  
        Started by ScottWalsh, Today, 06:52 PM
        4 responses
        36 views
        0 likes
        Last Post ScottWalsh  
        Started by olisav57, Today, 07:39 PM
        0 responses
        7 views
        0 likes
        Last Post olisav57  
        Started by trilliantrader, Today, 03:01 PM
        2 responses
        21 views
        0 likes
        Last Post helpwanted  
        Started by cre8able, Today, 07:24 PM
        0 responses
        10 views
        0 likes
        Last Post cre8able  
        Working...
        X