Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Advise on best time filter

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

    Advise on best time filter

    Hi All,

    I am trading a simple moving average cross over in the forex market, but I would like to limit trading to specific hours. In particular Monday to Friday 6am to 6pm, what would be the best way to apply this filter?

    Below is the code I am currently using (but I suspect I am missing something, as it is not 100% accurate):


    #region Variables
    private int tradeok = 1;
    #endregion

    [...]

    protected override void OnBarUpdate()

    {
    //Enter long
    if (CrossAbove(SMA(Fast), SMA(Slow), 1) && tradeok == 1)
    {
    EnterLong();
    }

    //Enter short
    if (CrossBelow(SMA(Fast), SMA(Slow), 1) && tradeok == 1)
    {
    EnterShort();
    }

    //Square position ahead of week-end
    if (Time[0].DayOfWeek == DayOfWeek.Friday && ToTime(Time[0]) > ToTime(18, 0, 0))
    {
    ExitLong();
    }

    if (Time[0].DayOfWeek == DayOfWeek.Friday && ToTime(Time[0]) > ToTime(18, 0, 0))
    {
    ExitShort();
    }

    if (Time[0].DayOfWeek == DayOfWeek.Sunday)
    {
    tradeok = 0;
    }

    if (Time[0].DayOfWeek == DayOfWeek.Monday && ToTime(Time[0]) < ToTime(6, 0, 0))
    {
    tradeok = 0;
    }

    if (Time[0].DayOfWeek == DayOfWeek.Friday && ToTime(Time[0]) > ToTime(18, 0, 0))
    {
    tradeok = 0;
    }

    else
    {
    tradeok = 1;
    }

    }

    #2
    I would suggest using the methods we have outlined in our sample on this topic:



    If you have any questions on how to use this filter, please let me know.
    MatthewNinjaTrader Product Management

    Comment


      #3
      Matt this doesn't help much, it's very basic, whilst i am trying to achieve something more customized, please read my post if you have time, it's well detailed and the tutorial doesn't help with this. I maybe should use some else if rather than if, but i am not sure how to scructure the code properly.

      Comment


        #4
        What exactly isn't working?
        MatthewNinjaTrader Product Management

        Comment


          #5
          Originally posted by NinjaTrader_Matthew View Post
          What exactly isn't working?
          I was filled at 1am last night running the code reported below, i think i might need to use "else if" rather than "if" in part of the code, but not sure exactly where.

          Comment


            #6
            Originally posted by sburtt View Post
            Hi All,

            I am trading a simple moving average cross over in the forex market, but I would like to limit trading to specific hours. In particular Monday to Friday 6am to 6pm, what would be the best way to apply this filter?

            Below is the code I am currently using (but I suspect I am missing something, as it is not 100% accurate):
            That will only trade from Monday 0600hrs to Friday 1800hrs. What have you seen on your charts to indicate that that is not what is happening?

            Comment


              #7
              Originally posted by koganam View Post
              That will only trade from Monday 0600hrs to Friday 1800hrs. What have you seen on your charts to indicate that that is not what is happening?
              not exactly, as you can see from the attached, last night it entered a short position at 1am in the morning (that is Monday). Would the use of "else if" (rather than "if") in some parts of the code help me resolve the issue? and moreover, would there be a better way of coding what I want to achieve?

              thanks for your time koganam
              Attached Files

              Comment


                #8
                Originally posted by sburtt View Post
                not exactly, as you can see from the attached, last night it entered a short position at 1am in the morning (that is Monday). Would the use of "else if" (rather than "if") in some parts of the code help me resolve the issue? and moreover, would there be a better way of coding what I want to achieve?

                thanks for your time koganam
                So it is doing what you coded. Your code says to trade at any and all times that lie within the continuous time slot starting at Monday 0600hrs and ending at Friday 1800hrs. That includes 1 AM on any weekday, other than Monday..

                If that is not what you intended, then first write down what you do intend, then code it.
                Last edited by koganam; 04-08-2013, 07:15 PM. Reason: Corrected spelling.

                Comment


                  #9
                  sburtt

                  You might want to learn about "else if" statement.

                  http://msdn.microsoft.com/en-us/library/vstudio/5011f09h.aspx



                  Code:
                  C#
                  // Change the values of these variables to test the results. 
                  bool Condition1 = true;
                  bool Condition2 = true;
                  bool Condition3 = true;
                  bool Condition4 = true;
                  
                  if (Condition1)
                  {
                      // Condition1 is true.
                  }
                  else if (Condition2)
                  {
                      // Condition1 is false and Condition2 is true.
                  }
                  else if (Condition3)
                  {
                      if (Condition4)
                      {
                          // Condition1 and Condition2 are false. Condition3 and Condition4 are true.
                      }
                      else
                      {
                          // Condition1, Condition2, and Condition4 are false. Condition3 is true.
                      }
                  }
                  else
                  {
                      // Condition1, Condition2, and Condition3 are false.
                  }

                  Comment


                    #10
                    Originally posted by sledge View Post
                    sburtt

                    You might want to learn about "else if" statement.

                    http://msdn.microsoft.com/en-us/library/vstudio/5011f09h.aspx



                    Code:
                    C#
                    // Change the values of these variables to test the results. 
                    bool Condition1 = true;
                    bool Condition2 = true;
                    bool Condition3 = true;
                    bool Condition4 = true;
                    
                    if (Condition1)
                    {
                        // Condition1 is true.
                    }
                    else if (Condition2)
                    {
                        // Condition1 is false and Condition2 is true.
                    }
                    else if (Condition3)
                    {
                        if (Condition4)
                        {
                            // Condition1 and Condition2 are false. Condition3 and Condition4 are true.
                        }
                        else
                        {
                            // Condition1, Condition2, and Condition4 are false. Condition3 is true.
                        }
                    }
                    else
                    {
                        // Condition1, Condition2, and Condition3 are false.
                    }
                    Thanks for the hint, i will try playing around with this, i suspect this is what i need

                    Comment


                      #11
                      Originally posted by koganam View Post
                      So it is doing what you coded. Your code says to trade at any and all times that lie within the continuous time slot starting at Monday 0600hrs and ending at Friday 1800hrs. That includes 1 AM on any weekday, other than Monday..

                      If that is not what you intended, then first write down what you do intend, then code it.
                      Koganam, that is exactly what i intended, however as i mention more than once this is not happening, as on Monday the strategy entered a position at 1am

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by sjsj2732, Yesterday, 04:31 AM
                      0 responses
                      31 views
                      0 likes
                      Last Post sjsj2732  
                      Started by NullPointStrategies, 03-13-2026, 05:17 AM
                      0 responses
                      286 views
                      0 likes
                      Last Post NullPointStrategies  
                      Started by argusthome, 03-08-2026, 10:06 AM
                      0 responses
                      283 views
                      0 likes
                      Last Post argusthome  
                      Started by NabilKhattabi, 03-06-2026, 11:18 AM
                      0 responses
                      133 views
                      1 like
                      Last Post NabilKhattabi  
                      Started by Deep42, 03-06-2026, 12:28 AM
                      0 responses
                      91 views
                      0 likes
                      Last Post Deep42
                      by Deep42
                       
                      Working...
                      X