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 CarlTrading, 03-31-2026, 09:41 PM
        1 response
        67 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by CarlTrading, 04-01-2026, 02:41 AM
        0 responses
        36 views
        0 likes
        Last Post CarlTrading  
        Started by CaptainJack, 03-31-2026, 11:44 PM
        0 responses
        59 views
        1 like
        Last Post CaptainJack  
        Started by CarlTrading, 03-30-2026, 11:51 AM
        0 responses
        62 views
        0 likes
        Last Post CarlTrading  
        Started by CarlTrading, 03-30-2026, 11:48 AM
        0 responses
        53 views
        0 likes
        Last Post CarlTrading  
        Working...
        X