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

Simple Limit Strategy with CancelOrder

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

    Simple Limit Strategy with CancelOrder

    I'm developing a simple strategy based on entering a limit order and then canceling it if not filled in 3 bars. I get no trades at all. That's what I did:

    In the Variables region

    private IOrder myEntryOrder = null;

    OnBarUpdate():

    // Entry Long Condition

    if (myEntryOrder == null)
    {
    myEntryOrder = EnterLongLimit(1, true, DefaultQuantity, MaxRange, "LongLimit");
    }

    // Cancel LimitOrders condition
    if (BarsSinceEntry() >= 3)
    CancelOrder(myEntryOrder);

    // Exit Condition Long
    if (conditions
    && Position.MarketPosition == MarketPosition.Long)

    {
    ExitLong("Take Profit Long", "LongStopLimit");
    myEntryOrder = null;
    }

    // Stop Loss Long
    if (conditions
    && Position.MarketPosition == MarketPosition.Long)
    {
    ExitLong("LongStopLoss", "LongStopLimit");
    myEntryOrder = null;
    }

    Thank you

    #2
    You need to debug your NinjaScript as per here: http://www.ninjatrader-support2.com/...ead.php?t=3418
    and here

    Comment


      #3
      I adjusted the Bar Index from 1 to 0.
      It takes the first trade now, but then it doesn't trade anymore.
      I printed my IOrder and after the first LongLimitOrder and Cancelled Order, it keeps on repeating on each following bar:
      Order='NT-00002/Back101' Name='LongLimit' State=Cancelled Instrument='$GBPCHF' Action=Buy Limit price=2,195 Stop price=0 Quantity=25.000 Strategy='BreakoutATRRangeLongLimit' Type=Limit Tif=Gtc Oco='' Filled=0 Fill price=0 Token='bfdd4edd5a814f6487eddd04d841af37' Gtd='01/12/2099 0.00.00'
      Always the same Cancelled state of the same order, but it doesn't take new Limit orders.
      Yet, I refresh the variable to myEntryOrder = null when:
      1) Stop Loss kicks in
      2) Take Profit kicks in
      3) The order gets canceled

      Thank you

      Comment


        #4
        Unfortunately we are unable to provide support down to the level of plain C# programming or actually coding your strategy.

        However here is a hint: as you call CancelOrder you need to set myEntryOrder to null in order to allow your if condition "if (myEntryOrder == null)" to trigger for another entry.

        Comment


          #5
          That's what I did, but I'm not sure I formulated it correctly.
          // Cancel LimitOrders condition
          if (BarsSinceEntry() >= 3)
          {
          CancelOrder(myEntryOrder);
          myEntryOrder = null;
          }

          Please another hint, I spent a lot of time on this code.

          Thank you very much

          Comment


            #6
            You need to trace if EnterLongLimit would be called again and if so what would happen to your signal (see TraceOrders).

            Comment


              #7
              So I did.
              I got the following output:
              03/01/2008 9.00.00 Entered internal PlaceOrder() method at 03/01/2008 9.00.00: Action=Buy OrderType=Limit Quantity=25.000 LimitPrice=0,7430 StopPrice=0 SignalName='LongLimit' FromEntrySignal=''
              0 Order='NT-00000/Back101' Name='LongLimit' State=Working Instrument='$EURGBP' Action=Buy Limit price=0,743 Stop price=0 Quantity=25.000 Strategy='BreakoutATRRangeLongLimit' Type=Limit Tif=Day Oco='' Filled=0 Fill price=0 Token='800c386990fc48468313fe550b23b7e8' Gtd='01/12/2099 0.00.00'
              0 Order='NT-00000/Back101' Name='LongLimit' State=Filled Instrument='$EURGBP' Action=Buy Limit price=0,743 Stop price=0 Quantity=25.000 Strategy='BreakoutATRRangeLongLimit' Type=Limit Tif=Day Oco='' Filled=25000 Fill price=0,743 Token='800c386990fc48468313fe550b23b7e8' Gtd='01/12/2099 0.00.00'
              1 Order='NT-00000/Back101' Name='LongLimit' State=Filled Instrument='$EURGBP' Action=Buy Limit price=0,743 Stop price=0 Quantity=25.000 Strategy='BreakoutATRRangeLongLimit' Type=Limit Tif=Day Oco='' Filled=25000 Fill price=0,743 Token='800c386990fc48468313fe550b23b7e8' Gtd='01/12/2099 0.00.00'
              2 Order='NT-00000/Back101' Name='LongLimit' State=Filled Instrument='$EURGBP' Action=Buy Limit price=0,743 Stop price=0 Quantity=25.000 Strategy='BreakoutATRRangeLongLimit' Type=Limit Tif=Day Oco='' Filled=25000 Fill price=0,743 Token='800c386990fc48468313fe550b23b7e8' Gtd='01/12/2099 0.00.00'
              3
              03/01/2008 11.30.00 Entered internal PlaceOrder() method at 03/01/2008 11.30.00: Action=Buy OrderType=Limit Quantity=25.000 LimitPrice=0,7458 StopPrice=0 SignalName='LongLimit' FromEntrySignal=''
              03/01/2008 11.30.00 Ignored PlaceOrder() method at 03/01/2008 11.30.00: Action=Buy OrderType=Limit Quantity=25.000 LimitPrice=0,7458 StopPrice=0 SignalName='LongLimit' FromEntrySignal='' Reason='Exceeded entry signals limit based on EntryHandling and EntriesPerDirection properties'

              Why do I get "Exceeded entry signals limit"? I have one Entry kicking off at 9AM, I'm working on 30min bars and I have the condition
              if (BarsSinceEntry() >= 3)
              {
              CancelOrder(myEntryOrder);
              myEntryOrder = null;
              }
              so, by 10:30AM, my first LongLimit order is supposed to be canceled and the new LongLimit Order should kick in. Why it doesn't happen?

              Thank you

              Comment


                #8
                stefy,

                If you are getting that message it means you still have an order working. You need to ensure you have actually received a complete cancelled state on your order before you submit your new entry order. To do this means you cannot reset myEntryOrder = null right after you call CancelOrder(). Instead you want to do it inside OnOrderUpdate() when you receive the cancel state.

                Untested code.
                Code:
                if (myEntryOrder != null && myEntryOrder.Token == order.Token)
                {
                     if (order.OrderState == OrderState.Cancelled)
                          myEntryOrder = null;
                }
                Josh P.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by algospoke, Today, 06:40 PM
                0 responses
                9 views
                0 likes
                Last Post algospoke  
                Started by maybeimnotrader, Today, 05:46 PM
                0 responses
                7 views
                0 likes
                Last Post maybeimnotrader  
                Started by quantismo, Today, 05:13 PM
                0 responses
                7 views
                0 likes
                Last Post quantismo  
                Started by AttiM, 02-14-2024, 05:20 PM
                8 responses
                168 views
                0 likes
                Last Post jeronymite  
                Started by cre8able, Today, 04:22 PM
                0 responses
                10 views
                0 likes
                Last Post cre8able  
                Working...
                X