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 andrewtrades, Today, 04:57 PM
          1 response
          5 views
          0 likes
          Last Post NinjaTrader_Manfred  
          Started by chbruno, Today, 04:10 PM
          0 responses
          3 views
          0 likes
          Last Post chbruno
          by chbruno
           
          Started by josh18955, 03-25-2023, 11:16 AM
          6 responses
          436 views
          0 likes
          Last Post Delerium  
          Started by FAQtrader, Today, 03:35 PM
          0 responses
          7 views
          0 likes
          Last Post FAQtrader  
          Started by rocketman7, Today, 09:41 AM
          5 responses
          19 views
          0 likes
          Last Post NinjaTrader_Jesse  
          Working...
          X