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 judysamnt7, 03-13-2023, 09:11 AM
    4 responses
    57 views
    0 likes
    Last Post DynamicTest  
    Started by ScottWalsh, Today, 06:52 PM
    4 responses
    36 views
    0 likes
    Last Post ScottWalsh  
    Started by olisav57, Today, 07:39 PM
    0 responses
    7 views
    0 likes
    Last Post olisav57  
    Started by trilliantrader, Today, 03:01 PM
    2 responses
    19 views
    0 likes
    Last Post helpwanted  
    Started by cre8able, Today, 07:24 PM
    0 responses
    9 views
    0 likes
    Last Post cre8able  
    Working...
    X