Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Move stop when certain prices levels are hit

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

    Move stop when certain prices levels are hit

    Hello--

    I have a stop strategy that I would like to model in my strategy whereby a stop is moved X points when a Y price level has been hit (i.e., exactly like the ATM lets you do in via the dialog box --> ATM Strategy dialog box > Stop Strategy > "<Custom>" > Stop Strategy > Auto Trail (ticks) > Step 1: Stop loss, Frequency, and Profit Trigger (Here is the Support Documentation on Auto Trail).

    So basically I would like to programmatically set the "Stop loss", "Frequency" and "Profit trigger". The NT Documentation has an example that says "Once our trade has 4 ticks profit, move our Stop Loss back 6 ticks and move it up for every additional 2 ticks in profit." So in this case the "Stop loss" would be equal to 6, the "Frequency" would be equal to 2 and the "Profit trigger" would be equal to 4.

    For instance, let's say I enter long at 1750 and I set a profit target at 1780 and a stop at 1740. I then want to immediately move my stop to 1750 when price hits 1760, and then I want the stop to immediately move to 1760 when price hits 1770, etc. I do not want to wait for the bar to close for the stop move to occur.

    Can I do the same thing programmatically in my strategy? In particular, I would really prefer to stick with the Managed Approach, but if I have to move into the more complex Unmanaged Approach so be it.

    Thanks in advance for your help. I really appreciate it.

    All best,

    Aventeren
    Last edited by aventeren; 05-25-2014, 05:41 PM.

    #2
    Originally posted by aventeren View Post
    Hello--

    I have a stop strategy that I would like to model in my strategy whereby a stop is moved X points when a Y price level has been hit (i.e., exactly like the ATM lets you do in via the dialog box --> ATM Strategy dialog box > Stop Strategy > "<Custom>" > Stop Strategy > Auto Trail (ticks) > Step 1: Stop loss, Frequency, and Profit Trigger (Here is the Support Documentation on Auto Trail).

    So basically I would like to programmatically set the "Stop loss", "Frequency" and "Profit trigger". The NT Documentation has an example that says "Once our trade has 4 ticks profit, move our Stop Loss back 6 ticks and move it up for every additional 2 ticks in profit." So in this case the "Stop loss" would be equal to 6, the "Frequency" would be equal to 2 and the "Profit trigger" would be equal to 4.

    For instance, let's say I enter long at 1750 and I set a profit target at 1780 and a stop at 1740. I then want to immediately move my stop to 1750 when price hits 1760, and then I want the stop to immediately move to 1760 when price hits 1770, etc. I do not want to wait for the bar to close for the stop move to occur.

    Can I do the same thing programmatically in my strategy? In particular, I would really prefer to stick with the Managed Approach, but if I have to move into the more complex Unmanaged Approach so be it.

    Thanks in advance for your help. I really appreciate it.

    All best,

    Aventeren
    Yes. Either use COBC = false, or use a different event handler that is not tied to the bar closing event. OnMarketData() comes to mind.

    Comment


      #3
      Thanks for the ideas; would I just use SetStop again or something else? Would the OCO still work, and how would I make sure that the stop I am moving is the correct stop (ie, order tag)?

      Also, does OnMarketUpdate() allow me to still use the Managed Approach?

      Thanks!

      Comment


        #4
        Originally posted by aventeren View Post
        Thanks for the ideas; would I just use SetStop again or something else? Would the OCO still work, and how would I make sure that the stop I am moving is the correct stop (ie, order tag)?
        Yes, OCO remains. You make sure that you are moving is the correct one the same way that you do so now: use the fromEntrySignal parameter.
        Also, does OnMarketUpdate() allow me to still use the Managed Approach?

        Thanks!
        Yes. Orders are orders, and are executed when they are programmed to execute: it does not matter from which event handler.

        Remember, however that you cannot directly backtest anything that handles realtime only events, whether it is COBC = false, or other events such as OnMarketData().

        Comment


          #5
          Hello Aventeren,

          Thank you for your post.

          You can find an example of modifying the Stop Loss when a certain level in profit is reached at the following link: http://www.ninjatrader.com/support/f...ead.php?t=3222

          Comment


            #6
            Originally posted by NinjaTrader_PatrickH View Post
            Hello Aventeren,

            Thank you for your post.

            You can find an example of modifying the Stop Loss when a certain level in profit is reached at the following link: http://www.ninjatrader.com/support/f...ead.php?t=3222
            Here we sit at 10p PT on a Sunday during Memorial Weekend, and you just helped me out. I've just downloaded this SamplePriceModification strategy, and I'm in the process of coding this up. I'll ping back with any further questions.

            Thanks to both you and koganam for getting back to me today. I really appreciate this forum.

            All best,

            Aventeren
            Last edited by aventeren; 05-25-2014, 11:10 PM.

            Comment


              #7
              I am using this sample for trailing stops. The long side works fine but the short side does not function and I can't figure out why. Everything looks about right.

              Thanks.



              //
              // Copyright (C) 2007, NinjaTrader LLC <www.ninjatrader.com>.
              // NinjaTrader reserves the right to modify or overwrite this NinjaScript component with each release.
              //
              #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.Strategy;
              #endregion

              // This namespace holds all strategies and is required. Do not change it.
              namespace NinjaTrader.Strategy
              {
              /// <summary>
              /// Sample strategy using StopLoss and ProfitTarget orders.
              /// </summary>
              [Description("Sample strategy using StopLoss and ProfitTarget orders. The final trailing action will not work in backtest, however, the breakeven movements will.")]
              public class trailingtest : Strategy
              {
              #region Variables
              private int stoplossticks = 20;
              private int profittargetticks = 50;
              private double currentStop = 0;
              private bool trailL = false;
              private bool trailS = false;
              #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()
              {
              // set the initial stop loss and profit target
              SetStopLoss(CalculationMode.Ticks, stoplossticks);
              SetProfitTarget(CalculationMode.Ticks, profittargetticks);

              CalculateOnBarClose = true;
              Enabled = true;
              }

              protected override void OnBarUpdate()
              {
              // Resets the stop loss to the original value when all positions are closed
              if (Position.MarketPosition == MarketPosition.Flat)
              {
              currentStop = 0;
              trailL = false;
              SetStopLoss(CalculationMode.Ticks, stoplossticks);
              }

              // If a long position is open, allow for stop loss modification to breakeven
              else if (Position.MarketPosition == MarketPosition.Long && trailL == false)
              {
              // After 10 ticks set stop loss to BE-10
              if (Close[0] >= Position.AvgPrice + 10 * TickSize && Close[0] < Position.AvgPrice + 15 * TickSize && currentStop < Position.AvgPrice - 10 * TickSize)
              {
              currentStop = Position.AvgPrice - 10 * TickSize;
              SetStopLoss(CalculationMode.Price, currentStop);
              }

              // After 15 ticks set stop loss to BE-5
              else if (Close[0] >= Position.AvgPrice + 15 * TickSize && Close[0] < Position.AvgPrice + 20 * TickSize && currentStop < Position.AvgPrice - 5 * TickSize)
              {
              currentStop = Position.AvgPrice - 5 * TickSize;
              SetStopLoss(CalculationMode.Price, currentStop);
              }

              // After 20 ticks set stop loss to BE
              else if (Close[0] > Position.AvgPrice + 20 * TickSize && currentStop < Position.AvgPrice)
              {
              currentStop = Position.AvgPrice;
              SetStopLoss(CalculationMode.Price, currentStop);

              // after the trail is set to true, the stop loss trail in OnMarketData will take over
              trailL = true;
              }
              }

              // Enter market conditions go here
              if (Position.MarketPosition == MarketPosition.Flat && RSI(14, 3)[0] <= 30)
              {
              EnterLong();
              }




              // Resets the stop loss to the original value when all positions are closed
              else if (Position.MarketPosition == MarketPosition.Flat)
              {
              currentStop = 0;
              trailS = false;
              SetStopLoss(CalculationMode.Ticks, stoplossticks);
              }

              // If a long position is open, allow for stop loss modification to breakeven
              else if (Position.MarketPosition == MarketPosition.Short && trailS == false)
              {
              // After 10 ticks set stop loss to BE-10
              if (Close[0] <= Position.AvgPrice - 10 * TickSize && Close[0] > Position.AvgPrice - 15 * TickSize && currentStop > Position.AvgPrice + 10 * TickSize)
              {
              currentStop = Position.AvgPrice + 10 * TickSize;
              SetStopLoss(CalculationMode.Price, currentStop);
              }

              // After 15 ticks set stop loss to BE-5
              else if (Close[0] <= Position.AvgPrice - 15 * TickSize && Close[0] > Position.AvgPrice - 20 * TickSize && currentStop > Position.AvgPrice + 5 * TickSize)
              {
              currentStop = Position.AvgPrice + 5 * TickSize;
              SetStopLoss(CalculationMode.Price, currentStop);
              }

              // After 20 ticks set stop loss to BE
              else if (Close[0] > Position.AvgPrice - 20 * TickSize && currentStop > Position.AvgPrice)
              {
              currentStop = Position.AvgPrice;
              SetStopLoss(CalculationMode.Price, currentStop);

              // after the trail is set to true, the stop loss trail in OnMarketData will take over
              trailS = true;
              }
              }

              // Enter market conditions go here
              if (Position.MarketPosition == MarketPosition.Flat && RSI(14, 3)[0] >= 70)
              {
              EnterShort();
              }
              }

              protected override void OnMarketData(MarketDataEventArgs e)
              {
              // After 30 ticks set stop loss to CurrentPrice - 10 Ticks. Repeat every 2 ticks.
              // This is done in OnMarketUpdate so that it happens intra-bar
              if (e.MarketDataType == MarketDataType.Last && trailL == true && e.Price > currentStop + 12 * TickSize)
              {
              currentStop = e.Price - 10 * TickSize;
              SetStopLoss(CalculationMode.Price, currentStop);
              }

              if (e.MarketDataType == MarketDataType.Last && trailS == true && e.Price < currentStop - 12 * TickSize)
              {
              currentStop = e.Price + 10 * TickSize;
              SetStopLoss(CalculationMode.Price, currentStop);
              }
              }

              #region Properties
              /// <summary>
              /// </summary>
              [Description("Numbers of ticks away from entry price for the Stop Loss order")]
              [Category("Parameters")]
              public int StopLossTicks
              {
              get { return stoplossticks; }
              set { stoplossticks = Math.Max(0, value); }
              }

              /// <summary>
              /// </summary>
              [Description("Number of ticks away from entry price for the Profit Target order")]
              [Category("Parameters")]
              public int ProfitTargetTicks
              {
              get { return profittargetticks; }
              set { profittargetticks = Math.Max(0, value); }
              }
              #endregion
              }
              }

              Comment


                #8
                Hi brucelevy,

                What is the behavior that is not correct?

                Have you added any prints to debug the behavior?
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  The short side stop loss does not move to break even

                  Comment


                    #10
                    Hi brucelevy,

                    Which condition triggers the breakeven movement?

                    Is this condition being triggered at the correct time?

                    (When the position is short, are the values being checked in the condition evaluating as true?)

                    If you set TraceOrders = true; in Initialize() this will print to the output window when an order is submitted or modified.

                    Are you seeing any messages that the order is being ignored when TraceOrders is enabled?
                    Chelsea B.NinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                    0 responses
                    569 views
                    0 likes
                    Last Post Geovanny Suaza  
                    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                    0 responses
                    330 views
                    1 like
                    Last Post Geovanny Suaza  
                    Started by Mindset, 02-09-2026, 11:44 AM
                    0 responses
                    101 views
                    0 likes
                    Last Post Mindset
                    by Mindset
                     
                    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                    0 responses
                    548 views
                    1 like
                    Last Post Geovanny Suaza  
                    Started by RFrosty, 01-28-2026, 06:49 PM
                    0 responses
                    548 views
                    1 like
                    Last Post RFrosty
                    by RFrosty
                     
                    Working...
                    X