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

Has anyone used .sort() on a List<> before?

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

    Has anyone used .sort() on a List<> before?

    Hello.

    I understand that this is outside the scope of NT support, so I am looking for help from someone who may understand how to use Lists properly within NT.

    So I am trying to create a strategy that will utilize a 1-minute chart as the primary series. It will get Open and Closing flags from a larger secondary series.

    Within those flags, I will be collecting some numbers using the smaller primary series data. This is to be able to see what happened inside of a larger secondary bar series.

    To simplify, I have created a basic indicator that reproduces my error. This indicator basically sets a flag at the close of the secondary Bar series, and on the next bar of the primary sets that to the Open. Then updates on each primary bar until the close of the Secondary series.

    At the close of the Secondary series is where I will store values obtained from the primary series into a List. Right now I am trying to create a PERCENTILE function and the first thing I need to be able to sort a LIST while not disturbing the original.

    The List will have a lookback, so I always delete the first value on each update to it so that it only has N values in it. I then pass the List to my function, THEN create a copy of that List so that I can sort it as to not disturb the original.

    The problem is that when I use the .Sort() function, it doesn't sort the new copied List, but rather seems to sort ALL values i have added to it. It is weird b/c I can see that the new list gets created with the proper values and will show the last N number of values. But when I apply sort to it, it suddenly shows me a sort of the highest values that where ever captured in the list? But they should be gone as I use List.RemoveAt(0) which should be removing them completely.

    I have tried this originally with an ArrayList and now a List and both seem to give me the same problems. I initially thought the problem was that an ArrayList was ALWAYS stored in memory which was the problem?

    I will attach the simplified code I created to explain the problem as well as post below. Thanks for any help!

    Code:
    #region Using declarations
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Data;
    using NinjaTrader.Gui.Chart;
    using System.Collections;  // add this declaration so you can use ArrayList()
    using System.Collections.Generic;  // add this declaration so you can use List()
    #endregion
    
    // This namespace holds all indicators and is required. Do not change it.
    namespace NinjaTrader.Indicator
    {
        /// <summary>
        /// Enter the description of your new custom indicator here
        /// </summary>
        [Description("Enter the description of your new custom indicator here")]
        public class NTforumTest : Indicator
        {
            #region Variables
    
            private int lBack = 3; // Default setting for LBack
    		
    		private bool startScript=false;  //Don't start script till first CurrentBars[1]
    		private bool getOpenTF1=false;  //Flag that new session has started on PRIMARY series
    		private int oBarTF1=0; //Will Hold Open Bar of Primary series AFTER secondary session update 
    		private int cBarTF1=0; //Will Hold Close Bar of Primary series AFTER secondary session update  
    		private double OC_TF1=0;  //Variable to store simulated OPen-Close of secondary session
    		List<double> LiOC;   //List to store Open-Close values
            #endregion
    
            /// <summary>
            /// This method is used to configure the indicator and is called once before any bar data is loaded.
            /// </summary>
            protected override void Initialize()
            {
                Overlay				= true;
    			Add(PeriodType.Minute, 1440); 	//INdex of 1 CurrentBars[1]
    			LiOC = new List<double>(); //List Script
            }
    
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
    			if(CurrentBars[0]==0)	 
    				ClearOutputWindow(); //Clear output window for debugging
    
    			
    			if (BarsInProgress ==1 )	//2nd Close
    			{	//This will signal end of secondary Close, next bar of Primary will be Open
    				DrawVerticalLine("TF1"+CurrentBar,0,Color.Red,DashStyle.Solid,1);
    				getOpenTF1=true;  startScript=true;  //sessTF1++;   
    			}
    			
    			if (BarsInProgress ==0 &&getOpenTF1 &&startScript)	{	//OpenBar using Primary Series
    				DrawDot("OpenBar"+CurrentBar,true,0,Open[0],Color.Orange);
    				oBarTF1=CurrentBar;  cBarTF1=CurrentBar;  
    				getOpenTF1=false; 
    			}
    			if (BarsInProgress ==0 &&CurrentBar-oBarTF1>0 &&startScript )	//EveryBar after OpenBar
    			{
    				cBarTF1=CurrentBar;  
    				OC_TF1=Math.Abs(Math.Round((Close[0]-Open[CurrentBar-oBarTF1])/TickSize,0));
    			}
    			
    		//------------EOD	
    			if (BarsInProgress ==1 )	//2ndary Close	
    			{
    				//DrawVerticalLine("TF1_Close"+CurrentBar,0,Color.Red,DashStyle.Solid,1);
    				LiOC.Add(OC_TF1); //List Script
    				
    				if(LiOC.Count>lBack)	{ //if Count is > lookback period
    					LiOC.RemoveAt(0);   //remove the first value so that List ONLY has the last values in lookback period
    				}
    				
    				double myTest=CalcPercentile(LiOC, 0.68);
    			}
    			
            }
    		
    		public double CalcPercentile(List<double> sequence, double excelPercentile) 
    		{
    			List<double> worker = new List<double>();  //Create Temp Array
    			worker =sequence; //Create Copy of Array... works as expected 
    //			worker.Sort();  //Sort Copy of Array.. HERE IS WHERE CODE BREAKS!!!!!!!!
    
    			foreach (double i in worker)
    				Print(Time[0]+"   "+i+"   ");   //this prints sorted array, but not last 5 values, 
    												//Instead it sorts ALL values ever added to the list	
    
    			return 0;  //Returning 0 for now 
    		}
    		
    
            #region Properties
    
            [Description("LookBack period for calcs")]
            [GridCategory("Parameters")]
            public int LBack
            {
                get { return lBack; }
                set { lBack = Math.Max(1, value); }
            }
            #endregion
        }
    }
    Attached Files
    Last edited by forrestang; 03-19-2016, 11:15 AM.

    #2
    I don't understand why this works? But I used GetRange in that function at the bottom I was working on, and it correctly picks out and puts the desire values into the new declared list.

    Code:
    		public double CalcPercentile(List<double> sequence, double excelPercentile) 
    		{
    			List<double> worker = sequence.GetRange(0,lBack);  //Create Temp Array
    		
    			worker.Sort();  //Sort Copy of Array.. HERE IS WHERE CODE BREAKS!!!!!!!! --- EDIT This works for some reason?
    
    			foreach (double i in worker)	{
    				Print(Time[0]+"   "+i+"   ");   //this prints sorted array, but not last 5 values, 
    			}									//Instead it sorts ALL values ever added to the list	
    
    			return 0;  //Returning 0 for now 
    		}

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by rocketman7, Today, 01:00 AM
    0 responses
    1 view
    0 likes
    Last Post rocketman7  
    Started by wzgy0920, 04-20-2024, 06:09 PM
    2 responses
    27 views
    0 likes
    Last Post wzgy0920  
    Started by wzgy0920, 02-22-2024, 01:11 AM
    5 responses
    32 views
    0 likes
    Last Post wzgy0920  
    Started by wzgy0920, 04-23-2024, 09:53 PM
    2 responses
    74 views
    0 likes
    Last Post wzgy0920  
    Started by Kensonprib, 04-28-2021, 10:11 AM
    5 responses
    193 views
    0 likes
    Last Post Hasadafa  
    Working...
    X