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

SetTrailStop Help

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

    SetTrailStop Help

    I've viewed the posts and help section regarding setting stop losses, but am still unable to see my strategy work correctly in backtesting.
    I'm simply trying to get a trailing stop that equals the price of an indicator (this could be any indicator).
    In other words, if i'm long, i might assign a trailing stop that is equal to a moving average that is currently below the quote.
    Below is the sample code of what i'm trying to achieve...can anyone shed some light on what i'm doing wrong - it compiles with no errors, but stop loss doesn't seem to work when i run backtest?

    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>
        /// MySampleTrail
        /// </summary>
        [Description("MySampleTrail")]
        public class MySampleTrail : Strategy
        {
            #region Variables
            // Wizard generated variables
            private int sMA1 = 100; // Default setting for SMA1
            private int sMA2 = 200; // Default setting for SMA2
            // 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(SMA1));
                Add(SMA(SMA2));
                Add(SMA(SMA1));
    
                CalculateOnBarClose = true;
            }
    
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
                // Resets the stop loss to the original value when all positions are closed
                if (Position.MarketPosition == MarketPosition.Flat)
                {
                    SetTrailStop(CalculationMode.Ticks, 300);
                }
                // If a long position is open, allow for stop loss modification
                else if (Position.MarketPosition == MarketPosition.Long)
                {
                    SetTrailStop(CalculationMode.Price, SMA(50)[0]);
                }
                // If a short position is open, allow for stop loss modification
                else if (Position.MarketPosition == MarketPosition.Short)
                {
                    SetTrailStop(CalculationMode.Price, SMA(50)[0]);
                }
                // Condition set 1
                if (CrossAbove(SMA(SMA1), SMA(SMA2), 1))
                {
                    EnterLong(DefaultQuantity, "");
                }
    
                // Condition set 2
                if (CrossBelow(SMA(SMA1), SMA(SMA2), 1))
                {
                    EnterShort(DefaultQuantity, "");
                }
            }
    
            #region Properties
            [Description("SMA1")]
            [Category("Parameters")]
            public int SMA1
            {
                get { return sMA1; }
                set { sMA1 = Math.Max(1, value); }
            }
    
            [Description("SMA2")]
            [Category("Parameters")]
            public int SMA2
            {
                get { return sMA2; }
                set { sMA2 = Math.Max(1, value); }
            }
            #endregion
        }
    }

    #2
    Originally posted by jvaughn View Post
    I've viewed the posts and help section regarding setting stop losses, but am still unable to see my strategy work correctly in backtesting.
    I'm simply trying to get a trailing stop that equals the price of an indicator (this could be any indicator).
    In other words, if i'm long, i might assign a trailing stop that is equal to a moving average that is currently below the quote.
    Below is the sample code of what i'm trying to achieve...can anyone shed some light on what i'm doing wrong - it compiles with no errors, but stop loss doesn't seem to work when i run backtest?

    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>
        /// MySampleTrail
        /// </summary>
        [Description("MySampleTrail")]
        public class MySampleTrail : Strategy
        {
            #region Variables
            // Wizard generated variables
            private int sMA1 = 100; // Default setting for SMA1
            private int sMA2 = 200; // Default setting for SMA2
            // 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(SMA1));
                Add(SMA(SMA2));
                Add(SMA(SMA1));
     
                CalculateOnBarClose = true;
            }
     
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
                // Resets the stop loss to the original value when all positions are closed
                if (Position.MarketPosition == MarketPosition.Flat)
                {
                    SetTrailStop(CalculationMode.Ticks, 300);
                }
                // If a long position is open, allow for stop loss modification
                else if (Position.MarketPosition == MarketPosition.Long)
                {
                    SetTrailStop(CalculationMode.Price, SMA(50)[0]);
                }
                // If a short position is open, allow for stop loss modification
                else if (Position.MarketPosition == MarketPosition.Short)
                {
                    SetTrailStop(CalculationMode.Price, SMA(50)[0]);
                }
                // Condition set 1
                if (CrossAbove(SMA(SMA1), SMA(SMA2), 1))
                {
                    EnterLong(DefaultQuantity, "");
                }
     
                // Condition set 2
                if (CrossBelow(SMA(SMA1), SMA(SMA2), 1))
                {
                    EnterShort(DefaultQuantity, "");
                }
            }
     
            #region Properties
            [Description("SMA1")]
            [Category("Parameters")]
            public int SMA1
            {
                get { return sMA1; }
                set { sMA1 = Math.Max(1, value); }
            }
     
            [Description("SMA2")]
            [Category("Parameters")]
            public int SMA2
            {
                get { return sMA2; }
                set { sMA2 = Math.Max(1, value); }
            }
            #endregion
        }
    }
    In your case I would replace SetTrailStop to SetStopLoss

    Comment


      #3
      Thanks for the reply roonius. My problem with using SetStopLoss is: if i were to plug in a volatility indicator as the price i want to use as a Stop then i'll end up with a stop loss that can move away from me. I figure if i can get SetTrailStop to work i can use just about any indicator for a stoploss.
      Does that make sense?

      Comment


        #4
        Originally posted by jvaughn View Post
        Thanks for the reply roonius. My problem with using SetStopLoss is: if i were to plug in a volatility indicator as the price i want to use as a Stop then i'll end up with a stop loss that can move away from me. I figure if i can get SetTrailStop to work i can use just about any indicator for a stoploss.
        Does that make sense?
        That what I was saying - just try it

        Comment


          #5
          I added a couple pics so you can see what i mean. If i use a volatility indicator the SetStopLoss doesn't act as a trailing stop and it allows the stoploss to move away from me.
          Attached Files

          Comment


            #6
            is there a way to reference the previous stoploss instead of the previous bar - so that the stoploss only moves if its > or < the previous stoploss?

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by algospoke, 04-17-2024, 06:40 PM
            3 responses
            26 views
            0 likes
            Last Post NinjaTrader_Jesse  
            Started by bmartz, 03-12-2024, 06:12 AM
            3 responses
            27 views
            0 likes
            Last Post NinjaTrader_Zachary  
            Started by Aviram Y, Today, 05:29 AM
            2 responses
            8 views
            0 likes
            Last Post Aviram Y  
            Started by gentlebenthebear, Today, 01:30 AM
            1 response
            8 views
            0 likes
            Last Post NinjaTrader_Jesse  
            Started by cls71, Today, 04:45 AM
            1 response
            7 views
            0 likes
            Last Post NinjaTrader_ChelseaB  
            Working...
            X