Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

retracement of a bar entry

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

    retracement of a bar entry

    Hello, I was wondering if somone might be able to point me in the right direction. Im only a beginner with trying to program so excuse any stupid questions.

    I want to add on a bit to my stratergy whereby instead of simply entering on the close (in the direction) of the bar that makes up my setup... Instead it will only enter if there is a retracement of this bar then enters using an order at the previous high or low of the current bar but only whilst that bar is active i.e canceling the order when a new bar begins if it has not been filled.

    So as a basic example (ive written the order long hand as this is the bit im struggeling with):

    if (Close[1] > Close[2]
    && CCI(14)[1] <= 100
    && Low[0] < Close[1] - 5)
    {
    Enter a long at High[1] +4 ticks for one bar then delete if not filled
    }

    Also how would I stop it entering loads of order while its below Close[1] -5 as im guessing it will want to enter an order for each tick its true. Would adding a variable like below work for that:

    if (Close[1] > Close[2]
    && CCI(14)[1] <= 100
    && Low[0] < Close[1] - 5
    && active=0)
    {
    Enter a long at High[1] +4 ticks for one bar then delete if not filled
    active=1
    }

    Many thanks.

    #2
    hello, i have done the following but it does not seem to want to cancel the order after one bar... also it only backtests on close price rather than intrabar ticks (can you even back test on intrabar data as you would have in the real world?)

    Code:
                if (Close[1] > Close[2] 
                    && CCI(14)[1] <= 100
                    && Low [0] < Close[1] -5)
                {
                    if (myEntryOrderLong == null) 
                    { 
                    myEntryOrderLong = EnterLongStop(0, true, 1, High[1]+4, "Long Entry"); 
    
                    barNumberOfOrder = CurrentBar; 
    
                    if (CurrentBar > barNumberOfOrder + 1) 
                    CancelOrder(myEntryOrderLong); 
                    myEntryOrderLong = null;
                    }
                }

    Comment


      #3
      Hello,

      You probably want to move this portion of your code out from within the order condition:
      if (CurrentBar > barNumberOfOrder + 1)
      CancelOrder(myEntryOrderLong);

      Also, you probably want to use High[1]+4 * TickSize and Close[1] -5 *TickSize instead of High[1]+4 and Close[1] -5.
      DenNinjaTrader Customer Service

      Comment


        #4
        Thanks for your previous post....
        Moving the CancelOrder() out of the 'if' condition kind of worked but now for some reason it will do the first order in a back test but no more... it will however print the yellow and black bars as stated so it must be just canceling all orders following the first no matter what??
        Below the code: any ideas...
        All I want is to delete the order after a number of bars should it not have already been filled.


        Code:
                      #region Variables
                private int        stoplossticks        = 25;
                private int        profittargetticks    = 55;
                 private int        barNumberOfOrder = 0;
                private IOrder     myEntryOrderShort = null; 
        
                #endregion
        
                protected override void Initialize()
                {
        
                    SetStopLoss(CalculationMode.Ticks, stoplossticks);
                    SetProfitTarget(CalculationMode.Ticks, profittargetticks);
                    
                    CalculateOnBarClose = true;
                }
        
            
                protected override void OnBarUpdate()
                {
        
                    if (Close[1] < Close[2] 
                        && RSI(14, 3)[2] >= 70
                        && High [0] > Close[1] +2)
                    {
                    if (myEntryOrderShort == null) 
                    {
                        myEntryOrderShort = EnterShortStop(0, true, 1, Low[1]-4, "Short Entry"); 
                        barNumberOfOrder = CurrentBar; 
                        BarColor = Color.Black;
                    }
        
                    }
                    
                    if (CurrentBar == barNumberOfOrder +5)
                    {
                    BarColor = Color.Yellow;
                    CancelOrder(myEntryOrderShort);
                    myEntryOrderShort = null;
                    
                    }
        
                }

        Comment


          #5
          Hello,

          Try using BarsSinceEntry() for your condition to Cancel:



          You also may want to use TraceOrders() to figure out what is going one exactly:
          http://www.ninjatrader-support.com/H...aceOrders.html
          DenNinjaTrader Customer Service

          Comment


            #6
            I dont think i can use BarsSinceEntry() since im only wanting to cancel a stop order that was not filled, so no entry took place??

            ive tried again from scratch but still no joy! using the code below if i delete the CancelOrder() then i get trades filled but that include ones i dont want but if i add CancelOrder() i get no trades at all and the following in the output window:

            01/12/2008 15:29:06 Entered internal PlaceOrder() method at 01/12/2008 15:29:06: Action=Buy OrderType=Stop Quantity=1 LimitPrice=0 StopPrice=1.2643 SignalName='Long Entry' FromEntrySignal=''

            I dont suppose you have an example of some code where by there is a conditional stop order entered that if not filled after so many bar have passed is canceld?

            Code:
            public class CancelOrder3 : Strategy
                {
                    #region Variables
                    // Wizard generated variables
                    private int myInput0 = 1; // Default setting for MyInput0
                    private IOrder myEntryOrder = null; 
                    private int barNumberOfOrder = 0; 
                    // User defined variables (add any user defined variables below)
                    #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, 10); 
                        TraceOrders        = true; 
                        Add(SMA(5));
                        Add(SMA(10));
                        CalculateOnBarClose = true;
                    }
            
                    /// <summary>
                    /// Called on each bar update event (incoming tick)
                    /// </summary>
                    protected override void OnBarUpdate()
                    {
                        
                        
                        
                        
             
            
                // Submit an entry order at the low of a bar 
                if (CrossAbove(SMA(5), SMA(10), 1)
                    && myEntryOrder == null) 
                {        
                    myEntryOrder = EnterLongStop(0, true, 1, High[1] +1 * TickSize, "Long Entry"); 
                    barNumberOfOrder = CurrentBar; 
                    BarColor = Color.Black;
                } 
            
                // If more than 5 bars has elapsed, cancel the entry order 
                if (CurrentBar == barNumberOfOrder + 2)
                {
                    
                    CancelOrder(myEntryOrder); 
                    BarColor = Color.Yellow;
                    myEntryOrder = null;
                }

            Comment


              #7
              I don't understand what part you are having trouble with. Just call CancelOrder() when you want to cancel the order. If the order is available to be cancelled it will cancel. You cannot reset to null just because you called CancelOrder(). The order is not cancelled unless you recevied a cancel event in OnOrderUpdate().
              Josh P.NinjaTrader Customer Service

              Comment


                #8
                you're right i think the problem is that it is dealing with the first order in the sequence fine but then 'myEntryOrder == null' no longer is equal to null so it never exicutes any more orders... BTW im trying run this as a back test and im thinking maybe this wont work on historical data???


                Code:
                        protected override void OnBarUpdate()
                        {
                            // Condition set 1
                            if (Close[0] >= Close[1] && Close[1] >= Close[2] && Close[2] >= Close[3] && myEntryOrder == null)
                            {
                                myEntryOrder = EnterShortStop(0, true, 1, Close[2], "Long Entry");
                                barNumberOfOrder = CurrentBar; 
                
                            }
                            
                                if (CurrentBar > barNumberOfOrder + 2) 
                                   CancelOrder(myEntryOrder); 
                
                        }

                Comment


                  #9
                  No reason why it wouldn't run in a backtest. Please see this tip on debugging: http://www.ninjatrader-support2.com/...ead.php?t=3418

                  You want to use Print()s and track your code.
                  Josh P.NinjaTrader Customer Service

                  Comment


                    #10
                    thanks Josh, that link really helped and i think im on the way to figuring it out now.... sorry if my questions were stupid... like i said im pretty new to programming and my previous experiance was actionscript (flash) so as you can imagine quite a learning curve.

                    Comment


                      #11
                      Okay so starting a fresh... i have been through everything and identified the problem.

                      Below is the sample code im using with various Print() elements to identify the orders.
                      All works as expected if i run a backtest with only short entrys and all works fine if i reverse the code to run for long entrys... BUT if i add both long and short to the same backtest code this is where my problem is... it will only seem to submit and cancel orders for the first two or three setups and no more... Using the output window i can see that the code gets as far as printing "were in short or long" but then nothing leading me to belive that for some reason it can't enter a new actutal stop order and so gets stuck.... as this works fine if run with either all shorts or all longs it must be having the combination of the two that is messing things up??

                      Any ideas why this might be?





                      Here is the sample code im using, short code activated and long in comments:
                      Code:
                      protected override void OnBarUpdate()
                                  
                      {    MyCurrentBar = CurrentBar;
                      // ShortEntry[INDENT][INDENT] if (Close[0] >= Open[0] && Close[1] <= Close[2] && Close[2] <= Close[3] && Position.MarketPosition == MarketPosition.Flat)
                      [/INDENT][/INDENT][INDENT][INDENT]{
                      [/INDENT][/INDENT][INDENT][INDENT]Print("we're in SHORT");
                      myEntryOrder2 = EnterShortStop(0, true, 1, Low[0]-10 * TickSize, "Short Stop Entry");
                      Print(myEntryOrder2.Token);
                      Print(myEntryOrder2.Time);
                      Print(myEntryOrder2.StopPrice);
                      barNumberOfOrderShort = CurrentBar;
                      Print(barNumberOfOrderShort);[/INDENT][/INDENT][INDENT]         }
                      [/INDENT][INDENT][INDENT]else if (CurrentBar == barNumberOfOrderShort + 2)
                      [/INDENT][/INDENT][INDENT][INDENT]{
                      Print(MyCurrentBar);    
                      Print("Cancel my order SHORT");
                      CancelOrder(myEntryOrder2); 
                      } 
                      [/INDENT][/INDENT]
                      
                                 
                       // LongEntry Disabled
                                  /*
                                  if (Close[0] <= Open[0] && Close[1] >= Close[2] && Close[2] >= Close[3] && Position.MarketPosition == MarketPosition.Flat)
                                  {
                                      
                                      Print("we're in LONG");
                                      myEntryOrder1 = EnterLongStop(0, true, 1, High[0]+10 * TickSize, "Long Stop Entry");
                                      Print(myEntryOrder1.Token);
                                      Print(myEntryOrder1.Time);
                                      Print(myEntryOrder1.StopPrice);
                                      barNumberOfOrderLong = CurrentBar; 
                                      Print(barNumberOfOrderLong);
                      
                                  }[INDENT][INDENT]else if (CurrentBar == barNumberOfOrderLong + 2)
                      [/INDENT][/INDENT]{
                                              Print(MyCurrentBar);    
                                              Print("Cancel my order LONG");
                                                 CancelOrder(myEntryOrder1); 
                                              
                                          }
                                  */
                      
                      
                      
                      }
                      Last edited by tradingcube; 12-22-2008, 06:41 AM.

                      Comment


                        #12
                        Hello,

                        Your order is likely rejected because you cannot be both long and short within the same market at the same time. I suggest you review the Internal Order Handling Rules here:


                        Also, it may help if you use TraceOrders = true to track your trades:
                        DenNinjaTrader Customer Service

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                        0 responses
                        558 views
                        0 likes
                        Last Post Geovanny Suaza  
                        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                        0 responses
                        324 views
                        1 like
                        Last Post Geovanny Suaza  
                        Started by Mindset, 02-09-2026, 11:44 AM
                        0 responses
                        101 views
                        0 likes
                        Last Post Mindset
                        by Mindset
                         
                        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                        0 responses
                        545 views
                        1 like
                        Last Post Geovanny Suaza  
                        Started by RFrosty, 01-28-2026, 06:49 PM
                        0 responses
                        547 views
                        1 like
                        Last Post RFrosty
                        by RFrosty
                         
                        Working...
                        X