Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Pause in time after conditions found

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

    Pause in time after conditions found

    Hi,

    I would like to make a pause in time (seconds or milliseconds) when my condition are found. So between "If conditons set"... and {do something}. For exemple, if RSI is below 30, (conditons are met), wait 3 seconds and then send a buy market order.
    I tried to play around with Time and ToTime but with no sucess

    #2
    Hello EURLSD,

    Thank you for your inquiry. If you are using a data series period of 1 second you can create something similar to the following code which will work with Historical and Realtime data:

    Code:
    #region Variables
    private DateTime RSIbelow30;
    #endregion
    protected override void Initialize()
    {
        CalculateOnBarClose = true;
    }
    protected override void OnBarUpdate()
    {
    if(RSI(14, 3)[0] < 30 && RSIbelow30 < Time[0]){
    		//Log("RSI is below 30 @ " + Time[0].ToString(), LogLevel.Information);
    		RSIbelow30 = Time[0].AddSeconds(3);
    	}
    	if(Time[0] == RSIbelow30){
    		//Log("Entered long @ " + Time[0].ToString(), LogLevel.Information);
    		EnterLong(DefaultQuantity, "");
    	}
    }
    }
    You can uncomment out the Log code to visually see at what time the condition is met and at what time the order is placed (3 seconds later for this example).

    If you are using a different data series period such as 1 minute, 30 minute, 60 minute, daily, etc. you can create something like the following code which will work with real time data or real time recorded market replay data only -- NOT historical).

    PLEASE NOTE: OnMarketData() is not guaranteed to be called every second so this code may miss opportunities to enter long. You would want to adjust the condition to better meet the needs of your specific strategy.

    Code:
    #region Variables
    DateTime RSIbelow30;
    #endregion
    protected override void Initialize()
    {
        CalculateOnBarClose = true;
    }
    protected override void OnBarUpdate(){}
    protected override void OnMarketData(MarketDataEventArgs e)
    {
            if(RSI(14, 3)[0] < 30 && RSIbelow30 < e.Time){
    		Log("RSI is below 30 @ " + e.Time.ToString(), LogLevel.Information);
    		RSIbelow30 = e.Time.AddSeconds(3);
    	}
    	if(e.Time == RSIbelow30){
    		Log("Entered long @ " + e.Time.ToString(), LogLevel.Information);
    		EnterLong(DefaultQuantity, "");
    		RSIbelow30 = e.Time;
    	}
    }
    How both of these codes work is that a check is made to determine if the RSI is below 30 at the current bar, if it is then it also makes sure the strategy isn't already waiting 3 seconds to enter a position. If both those conditions are met, then it will set the RSIbelow30 variable to 3 seconds after the first set of conditions were met so that in the next check (if the current bar time is the same as the RSIbelow30 variable) the position can be entered.

    A very basic example of how you might adjust the second code's entry condition to more accurately enter long when it is supposed to:

    Code:
    if(e.Time.AddSeconds(-2) == RSIbelow30 || e.Time.AddSeconds(-1) == RSIbelow30 || e.Time == RSIbelow30 || e.Time.AddSeconds(1) == RSIbelow30 || e.Time.AddSeconds(2) == RSIbelow30)
    {
            EnterLong();
    }
    This example would make it so as long as OnMarketData() was called within +/- 2 seconds of the 3 seconds after the RSI condition was met, it would enter long.

    You may find further information on working with the OnMarketData() method in our help guide here: http://www.ninjatrader.com/support/h...marketdata.htm

    If we may be of further assistance, please let us know.
    Last edited by NinjaTrader_MichaelM; 06-02-2015, 08:42 AM.
    Michael M.NinjaTrader Quality Assurance

    Comment


      #3
      Thank you for your answer Michael.
      I get the idea, but I'm working with Tick data. I tried to add "currentbar" on 1 second and write "Time[1][0]" But it doesn't work. I think I'm wrong

      Comment


        #4
        Hello EURUSD,

        Could you please provide the code in question so I may investigate the issue?

        You can either post it to this thread or e-mail us at platformsupport[AT]ninjatrader[DOT]com and include the ticket #1327784 in the subject line.

        I look forward to assisting you further.
        Michael M.NinjaTrader Quality Assurance

        Comment


          #5
          Sent by email. I apply this strategy to a 34 tick chart .

          Comment


            #6
            Hello EURLSD,

            Thank you for sending the code. I am reviewing it now and will respond directly to your e-mail.

            We appreciate your patience.
            Michael M.NinjaTrader Quality Assurance

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            558 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            324 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            101 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            545 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            547 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X