Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

15 minute ExitLong timer from entry time or entry price.

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

    15 minute ExitLong timer from entry time or entry price.

    Hello everyone,

    I'm trying to write an if statement that exists a long under a set of conditions: a 15 minute timer that starts from entry price. No luck using the strategy builder and i've tried to understand how to code with a time Dataset... taking me a very long time to understand. I figured i might as well ask here since it is probably a single line of code.

    Any help is appreciated.

    Basically:

    if ( EntryPriceTime + EntryPrice[3bars later OR NOW] = 15 minutes)
    Then ExitLong()
    Last edited by mohdhm; 06-26-2018, 12:22 PM.

    #2
    Hello mohdhm,

    Thanks for the note.

    The most straightforward way of doing this would be to create a new DateTime object at the time of entry. Then have an IF statement that checks if the current DateTime.Now.Second is > 15 seconds from the entry DateTime, with the hour, minute, etc equal to each other.

    Represents an instant in time, typically expressed as a date and time of day.


    Alternatively, you can look at the BarTimer indicator that uses a dispatcher to time a bar.

    .NET also has a StopWatch class, you would need to use a dispatcher for this as well.
    Provides a set of methods and properties that you can use to accurately measure elapsed time.


    Please let me know if I may be of any further assistance.
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Thanks Chris.

      Not sure about this, but why did you add a 15 second offset? Here is the code:

      Code:
      			///Short Entry - Overbought RSI
      			if ((RSI1.Default[0] >= OverboughtRSI)
      				 && (CrossBelow(RSI1.Default, RSI1.Avg, 1))
      				 && (MACD1.Diff[1] >= MACD1.Diff[0]))
      			{
      				EnterShort(1);
      				DateTime.Now = EntryTime;
      				
      			    //DateTime.Now.Ticks = EntryTime;
      				//ExpiryTime = EntryTime 
      			}
      								
      			if (DateTime.Now.Second > EntryTime + DateTime(0, 0, 15))
      			{
      				ExitShort(1);
      			}

      Now my only problem is that "EntryTime" is giving me a "did not exist in this current context. How do you define a 'new' variable in c#. I tried putting it in private but that led to a host of other errors.

      Help appreciated as always

      Originally posted by NinjaTrader_ChrisL View Post
      Hello mohdhm,

      Thanks for the note.

      The most straightforward way of doing this would be to create a new DateTime object at the time of entry. Then have an IF statement that checks if the current DateTime.Now.Second is > 15 seconds from the entry DateTime, with the hour, minute, etc equal to each other.

      Represents an instant in time, typically expressed as a date and time of day.


      Alternatively, you can look at the BarTimer indicator that uses a dispatcher to time a bar.

      .NET also has a StopWatch class, you would need to use a dispatcher for this as well.
      Provides a set of methods and properties that you can use to accurately measure elapsed time.


      Please let me know if I may be of any further assistance.

      Comment


        #4
        Hello mohdhm,

        Thanks for the reply.

        This script would be a bit too complex for the Strategy Builder. You would have to code this by hand. I have provided an example that simply enters long, marks the time, then exits 15 seconds later.

        Please let me know if I may be of any further assistance.
        Attached Files
        Last edited by NinjaTrader_ChrisL; 06-26-2018, 03:00 PM.
        Chris L.NinjaTrader Customer Service

        Comment


          #5
          Thank you Chris,

          I have incorporated that code into mine and it works.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by jpapa, Today, 07:22 AM
          1 response
          3 views
          0 likes
          Last Post NinjaTrader_Gaby  
          Started by kevinenergy, 02-17-2023, 12:42 PM
          116 responses
          2,757 views
          1 like
          Last Post kevinenergy  
          Started by franatas, 12-04-2023, 03:43 AM
          7 responses
          106 views
          0 likes
          Last Post NinjaTrader_ChelseaB  
          Started by Jltarrau, Today, 05:57 AM
          3 responses
          8 views
          0 likes
          Last Post Jltarrau  
          Started by f.saeidi, Today, 05:56 AM
          2 responses
          8 views
          0 likes
          Last Post NinjaTrader_Erick  
          Working...
          X