NinjaTrader Support Forum  

Go Back   NinjaTrader Support Forum > NinjaScript Development Support > General Programming

General Programming General NinjaScript programming questions.

Reply
 
Thread Tools Display Modes
Old 02-28-2009, 06:43 PM   #1
jvaughn
Junior Member
 
Join Date: Jan 2009
Posts: 4
Thanks: 0
Thanked 0 times in 0 posts
Default 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
    }
}
jvaughn is offline  
Reply With Quote
Old 02-28-2009, 11:35 PM   #2
roonius
Certified NinjaScript Consultant
 
Join Date: Oct 2008
Location: Chicago, IL
Posts: 523
Thanks: 0
Thanked 3 times in 3 posts
Send a message via Skype™ to roonius
Default

Quote:
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
roonius is offline  
Reply With Quote
Old 03-01-2009, 07:01 AM   #3
jvaughn
Junior Member
 
Join Date: Jan 2009
Posts: 4
Thanks: 0
Thanked 0 times in 0 posts
Default

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?
jvaughn is offline  
Reply With Quote
Old 03-01-2009, 03:26 PM   #4
roonius
Certified NinjaScript Consultant
 
Join Date: Oct 2008
Location: Chicago, IL
Posts: 523
Thanks: 0
Thanked 3 times in 3 posts
Send a message via Skype™ to roonius
Default

Quote:
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
roonius is offline  
Reply With Quote
Old 03-01-2009, 04:51 PM   #5
jvaughn
Junior Member
 
Join Date: Jan 2009
Posts: 4
Thanks: 0
Thanked 0 times in 0 posts
Default

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 Images
File Type: jpg volstop2.jpg (87.8 KB, 29 views)
File Type: jpg volstop3.jpg (97.3 KB, 21 views)
jvaughn is offline  
Reply With Quote
Old 03-07-2009, 08:14 PM   #6
jvaughn
Junior Member
 
Join Date: Jan 2009
Posts: 4
Thanks: 0
Thanked 0 times in 0 posts
Default

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?
jvaughn is offline  
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
SetTrailStop frequency ctrlbrk Strategy Development 5 11-01-2008 03:05 PM
How to SetStopLoss and SetTrailStop cassb Strategy Development 14 08-03-2008 09:19 PM
SetTrailStop() funk101 Strategy Development 1 04-04-2007 03:39 AM
setTrailStop() not executing funk101 Strategy Development 1 04-02-2007 08:32 AM
SetTrailStop() funk101 Strategy Development 8 03-27-2007 07:42 AM


All times are GMT -6. The time now is 03:38 AM.