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

Keep track of prices

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

    Keep track of prices

    Hi,

    As part of the script, I would like to keep track of the prices every time that the indicator is triggered. For example with two moving averages, keep track of the price at the time that one MA crosses above the other MA and then also at the time that it crosses below.
    How do I go about to accomplish it?

    Thanks in advance for any input that you can provide.

    #2
    Hello 2Look4me,

    Thank you for your note.

    If you only need the most recent cross level store the value of the Close in a double when the cross occurs.
    For example:
    Code:
    if (CrossAbove(SMA(fast), SMA(slow), 1))
    {
    crossAboveDouble = Close[0];
    }
    If you want to hold historical values as well you may wish to use a DataSeries or an Array depending on your needs.

    Comment


      #3
      Hi Patrick,

      Thanks for the reply. I don't think I need to hold historical values, but based on your example, how would I keep track of the MA crossovers (Cumulative/Summation). For instance, the first MA crossover above is at 50, then crosses below at 52; the following crossover above is at 54 then crosses below at 55 and on...

      I would like the Sum of the first set of MA crossovers (52 -50) + the second set of crossovers (55 - 54) and on and on...

      Thanks again

      Comment


        #4
        Hello 2Look4me,

        Thank you for your response.

        You could instead use a List and then add each time a cross occurs. Then loop through the full count to perform whatever calculations needed. Below is an example of adding to the list during the crosses:
        Code:
                #region Variables
                List<int> myList;
                #endregion
        
                protected override void Initialize()
                {
        			myList = new List<int>();
                    Overlay				= true;
                }
        
                protected override void OnBarUpdate()
                {
                    if (CrossAbove(SMA(10), SMA(20), 1))
        			{
        				myList.Add(SMA(10)[0]);
        			}
        			if (CrossBelow(SMA(10), SMA(20), 1))
        			{
        				myList.Add(SMA(10)[0]);
        			}
                }
        For information on List please visit the following link: https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx
        For information on for loops (to loop through the list) please visit the following link: https://msdn.microsoft.com/en-us/library/ch45axte.aspx

        Comment


          #5
          Correction to my code. Should be a double list and not an int:
          Code:
                  #region Variables
                  List<double> myList;
                  #endregion
          
                  protected override void Initialize()
                  {
          			myList = new List<double>();
                      Overlay				= true;
                  }
          
                  protected override void OnBarUpdate()
                  {
                      if (CrossAbove(SMA(10), SMA(20), 1))
          			{
          				myList.Add(SMA(10)[0]);
          			}
          			if (CrossBelow(SMA(10), SMA(20), 1))
          			{
          				myList.Add(SMA(10)[0]);
          			}
                  }
          Originally posted by NinjaTrader_PatrickH View Post
          Hello 2Look4me,

          Thank you for your response.

          You could instead use a List and then add each time a cross occurs. Then loop through the full count to perform whatever calculations needed. Below is an example of adding to the list during the crosses:
          Code:
                  #region Variables
                  List<int> myList;
                  #endregion
          
                  protected override void Initialize()
                  {
          			myList = new List<int>();
                      Overlay				= true;
                  }
          
                  protected override void OnBarUpdate()
                  {
                      if (CrossAbove(SMA(10), SMA(20), 1))
          			{
          				myList.Add(SMA(10)[0]);
          			}
          			if (CrossBelow(SMA(10), SMA(20), 1))
          			{
          				myList.Add(SMA(10)[0]);
          			}
                  }
          For information on List please visit the following link: https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx
          For information on for loops (to loop through the list) please visit the following link: https://msdn.microsoft.com/en-us/library/ch45axte.aspx

          Comment


            #6
            I added the following to the script:
            Code:
            #region Using declarations
            using System.Collections.Generic;
            #endregion
            
            #region Variables
            private double   Entry  = 0;
            private double   Exit     = 0;
            List<double>myList;
            #endregion
            
            protected override void Initialize()
            {
                myList=new List<double>();
            }
            
            protected override void OnBarUpdate()
            {
                if(CrossAbove(SMA(10),SMA(20),1))
                    {
                        Entry = Close[0];
                        Exit = 0;
                    }
                else if(CrossBelow(SMA(10),SMA(20),1))
                    {
                        Exit = Close[0];
                    }
                if (Entry !=0 && Exit !=0)
                    {
                        myList.Add(Exit - Entry);
                        Entry = 0;
                        Exit = 0;
                    }
                Print ("Entry =" + Entry);
                Print ("Exit ="  + Exit);
                Print ("myList=" +myList);
            }
            Why the output window shows myList = System.Collections.GenericList1[System.Dougle] instead of the calculation?
            How to go about plotting myList ?

            Comment


              #7
              Hello 2Look4me,

              Thank you for your post.

              You need to call the index of the List. Just calling the list by name is not enough, you need to call the item from the list.

              For example: myList[0]

              Comment


                #8
                Patrick, thanks for the reply.
                By making the change:
                Code:
                Print("myList=" + myList[0]);
                I'm now getting the error: "You are accessing an index with a value that is invalid since its out of range......with a value of 5 when there are only 4 bars on the chart"

                I tried by adding
                Code:
                if(CurrentBar < 10)
                return;
                but still getting a similar error.

                Comment


                  #9
                  When and where are you calling myList[0]? Is this before it is defined?
                  Try myList[1] as well.

                  Comment


                    #10
                    I have myList defined as
                    Code:
                    #region Variables
                    List<double>myList;
                    #endregion
                    
                    protected override void Initialize()
                    myList=new List<double>();
                    
                    protected override void OnBarUpdate()
                    if(CurrentBar)<10
                    return;
                    
                    if(conditions)
                    {
                       myList.Add(Exit - Entry);
                    }
                    
                    Print("myList =" + myList[0]);
                    With either myList[0] or myList[1], the output window and the log, will still give me the same error message: "You are accessing an index with a value that is invalid since its out of range......with a value of 5 when there are only 4 bars on the chart"...
                    Thanks

                    Comment


                      #11
                      You have it set up to access the value even if it does not exist.
                      Code:
                      if(conditions)
                      {
                         myList.Add(Exit - Entry);
                      }
                      
                      Print("myList =" + myList[0]);
                      This should actually be this:
                      Code:
                      if(conditions)
                      {
                         myList.Add(Exit - Entry);
                      }
                      
                      if (myList.Count > 1)
                      Print("myList =" + myList[0]);
                      I highly recommend that you review the details on List: https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by Jon17, Today, 04:33 PM
                      0 responses
                      1 view
                      0 likes
                      Last Post Jon17
                      by Jon17
                       
                      Started by Javierw.ok, Today, 04:12 PM
                      0 responses
                      4 views
                      0 likes
                      Last Post Javierw.ok  
                      Started by timmbbo, Today, 08:59 AM
                      2 responses
                      10 views
                      0 likes
                      Last Post bltdavid  
                      Started by alifarahani, Today, 09:40 AM
                      6 responses
                      40 views
                      0 likes
                      Last Post alifarahani  
                      Started by Waxavi, Today, 02:10 AM
                      1 response
                      19 views
                      0 likes
                      Last Post NinjaTrader_LuisH  
                      Working...
                      X