Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Trailing Stop in strategy

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

    Trailing Stop in strategy

    Hi,

    Can someone help me implementing trailing stop in a strategy ?
    In strategy builder I could find trailing step !

    1. How can I implement a trailing stop with a defined trailing step in a strategy ?
    2. Is it possible to adjust the trailing step if my position reached a certain level of profit ?
    3. How can I optimize the stop loss and profit target ?
    I tried SetStopLoss variable at user defined inputs, but does not work...

    thanks

    #2
    josekapucinaro, welcome to the forums!

    You can take a look at the generic SetTrailStop - http://www.ninjatrader-support.com/H...TrailStop.html

    For a custom trailing stop, you want to work with SetStopLoss (can't combine this with the above SetTrailStop on the same position) and then change the StopLoss value upon your trail conditions - http://www.ninjatrader-support2.com/...ead.php?t=3222

    You can create used input parameters for the trailing stop and / or target, please check into this link for the general approach - http://www.ninjatrader-support2.com/...ead.php?t=5782
    BertrandNinjaTrader Customer Service

    Comment


      #3
      Bertrand,

      thanks for your help,
      I just went through those codes you referred to, and I still have some questions.

      1. I could only use Calculationmode.Ticks, not Percent nor Price does not work in my strategy. For percent I tried to use the 0.01 value for 1 %, at defining variables like this : "private int stoplosspercent = 0.01;" but cannot compile the code, the Error message is "Cannot implicitly convert type 'double' to 'int'. An explicit conversion exists. (are you missing a cast?)"
      What is the correct syntax for percent ?
      2. setstoploss to breakeven if a certain profit is reached generates strange results. At setting "+100 tick" it moves the stoploss to breakeven even if the price was only + 40 pip = + 80 tick....

      thanks
      Jose

      Comment


        #4
        Jose, please set the variable up as a double then it should compile.

        Which code did you use exactly to move the SetStopLoss to trail?

        BertrandNinjaTrader Customer Service

        Comment


          #5
          Bertrand,

          I used the code I found in the link you just sent me.
          Like this:
          // If a long position is open, allow for stop loss modification to breakeven
          else if (Position.MarketPosition == MarketPosition.Long)
          {
          // Once the price is greater than entry price+50 ticks, set stop loss to breakeven
          if (Close[0] > Position.AvgPrice + 100 * TickSize)
          {
          SetStopLoss(CalculationMode.Price, Position.AvgPrice);
          }
          }

          Is it correct ?

          Comment


            #6
            Looks good to me, to reset it also when flat like in the sample?
            BertrandNinjaTrader Customer Service

            Comment


              #7
              Bertrand,

              I am still suffering with the code containing percent values.
              I set the variables (profittarget, stoploss) to double, it compiles, but not at optimization nor backtest it does not take the profittarget and stoploss into account.
              All position is closed by "close position".

              any idea what is wrong ?

              thanks
              Jose

              Comment


                #8
                Please provide the snippet you are using.
                Josh P.NinjaTrader Customer Service

                Comment


                  #9
                  In the meantime I just found the solution,
                  I suppose it is because of forex strategy,
                  with the argument 1%=0,0001 it works fine.

                  But I faced another problem implementing the code to move the stop to BE.
                  I would like to use percent here as well, I modified the code accordingly:

                  // Resets the stop loss to the original value when all positions are closed
                  if (Position.MarketPosition == MarketPosition.Flat)
                  {
                  SetStopLoss(CalculationMode.Percent, stoplosspercent);
                  }

                  // If a long position is open, allow for stop loss modification to breakeven
                  else if (Position.MarketPosition == MarketPosition.Long)
                  {
                  // Once the price is greater than entry price+profittrail percent, set stop loss to breakeven
                  if (Close[0] > Position.AvgPrice + profittrail * Position.AvgPrice)
                  {
                  SetStopLoss(CalculationMode.Price, Position.AvgPrice);
                  }
                  }

                  is that correct ?

                  Comment


                    #10
                    0.01 = 1%
                    If you are using forex you should make sure you have the correct pip settings set up for your connection in Tools -> Options -> Misc.
                    Josh P.NinjaTrader Customer Service

                    Comment


                      #11
                      I cannot understand this then.
                      I adjusted the pip settings to half pip as described in IB connection settings.
                      But still I get normal percent levels at 1%=0,0001.
                      It is a crucial issue since I would like to test it in real conditions.
                      any idea ?

                      The other thing is the email alert worked once, but not anymore.

                      thanks,
                      Jose

                      Comment


                        #12
                        Jose, is this the same for all currencies you tested? If you don't mind please post the code you use now so we can review - thanks
                        BertrandNinjaTrader Customer Service

                        Comment


                          #13
                          yes, the problem is the same for each currencies.
                          here is the code.
                          please review the email issue as well.
                          thanks,
                          jose

                          #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>
                          /// EMA cross
                          /// </summary>
                          [Description("EMA cross")]
                          public class strat_percent : Strategy
                          {
                          #region Variables
                          private int period = 1; // Default setting for Period
                          private double stoplosspercent = 0.1; // Default setting for stoplosspercent
                          private double profittargetpercent = 0.1; // Default setting for profittargetpercent
                          #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(EMA(Period));
                          SetProfitTarget(CalculationMode.Percent, profittargetpercent);
                          SetStopLoss(CalculationMode.Percent, stoplosspercent);

                          CalculateOnBarClose = true;
                          }

                          /// <summary>
                          /// Called on each bar update event (incoming tick)
                          /// </summary>
                          protected override void OnBarUpdate()
                          {

                          // Condition set 1
                          if (CrossAbove(Close, EMA(Period), 10)
                          && Close[1] > Open[1]
                          && Close[0] > Open[0])
                          {
                          EnterLong(100000, "long");
                          SendMail("[email protected]", "[email protected]", "enter long", "");
                          }

                          // Condition set 2
                          if (CrossBelow(Close, EMA(Period), 10)
                          && Close[1] < Open[1]
                          && Close[0] < Open[0])
                          {
                          EnterShort(100000, "short");
                          SendMail("[email protected]", "[email protected]", "enter short", "");
                          }

                          }

                          #region Properties
                          [Description("")]
                          [Category("Parameters")]
                          public int Period
                          {
                          get { return period; }
                          set { period = Math.Max(1, value); }
                          }

                          [Description("")]
                          [Category("Parameters")]
                          public double StopLossPercent
                          {
                          get { return stoplosspercent; }
                          set { stoplosspercent = value; }
                          }
                          [Description("Number of ticks away from entry price for the Profit Target order")]
                          [Category("Parameters")]
                          public double ProfitTargetPercent
                          {
                          get { return profittargetpercent; }
                          set { profittargetpercent = value; }
                          }
                          #endregion
                          }
                          }

                          Comment


                            #14
                            Jose, you still set those value to a default of 10 % -

                            private double stoplosspercent = 0.1; // Default setting for stoplosspercent
                            private double profittargetpercent = 0.1; // Default setting for profittargetpercent

                            Are you changing those to 0.01 on runtime?

                            The email alert looks good, however it would work generally only in realtime.
                            BertrandNinjaTrader Customer Service

                            Comment


                              #15
                              default settings does not matter, does it ?
                              when backtesting I have to set it 0.001 for getting 10 % .

                              could you test it with an IB account ?

                              thanks,
                              jose

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by f.saeidi, Today, 12:14 PM
                              0 responses
                              1 view
                              0 likes
                              Last Post f.saeidi  
                              Started by giulyko00, Today, 12:03 PM
                              0 responses
                              4 views
                              0 likes
                              Last Post giulyko00  
                              Started by AttiM, 02-14-2024, 05:20 PM
                              12 responses
                              213 views
                              0 likes
                              Last Post DrakeiJosh  
                              Started by cre8able, 02-11-2023, 05:43 PM
                              3 responses
                              238 views
                              0 likes
                              Last Post rhubear
                              by rhubear
                               
                              Started by frslvr, 04-11-2024, 07:26 AM
                              8 responses
                              117 views
                              1 like
                              Last Post NinjaTrader_BrandonH  
                              Working...
                              X