Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Problems with Trigger Custom Event; OnTimer

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

    Problems with Trigger Custom Event; OnTimer

    Hello,

    I have been on the support forum before for this same issue, but it never led to any solution. I just can not understand my problem can't be solved by any of you people around here.

    It's about the Trigger Custom Event. I want to use a timer to close all open positions minutes before the market closes. The ExitOnClose won't help me, because this will result in strategies being not in sync after that execution. So please skip that possible solution.

    My problem is that the trigger itself works, but strangely the execution of the order is not. I know the trigger works, because the output window shows whatever I put as command on the trigger, but only the orders are not executed. I use the same lines of code to close open positions on an OnBarUpdate, which works fine. It just seems the trigger does not want to fire the order execution, where other command are fired.
    On the last bar of a session (the flat bar), the closing of positions is fired (because of the OnBarUpdate) after all, but this is too late, because I want to avoid gaps between sessions and open positions in the weekends.

    I added the code for your insight. Please tell me where to look, because I am just out of options here... the bold parts refer to the trigger.

    Kind regards,
    Francis
    --------------------------------------
    #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;
    using System.Windows.Forms;
    #endregion

    // This namespace holds all strategies and is required. Do not change it.
    namespace NinjaTrader.Strategy
    {
    /// <summary>

    /// </summary>
    [Description("RenkoFibRetLongTest: ")]
    public class RenkoFibRetLongTest: Strategy
    {
    #region Variables

    private int intTimeClose = 221400; // Default setting for IntTimeClose
    private int intTimeOpen = 221600; // Default setting for IntTimeOpen
    private int intTimeCloseFriday = 231400; // Default setting for IntTimeCloseFriday
    private int intTimeOpenFriday = 231600; // Default setting for IntTimeOpenFriday

    private System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();

    // User defined variables (add any user defined variables below)
    #endregion

    /// <summary>
    /// This method is used to configure the strategy and is called once before any strategy method is called.
    /// </summary>
    protected override void Initialize()
    {
    CalculateOnBarClose = true;
    MaxProcessedEvents = 1000;
    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnStartUp()
    {
    // Initiate our Timer object with an interval of 30000ms (30 seconds)
    myTimer.Tick += new EventHandler(TimerEventProcessor);
    myTimer.Interval = 30000;
    myTimer.Start();

    }

    protected override void OnBarUpdate()
    {
    ... Some Strategy Code...
    }

    // Timer's tick event handler. Called at every tick of 30000ms.
    private void TimerEventProcessor(Object myObject, EventArgs myEventArgs)
    {
    /* Important to use the TriggerCustomEvent() to ensure that NinjaScript indexes and pointers are correctly set.
    Do not process your code here. Process your code in the MyCustomHandler method. */
    TriggerCustomEvent(MyCustomHandler, 0, myTimer.Interval);
    }

    private void MyCustomHandler(object state)
    {
    if ((Time[0].DayOfWeek != DayOfWeek.Friday
    && ToTime(Time[0]) >= intTimeClose
    && ToTime(Time[0]) < intTimeOpen)
    || (Time[0].DayOfWeek == DayOfWeek.Friday
    && ToTime(Time[0]) >= intTimeCloseFriday
    && ToTime(Time[0]) < intTimeOpenFriday))
    {
    ExitLong();
    }
    }

    // Important to clean up our resources
    protected override void OnTermination()
    {
    // Cleans up our Timer object
    myTimer.Dispose();

    }


    #region Properties

    #endregion
    }
    }

    #2
    Hello Francis,

    Thank you for your post.

    If you add a Print() statement to the following condition do you see it printed to the Output window (Tools > Output window):
    Code:
    if ((Time[0].DayOfWeek != DayOfWeek.Friday
    && ToTime(Time[0]) >= intTimeClose
    && ToTime(Time[0]) < intTimeOpen)
    || (Time[0].DayOfWeek == DayOfWeek.Friday
    && ToTime(Time[0]) >= intTimeCloseFriday
    && ToTime(Time[0]) < intTimeOpenFriday))
    {
    ExitLong();
    Print("Exit submitted.");
    }
    I look forward to your response.

    Comment


      #3
      Hello Patrick,

      Thank you for your reply.

      Yes, the trigger itself works. That is what I meant that I can plot output in the output window. In my case every 30 seconds (when the time conditions are met). That is why I really do not get what is wrong here. Within the same statement the printing works, but the order execution is not. While on a bar update, exactly the same conditons (the time between which to execute) works fine, 100% of the time.

      Kind regards,
      Francis

      Comment


        #4
        Hello Francis,

        Thank you for your response.

        Are you running the test in the Strategy Analyzer? What is the primary bar series the strategy is applied to?

        Comment


          #5
          Hello Patrick,

          I solely run on renko's. Ranging from 2 till about 14.

          I'm running the strategy on live trading. So that is where I also can witness at the actual time the timer does not trigger the closing orders. Running these strategies when backtesting and potimizing, I use the setting to exit on close. With live trading the exit on close causes me other problems, so that is why I'm looking far an alternative. Some bars do not update that frequently to use the onbarupdate.

          Comment


            #6
            Hello Francis,

            Thank you for your response.

            Do you have a sample of your code that re-creates this item that I may test on my end?

            If you prefer you can send this to support[at]ninjatrader[dot]com with 'ATTN: Patrick - 936057' in the subject line and a reference to this thread in the body of the e-mail: http://www.ninjatrader.com/support/f...ad.php?t=61403

            I look forward to assisting you further.

            Comment


              #7
              For knowledge base purpose I would like to add the solution that worked for me:

              The timer itself worked, but did not got the proper data to act on. It's underlying data is based on renko bars. When the timer was triggered, it looked for the time of close on the last renko bar to made it's decision on. That renko bar could be older than the current time, so nothing happenend.

              Ninjatrader support suggested to add a second time series based on minutes on which to refer to in the timer, instead to refer to the last renko bar. This solution worked. So to add a second time serie and create a multiple time series strategy could solve alike problems.

              Note: In my case I use also BarsSinceEntry. This method needs an alteration to work in with multiple time series.
              I also use MRO (Most Recent Occurance). This method does not work at all in multiple time series.

              Thanks NT, Bertrand and Patrick.

              Cheers,
              Francis

              Comment

              Latest Posts

              Collapse

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