Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

To write a shorter version for multiple bars closing greater than the open

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

    To write a shorter version for multiple bars closing greater than the open

    Instead of writing it like this: Close(3) > Open(3) && Close(2) > Open(2) && Close(1) > Open(1) && Close(0) > Open(0)

    Can I write it more like this?

    Close(3),2,1,0)); Similar to the slope which is where I saw it but want to apply it to the bars. Print(Slope(SMA(20), 10, 0));

    If I can, I just want to see the correct way of writing it. Thanks.


    #2
    Hi trdninstyle, thanks for posting. The only way to index the price value is Close[], with brackets. There is no short hand way to write this, you can add the code into a method so you do not need to keep writing the same code multiple times.

    Kind regards,

    -ChrisL

    Comment


      #3
      Thanks, ChrisL,

      Can you please show me a brief example? Just so I can better understand.

      Comment


        #4
        Hi trdninstyle, Im happy to help out. Please make sure to study the C# fundamentals as it will greatly help in speeding up NinjaScript development process. The Microsoft documentation is the best place for text based tutorials on the C# basics:

        https://learn.microsoft.com/en-us/do...tructs/methods (publicly available)

        One way of doing this would be:


        Code:
        public class MyCustomIndicatorClass : Indicator
        {
        
        private bool MyCustomMethod()
        {
        return (Close[0] > Open[0]);
        }
        
        protected override void OnBarUpdate()
        {
        
            if(MyCustomMethod())
            {
               //the method returned true
            }
        
        }
        ​​

        Comment


          #5
          Thank you for showing me. I will study that.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Mindset, 04-21-2026, 06:46 AM
          0 responses
          88 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by M4ndoo, 04-20-2026, 05:21 PM
          0 responses
          134 views
          0 likes
          Last Post M4ndoo
          by M4ndoo
           
          Started by M4ndoo, 04-19-2026, 05:54 PM
          0 responses
          68 views
          0 likes
          Last Post M4ndoo
          by M4ndoo
           
          Started by cmoran13, 04-16-2026, 01:02 PM
          0 responses
          119 views
          0 likes
          Last Post cmoran13  
          Started by PaulMohn, 04-10-2026, 11:11 AM
          0 responses
          69 views
          0 likes
          Last Post PaulMohn  
          Working...
          X