Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Low of last 7 days

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

    Low of last 7 days

    Hi there,

    I want a very simple system, just buy when the close is the low of last 7 bars (and close is above 200SMA)
    Does anybody know what's wrong with this code?

    PHP Code:
           protected override void Initialize()
            {
                Add(SMA(200));
    
                CalculateOnBarClose = true;
            }
      protected override void OnBarUpdate()
            {
                // Condition set 1
                if (Close[0] > SMA(200)[0]
                    && Close[0] == LowestBar(DefaultInput, 7))
                {
                    EnterLong(DefaultQuantity, "long");
                } 
    

    #2
    Please try using MIN instead of LowestBar -

    Comment


      #3
      Thanks that works.

      But now I want to close that position when it is highest close of last 7 days.
      But this doesn't work:

      PHP Code:
              protected override void OnBarUpdate()
              {
                  // Open Long
                  if (Close[0] > SMA(200)[0]
                      && Close[0] == MIN(7)[0])
                  {
                      EnterLong(DefaultQuantity, "long");
                  }
      
                  // Exit Long
                  if (Close[0] == MAX(7)[0])
                  {
                      ExitLong("ExitL", "Long");
                  }
              } 
      
      I know these are very basic questions, is there a kind of course I can follow?

      Comment


        #4
        NinjaScript as C# is case sensitive, please try this Exit condition below -

        // Exit Long
        if (Close[0] == MAX(7)[0])
        {
        ExitLong("ExitL", "long");
        }

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by charlesugo_1, 05-26-2026, 05:03 PM
        0 responses
        61 views
        0 likes
        Last Post charlesugo_1  
        Started by DannyP96, 05-18-2026, 02:38 PM
        1 response
        149 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by CarlTrading, 05-11-2026, 05:56 AM
        0 responses
        162 views
        0 likes
        Last Post CarlTrading  
        Started by CarlTrading, 05-10-2026, 08:12 PM
        0 responses
        99 views
        0 likes
        Last Post CarlTrading  
        Started by Hwop38, 05-04-2026, 07:02 PM
        0 responses
        286 views
        0 likes
        Last Post Hwop38
        by Hwop38
         
        Working...
        X