Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to Limit Code to Executing Once per Day

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

    How to Limit Code to Executing Once per Day

    Hi All,

    I am trying to limit my code to executing only once per day while having COBC = false. I started with what I found previously:

    if (CurrentBar < 2) return;
    if (Time[0].DayOfWeek != Time[1].DayOfWeek) // new day, reset bool flag
    {
    signalMadeToday = false;
    }

    if(signalMadeToday = false)
    {....Code...
    signalMadeToday = true;
    }


    But when I have COBC = false, it constantly resets. I took a look at the printed output with the following and can see the first print shows a bool of True, while the second shows False.

    if (CurrentBar < 2) return;
    if (Time[0].DayOfWeek != Time[1].DayOfWeek) // new day, reset bool flag
    {
    Print(signalMadeToday.ToString());
    signalMadeToday = false;
    Print(signalMadeToday.ToString());
    }

    What is the issue here? I saw a post about this that recommended to do the following:

    if (Bars.FirstBarOfSession)
    {
    // Store the strategy's prior number of trades
    priorTradesCount = Performance.AllTrades.Count;
    }

    {
    // Returns out of the OnBarUpdate() method. This prevents any further evaluation of trade logic in the OnBarUpdate() method.
    return;
    }

    But this caused my strategy to not function properly. I didn't spend too much time looking at this option, but even if it will work, it only pertains to trades (which is most important). But I also want to limit emails, drawing objects, etc., and I don't think this will do that.

    So what is the issue with the bool being reset even though the logic says it should not reset until the next day? How can I get it to stay true throughout the day with COBC = false? Or what other way can I simply limit the code to executing once per day whatever it may be?

    Thank you,

    Regards,

    Lee

    #2
    Hello Lee,

    if (Time[0].DayOfWeek != Time[1].DayOfWeek) // new day, reset bool flag
    {
    signalMadeToday = false;
    }
    This would be true throughout the day and therefore occur all day.

    Comment


      #3
      Originally posted by lee612801 View Post
      Hi All,

      I am trying to limit my code to executing only once per day while having COBC = false. I started with what I found previously:

      if (CurrentBar < 2) return;
      if (Time[0].DayOfWeek != Time[1].DayOfWeek && FirstTickOfBar) // new day, reset bool flag
      {
      signalMadeToday = false;
      }

      if(signalMadeToday = false)
      {....Code...
      signalMadeToday = true;
      }


      But when I have COBC = false, it constantly resets. I took a look at the printed output with the following and can see the first print shows a bool of True, while the second shows False.

      if (CurrentBar < 2) return;
      if (Time[0].DayOfWeek != Time[1].DayOfWeek) // new day, reset bool flag
      {
      Print(signalMadeToday.ToString());
      signalMadeToday = false;
      Print(signalMadeToday.ToString());
      }

      What is the issue here? I saw a post about this that recommended to do the following:

      if (Bars.FirstBarOfSession)
      {
      // Store the strategy's prior number of trades
      priorTradesCount = Performance.AllTrades.Count;
      }

      {
      // Returns out of the OnBarUpdate() method. This prevents any further evaluation of trade logic in the OnBarUpdate() method.
      return;
      }

      But this caused my strategy to not function properly. I didn't spend too much time looking at this option, but even if it will work, it only pertains to trades (which is most important). But I also want to limit emails, drawing objects, etc., and I don't think this will do that.

      So what is the issue with the bool being reset even though the logic says it should not reset until the next day? How can I get it to stay true throughout the day with COBC = false? Or what other way can I simply limit the code to executing once per day whatever it may be?

      Thank you,

      Regards,

      Lee
      Addition in red in your original code.

      Comment


        #4
        Great, I will give this a try! I am running day bars btw.

        Thanks for the response and help!

        Regards,

        Lee

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by SalmaTrader, 07-07-2026, 10:26 PM
        0 responses
        47 views
        0 likes
        Last Post SalmaTrader  
        Started by CarlTrading, 07-05-2026, 01:16 PM
        0 responses
        22 views
        0 likes
        Last Post CarlTrading  
        Started by CaptainJack, 06-17-2026, 10:32 AM
        0 responses
        15 views
        0 likes
        Last Post CaptainJack  
        Started by kinfxhk, 06-17-2026, 04:15 AM
        0 responses
        21 views
        0 likes
        Last Post kinfxhk
        by kinfxhk
         
        Started by kinfxhk, 06-17-2026, 04:06 AM
        0 responses
        23 views
        0 likes
        Last Post kinfxhk
        by kinfxhk
         
        Working...
        X