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 gtheaded, Today, 04:38 PM
    0 responses
    1 view
    0 likes
    Last Post gtheaded  
    Started by trader252, Today, 04:34 PM
    0 responses
    4 views
    0 likes
    Last Post trader252  
    Started by WiseAmo, Today, 04:34 PM
    0 responses
    2 views
    0 likes
    Last Post WiseAmo
    by WiseAmo
     
    Started by Don22Trader1, Today, 04:20 PM
    0 responses
    2 views
    0 likes
    Last Post Don22Trader1  
    Started by Tradarr, Yesterday, 06:23 PM
    2 responses
    49 views
    0 likes
    Last Post -=Edge=-  
    Working...
    X