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()
                
                
            {
                {
                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();
                }
            }
    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 rolandaallen, Today, 10:38 PM
    0 responses
    1 view
    0 likes
    Last Post rolandaallen  
    Started by RogersuDarla, Today, 10:11 PM
    0 responses
    1 view
    0 likes
    Last Post RogersuDarla  
    Started by felixolmo, Today, 09:35 PM
    0 responses
    8 views
    0 likes
    Last Post felixolmo  
    Started by nandhumca, Today, 08:47 PM
    0 responses
    7 views
    0 likes
    Last Post nandhumca  
    Started by BullyWig Trader, Today, 07:15 PM
    0 responses
    6 views
    0 likes
    Last Post BullyWig Trader  
    Working...
    X