Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Time Limit on Trade.

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

    Time Limit on Trade.

    Hey everyone. I'm trying to create strategy that creates a time limit on every trade made. I trade using mostly range bars but I can only accurately predict with my strategies within roughly an hour based on what indications I see in the market.

    What I want to do is have a ninjascript I can add to a chart(or an automated strategy for Market replay purposes) that allows me to enter a time limit, 5 minutes for example and once 5 minutes passes from the time the entry was executed exit the trade no matter what position it's in. The largest problem I'm running into is that the time series used in ninjatrader rolls over at the hour and minute by 4000 and 40 respectively.

    Kinda like a backup in case I forget I'm still in the trade and it starts going against me until it stops out.

    #2
    Hello,

    Thank you for the question.

    Placing time requirements on trades from NinjaScript would not be a problem, but I did want to clarify are you referring to manually placed trades?

    A NinjaScript strategy could both place trades and also close those positions/orders at X interval but it would need to do both submitting and closing.

    If you are instead referring to a manually placed order, there are no pre made ways to do this from NinjaScript. This would be a unsupported/undocumented subject but could possibly be accomplished. I had previously commented on this topic in the post here: http://ninjatrader.com/support/forum...78&postcount=2

    Something like this could in turn be used, but because this is not documented/supported we really cant define if the outcome would be 100% successful. This would be something that would need to be developed and tested if used.

    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      That's exactly what I was looking for but it doesn't work. Here is the code I'm using.

      Code:
                  {
                      EnterShort(DefaultQuantity, "");
      				entryTime = DateTime.Now.AddSeconds(60);
      				Print(DateTime.Now);
      				Print(entryTime);
                  }
      
      			
      			if (DateTime.Now >= entryTime)
      			{
      				ExitShort("", "");
      				ExitLong("", "");
      			}
      The problem is I'm running the test in market replay so I can't test using this code because it will take my machines current time, not the time of the market replay.

      Comment


        #4
        Hello,

        Generally in the platform you would want to avoid using DateTime.Now as that represents your computer time which is not the same as when the platform is processing bars. Because you can be in Past times or the Current time in the platform, you would instead want to reference the Bars time that is being processed.

        You could use Time[0], this would return the bar close time. As the script processes each bar, this time could be used especially if you are trying to mark a specific time of an entry in contrast to the chart data.

        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service

        Comment


          #5
          So I changed the code a bit and let it run all day today and it seems to take 11 minutes per trade even though I'm adding 60 seconds.

          Here is the new code for entries only.

          Code:
                      // Condition set 1
                      {
                          EnterLong(DefaultQuantity, "");
          		entryTime = Time[0].AddSeconds(60);
          		Print(entryTime);
                      }
          
                      // Condition set 2
                      {
                          EnterShort(DefaultQuantity, "");
          		entryTime = Time[0].AddSeconds(60);
          		Print(entryTime);
                      }
          			
          	    if (Time[0] >= entryTime)
          	    {
          	        	ExitShort("", "");
          			ExitLong("", "");
          	    }
          .
          Here is a screen shot of my analysis showing that its averaging 11 minutes even though it is set to on bar close = false so it should close after 60 seconds no matter what. Do you know why that is?

          Comment


            #6
            Originally posted by magnatauren View Post
            So I changed the code a bit and let it run all day today and it seems to take 11 minutes per trade even though I'm adding 60 seconds.

            Here is the new code for entries only.

            Code:
                        // Condition set 1
                        {
                            EnterLong(DefaultQuantity, "");
            		entryTime = Time[0].AddSeconds(60);
            		Print(entryTime);
                        }
            
                        // Condition set 2
                        {
                            EnterShort(DefaultQuantity, "");
            		entryTime = Time[0].AddSeconds(60);
            		Print(entryTime);
                        }
            			
            	    if (Time[0] >= entryTime)
            	    {
            	        	ExitShort("", "");
            			ExitLong("", "");
            	    }
            .
            Here is a screen shot of my analysis showing that its averaging 11 minutes even though it is set to on bar close = false so it should close after 60 seconds no matter what. Do you know why that is?

            http://imgur.com/a/kewHh
            That would seem to indicate that you are running this on a 10-minute chart?

            If you want to set a trade time limit, you want to do it from OnExecution(), after the trade is filled, which is when you will get the relevant event that the trade has actually started.

            Comment


              #7
              I'm actually running this on range bars and just testing on extremely low time frame ones. The strategy itself is also set to on bar close = false so it should exit at 1 minute exactly. The bars in trade is also 20, so it's holding onto most trades for more than 20 bars on a 1 pip range for over 11 minutes. That definitely shouldn't be happening if this were working properly. If this were working it should be holding onto them for exactly 1 minute give or take 6 seconds.


              Is there something that I'm missing in the exit timing portion? or is this just not possible to create something to accurately exit a specific time after the trade. I'll switch it over to OnExecution() to test it but that doesn't seem it should fix the exit timing problem.

              Comment


                #8
                Originally posted by magnatauren View Post
                I'm actually running this on range bars and just testing on extremely low time frame ones. The strategy itself is also set to on bar close = false so it should exit at 1 minute exactly. The bars in trade is also 20, so it's holding onto most trades for more than 20 bars on a 1 pip range for over 11 minutes. That definitely shouldn't be happening if this were working properly. If this were working it should be holding onto them for exactly 1 minute give or take 6 seconds.


                Is there something that I'm missing in the exit timing portion? or is this just not possible to create something to accurately exit a specific time after the trade. I'll switch it over to OnExecution() to test it but that doesn't seem it should fix the exit timing problem.
                Hm. That does sound strange now. This may be a silly question, but are you sure that that is not a report from a backtest, rather that MarketReplay?

                Comment


                  #9
                  The screen shot was from the control panel that was tracking what was happening in the market replay trades. I was having it trade in market replay to try and find what it was doing. I can run it again to show the same results, but it's definitely holding onto some for way longer than required and most for more than a minute greater than it should.

                  Comment


                    #10
                    Hello magnatauren,

                    Would it be possible to provide a more complete code sample? I am curious as to whether this is a multi-time frame strategy. If this is a multi time frame strategy, I would like to recommend filtering on your BarsInProgress member, and testing without any additional bars series.
                    Jessica P.NinjaTrader Customer Service

                    Comment


                      #11
                      This is my current test code just to make sure that my findings are happening as they seem.

                      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
                      
                      namespace NinjaTrader.Strategy
                      {
                          [Description("")]
                          public class NAME : Strategy
                          {
                              #region Variables
                      		private DateTime entryTime = DateTime.MinValue;
                      		
                      		private int periodShort = 12;
                      		private int periodLong = 26;
                      		private int seconds = 60;
                      
                              #endregion
                              protected override void Initialize()
                              {
                                  Add(VWMA(periodShort));
                                  Add(VWMA(PeriodLong));
                                  Add(VWMA(periodShort));
                                  Add(VWMA(PeriodLong));			
                      
                                  CalculateOnBarClose = false;
                              }
                      
                              protected override void OnBarUpdate()
                              {
                                  // Condition set 1
                                  if (Close[0] > Close[1]
                                      && Position.MarketPosition == MarketPosition.Flat)
                                  {
                                                      EnterLong(DefaultQuantity, "");
                      				entryTime = Time[0].AddSeconds(seconds);
                      //				Print(DateTime.Now);
                      				Print(entryTime);
                                  }
                      
                                  // Condition set 2
                                  if (Close[0] < Close[1]
                                      && Position.MarketPosition == MarketPosition.Flat)
                                  {
                                                      EnterShort(DefaultQuantity, "");
                      				entryTime = Time[0].AddSeconds(seconds);
                      //				Print(DateTime.Now);
                      				Print(entryTime);
                                  }
                      			
                      	    if (Time[0] >= entryTime)
                      	    {
                      	    	ExitShort("", "");
                      		ExitLong("", "");
                      	    }
                      			
                              }
                      
                              #region Properties
                      		[Description("Numbers of bars used for calculations")]
                      		[GridCategory("Parameters")]
                      		public int PeriodLong
                      		{
                      			get { return periodLong; }
                      			set { periodLong = Math.Max(1, value); }
                      		}
                      		
                      		[Description("Numbers of bars used for calculations")]
                      		[GridCategory("Parameters")]
                      		public int PeriodShort
                      		{
                      			get { return periodShort; }
                      			set { periodShort = Math.Max(1, value); }
                      		}
                      		
                      		[Description("Numbers of bars used for calculations")]
                      		[GridCategory("Parameters")]
                      		public int Seconds
                      		{
                      			get { return seconds; }
                      			set { seconds = Math.Max(1, value); }
                      		}
                      		
                      		
                              #endregion
                          }
                      }
                      Last edited by magnatauren; 10-03-2016, 04:02 PM.

                      Comment


                        #12
                        Thank you magnatauren for providing that sample.

                        I didn't see anything obvious in there that would cause this behavior. I did notice that while we do have some guarantees provided by checking MarketPosition, and your code is sound, we have executable paths which cover both Exit and Entry orders. I have also noticed that we are using overloads which contain blank signal names. While I do not believe either of these is causing the behavior we are seeing, to reduce our solution space, I have provided a suggested refactor attached to this reply. The refactor forces a choice in a single OnBarUpdate run between entry and exit orders, and does not introduce signal names as a variable. Finally, since we are just trying to determine why our exit behavior is not working the way we expect, I have allowed exiting on any tick but have forced entering on the first tick of a bar. With three fewer variables we should have an easier time tracking down a solution.

                        So that we can further assist with debugging, would it be possible to provide us with the name of the instrument, the expiry, and a date and time where your strategy reliably places trades?
                        Attached Files
                        Last edited by NinjaTrader_JessicaP; 10-04-2016, 09:26 AM.
                        Jessica P.NinjaTrader Customer Service

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by TraderG23, 12-08-2023, 07:56 AM
                        9 responses
                        382 views
                        1 like
                        Last Post Gavini
                        by Gavini
                         
                        Started by oviejo, Today, 12:28 AM
                        0 responses
                        1 view
                        0 likes
                        Last Post oviejo
                        by oviejo
                         
                        Started by pechtri, 06-22-2023, 02:31 AM
                        10 responses
                        125 views
                        0 likes
                        Last Post Leeroy_Jenkins  
                        Started by judysamnt7, 03-13-2023, 09:11 AM
                        4 responses
                        59 views
                        0 likes
                        Last Post DynamicTest  
                        Started by ScottWalsh, Yesterday, 06:52 PM
                        4 responses
                        36 views
                        0 likes
                        Last Post ScottWalsh  
                        Working...
                        X