• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

SetStopLoss, SetProfitTarget, SetTrailStop not working?

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

    SetStopLoss, SetProfitTarget, SetTrailStop not working?

    Hi,

    I have created a simple MSCrossOver strategy using the wizard. It has SetStopLoss, SetProfitTarget and SetTrailStop set but they do not seem to make any difference (in the strategy analyser backtests) when I set different target, stops, trailingstops values.

    Thanks

    The generated code is shown below:

    #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 MFMAsCrossover : Strategy
    {
    #region Variables
    // Wizard generated variables
    private int slowMA = 4; // Default setting for SlowMA
    private int fastMA = 2; // Default setting for FastMA
    private double limitPercent = 3; // Default setting for LimitPercent
    private double stopPercent = 3.000; // Default setting for StopPercent
    private double trailingPercent = 0.500; // Default setting for TrailingPercent
    // 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(FastMA));
    Add(SMA(SlowMA));
    Add(SMA(FastMA));
    Add(SMA(SlowMA));
    SetProfitTarget("Enter Long", CalculationMode.Percent, LimitPercent);
    SetStopLoss("Enter Short", CalculationMode.Percent, StopPercent, false);
    SetTrailStop("Enter Long", CalculationMode.Percent, TrailingPercent, false);
    SetProfitTarget("Enter Short", CalculationMode.Percent, TrailingPercent);
    SetStopLoss("Enter Long", CalculationMode.Percent, TrailingPercent, false);
    SetTrailStop("Enter Short", CalculationMode.Percent, TrailingPercent, false);

    CalculateOnBarClose = true;
    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    // Condition set 1
    if (CrossAbove(SMA(FastMA), SMA(SlowMA), 1))
    {
    EnterLong(DefaultQuantity, "Enter Long");
    }

    // Condition set 2
    if (CrossBelow(SMA(FastMA), SMA(SlowMA), 1))
    {
    EnterShort(DefaultQuantity, "Enter Long");
    }
    }

    #region Properties
    [Description("")]
    [GridCategory("Parameters")]
    public int SlowMA
    {
    get { return slowMA; }
    set { slowMA = Math.Max(4, value); }
    }

    [Description("")]
    [GridCategory("Parameters")]
    public int FastMA
    {
    get { return fastMA; }
    set { fastMA = Math.Max(2, value); }
    }

    [Description("")]
    [GridCategory("Parameters")]
    public double LimitPercent
    {
    get { return limitPercent; }
    set { limitPercent = Math.Max(0.1, value); }
    }

    [Description("")]
    [GridCategory("Parameters")]
    public double StopPercent
    {
    get { return stopPercent; }
    set { stopPercent = Math.Max(0.100, value); }
    }

    [Description("")]
    [GridCategory("Parameters")]
    public double TrailingPercent
    {
    get { return trailingPercent; }
    set { trailingPercent = Math.Max(0.500, value); }
    }
    #endregion
    }
    }

    #2
    Hello,
    If you are trying to set a stop or target using the CalculationMode.Percent then please set the value as 0.02 for 2% stop/target. Currently use are using absolute values like 2 etc.

    http://www.ninjatrader.com/support/h...etstoploss.htm
    http://www.ninjatrader.com/support/h...ofittarget.htm

    Please let me know if I can assist you any further.
    JoydeepNinjaTrader Customer Service

    Comment


      #3
      Setting stops and targets

      Hi Joydeep,

      Yes, thanks, that fixed it.

      Comment


        #4
        Hello Downton,
        Glad to know everything is working fine at your end.

        Please let me know if I can assist you any further.
        JoydeepNinjaTrader Customer Service

        Comment


          #5
          Further on Stop loss

          Hi,

          I am now setting the SL to 0.01 (1%) in the code I posted in these thread. The strategy stops trades at -1% (correctly) and those are marked as 'stop loss', but there are a few trades (see attached .doc for screen image of the strategy performance trades grid) that are not stopped at 1% and run into a greater loss and are marked as 'Close position'. Could you advise on why this is happening, please.

          Note that I have the 'check on bar close' set to false.

          thanks
          Attached Files

          Comment


            #6
            Hello Downton,
            The entry trades are being closed due to an reversal signals and not due to a stop loss. What stop codes have you used for the long trades?
            JoydeepNinjaTrader Customer Service

            Comment


              #7
              Further on Stop loss

              The code I'm executing is the code pasted into the earlier message in this thread (created by the strategy wizard).

              And I set the strategy parameter 'stopPercent' to 0.01.

              I hope it's clear

              Thanks

              Comment


                #8
                SL issue

                I think I know what the issue is, it's my own code:

                SetStopLoss("Enter Long", CalculationMode.Percent, TrailingPercent, false); should be
                SetStopLoss("Enter Long", CalculationMode.Percent, StopPercent, false);

                will let you know shortly...

                Comment


                  #9
                  Hello Downton,
                  You have used the below code to enter the short position
                  Code:
                  EnterShort(DefaultQuantity, "Enter Long");
                  I suppose you should change the name to "Enter Short".

                  Also you have used both SetStopLoss and SettrialStop in your code. Please use any of the one.
                  The SetTrailStop() method can NOT be used concurrently with the SetStopLoss() method for the same position, if both methods are called for the same position (fromEntrySignal) the SetStopLoss() will always take precedence. You can however, use both methods in the same strategy if they reference different signal names.
                  http://www.ninjatrader.com/support/h...ttrailstop.htm

                  Please let me know if I can assist you any further.
                  JoydeepNinjaTrader Customer Service

                  Comment


                    #10
                    SL issue

                    Ok, Joydeep, thanks v much.

                    Comment


                      #11
                      MAsCrossOver with profit target

                      Hi,

                      I added a profit target to my wizard generated MAsCrossOver strategy. When the profit target is reached the trade is closed as expected but it is then re-entered automaticaly on the next bar (presumably because the MAs are still in the same relationship to each other), is there a way I could stop the trade from being re-entered?

                      Thanks

                      Comment


                        #12
                        Hello Downton,
                        You can further filter your entry code with BarSinceExit parameter. Please refer here to know more about it.
                        http://www.ninjatrader.com/support/h...ssinceexit.htm

                        Please let me know if I can assist you any further.
                        JoydeepNinjaTrader Customer Service

                        Comment


                          #13
                          thanks Joydeep

                          Comment


                            #14
                            command button

                            Hi,

                            I have developed an automated strategy but I want to add a command button to allow a user to interact with it so that when the user clicks on the button I can detect this event to do some processing within the strategy. Can you advise on how to add such a command button to the screen and how to intercept the button click event.

                            Thanks

                            Comment


                              #15
                              Hello Downton,
                              Unfortunately this is beyond what we could support however you can have a look at this member submitted code to get some idea regarding this.

                              http://ninjatrader.com/support/forum...atid=5&lpage=1
                              JoydeepNinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by jrobin747, Today, 02:37 PM
                              2 responses
                              11 views
                              0 likes
                              Last Post bltdavid  
                              Started by Gav_G, Today, 01:23 PM
                              1 response
                              9 views
                              0 likes
                              Last Post NinjaTrader_ChrisL  
                              Started by webus, Today, 01:05 PM
                              2 responses
                              22 views
                              0 likes
                              Last Post webus
                              by webus
                               
                              Started by Wick Wrangler, Today, 12:42 AM
                              1 response
                              22 views
                              0 likes
                              Last Post NinjaTrader_Kate  
                              Started by pman777, 04-15-2020, 05:09 AM
                              5 responses
                              49 views
                              0 likes
                              Last Post NinjaTrader_Kate  
                              Working...
                              X