Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

NT7 adding a time to exit trade.

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

    NT7 adding a time to exit trade.

    Hello all,

    I am trying to add a timer(1minute to 60 minute) to exit my trade. for example, on my 15 minute chart, I open trade at 13.00 and want close trade at 13.02... I was looking on the forum, but do not find anything relative to this.

    can anyone help me going forward?

    Thanks!

    #2
    Hello Frederic,

    Thanks for your post.

    Please see the reference indicator that shows how to work with datetime and timespan objects here: http://ninjatrader.com/support/forum...ad.php?t=19292
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      thanks a lot for answer. do you have maybe any example already in a strategy?

      I cant imagine people are always using bars to exit, and not a timer in a strategy..

      Comment


        #4
        Hello Frederic,

        Thanks for your reply.

        I don't have an example strategy. Here is some example code of capturing the time of a trade and testing if 2 minutes have passed then exiting the trade (assuming CalculateOnBarClose = false).

        Code:
        			
        if (Historical) return;  // use on real data
        			
        if (Open[0] > Close[0] && doitonce)  //simple entry condition
        	{
        	   EnterLong();  // market order long
        	   test = DateTime.Now;   // declared private DateTime test; in variables section
        	   Print (" Entered order: "+test);  // output entry time to output  window
        	   test = test.AddMinutes(2); // add 2 minutes so trade exits in 2 minutes
        	   doitonce = false;  // bool to prevent multiple entries/prints
        	}
        			
        if (Position.MarketPosition == MarketPosition.Long && DateTime.Now > test)
        	{
        	   ExitLong();  // exit market order
        	   Print ("Exitnow: "+DateTime.Now);  // output exit time to output window
        	}
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          Hello
          I try your code, and this is not working at all. do you have maybe any code that is working??

          in backtest, i can see that ninja open positions, but close them after 2 days, or 1 week, or some hours. useless...

          an exemple with a working code will be appreciated.

          Thanks!

          Comment


            #6
            Hello Frederic,

            Thanks for your follow-up post.

            The example I provided 3 months ago was for real time data. The first line of the code if (Historical) return; // use on real data would cause the time test not to be executed on historical data.

            Your very first post mentions 15 minute bars and wanting to exit after 2 minutes of the 15 minute bar. If you are backtesting this would not be possible because the only information available for each bar is the OHLC values and the time of the bar close which is every 15 minutes. Can you clarify what you would like to accomplish?
            Paul H.NinjaTrader Customer Service

            Comment


              #7
              Hello,

              I simply try to accomplish that ninjatrader open a position on a 15 minutes, and close this position after 3 minutes. do you know what I mean? do you have any code who will make the deal?

              Comment


                #8
                Hello Frederic,

                Thanks for your post.

                For backtesting you would want to add a secondary smaller time frame data series because in backtesting you do not have all of the ticks that live data does. The strategy only knows the end Open, high, Low, close and close time of each bar. By adding a smaller time frame you will get the strategy to function as you want.

                In the case of 15 minute bars and your expressed desire to exit after x minutes, I would add 1 minute bars to allow the strategy to exit after X minutes.

                Here is a link to a working strategy that enters on one time frame and exist on another which what you will need to do to do back testing: http://ninjatrader.com/support/forum...ead.php?t=5787
                Paul H.NinjaTrader Customer Service

                Comment


                  #9
                  Originally posted by NinjaTrader_Paul View Post
                  Hello Frederic,

                  Thanks for your reply.

                  I don't have an example strategy. Here is some example code of capturing the time of a trade and testing if 2 minutes have passed then exiting the trade (assuming CalculateOnBarClose = false).

                  Code:
                  			
                  if (Historical) return;  // use on real data
                  			
                  if (Open[0] > Close[0] && doitonce)  //simple entry condition
                  	{
                  	   EnterLong();  // market order long
                  	   test = DateTime.Now;   // declared private DateTime test; in variables section
                  	   Print (" Entered order: "+test);  // output entry time to output  window
                  	   test = test.AddMinutes(2); // add 2 minutes so trade exits in 2 minutes
                  	   doitonce = false;  // bool to prevent multiple entries/prints
                  	}
                  			
                  if (Position.MarketPosition == MarketPosition.Long && DateTime.Now > test)
                  	{
                  	   ExitLong();  // exit market order
                  	   Print ("Exitnow: "+DateTime.Now);  // output exit time to output window
                  	}
                  To whom it may concern,

                  Just wanted to add that the above code does indeed work for back testing if you exclude the Historical condition Paul mention AND you switch use of DateTime.Now to something like Time[0].

                  DateTime.Now will be retrieving your local PC/server time (wherever you're running you back test from), so this will not be in sync with the time on the charts when back testing. Hope that makes sense and helps as I had to figure this one out on my own.

                  Here's the sample code for that section if needed:
                  --------------------
                  //place this directly after entry order
                  target2 = EnterLong(1,"Entry2");
                  tradeduration = Time[0];
                  tradeduration = tradeduration.AddMinutes(15);//adding 15 minutes to entry time
                  --------------------------
                  //then I run this condition wherever needed in OnBarUpdate
                  //if everything is in OnBarUpdate, then below section or reference must be placed above your entry logic so it checks first on each bar if you're still in a position and applies the timer logic

                  if ((Position.MarketPosition == MarketPosition.Long) && (Close[0] < target2.AvgFillPrice + 12 * TickSize) && (Time[0] > tradeduration))
                  Print("Been in trade longer than 15 minutes and still less than 12 ticks profits");
                  ExitLong();

                  -----------------------------

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by algospoke, 04-17-2024, 06:40 PM
                  6 responses
                  49 views
                  0 likes
                  Last Post algospoke  
                  Started by arvidvanstaey, Today, 02:19 PM
                  4 responses
                  11 views
                  0 likes
                  Last Post arvidvanstaey  
                  Started by samish18, 04-17-2024, 08:57 AM
                  16 responses
                  61 views
                  0 likes
                  Last Post samish18  
                  Started by jordanq2, Today, 03:10 PM
                  2 responses
                  9 views
                  0 likes
                  Last Post jordanq2  
                  Started by traderqz, Today, 12:06 AM
                  10 responses
                  21 views
                  0 likes
                  Last Post traderqz  
                  Working...
                  X