Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

How to calc Avg(SMA_L -SMA_S)[from period the session start's]

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

    How to calc Avg(SMA_L -SMA_S)[from period the session start's]

    Please if some one nows:

    How to calculate the arithmetic mean of the difference of two SMA since the beginning of the session.
    I need it for recognizing flat trand

    Avg(SMA_L -SMA_S)[from period the session start's]

    #2
    Hello iskip,

    Thank you for writing in.

    We'll need to manually calculate the mean.

    As an example, I have created a sample here that will plot the mean close price since the beginning of the session:

    Code:
    private double sum = 0;
    private double numberOfBars = 0;
    
    protected override void Initialize()
    {
         Add(new Plot(Color.Blue, "Average"));
    }
    
    protected override void OnBarUpdate()
    {
         // if it's the first bar of session, reset our values
         if (Bars.FirstBarOfSession)
         {
              sum = 0;
              numberOfBars = 0;
         }
    
         // add the current close value to the sum
         sum += Close[0];
         // increment numberOfBars by one as one bar has elapsed
         numberOfBars++;
         // divide the sum by the numberOfBars to find the mean and plot the current mean value
         Value.Set(sum / numberOfBars);
    }
    In order to calculate the mean of the difference between two indicators, you'll need to modify the logic above to obtain the value of the difference between the two indicator outputs, but it should provide a base as to how you can plot the mean.

    Please, let us know if we may be of further assistance.
    Zachary G.NinjaTrader Customer Service

    Comment


      #3
      Tnx a lot. When i will make code with your base example and i will show it here.

      Comment


        #4
        Originally posted by NinjaTrader_ZacharyG View Post

        Code:
        protected override void Initialize()
        {
             Add(new Plot(Color.Blue, "Average"));
        }
        For this line he said: Error CS1503
        Do not get what he wants.
        Help please

        Comment


          #5
          Hello iskip,

          Are you placing that syntax in an indicator or a strategy?

          My example is for an indicator. You cannot add plots in a strategy.
          Zachary G.NinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by llanqui, Today, 03:53 AM
          0 responses
          6 views
          0 likes
          Last Post llanqui
          by llanqui
           
          Started by burtoninlondon, Today, 12:38 AM
          0 responses
          10 views
          0 likes
          Last Post burtoninlondon  
          Started by AaronKoRn, Yesterday, 09:49 PM
          0 responses
          15 views
          0 likes
          Last Post AaronKoRn  
          Started by carnitron, Yesterday, 08:42 PM
          0 responses
          11 views
          0 likes
          Last Post carnitron  
          Started by strategist007, Yesterday, 07:51 PM
          0 responses
          14 views
          0 likes
          Last Post strategist007  
          Working...
          X