Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Problem with Stop Orders

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

    Problem with Stop Orders

    Hi,

    I've got a strategy which uses MAs I've created. Trades are triggered based upon, as per usual, cross above and below. It works well as a simple Buy and Sell so you're always in the market however I'm trying to incorporate Stop orders so that it only goes long at 1 pip above the high of the signal candle and vice-versa for short orders. I've got it to work to the extent that if the proceeding candle goes 1 pip higher/lower it will enter the order however the Stop order appears to be eliminated after this, so if the next candle goes higher/lower than the signal candle no order is entered. I'd like the Stop order to stay active until the Fast and Slow MA's cross each other again at which point a new Stop order is created. At the moment my code looks like the following:

    Code:
    				if (CrossAbove(MAseries1, MAseries2, 1))
    				{EnterLongStop(1, High[1]+0.0001, "L");
    					ExitShort();}
    				else if (CrossBelow(MAseries1, MAseries2, 1))
    				{EnterShortStop(1, Low[1]-0.0001, "S");
    					ExitLong();}
    If anyone could suggest how this could be altered to allow for what I have described above that would be brilliant.

    Kind Regards,
    Harry Seager
    Last edited by hazza147; 09-12-2012, 10:15 AM.

    #2
    Hello,

    You may want to use something like this :

    TimeInForce = Cbi.TimeInForce.Gtc; //orders are good till cancelled.

    EnterLongStop(int barsInProgressIndex, bool liveUntilCancelled, int quantity, double stopPrice, string signalName)

    ^ Use liveUntilCancelled = true

    Then you would need to be sure to cancel your Stop orders when you deem it necessary.

    Adam P.NinjaTrader Customer Service

    Comment


      #3
      Hi Adam,

      Thanks for your quick reply. I have come up with the following code:

      Code:
      private IOrder myEntryOrder = null;
              protected override void OnBarUpdate()
              {
      				
      				if (CrossAbove(MAseries1, MAseries2, 1))
      				{	CancelOrder(myEntryOrder);
      					myEntryOrder = EnterLongStop(1, true, 1, High[0]+0.0001, "L");
      					ExitShort();}
      				else if (CrossBelow(MAseries1, MAseries2, 1))
      				{	CancelOrder(myEntryOrder);
      					myEntryOrder = EnterShortStop(1, true, 1, Low[0]-0.0001, "S");
      					ExitLong();}
      However now whenever I go to open the strategy on a chart my MA's are now permanently set to 0.0000. Do you have any idea what could be causing this?

      Comment


        #4
        hazza,

        I am not sure here. Where are you assigning the MAseries?

        Also, one thing I saw is you are using 0.0001 presumable for 1 tick (or pip). You can use "TickSize" here to get that value automatically for the instrument you are using.

        Adam P.NinjaTrader Customer Service

        Comment


          #5
          Hi Adam,

          Thanks for the tip on TickSize I've edited this in. What do you mean by where am I assigning the MAseries?

          A chart with the standard Buy and Sell upon MA crosses works fine successfully calculating the MAs however this new code is causing a problem and I have no logical reason for why!

          Cheers,
          Harry

          Comment


            #6
            Hello,

            This is what I am referring to :

            CrossAbove(MAseries1, MAseries2, 1)

            The MASeries1 and MASeries2 are presumably DataSeries. They don't have anything in them unless you stick something in there.

            MAseries1.Set(EMA(15)[0]); for example puts the current value of the EMA of period 15 in this series.

            I was just wondering if you had more code to post so I could look at where the series are being assigned a value.
            Adam P.NinjaTrader Customer Service

            Comment


              #7
              Hi Adam,

              Please see code below:

              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.Strategy;
              #endregion
              
              namespace NinjaTrader.Strategy
              {
              	#region Header
              		[Description("simple MA/MA crossover")]
              		public class H_MA_MA_xover_stop : Strategy
              		#endregion
                  {
              		#region Variables	// default values
              			private double fast_MA_len = 26; 	
              			private double fast_MA_phase = -50; 
              			private int    slow_MA_lag = 6;
              			private double slow_MA_len = 10; 
              			private double slow_MA_phase = 75;
              			// ---------------------------------
              			private DataSeries MAseries1;
              			private DataSeries MAseries2;
              			private IOrder myEntryOrder = null;
              			#endregion
              		
              		#region Input Parameters
              			[Description("fast MA length, any value >= 1")]
              			[GridCategory("Parameters")]
              			public double fast_MA_len
              			{
              				get { return fast_MA_len; }
              				set { fast_MA_len = Math.Max(1, value); }
              			}
              			
              			[Description("fast MA phase, any value between -100 and +100")]
              			[GridCategory("Parameters")]
              			public double fast_MA_phase
              			{
              				get { return fast_MA_phase; }
              				set { fast_MA_phase = Math.Max(-100, Math.Min(100,value)); }
              			}	
              	
              			[Description("slow MA lag, any integer >= 0")]
              			[GridCategory("Parameters")]
              			public int slow_MA_lag
              			{
              				get { return slow_MA_lag; }
              				set { slow_MA_lag = Math.Max(-100, Math.Min(100,value)); }
              			}	
              			
              			[Description("slow MA length, any value >= 1")]
              			[GridCategory("Parameters")]
              			public double slow_MA_len
              			{
              				get { return slow_MA_len; }
              				set { slow_MA_len = Math.Max(1, value); }
              			}
              			
              			[Description("slow MA phase, any value between -100 and +100")]
              			[GridCategory("Parameters")]
              			public double slow_MA_phase
              			{
              				get { return slow_MA_phase; }
              				set { slow_MA_phase = Math.Max(-100, Math.Min(100,value)); }
              			}	
              			#endregion
              
              		protected override void Initialize()
                      {
              			#region Chart Features
              				Add(H_MA_custom( 0, fast_MA_len , fast_MA_phase));
              				Add(H_MA_custom( slow_MA_lag, slow_MA_len , slow_MA_phase ));
              				H_MA_custom( 0, fast_MA_len , fast_MA_phase).Plots[0].Pen.Color = Color.Blue;
              				H_MA_custom( slow_MA_lag, slow_MA_len , slow_MA_phase).Plots[0].Pen.Color = Color.Black;
              				CalculateOnBarClose = true;
              				#endregion
              			
              			#region Series Initialization
              				MAseries1 = new DataSeries(this);	// sync dataseries to historical data bars
              				MAseries2 = new DataSeries(this);	// sync dataseries to historical data bars
              				#endregion
              		}
                      protected override void OnBarUpdate()
                      {
              			#region Strategy Formula
              				MAseries1.Set( H_MA_custom( 0, fast_MA_len , fast_MA_phase ).MA_Series[0] );
              				MAseries2.Set( H_MA_custom( slow_MA_lag, slow_MA_len , slow_MA_phase ).MA_Series[0] );
              				
              				if (CrossAbove(MAseries1, MAseries2, 1))
              				{	CancelOrder(myEntryOrder);
              					myEntryOrder = EnterLongStop(1, true, 1, High[0]-TickSize, "L");
              					ExitShort();}
              				else if (CrossBelow(MAseries1, MAseries2, 1))
              				{	CancelOrder(myEntryOrder);
              					myEntryOrder = EnterShortStop(1, true, 1, Low[0]-TickSize, "S");
              					ExitLong();}
              				#endregion			
                      }
                  }
              }
              I have attached a picture of the chart which is being produced when you enable the strategy on it. You can see the line at the bottom at a constant 0.0000 this is the MAs which return 0.0000 for all data values. Let me know if you can see anything in the code that might be producing this.

              Many thanks,
              Harry
              Attached Files
              Last edited by hazza147; 09-12-2012, 12:34 PM.

              Comment


                #8
                Harry,

                This would probably be in the indicator code itself : H_MA_custom

                If you can post that it may be a good start to find out what's going on.
                Adam P.NinjaTrader Customer Service

                Comment


                  #9
                  Hi Adam,

                  It can't be because the strategy without the StopOrders just simply Buying and Selling on the MA crosses works absolutely fine. The thing that is causing problems here is the addition of StopOrders. Is there not a modification to the original piece of code I posted that can simply Keep the StopOrder active until it is either filled or we have an MA cross in the opposite direction?

                  Cheers,
                  Harry

                  Comment


                    #10
                    Harry,

                    Basically the strategy is trying to get values for these series here.

                    MAseries1.Set( H_MA_custom( 0, fast_MA_len , fast_MA_phase ).MA_Series[0] );

                    The addition of your order management / entry code doesn't affect this part here. I suspect something else is going on inside the indicator code itself to make it so its plotting zeroes.

                    As far as your question about order handling here, it may be helpful to read this : http://www.ninjatrader.com/support/h...d_approach.htm
                    Adam P.NinjaTrader Customer Service

                    Comment


                      #11
                      Hi Adam,

                      I think I may have found the root cause of the problem:

                      Code:
                      EnterLongStop([B]1[/B], true, 1, High[0]+TickSize, "L");
                      When the bold is set to 1 it doesn't work, however when it is set to 0 it does work. Can you explain to me why this is?

                      Cheers,
                      Harry

                      Comment


                        #12
                        As soon as I bring in a CancelOrder() command it zero's all the MA's. I just don't understand it!?

                        Comment


                          #13
                          Hello,

                          When you have the 1 there that tells NT to submit it on a secondary series. I can see in your sample code you do not have a secondary added time frame therefor there is no secondary time frame to submit on therefor it would not work.



                          You are using the last overload which is for advanced MTF programming, from what I can see in the sample code you do not need to use this simply use one of the other overloads and do not specify BarsInProgress index.

                          -Brett
                          BrettNinjaTrader Product Management

                          Comment


                            #14
                            Originally posted by NinjaTrader_Brett View Post
                            Hello,

                            When you have the 1 there that tells NT to submit it on a secondary series. I can see in your sample code you do not have a secondary added time frame therefor there is no secondary time frame to submit on therefor it would not work.



                            You are using the last overload which is for advanced MTF programming, from what I can see in the sample code you do not need to use this simply use one of the other overloads and do not specify BarsInProgress index.

                            -Brett
                            Still got this problem. To clarify, I want a LongStop order to be entered when the MA's cross (Fast>Slow) at the high of the bar where the cross occured + 1 tick, and I want this order to stay pending until the next MA cross where it will be a ShortStop at low of the bar where the cross occured - 1 tick.

                            Whenever the CancelOrder() function is called it Zeros all MAs.

                            Comment


                              #15
                              Hello,

                              Thanks for the note.

                              In this case you would need to use the following tutorial to add Print statements to isolate what exactly is going on. From the Print statements isolating the strategy behaivior we would be able to get more information that would allow us to further assist.






                              I would start be using both of these and then adding Prints before and after my order methods and also track when OnBarUpdate runs and the Prints should help you isolate what is going on in the script. Once you get to that point and if you are still running into issue please post some sample code with the prints and the it produced with any other details that are relevant and we can further assist.
                              BrettNinjaTrader Product Management

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              647 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              368 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
                              571 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