Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Intrabar backtesting-stoploss execution problem

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

    Intrabar backtesting-stoploss execution problem

    Greetings,
    The backtest strategy below uses the ES 60min bars to test for a condition: if true, 2 positions are entered LONG on the 1min. Stoploss and profit targets are also intended to execute on 1min. The stoploss is adjusted if the 1st target is reached. (Note: I could do this entirely on the sub-time frame, but then the 60min/primary chart can not be viewed in the strategy analyzer.)

    The problem is that on large 60min down bars (e.g., 12/7/10), the trade is entered and the stoploss is executed for each minute. When I look at execution of the trades it looks like this:
    12/7/2010 2:03:00 PM Entry Long1
    12/7/2010 2:03:00 PM Entry Long2
    12/7/2010 3:00:00 PM Stoploss
    12/7/2010 3:00:00 PM Stoploss
    12/7/2010 2:04:00 PM Entry Long1
    12/7/2010 2:04:00 PM Entry Long2
    12/7/2010 3:00:00 PM Stoploss
    12/7/2010 3:00:00 PM Stoploss
    etc...

    From the timestamp above, it appears the Stoploss is being applied on 60min, which doesn't make sense to me in two ways: 1. the stoploss was entered on the 1min, and 2. the time is not chronological.

    Please help me understand how to correctly implement the strategy given below.

    Thanks in Advance!

    Code:
    #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>
        /// ExampleIntrabarStopLoss
        /// </summary>
        [Description("ExampleIntrabarStopLoss")]
        public class ExampleIntrabarStopLoss : Strategy
        {
            #region Variables
            // Wizard generated variables
            private int    nContracts = 1;    // Default setting for NContracts
            private double currentHrBarOpen = 1000000;  // set high so entry not falsely triggered
            private double target1 = 2.000;   // Default setting for Target1
            private double target2 = 4.000;   // Default setting for Target2
            private double target3 = 1;       // Default setting for Target3
            private double target4 = 0.000;   // Default setting for Target4
            private double stopLoss = 4.000; // Default setting for StopLoss
            // User defined variables (add any user defined variables below)
            #endregion
    
            // Strategy:     ExampleIntrabarStopLoss
            // Instrument:     ES
            // Series:       Primary:  60min
            //                 Secondary: 1min
            // Description:  Backtest of simple strategy the uses the 60min bars for entry
            //               signals.  
            //               Entry and Exit on 1min timeframe 
            //
            // This method is used to configure the strategy and is called once before any strategy method is called.
            protected override void Initialize()
            {
                // Buy 2*NContracts and have separate exits
                EntriesPerDirection = 2; 
                
                //Note: The primary bar series is whatever you choose for the strategy at startup. 
                //      Strategy Analyzer Chart displays primary bars ONLY
                //Primary:   60min
                //Add Secondary: 1min
                Add(PeriodType.Minute, 1);
                            
                CalculateOnBarClose = true;  // set to true for historical testing
            }
    
            /// Called after each bar close if CalculateOnBarClose == true
            protected override void OnBarUpdate()
            {
                // Sets the back color to empty when Time is between 9am-4:15pm EST, and to Lavender otherwise
                BackColor = (ToTime(Time[0]) >= 90000 && ToTime(Time[0]) <= 161500) ? Color.Empty : Color.Lavender;
                
                // Return unless time between 9:00am and 4:15pm.  
                if (ToTime(Time[0]) < ToTime(9, 00, 0) || ToTime(Time[0]) > ToTime(16, 15, 0)) return;
                
                // BarsInProgress = 0 for 60min bars; 1 for 1min bars
                if (BarsInProgress == 1)
                {
                    int nbar1 = 0;
                    double exitPrice = 0;
                    double entryPrice = 0;
                    
                    // Check for zeroth minute
                    if( Time[0].Minute == 0 )  
                    {
                        nbar1 = 1;  //1min 00min called after 60min 00min, therefore need to index previous 60min bar
                    }
                    // Check for 1st minute of hour to set open value (for hour)
                    else if ( Time[0].Minute == 1 )
                    {
                        CurrentHrBarOpen = Open[0];
                        Print("CurrentHrBarOpen: " 
                                + " nBar: " + CurrentBar.ToString() 
                                + " Time: " + ToTime(Time[0]).ToString() 
                                + " Open[0]: " + Open[0].ToString() 
                                + " CurrentHrBarOpen " + CurrentHrBarOpen.ToString());
                    }
                    
                    // Order entry note: Long entry order only valid for current bar of current barArray unless 'LiveToCancelled' == 1
                    // Only check for long entry condition if no position is open
                    //if (Position.Quantity == 0)
                    if (Position.MarketPosition == MarketPosition.Flat)
                    {
                        if ( Closes[0][nbar1] > Highs[0][nbar1+1]
                             && ToTime(Time[0]) < ToTime(16, 0, 0)) 
                        {
                            entryPrice = Highs[0][nbar1] + 0.25;
                            
                            // if minute 0: will not know open at minute 1, so execute a 'EnterLongStop'
                            if( Time[0].Minute == 0 )  
                            {
                                EnterLongStop( NContracts, entryPrice, "Long1");  
                                EnterLongStop( NContracts, entryPrice, "Long2");    
                            }
                            else if (CurrentHrBarOpen < Highs[0][nbar1])
                            {
                                EnterLongLimit( NContracts, entryPrice, "Long1");
                                EnterLongLimit( NContracts, entryPrice, "Long2");
                            }
                            Variable0 = 1;
                            
                            SetStopLoss("Long1", CalculationMode.Ticks, 4.0*StopLoss, true);
                            SetStopLoss("Long2", CalculationMode.Ticks, 4.0*StopLoss, true);
                            
                            SetProfitTarget("Long1", CalculationMode.Ticks, 4.0*Target1);
                            SetProfitTarget("Long2", CalculationMode.Ticks, 4.0*Target2);
                        }
                    }
                    
                    // Check targets 
                    else if (Position.Quantity == 2*NContracts)
                    {
                        
                        //SetStopLoss("Long1", CalculationMode.Ticks, 16.0, true);
                        //SetStopLoss("Long2", CalculationMode.Ticks, 16.0, true);
    
                        if (ToTime(Time[0]) == ToTime(16, 14, 0) )
                        {
                            ExitLong("Time Exit L1", "Long1");
                            ExitLong("Time Exit L2", "Long2");
                        }
                    }
                    else if (Position.Quantity == NContracts)
                    {                
                        //adjust stop loss to Target4 (breakeven)
                        SetStopLoss("Long2", CalculationMode.Ticks, 0.0, true);
    
                        if (ToTime(Time[0]) == ToTime(16, 14, 0) )
                        {
                            ExitLong("Time Exit L2", "Long2"); //ExitLong("Long2");
                        }
                    }
                    
                }
                
                //Print out section outside of BarsInProgress condition statement
                if (Position.Quantity != 1000)
                {
                    Print(Time[0].ToString()
                    //+ "BarsInProgress: " + BarsInProgress.ToString()
                    + " nBar: " + CurrentBar.ToString() 
                    + " AvgPrice " + Position.AvgPrice.ToString() 
                    + " Qnty: " + Position.Quantity.ToString());
                }
            }
    
            #region Properties
            [Description("")]
            [GridCategory("Parameters")]
            public int NContracts
            {
                get { return nContracts; }
                set { nContracts = Math.Max(1, value); }
            }
    
            [Description("")]
            [GridCategory("Parameters")]
            public double CurrentHrBarOpen
            {
                get { return currentHrBarOpen; }
                set { currentHrBarOpen = Math.Max(0, value); }
            }
            
            [Description("")]
            [GridCategory("Parameters")]
            public double Target1
            {
                get { return target1; }
                set { target1 = Math.Max(0, value); }
            }
    
            [Description("")]
            [GridCategory("Parameters")]
            public double Target2
            {
                get { return target2; }
                set { target2 = Math.Max(0, value); }
            }
    
            [Description("")]
            [GridCategory("Parameters")]
            public double Target3
            {
                get { return target3; }
                set { target3 = Math.Max(0, value); }
            }
    
            [Description("")]
            [GridCategory("Parameters")]
            public double Target4
            {
                get { return target4; }
                set { target4 = Math.Max(0, value); }
            }
    
            [Description("")]
            [GridCategory("Parameters")]
            public double StopLoss
            {
                get { return stopLoss; }
                set { stopLoss = Math.Max(-1000.000, value); }
            }
            #endregion
        }
    }

    #2
    Hello,

    Thanks for your forum post and welcome to the forums!

    First thing is see is that you are using your Enter Conditions before settings your stop loss. SetStopLoss() needs to be set before submitting your entry order. Therefor please SetStopLoss() before you submit your EnterLong(). You need to move this up higher in your code.

    Also, If you add TraceOrders = True in the Initialize Method you will be able to trace the orders to see what is happening.



    Please add this and if the above does not resolve the issue please post your TraceOrders output to make sure the stop loss is being submitted the same time as the entry.

    Let me know if I can be of further assistance.

    Comment


      #3
      Thanks for the reply. I implemented your two suggestions. The stop loss issue remains the same, but perhaps you can better understand the trace output. Below is a portion of the trace output that repeats for each minute starting at 2:01:00PM- 3:00:00PM.

      12/7/2010 2:05:00 PM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='Long1' Mode=Ticks Value=16 Currency=0 Simulated=True
      12/7/2010 2:05:00 PM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='Long2' Mode=Ticks Value=16 Currency=0 Simulated=True
      12/7/2010 2:05:00 PM Entered internal SetStopTarget() method: Type=Target FromEntrySignal='Long1' Mode=Ticks Value=8 Currency=0 Simulated=False
      12/7/2010 2:05:00 PM Entered internal SetStopTarget() method: Type=Target FromEntrySignal='Long2' Mode=Ticks Value=16 Currency=0 Simulated=False
      12/7/2010 2:05:00 PM Entered internal PlaceOrder() method at 12/7/2010 2:05:00 PM: BarsInProgress=1 Action=Buy OrderType=Limit Quantity=1 LimitPrice=1232.00 StopPrice=0 SignalName='Long1' FromEntrySignal=''
      12/7/2010 2:05:00 PM Entered internal PlaceOrder() method at 12/7/2010 2:05:00 PM: BarsInProgress=1 Action=Buy OrderType=Limit Quantity=1 LimitPrice=1232.00 StopPrice=0 SignalName='Long2' FromEntrySignal=''
      12/7/2010 2:05:00 PM nBar: 6493 AvgPrice 0 Qnty: 0
      12/7/2010 2:05:00 PM Cancelled pending exit order, since associated position is closed: Order='NT-00057/Backtest' Name='Profit target' State=Working Instrument='ES 12-10' Action=Sell Limit price=1234 Stop price=0 Quantity=1 Strategy='ExampleIntrabarStopLoss' Type=Limit Tif=Gtc Oco='NT-00032-213' Filled=0 Fill price=0 Token='a4241e781c97421491c6a2432ad242de' Gtd='12/1/2099 12:00:00 AM'
      12/7/2010 2:00:00 PM Cancelled OCO paired order: BarsInProgress=0: Order='NT-00057/Backtest' Name='Profit target' State=Cancelled Instrument='ES 12-10' Action=Sell Limit price=1234 Stop price=0 Quantity=1 Strategy='ExampleIntrabarStopLoss' Type=Limit Tif=Gtc Oco='NT-00032-213' Filled=0 Fill price=0 Token='a4241e781c97421491c6a2432ad242de' Gtd='12/1/2099 12:00:00 AM'
      12/7/2010 2:05:00 PM Cancelled pending exit order, since associated position is closed: Order='NT-00059/Backtest' Name='Profit target' State=Working Instrument='ES 12-10' Action=Sell Limit price=1236 Stop price=0 Quantity=1 Strategy='ExampleIntrabarStopLoss' Type=Limit Tif=Gtc Oco='NT-00033-213' Filled=0 Fill price=0 Token='eb368323d23545e6ad1422a0d52192d9' Gtd='12/1/2099 12:00:00 AM'
      12/7/2010 2:00:00 PM Cancelled OCO paired order: BarsInProgress=0: Order='NT-00059/Backtest' Name='Profit target' State=Cancelled Instrument='ES 12-10' Action=Sell Limit price=1236 Stop price=0 Quantity=1 Strategy='ExampleIntrabarStopLoss' Type=Limit Tif=Gtc Oco='NT-00033-213' Filled=0 Fill price=0 Token='eb368323d23545e6ad1422a0d52192d9' Gtd='12/1/2099 12:00:00 AM'
      12/7/2010 2:06:00 PM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='Long1' Mode=Ticks Value=16 Currency=0 Simulated=True
      12/7/2010 2:06:00 PM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='Long2' Mode=Ticks Value=16 Currency=0 Simulated=True

      Comment


        #4
        Hello,

        I'm not seeing that the stop losses ever get a change to be submitted as buy the time the stop losses try to submit the position is already closed is what I get from this output.

        Something is closing the position the second it is opened it seems.

        Perhaps this:

        else if (Position.Quantity == NContracts)
        {
        //adjust stop loss to Target4 (breakeven)
        SetStopLoss("Long2", CalculationMode.Ticks, 0.0, true);


        Try commenting this out does this change anything. If not please post your code again with your changes and I will give it a quick run on my side. However I could see the above giving issue as you split your entried up into 2 entries. Therefor 1 could fill, satisfy this condition before the other half fills for example. Would be a rare case however and would qualify as whats known as a race condition.

        I look forward to assisting you further.

        Comment


          #5
          Hi Brett,

          Thanks for the very quick reply! I commented out the second entry ("Long2") as well as associated stop loss adjustment. It does appear the position is opened and exited immediately at the stoploss for each minute of the large down 60min bar. I ran the backtest from 12/1/10 to present using the ES 60min. The following code just has LongEntryLimit(), SetStopLoss(), and SetProfitTarget for 1 position, "Long1".

          Thanks again for your help!

          Code:
          #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>
              /// ExampleIntrabarStopLoss
              /// </summary>
              [Description("ExampleIntrabarStopLoss")]
              public class ExampleIntrabarStopLoss : Strategy
              {
                  #region Variables
                  // Wizard generated variables
                  private int    nContracts = 1;    // Default setting for NContracts
                  private double currentHrBarOpen = 1000000;  // set high so entry not falsely triggered
                  private double target1 = 2.000;   // Default setting for Target1
                  private double target2 = 4.000;   // Default setting for Target2
                  private double target3 = 1;       // Default setting for Target3
                  private double target4 = 0.000;   // Default setting for Target4
                  private double stopLoss = 4.000; // Default setting for StopLoss
                  // User defined variables (add any user defined variables below)
                  #endregion
          
                  // Strategy:     ExampleIntrabarStopLoss
                  // Instrument:     ES
                  // Series:       Primary:  60min
                  //                 Secondary: 1min
                  // Description:  Backtest of simple strategy the uses the 60min bars for entry
                  //               signals.  
                  //               Entry and Exit on 1min timeframe 
                  //
                  // This method is used to configure the strategy and is called once before any strategy method is called.
                  protected override void Initialize()
                  {
                      // Buy 2*NContracts and have separate exits
                      EntriesPerDirection = 2; 
                      
                      //Note: The primary bar series is whatever you choose for the strategy at startup. 
                      //      Strategy Analyzer Chart displays primary bars ONLY
                      //Primary:       60min
                      //Add Secondary: 1min
                      Add(PeriodType.Minute, 1);
                      
                      TraceOrders        = true; 
                                  
                      CalculateOnBarClose = true;  // set to true for historical testing
                  }
          
                  /// Called after each bar close if CalculateOnBarClose == true
                  protected override void OnBarUpdate()
                  {
                      // Sets the back color to empty when Time is between 9am-4:15pm EST, and to Lavender otherwise
                      BackColor = (ToTime(Time[0]) >= 90000 && ToTime(Time[0]) <= 161500) ? Color.Empty : Color.Lavender;
                      
                      // Return unless time between 9:00am and 4:15pm.  
                      if (ToTime(Time[0]) < ToTime(9, 00, 0) || ToTime(Time[0]) > ToTime(16, 15, 0)) return;
                              
                      // BarsInProgress = 0 for 60min bars; 1 for 1min bars
                      if (BarsInProgress == 1)
                      {
                          int nbar1 = 0;
                          double exitPrice = 0;
                          double entryPrice = 0;
                          
                          // Check for zeroth minute
                          if( Time[0].Minute == 0 )  
                          {
                              nbar1 = 1;  //1min 00min called after 60min 00min, therefore need to index previous 60min bar
                          }
                          // Check for 1st minute of hour to set open value (for hour)
                          else if ( Time[0].Minute == 1 )
                          {
                              CurrentHrBarOpen = Open[0];
                              Print("CurrentHrBarOpen: " 
                                      + " nBar: " + CurrentBar.ToString() 
                                      + " Time: " + ToTime(Time[0]).ToString() 
                                      + " Open[0]: " + Open[0].ToString() 
                                      + " CurrentHrBarOpen " + CurrentHrBarOpen.ToString());
                          }
                          
                          // Order entry note: Long entry order only valid for current bar of current barArray unless 'LiveToCancelled' == 1
                          // Only check for long entry condition if no position is open
                          //if (Position.Quantity == 0)
                          if (Position.MarketPosition == MarketPosition.Flat)
                          {
                              if ( Closes[0][nbar1] > Highs[0][nbar1+1]
                                   && ToTime(Time[0]) < ToTime(16, 0, 0)) 
                              {
                                  entryPrice = Highs[0][nbar1] + 0.25;
                                  
                                  // StopLoss has to be entered before submitting EnterLong 
                                  SetStopLoss("Long1", CalculationMode.Ticks, 4.0*StopLoss, true);
                                  //SetStopLoss("Long2", CalculationMode.Ticks, 4.0*StopLoss, true);
                                  
                                  // Might as well move up ProfitTarget as well
                                  SetProfitTarget("Long1", CalculationMode.Ticks, 4.0*Target1);
                                  //SetProfitTarget("Long2", CalculationMode.Ticks, 4.0*Target2);
                                  
                                  // if minute 0: will not know open at minute 1, so execute a 'EnterLongStop'
                                  if( Time[0].Minute == 0 )  
                                  {
                                      EnterLongStop( NContracts, entryPrice, "Long1");  
                                      //EnterLongStop( NContracts, entryPrice, "Long2");    
                                  }
                                  else if (CurrentHrBarOpen < Highs[0][nbar1])
                                  {
                                      EnterLongLimit( NContracts, entryPrice, "Long1");
                                      //EnterLongLimit( NContracts, entryPrice, "Long2");
                                  }
                               }
                          }
                          
                          // Check targets 
                          /*
                          else if (Position.Quantity == 2*NContracts)
                          {
                              if (ToTime(Time[0]) == ToTime(16, 14, 0) )
                              {
                                  ExitLong("Time Exit L1", "Long1");
                                  ExitLong("Time Exit L2", "Long2");
                              }
                          }
                          else if (Position.Quantity == NContracts)
                          {                
                              //adjust stop loss to Target4 (breakeven)
                              //SetStopLoss("Long2", CalculationMode.Ticks, 0.0, true);
          
                              if (ToTime(Time[0]) == ToTime(16, 14, 0) )
                              {
                                  ExitLong("Time Exit L2", "Long2"); //ExitLong("Long2");
                              }
                          }
                          */
                          
                      }
                      
                      //Print out section outside of BarsInProgress condition statement
                      if (Position.Quantity != 1000)
                      {
                          Print(Time[0].ToString()
                          + "BarsInProgress: " + BarsInProgress.ToString()
                          + " nBar: " + CurrentBar.ToString() 
                          + " AvgPrice " + Position.AvgPrice.ToString() 
                          + " Qnty: " + Position.Quantity.ToString());
                      }
                  }
          
                  #region Properties
                  [Description("")]
                  [GridCategory("Parameters")]
                  public int NContracts
                  {
                      get { return nContracts; }
                      set { nContracts = Math.Max(1, value); }
                  }
          
                  [Description("")]
                  [GridCategory("Parameters")]
                  public double CurrentHrBarOpen
                  {
                      get { return currentHrBarOpen; }
                      set { currentHrBarOpen = Math.Max(0, value); }
                  }
                  
                  [Description("")]
                  [GridCategory("Parameters")]
                  public double Target1
                  {
                      get { return target1; }
                      set { target1 = Math.Max(0, value); }
                  }
          
                  [Description("")]
                  [GridCategory("Parameters")]
                  public double Target2
                  {
                      get { return target2; }
                      set { target2 = Math.Max(0, value); }
                  }
          
                  [Description("")]
                  [GridCategory("Parameters")]
                  public double Target3
                  {
                      get { return target3; }
                      set { target3 = Math.Max(0, value); }
                  }
          
                  [Description("")]
                  [GridCategory("Parameters")]
                  public double Target4
                  {
                      get { return target4; }
                      set { target4 = Math.Max(0, value); }
                  }
          
                  [Description("")]
                  [GridCategory("Parameters")]
                  public double StopLoss
                  {
                      get { return stopLoss; }
                      set { stopLoss = Math.Max(-1000.000, value); }
                  }
                  #endregion
              }
          }

          Comment


            #6
            Hello,

            So to reclarify did the comment out did allow the trade to last longer or its still getting closed still on entry.

            Thanks.

            Comment


              #7
              I see no change in the problem after commenting out both the stoploss adjustment and multiple entry. The trade is still getting closed out immediately; i.e., within the minute bar that it is entered. The code that I posted is about as simple as it can be. The timestamp of stop-loss is still odd (not chronological) as I mentioned previously. Please give it a try.

              Many Thanks...

              Comment


                #8
                Here's another thought: After reviewing the Help Guide for SetStopLoss(), and SetProfitTarget(), I see that there is no method overload that includes barsInProgress input variable. Does this mean that the SetStopLoss() and SetProfitTarget() methods are NOT specific to the Bars object?

                Also, there is this from the Help Guide:
                Note: Should you have multiple Bars objects of the same instrument and are using Set() methods in your strategy, you should only submit orders for this instrument to the first Bars context of that instrument. This is to ensure your order logic is processed correctly and any necessary order amendments are done properly.

                Hmmm...my example code does not follow the above.

                If I can not use SetStopLoss and SetProfitTarget to work ONLY on the intrabar timeframe (1min), please recommend how I may implement that functionality on the intrabar timeframe.
                Last edited by ultrablue; 12-24-2010, 05:07 AM.

                Comment


                  #9
                  ultrablue, correct they are not tied to a bars object series - if you need to tie the exits to a bars series, please work with the Exit methods offering this parameter in their overload.
                  BertrandNinjaTrader Customer Service

                  Comment


                    #10
                    If I can not use SetStopLoss and SetProfitTarget to work ONLY on the intrabar timeframe (1min), please recommend how I may implement that functionality on the intrabar timeframe.
                    and then...

                    ultrablue, correct they are not tied to a bars object series - if you need to tie the exits to a bars series, please work with the Exit methods offering this parameter in their overload.
                    In using this forum, I was hoping to get some real help. Yes, the EXIT methods must be used, but that doesn't fully answer my question.

                    Again, if I can not use SetStopLoss and SetProfitTarget to work ONLY on the intrabar timeframe (1min), please recommend how I may implement that functionality on the intrabar timeframe.

                    I am a competent programmer. Through trial and error, I can come to a workable solution. But then again, isn't the purpose of this forum to help reduce the learning curve? So far, it hasn't worked that way even though I have been very clear and detailed with the problem.

                    Thanks is advance for you reply and time. I look forward to a practical solution to replicate the functionality of the SetStopLoss and SetProfitTarget methods.

                    Comment


                      #11
                      ultrablue, I'm not sure I follow you - the Set's are indeed not tied to specific bars object, if you need this to implement your custom strategy the previous recommendation stands to work with the Exit methods offering a dedicated BIP parameter to submit the order against.
                      BertrandNinjaTrader Customer Service

                      Comment


                        #12
                        Hello,

                        I know this is an old topic, sorry to bring it back. I found this topic looking for an issue on intrabar stop loss during backtesting being triggered irrationally on worst price.

                        But could you confirm my understanding. If we write a strategy using several bar series (minute, 5 min, etc), it is not possible to call a SetStopLoss on every tick to generate an in house trailing stop?

                        And this is not possible because our strategy has several time series?

                        So in this case, we can not have stop loss but only market exits?

                        Many thanks,
                        Nicolas

                        Comment


                          #13
                          Hello AlgoNaute,

                          Could you clarify what your Primary Bar Series Type that you are using?

                          As for calculating a Stop Loss every tick it would be possible to calculate on each incoming tick, but you would have to set the Stop order (using Exit method) and have a Bar Series of 1 Tick. This is due to the fact that the SetStopLoss or SetTrailStop method are going to use the Primary Bar Series for its calculations.

                          Happy to be of further assistance.
                          JCNinjaTrader Customer Service

                          Comment


                            #14
                            Hi JC,

                            As primary serie, I use N minutes bar data but with CalculateOnClose at false so I can get tick data through it.

                            I changed the code to update stop loss only when we process primary data bar. Seems to work ok on backtest now. But need to make sure it is working well in live too with tick call back through CalculateOnClose at false.

                            Thanks for your time.
                            Last edited by AlgoNaute; 08-20-2013, 07:23 AM.

                            Comment


                              #15
                              Hello AlgoNaute,

                              Glad to hear it.

                              Just a note, using the Strategy Analyzer to Backtest your Strategy CalculateOnBarClose (COBC) will always be true, so if you would like it to be a true Tick by Tick like COBC false, you would have to add intrabar granularity like in the following example:

                              You can submit orders to different Bars objects. This allows you the flexibility of submitting orders to different timeframes. Like in live trading, taking entry conditions from a 5min chart means executing your order as soon as possible instead of waiting until the next 5min bar starts building. You can achieve this by
                              JCNinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by kempotrader, Today, 08:54 AM
                              0 responses
                              0 views
                              0 likes
                              Last Post kempotrader  
                              Started by mmenigma, Today, 08:54 AM
                              0 responses
                              1 view
                              0 likes
                              Last Post mmenigma  
                              Started by halgo_boulder, Today, 08:44 AM
                              0 responses
                              1 view
                              0 likes
                              Last Post halgo_boulder  
                              Started by drewski1980, Today, 08:24 AM
                              0 responses
                              3 views
                              0 likes
                              Last Post drewski1980  
                              Started by rdtdale, Yesterday, 01:02 PM
                              2 responses
                              17 views
                              0 likes
                              Last Post rdtdale
                              by rdtdale
                               
                              Working...
                              X