NexusFi: Find Your Edge


Home Menu

 





Calling values from Dictionary/List


Discussion in NinjaTrader

Updated
    1. trending_up 797 views
    2. thumb_up 1 thanks given
    3. group 2 followers
    1. forum 3 posts
    2. attach_file 0 attachments




 
Search this Thread

Calling values from Dictionary/List

  #1 (permalink)
DirtyGoats
Lakeville
 
Posts: 11 since Dec 2017
Thanks Given: 3
Thanks Received: 1

Good day and appreciate your time in reading my issue currently faced and helping me learn.

I've been attempting to learn C# and the learnings I'm coming across are not translating into NinjaTrader.

I have the below snippet of sample code that is working as expected. It creates a Dictionary with multiple values per line.

The output I get is:

1 3956 valueA valueB
2 3955.50 valueA valueB
3 3955.25 valueA valueB
4 3955.75 valueA valueB
5 3955 valueA valueB

What i'm having a hard time achieving is pulling a specific valueA or valueB from a specific index/price.

I'm also not sure if my list needs to be sorted by price...and if yes...i'm not sure how to accomplish that either.

My ask: Can someone help me code the "how to pull a specific valueA or valueB from a specific index/price. And how to properly sort my Dictionary/List if needed.

Thank you!

 
Code
public class Dictionary_Test : Indicator
	{
		private Series<Dictionary<double, List<double>>> listValues;
		
		protected override void OnStateChange()
		{
			if (State == State.SetDefaults)
			{
				Description = @"Enter the description for your new custom Indicator here.";
				Name = "Dictionary_Test";
				Calculate = Calculate.OnEachTick;
				IsOverlay = false;
				DisplayInDataBox = true;
				DrawOnPricePanel = true;
				DrawHorizontalGridLines = true;
				DrawVerticalGridLines = true;
				PaintPriceMarkers = true;
				ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
				IsSuspendedWhileInactive = true;
			}
			else if (State == State.DataLoaded)
			{
				listValues = new Series<Dictionary<double, List<double>>>(this);
				
			}
		}
		
		protected override void OnBarUpdate()
		{
			double valueA 			= //some calculation ;
			double valueB 			= //some calculation ;

			if(IsFirstTickOfBar)
			{
				// only create our dictionary once per bar
				listValues[0] = new Dictionary<double, List<double>>();
			}

			if(listValues[0].ContainsKey(Close[0]))
			{
			// update valueA/valueB for price if we've already recorded something at that price
			listValues[0][Close[0]] = new List<double> { valueA, valueB };
			}
			else
			{
			// if we don't have that price as a key, add it and its valueA/valueB volume
			listValues[0].Add(Close[0], new List<double> {valueA, valueB });
			}
			
			
			int i = 1;
			foreach(var keyValue in listValues[0])
			{
				Print( i++);
				var key = keyValue.Key;
				var value = keyValue.Value;
				Print(key +" - "+value[0]+" - "+value[1]);
			}

		
		}

Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Better Renko Gaps
The Elite Circle
ZombieSqueeze
Platforms and Indicators
MC PL editor upgrade
MultiCharts
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
How to apply profiles
Traders Hideout
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Just another trading journal: PA, Wyckoff & Trends
33 thanks
Tao te Trade: way of the WLD
24 thanks
My NQ Trading Journal
14 thanks
HumbleTraders next chapter
11 thanks
GFIs1 1 DAX trade per day journal
11 thanks
  #2 (permalink)
 
sam028's Avatar
 sam028 
Site Moderator
 
Posts: 3,765 since Jun 2009
Thanks Given: 3,825
Thanks Received: 4,629

A Series of dictionary? I don't think this is what you want.
If I understand correctly you should create a <double, double, double> object, and create a List for it.

A Dictionary doesn't allow duplicate keys.

Success requires no deodorant! (Sun Tzu)
Follow me on Twitter Reply With Quote
  #3 (permalink)
DirtyGoats
Lakeville
 
Posts: 11 since Dec 2017
Thanks Given: 3
Thanks Received: 1


Appreciate the response. The results of the provided code do not create duplicate keys.


Again, the output i get is:

1 3956 valueA valueB
2 3955.50 valueA valueB
3 3955.25 valueA valueB
4 3955.75 valueA valueB
5 3955 valueA valueB



My ask: Can someone help me code the "how to pull a specific valueA or valueB from a specific index/price. And how to properly sort my Dictionary/List if needed.

Reply With Quote
  #4 (permalink)
 
sam028's Avatar
 sam028 
Site Moderator
 
Posts: 3,765 since Jun 2009
Thanks Given: 3,825
Thanks Received: 4,629


DirtyGoats View Post
Appreciate the response. The results of the provided code do not create duplicate keys.


Again, the output i get is:

1 3956 valueA valueB
2 3955.50 valueA valueB
3 3955.25 valueA valueB
4 3955.75 valueA valueB
5 3955 valueA valueB



My ask: Can someone help me code the "how to pull a specific valueA or valueB from a specific index/price. And how to properly sort my Dictionary/List if needed.

Very quick & dirty:
 
Code
                            
namespace NinjaTrader.NinjaScript.Indicators {

    public class 
myData    {
        public 
double A getset; }
        public 
double B getset; }
        public 
myData(double adouble b)
        {
            
this.a;
            
this.b;
        }
    }
    
 public class 
DictionaryTest Indicator    {
    private 
Dictionary<double,myDatamyDict;
        
    protected 
override void OnStateChange() {
       if (
State == State.SetDefaults)
       {
                ...
       }
       else if (
State == State.DataLoaded)       {
                
myDict = new Dictionary<doublemyData>();
        }
       }

       protected 
override void OnBarUpdate() {
          if (
myDict.ContainsKey(Close[0]))
          
myDict[Close[0]] = new myData(1.02.0);
          else
          
myDict.Add(Close[0], new myData(5.06.0));
          }
    }

Then you can pull a specific item like this:
 
Code
                            
Print(myDict[3956].A+" "+myDict[3956].B); 

No need to sort a dictionary.

Success requires no deodorant! (Sun Tzu)
Follow me on Twitter Reply With Quote
Thanked by:




Last Updated on November 26, 2022


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts