Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Give me a break (even)

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

    Give me a break (even)

    My break-even tests are behaving strange. Sometimes it exits on the same bar with a stop order (see picture).
    Can you point me to the error in my strategy?

    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>
        /// Enter the description of your strategy here
        /// </summary>
        [Description("Enter the description of your strategy here")]
        public class SMAcrossBreakeven2 : Strategy
        {
            #region Variables
            // Wizard generated variables
            private int fast = 1; // Default setting for Fast
            private int slow = 1; // Default setting for Slow
            private double trigger = 100; // Default setting for Trigger
            
            
            // 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()
            {
                Add(SMA(Close, Fast));
                Add(SMA(Close, Slow));
                
                SetStopLoss(CalculationMode.Ticks, 100);
                
                CalculateOnBarClose = true;
            }
    
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
                // Beginning of Auto-Break-Even            
                // If a long position is open
                if (Position.MarketPosition == MarketPosition.Long)
                    {
                        
                    // Once the price is greater than entry price + Trigger * ticks, set stop loss to breakeven
                    if (Close[0] > Position.AvgPrice + trigger * TickSize)
                        {
                            SetStopLoss(CalculationMode.Price, Position.AvgPrice);
                        }
                    }
                // If a short position is open
                if (Position.MarketPosition == MarketPosition.Short)
                    {    
                        
                    // Once the price is smaller than entry price - Trigger * ticks, set stop loss to breakeven
                    if (Close[0] < Position.AvgPrice - trigger * TickSize)
                        {
                            SetStopLoss(CalculationMode.Price, Position.AvgPrice);
                        }
                    }
                // Ende of Auto-Break-Even
                    
                    // Condition set 1
                    if (CrossAbove(SMA(Close, Fast), SMA(Close, Slow), 1))
                    {
                        EnterLong(DefaultQuantity, "");
                    }
    
                // Condition set 2
                if (CrossBelow(SMA(Close, Fast), SMA(Close, Slow), 1))
                {
                    EnterShort(DefaultQuantity, "");
                }
                
            }
    
            #region Properties
            [Description("")]
            [GridCategory("Parameters")]
            public int Fast
            {
                get { return fast; }
                set { fast = Math.Max(1, value); }
            }
    
            [Description("")]
            [GridCategory("Parameters")]
            public int Slow
            {
                get { return slow; }
                set { slow = Math.Max(1, value); }
            }
    
            [Description("")]
            [GridCategory("Parameters")]
            public double Trigger
            {
                get { return trigger; }
                set { trigger = Math.Max(0, value); }
            }
            #endregion
        }
    }
    Attached Files

    #2
    Hello,

    You should make sure you are resetting our stop loss when your position is flat.

    Code:
    			// Resets the stop loss to the original value when all positions are closed
    			if (Position.MarketPosition == MarketPosition.Flat)
    			{
    				            SetStopLoss(CalculationMode.Ticks, 100);
    			}
    Otherwise, it may use the same stop loss value as the previous position.
    MatthewNinjaTrader Product Management

    Comment


      #3
      Still strange

      I added the code, but get the same false stops on some trades. (See picture)
      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>
          /// Enter the description of your strategy here
          /// </summary>
          [Description("Enter the description of your strategy here")]
          public class SMAcrossBreakeven2 : Strategy
          {
              #region Variables
              // Wizard generated variables
              private int fast = 1; // Default setting for Fast
              private int slow = 1; // Default setting for Slow
              private double trigger = 100; // Default setting for Trigger
              
              
              // 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()
              {
                  Add(SMA(Close, Fast));
                  Add(SMA(Close, Slow));
                  
                  SetStopLoss(CalculationMode.Ticks, 100);
                  
                  CalculateOnBarClose = true;
              }
      
              /// <summary>
              /// Called on each bar update event (incoming tick)
              /// </summary>
              protected override void OnBarUpdate()
              {
                  // Beginning of Auto-Break-Even    
                  
                  // Resets the stop loss to the original value when all positions are closed
                  if (Position.MarketPosition == MarketPosition.Flat)
                  {
                                  SetStopLoss(CalculationMode.Ticks, 100);
                  }
                  
                  // If a long position is open
                  if (Position.MarketPosition == MarketPosition.Long)
                      {
                          
                      // Once the price is greater than entry price + Trigger * ticks, set stop loss to breakeven
                      if (Close[0] > Position.AvgPrice + trigger * TickSize)
                          {
                              SetStopLoss(CalculationMode.Price, Position.AvgPrice);
                          }
                      }
                  // If a short position is open
                  if (Position.MarketPosition == MarketPosition.Short)
                      {    
                          
                      // Once the price is smaller than entry price - Trigger * ticks, set stop loss to breakeven
                      if (Close[0] < Position.AvgPrice - trigger * TickSize)
                          {
                              SetStopLoss(CalculationMode.Price, Position.AvgPrice);
                          }
                      }
                  // Ende of Auto-Break-Even
                      
                      // Condition set 1
                      if (CrossAbove(SMA(Close, Fast), SMA(Close, Slow), 1))
                      {
                          EnterLong(DefaultQuantity, "");
                      }
      
                  // Condition set 2
                  if (CrossBelow(SMA(Close, Fast), SMA(Close, Slow), 1))
                  {
                      EnterShort(DefaultQuantity, "");
                  }
                  
              }
      
              #region Properties
              [Description("")]
              [GridCategory("Parameters")]
              public int Fast
              {
                  get { return fast; }
                  set { fast = Math.Max(1, value); }
              }
      
              [Description("")]
              [GridCategory("Parameters")]
              public int Slow
              {
                  get { return slow; }
                  set { slow = Math.Max(1, value); }
              }
      
              [Description("")]
              [GridCategory("Parameters")]
              public double Trigger
              {
                  get { return trigger; }
                  set { trigger = Math.Max(0, value); }
              }
              #endregion
          }
      }
      Attached Files

      Comment


        #4
        Originally posted by moon_121 View Post
        I added the code, but get the same false stops on some trades. (See picture)
        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>
            /// Enter the description of your strategy here
            /// </summary>
            [Description("Enter the description of your strategy here")]
            public class SMAcrossBreakeven2 : Strategy
            {
                #region Variables
                // Wizard generated variables
                private int fast = 1; // Default setting for Fast
                private int slow = 1; // Default setting for Slow
                private double trigger = 100; // Default setting for Trigger
         
         
                // 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()
                {
                    Add(SMA(Close, Fast));
                    Add(SMA(Close, Slow));
         
                    SetStopLoss(CalculationMode.Ticks, 100);
         
                    CalculateOnBarClose = true;
                }
         
                /// <summary>
                /// Called on each bar update event (incoming tick)
                /// </summary>
                protected override void OnBarUpdate()
                {
                    // Beginning of Auto-Break-Even    
         
                    // Resets the stop loss to the original value when all positions are closed
                    if (Position.MarketPosition == MarketPosition.Flat)
                    {
                                    SetStopLoss(CalculationMode.Ticks, 100);
                    }
         
                    // If a long position is open
                    if (Position.MarketPosition == MarketPosition.Long)
                        {
         
                        // Once the price is greater than entry price + Trigger * ticks, set stop loss to breakeven
                        if (Close[0] > Position.AvgPrice + trigger * TickSize)
                            {
                                SetStopLoss(CalculationMode.Price, Position.AvgPrice);
                            }
                        }
                    // If a short position is open
                    if (Position.MarketPosition == MarketPosition.Short)
                        {    
         
                        // Once the price is smaller than entry price - Trigger * ticks, set stop loss to breakeven
                        if (Close[0] < Position.AvgPrice - trigger * TickSize)
                            {
                                SetStopLoss(CalculationMode.Price, Position.AvgPrice);
                            }
                        }
                    // Ende of Auto-Break-Even
         
                        // Condition set 1
                        if (CrossAbove(SMA(Close, Fast), SMA(Close, Slow), 1))
                        {
                            EnterLong(DefaultQuantity, "");
                        }
         
                    // Condition set 2
                    if (CrossBelow(SMA(Close, Fast), SMA(Close, Slow), 1))
                    {
                        EnterShort(DefaultQuantity, "");
                    }
         
                }
         
                #region Properties
                [Description("")]
                [GridCategory("Parameters")]
                public int Fast
                {
                    get { return fast; }
                    set { fast = Math.Max(1, value); }
                }
         
                [Description("")]
                [GridCategory("Parameters")]
                public int Slow
                {
                    get { return slow; }
                    set { slow = Math.Max(1, value); }
                }
         
                [Description("")]
                [GridCategory("Parameters")]
                public double Trigger
                {
                    get { return trigger; }
                    set { trigger = Math.Max(0, value); }
                }
                #endregion
            }
        }
        Your system is a Stop&Reverse system, so you should set your StopLoss immediately before your entries, because you will never be Flat except for before the first entry.
        Code:
                        // Condition set 1
                        if (CrossAbove(SMA(Close, Fast), SMA(Close, Slow), 1))
                        {
                            SetStopLoss(CalculationMode.Ticks, 100);
                            EnterLong(DefaultQuantity, "");
                        }
         
                    // Condition set 2
                    if (CrossBelow(SMA(Close, Fast), SMA(Close, Slow), 1))
                    {
                        SetStopLoss(CalculationMode.Ticks, 100);
                        EnterShort(DefaultQuantity, "");
                    }

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by mjairg, 07-20-2023, 11:57 PM
        3 responses
        213 views
        1 like
        Last Post PaulMohn  
        Started by TheWhiteDragon, 01-21-2019, 12:44 PM
        4 responses
        544 views
        0 likes
        Last Post PaulMohn  
        Started by GLFX005, Today, 03:23 AM
        0 responses
        3 views
        0 likes
        Last Post GLFX005
        by GLFX005
         
        Started by XXtrader, Yesterday, 11:30 PM
        2 responses
        12 views
        0 likes
        Last Post XXtrader  
        Started by Waxavi, Today, 02:10 AM
        0 responses
        7 views
        0 likes
        Last Post Waxavi
        by Waxavi
         
        Working...
        X