Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Simple SL, TP, Breakeven don't work

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

    Simple SL, TP, Breakeven don't work

    Hi,

    I have prepared simple code based on below sample:

    http://www.ninjatrader.com/support/f...ead.php?t=3222

    The code is as follows:

    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>
        /// 
        /// </summary>
        [Description("Stop Loss, Breakeven, Take Profit")]
        public class SLBETP : Strategy
        {
            #region Variables
            // Wizard generated variables
            // User defined variables (add any user defined variables below)
    		
    		private int		ryzyko = 100;
    					
    		private double 	Psize;
    		
            #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()
            {
                // !!! SetStopLoss(CalculationMode.Ticks, stoplossticks);
    			SetStopLoss(100);
    			SetProfitTarget(300);
    			
    			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)
    			{
    				SetStopLoss(100);
    			}
    			
    			// If a long position is open, allow for stop loss modification to breakeven
    			else if (Position.MarketPosition == MarketPosition.Short)
    			{
    				// Once the price is greater than entry price+50 ticks, set stop loss to breakeven
    				if (Close[0] > Position.AvgPrice + 100)
    				{
    					SetStopLoss(100);
    				}
    			}
    			
    			// Entry Condition
    			if (DonchianChannel(2).Upper[0] == DonchianChannel(2).Upper[1]
                    && Close[0] < Close[1]
                    && High[1] == DonchianChannel(2).Upper[1])
    			{
                    Pozycja();
    				EnterShort((int)Psize, "PKU-Open");
    			}	
    			
    			// Exit Condition
                if (DonchianChannel(2).Upper[0] > DonchianChannel(2).Upper[1])
                {
                    ExitShort("PKU-Close", "PKU-Open");
                }
    			
    		}
    
    		#region PozycjaFunction	
    		void Pozycja()
    		{
    
    			Psize = (ryzyko/((DonchianChannel(2).Upper[0]-Close[0])*10000))/10*100000;
    		
    		}
    	#endregion
    				
            #region Properties
    		
    		/// <summary>
    		/// </summary>
    		[Description("Ryzyko w USD - 1R")]
    		[Category("Parameters")]
    		public int Ryzyko
    		{
    			get { return ryzyko; }
    			set { ryzyko = Math.Max(0, value); }
    		}
    		#endregion
        }
    }
    The strategy open and close every position in the same moment. Please find attached image as an example.



    Please support.

    #2
    kucharek,

    I am happy to assist you.

    I believe your SetStopLoss() is to blame for this issue. You are using it as follows :

    SetStopLoss(double currency)

    Please use the following format :

    SetStopLoss(CalculationMode mode, double value)

    For more information, please see the following link : http://www.ninjatrader.com/support/h...etstoploss.htm

    Please let me know if I may assist further.
    Adam P.NinjaTrader Customer Service

    Comment


      #3
      Ok, thanks for that. I will work on that.

      I am wondering why I can not use SetStopLoss(double currency) syntax here.

      Comment


        #4
        kucharek,

        You can use this, but depending on your order size 100$ may be small swings. I also noticed your lot size is quite large when I tested on forex. If you use ticks you can guarantee you give it enough room to move as expected on your chart.

        Please let me know if I may assist further.
        Adam P.NinjaTrader Customer Service

        Comment


          #5
          Hi,

          Ticks mode is giving me what I wanted. Thanks a lot.

          Stop Loss and Take Profit are working fine. One thing that doesn't work and I do not know how to resolve is "breakeven". Please support what is wrong with my coding regarding breakeven.

          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>
              /// 
              /// </summary>
              [Description("Stop Loss, Breakeven, Take Profit")]
              public class SLBETP : Strategy
              {
                  #region Variables
                  // Wizard generated variables
                  // User defined variables (add any user defined variables below)
          		
          		private int		ryzyko = 100;
          					
          		private double 	Psize;
          		private double 	Stoploss;
          		
                  #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()
                  {	
          			CalculateOnBarClose = true;
                  }
          
                  /// <summary>
                  /// Called on each bar update event (incoming tick)
                  /// </summary>
                  protected override void OnBarUpdate()
                  {        			
          		// If a short position is open, allow for stop loss modification to breakeven
          		if (Position.MarketPosition == MarketPosition.Short)
          			{
          				// Once the price is greater than entry price - SLpips, set stop loss to breakeven
          				if (Close[0] < Position.AvgPrice - (double)Stoploss * TickSize)
          				{
          					SetStopLoss(CalculationMode.Price, Position.AvgPrice);
          				}
          			}
          			
          			// Entry Condition
          			if (DonchianChannel(2).Upper[0] == DonchianChannel(2).Upper[1]
                          && Close[0] < Close[1]
                          && High[1] == DonchianChannel(2).Upper[1])
          			{
                          Pozycja();
          				EnterShort((int)Psize, "PKU-Open");
          				
          				SLpips();
          				SetStopLoss("PKU-Open", CalculationMode.Ticks, (double)Stoploss, false);
          				SetProfitTarget("PKU-Open", CalculationMode.Ticks, (double)Stoploss * 3);
          			}	
          			
          			// Exit Condition
                      if (DonchianChannel(2).Upper[0] > DonchianChannel(2).Upper[1])
                      {
                         ExitShort("PKU-Close", "PKU-Open");
                      }
          			
          		}
          
          		#region PozycjaFunction	
          		void Pozycja()
          		{
          
          			Psize = (ryzyko/((DonchianChannel(2).Upper[0]-Close[0])*10000))/10*100000;
          					
          		}
          		
          		void SLpips()
          		{	
          			Stoploss = (DonchianChannel(2).Upper[0]-Close[0])*10000;
          		}
          	#endregion
          				
                  #region Properties
          		
          		/// <summary>
          		/// </summary>
          		[Description("Ryzyko w USD - 1R")]
          		[Category("Parameters")]
          		public int Ryzyko
          		{
          			get { return ryzyko; }
          			set { ryzyko = Math.Max(0, value); }
          		}
          		#endregion
              }
          }

          Comment


            #6
            kucharek,

            You are using a breakeven of Position.AvgPrice, which is the average price of your position, not the break even point. You will want to keep track of your entrance price and use this for breakeven.

            Please let me know if I may assist further.
            Adam P.NinjaTrader Customer Service

            Comment


              #7
              Hi,

              I don't understand. Shall I change Position.AvgPrice to other command?

              Please note that I did that way:

              SetStopLoss(CalculationMode.Price, Position.AvgPrice);

              according to Price Modification Sample.

              Please explain in more details what you mean by entrance price.

              Comment


                #8
                Hello,

                Can you please clarify what exactly about the break even is not working correctly. As you may or may not be having any issue with Position.AvgPrice.

                If it not activating or is it moving it to an incorrect price?

                What Print() and tracing have you done to narrow down whats going on?

                -Brett
                BrettNinjaTrader Product Management

                Comment


                  #9
                  Hi,

                  I have implemented print and tracing, tested it and found an issue. Thanks for your support.

                  Comment

                  Latest Posts

                  Collapse

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