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

Newbie - Help with simple programming

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

    Newbie - Help with simple programming

    I am attempting to learn how to do simple programming to create strategies. I wish to just take the sum of all prices from 200 bars ago and display them.

    I need help with:

    When I attempt to compile, I get an error message saying sumBarPrice is already defined
    Also, I would like to draw the number on the chart somewhere in the right top corner fixed... something like 'DrawTextFixed(string tag, string text, TextPosition textPosition)' but I am unsure of how to implement this this
    Any help is greatly appreciated!

    This is what I have so far using a for loop

    Code:
    namespace NinjaTrader.Indicator {
    
    public class NewStrategy101 : Indicator
    {
        #region Variables
    
        int sumBarPrice = 0;
    
        #endregion
    
        protected override void Initialize()
        {
            CalculateOnBarClose = true;
    
            for (int barsAgo = 0; barsAgo < 200; barsAgo++)
                {
                    sumBarPrice = sumBarPrice + Input[barsAgo];
                }    
        }
    
        protected override void OnBarUpdate()
        {
        }
    
        #region Properties
    
        public int sumBarPrice
        {
            get { return sumBarPrice; }
            set { sumBarPrice = Math.Max(1, value); }
        }
        #endregion
    
    }
    
    }
    Last edited by NinjaTrader_Matthew; 12-27-2012, 02:34 PM.

    #2
    I've highlighted in Red below where the error was coming from and correct it for you. You will want to ensure the Public variable for the property does not have the same namecase as the private variable. Notice how I've simply changed it from sumBarPrice to SumBarPrice

    Code:
            #region Properties
            
    [COLOR="Red"]        public int SumBarPrice[/COLOR]
            {
                get { return sumBarPrice; }
                set { sumBarPrice = Math.Max(1, value); }
            }
            #endregion
            
        }
    }
    Below is an example of how to use DrawTextFixed:

    Code:
    DrawTextFixed("myText", sumBarPrice.ToString(), TextPosition.TopRight);
    Please let me know if you have additional questions.
    MatthewNinjaTrader Product Management

    Comment


      #3
      help!

      This is what I have so far, I load the strategy but it doesn't seem to show anything on the chart I load it on. Help please! To summarize, I just want the sum of the last 200 bars of price data shown in the top right corner fixed.

      Code:
      namespace NinjaTrader.Strategy
      {
      
          [Description("Take sum of last 200 bars")]
          public class Newbie101 : Strategy
          {
              #region Variables
      
              double sumBarPrice; 
      
              #endregion
      
              protected override void Initialize()
              {            
                  for (int barsAgo = 0; barsAgo < 200; barsAgo++)
                      {
                          sumBarPrice = sumBarPrice + Input[barsAgo];
                      }
                      
                  DrawTextFixed("myText", sumBarPrice.ToString(), TextPosition.TopRight);
              }
              
              protected override void OnBarUpdate()
              {
              }
              
              #region Properties
              [Description("")]
              [GridCategory("Parameters")]
              public double SumBarPrice
              {
                  get { return sumBarPrice; }
                  set { sumBarPrice = Math.Max(1, value); }
              }
              #endregion
      
          }
      }

      Comment


        #4
        Sorry, I missed this yesterday - you want to put your logic in OnBarUpdate().

        Initialize() is only called when you first start the strategy. OnBarUpdate() will run through each historical bar on the chart and will continue to be called when each new bar is updated.
        MatthewNinjaTrader Product Management

        Comment


          #5
          I tried that but still I cannot see anything. Nothing is displayed

          Comment


            #6
            Check the log tab of the Control Center for any errors.

            You'll also want to try using Print() statements to verify that the information that is being calculated is what you expect.

            We've gathered more information on debugging your code in the thread below:



            Additionally, we have a native SUM() method to give you the summation of a data series:



            Code:
            protected override void OnBarUpdate()
            {
            sumBarPrice = SUM(200)[0];
            
             DrawTextFixed("myText", sumBarPrice.ToString(), TextPosition.TopRight);
            }
            Last edited by NinjaTrader_Matthew; 12-28-2012, 01:05 PM.
            MatthewNinjaTrader Product Management

            Comment


              #7
              This thing doesn't seem to do anything at all

              Code:
              DrawTextFixed("tag1", sumBarPrice.ToString(), TextPosition.TopRight);

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by traderqz, Today, 09:44 AM
              2 responses
              4 views
              0 likes
              Last Post NinjaTrader_Gaby  
              Started by stafe, 04-15-2024, 08:34 PM
              8 responses
              40 views
              0 likes
              Last Post stafe
              by stafe
               
              Started by rocketman7, Today, 09:41 AM
              2 responses
              7 views
              0 likes
              Last Post rocketman7  
              Started by rocketman7, Today, 02:12 AM
              7 responses
              31 views
              0 likes
              Last Post NinjaTrader_ChelseaB  
              Started by guillembm, Yesterday, 11:25 AM
              3 responses
              16 views
              0 likes
              Last Post NinjaTrader_Jesse  
              Working...
              X