Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Custom Tick Sizes for TrailStop

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

    Custom Tick Sizes for TrailStop

    I'm pretty new to coding, but I'm trying to teach myself by reading topics here and watching educational videos. I tried searching for my problem but I wasn't successful.

    I want to create the same trailing stop for both of my entries from my scaling out long positions "Long1" and "Long2". Instead of specifying a tick size, I want the value of my trailing stop to be something like (PriorDayOHLC().PriorHigh[0] - PriorDayOHLC().PriorLow[0]). How can I create a trailing stop that calls upon a value from another indicator?

    #2
    Hi IFT20,

    Thank you for posting and welcome to the Forum!

    Unfortunately, you can only use Tick or Percentage for the trailing stop.

    You can create a StopLoss and dynamically change the value of the StopLoss via the code.
    Here is a reference sample that has sample code of how to achieve this.
    http://www.ninjatrader.com/support/f...ead.php?t=3222

    Let me know if I can be of further assistance.
    Cal H.NinjaTrader Customer Service

    Comment


      #3
      Thanks for the quick reply Cal!

      Just to clarify, it would not be possible to assign something for the value of (PriorDayOHLC().PriorHigh[0] - PriorDayOHLC().PriorLow[0]) and use it as a tick size for the trailing stop?

      In the meantime, I'll try to work with the SetStopLoss and see if I can achieve the desired results. Thanks for the help!

      Comment


        #4
        IFT20,

        You can try using that input as the value of the trailing stop. It should work since they are both a double value.
        I would use this overload method however...
        Code:
        SetTrailStop(CalculationMode.Ticks, PriorDayOHLC().PriorHigh[0] - PriorDayOHLC().PriorLow[0]);
        Let me know if that works for you.
        Cal H.NinjaTrader Customer Service

        Comment


          #5
          It worked- Thanks Cal!

          Comment


            #6
            I'm trying to implement this trailing stop to my strategy, but I'm running into a problem. The code compiles fine, but it won't load in the strategy analyzer. The output window tells me it has "Failed to call method 'Initialize' for strategy "Strategy1"... Object reference not set to an instance of an object."

            The code I put under Initialize() is
            Code:
            SetTrailStop("Long1a", CalculationMode.Ticks, (PriorDayOHLC().PriorHigh[0] - PriorDayOHLC().PriorLow[0]), false);
            I've isolated this as the problem- my strategy will run if I remove this. I can't seem to find the problem in my code, any ideas what I could be doing wrong here?

            Comment


              #7
              IFT20,

              You will need to use this code in the OnBarUpdate method since there is no Bar data being loaded in the initialize and therefore causes the error.

              You can dynamically have it being placed in the OnBarUpdate, just ensure that you reset the values when you are back to a Flat position.
              Cal H.NinjaTrader Customer Service

              Comment


                #8
                I've moved it under the OnBarUpdate, and I'm trying to reset the values when I am flat, but I'm messing something up and the trailing stops are not working.

                Code:
                        protected override void OnBarUpdate()
                        {
                        	if (Position.MarketPosition == MarketPosition.Flat)
                				{
                					SetTrailStop("Long1b", CalculationMode.Ticks, 5, false);
                					SetTrailStop("Short1b", CalculationMode.Ticks, 5, false);
                				}
                			
                			else if (Position.MarketPosition == MarketPosition.Long)
                				{
                					if (Close[0] >= 60)
                						{
                							SetTrailStop("Long1b", CalculationMode.Ticks, (PriorDayOHLC().PriorHigh[0] - PriorDayOHLC().PriorLow[0]), false);
                						}
                				}
                			
                			else if (Position.MarketPosition == MarketPosition.Short)
                				{
                					if (Close[0] <= 30)
                						{
                							SetTrailStop("Short1b", CalculationMode.Ticks, (PriorDayOHLC().PriorHigh[0] - PriorDayOHLC().PriorLow[0]), false);
                						}
                				}
                I really just want to create trail stops for "Long1b" and "Short1b" to always be equal to (PriorDayOHLC().PriorHigh[0] - PriorDayOHLC().PriorLow[0]). I'm never trying to have a trailing stop at 5 ticks (like the first condition establishes). I tried to emulate the strategy found in the PriceModification sample strategy. I feel like I could be approaching this the wrong way, but everything I try fails to reach the intended result.

                Comment


                  #9
                  IFT20,

                  In your code, while that would work, however you the trail stop is being reset on each bar update so long as those conditions are true.

                  You can use the a switch variable that when the SetTrailStop his set it switches the value so you don't have the constant reset

                  E.G.
                  Code:
                   protected override void OnBarUpdate()
                   {
                  
                  double switchset = 0;
                  
                  if (Position.MarketPosition == MarketPosition.Flat && switchset == 0)
                  				{
                  					SetTrailStop("Long1b", CalculationMode.Ticks, 5, false);
                  					SetTrailStop("Short1b", CalculationMode.Ticks, 5, false);
                                                          switchset = 1;
                  				}
                  			
                  			else if (Position.MarketPosition == MarketPosition.Long && switchset  == 1)
                  				{
                  					if (Close[0] >= 60)
                  						{
                  							SetTrailStop("Long1b", CalculationMode.Ticks, (PriorDayOHLC().PriorHigh[0] - PriorDayOHLC().PriorLow[0]), false);
                                                                          switchset = 0;
                  						}
                  				}
                  			
                  			else if (Position.MarketPosition == MarketPosition.Short && switchset  == 1)
                  				{
                  					if (Close[0] <= 30)
                  						{
                  							SetTrailStop("Short1b", CalculationMode.Ticks, (PriorDayOHLC().PriorHigh[0] - PriorDayOHLC().PriorLow[0]), false);
                                                                          switchset = 0;
                  						}
                  				}
                  Cal H.NinjaTrader Customer Service

                  Comment


                    #10
                    Thank you so much Cal. Like I said, I am new to coding and you've been really helpful. I understand how you used the setswitch and it is a useful tool, but I want to go back to my original inquiry.

                    I oversimplified my code in my previous post to state that the original trailstop would be 5 ticks, but in actuality I want it to be (PriorDayOHLC().PriorHigh[0] - PriorDayOHLC().PriorLow[0]) ticks.

                    Code:
                    SetTrailStop("Long1b", CalculationMode.Ticks, (PriorDayOHLC().PriorHigh[0] - PriorDayOHLC().PriorLow[0]), false);
                    I'm not getting why I cannot have this done. I know I am interpreting something wrong but I don't know what it is. The code will compile fine, but backtesting the strategy will create stops right at the entry price instead of (PriorDayOHLC().PriorHigh[0] - PriorDayOHLC().PriorLow[0]) ticks away.
                    Last edited by IFT20; 08-22-2013, 05:00 PM.

                    Comment


                      #11
                      IFT20,

                      The trail stop for NinjaScript will work the second you have favor in your market.

                      While this is similar to the ATM trail stop, there are some differences such as; there is no profit trigger, meaning the trail stop will start trailing your market the second there is a tick in your favor and will continue to trail each tick in your favor.

                      When the market goes against then the trailing stops.

                      You may see this in backtest results where the stop loss is the same as your entry price cause the market went in favor and then got stopped out later on.

                      If these are the not the results you are seeing, would you be willing to share your strategy and any screen shots?
                      Cal H.NinjaTrader Customer Service

                      Comment


                        #12
                        help

                        can't find the way to start a post

                        Comment


                          #13
                          Hi Ilgrigg,

                          You can start a new thread by going to the support page and selecting which area you want to start a thread.

                          Once there, you should see a button on the top label NEW THREAD

                          See the picture attached.

                          Let me know if I can be of further assistance.
                          Attached Files
                          Cal H.NinjaTrader Customer Service

                          Comment


                            #14
                            My positions are stopping out immediately at the entry point, regardless of favor in the market (or any price movement at all).

                            Code:
                                    protected override void Initialize()
                                    {
                            			EntriesPerDirection = 1;
                            			EntryHandling 		= EntryHandling.UniqueEntries;
                                        CalculateOnBarClose = false;
                                    }
                            
                                    protected override void OnBarUpdate()
                                    {
                                         SetTrailStop("Long1b", CalculationMode.Ticks, (PriorDayOHLC().PriorHigh[0] - PriorDayOHLC().PriorLow[0]), false);
                            	     SetProfitTarget("Long1a", CalculationMode.Ticks, 20);
                                         SetStopLoss("Long1a", CalculationMode.Ticks, 20, false);
                            
                            			
                                        if (CrossAbove(EMA(7), EMA(15), 1))
                                        {
                                            EnterLong(60, "Long1a");
                                            EnterLong(40, "Long1b");
                                        }
                                    }
                            
                                    #region Properties
                                    [Description("")]
                                    [GridCategory("Parameters")]
                                    public int MyInput0
                                    {
                                        get { return myInput0; }
                                        set { myInput0 = Math.Max(1, value); }
                                    }
                                    #endregion
                                }
                            }
                            Attached Files
                            Last edited by IFT20; 08-23-2013, 08:11 AM.

                            Comment


                              #15
                              IFT20,

                              Can you print out the values of the trail stop that you are trying to place?
                              Insert this code line after the setprofit target -
                              Code:
                              Print(PriorDayOHLC().PriorHigh[0] - PriorDayOHLC().PriorLow[0]);
                              This will display a value in the output window which can be accessed by Tools > Output window.

                              When the values print, are they the values you want?
                              Cal H.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Davidtowleii, Today, 12:15 AM
                              0 responses
                              3 views
                              0 likes
                              Last Post Davidtowleii  
                              Started by guillembm, Yesterday, 11:25 AM
                              2 responses
                              9 views
                              0 likes
                              Last Post guillembm  
                              Started by junkone, 04-21-2024, 07:17 AM
                              9 responses
                              68 views
                              0 likes
                              Last Post jeronymite  
                              Started by trilliantrader, 04-18-2024, 08:16 AM
                              4 responses
                              20 views
                              0 likes
                              Last Post trilliantrader  
                              Started by mgco4you, Yesterday, 09:46 PM
                              1 response
                              12 views
                              0 likes
                              Last Post NinjaTrader_Manfred  
                              Working...
                              X