Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Monday to Friday code

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

    Monday to Friday code

    I try to code a strategy for backtesting, that works only from Monday 6 am til Friday 8 pm.
    It seems to have trouble to re-enter the market on Mondays. Could you please check the code in my sample strategy.
    Code:
    /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
                
                
            {
                {
                if (Time[0].DayOfWeek == DayOfWeek.Monday && ToTime(Time[0]) >= 060000)
                
                    
                    {
                        // Condition set 1
                        if (SMA(Close, Fast)[0] > SMA(Close, Slow)[0])
                        {
                            EnterLong(DefaultQuantity, "");
                        }
    
                        // Condition set 2
                        if (SMA(Close, Fast)[0] < SMA(Close, Slow)[0])
                        {
                            EnterShort(DefaultQuantity, "");
                        }
                    }
                }
                if (Time[0].DayOfWeek == DayOfWeek.Friday && ToTime(Time[0]) >= 200000)
                {
                    ExitLong();
                    ExitShort();
                }
            }
    
            #region Properties
            [Description("")]
            [GridCategory("Parameters")]
            public int Fast
            {
                get { return fast; }
                set { fast = Math.Max(1, value); }
            }
    
            [Description("")]
            [GridCategory("Parameters")]
            public int Slow
            {
                get { return slow; }
                set { slow = Math.Max(1, value); }
            }
            #endregion

    #2
    Hello moon_121,

    Beside an extra set of curly braces "{}" there is nothing wrong with your code.

    Originally posted by moon_121 View Post
    Code:
    /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
                
                
            {
                [B][I][U][COLOR="Magenta"]{[/COLOR][/U][/I][/B]
                if (Time[0].DayOfWeek == DayOfWeek.Monday && ToTime(Time[0]) >= 060000)
                
                    
                    {
                        // Condition set 1
                        if (SMA(Close, Fast)[0] > SMA(Close, Slow)[0])
                        {
                            EnterLong(DefaultQuantity, "");
                        }
    
                        // Condition set 2
                        if (SMA(Close, Fast)[0] < SMA(Close, Slow)[0])
                        {
                            EnterShort(DefaultQuantity, "");
                        }
                    }
                [B][I][U][COLOR="Magenta"]}[/COLOR][/U][/I][/B]
                if (Time[0].DayOfWeek == DayOfWeek.Friday && ToTime(Time[0]) >= 200000)
                {
                    ExitLong();
                    ExitShort();
                }
            }
    With the code that you have above though will only enter a trade on Monday and Exit on Friday. If you would like it to enter a trade on other days besides just Monday you would want to define them inside the Condition. For Example:

    Code:
    if (Time[0].DayOfWeek == DayOfWeek.Monday || Time[0].DayOfWeek == DayOfWeek.Tuesday || Time[0].DayOfWeek == DayOfWeek.Wednesday || Time[0].DayOfWeek == DayOfWeek.Thursday && ToTime(Time[0]) >= 060000)
    Let us know if we can be of further assistance.
    JCNinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by benmarkal, Yesterday, 12:52 PM
    3 responses
    22 views
    0 likes
    Last Post NinjaTrader_Gaby  
    Started by helpwanted, Today, 03:06 AM
    1 response
    18 views
    0 likes
    Last Post sarafuenonly123  
    Started by Brevo, Today, 01:45 AM
    0 responses
    11 views
    0 likes
    Last Post Brevo
    by Brevo
     
    Started by aussugardefender, Today, 01:07 AM
    0 responses
    6 views
    0 likes
    Last Post aussugardefender  
    Started by pvincent, 06-23-2022, 12:53 PM
    14 responses
    244 views
    0 likes
    Last Post Nyman
    by Nyman
     
    Working...
    X