Alternative to dynamic variable name in C #

I write my custom indicators in NinjaTrader, which has a scripting language built in C #. I would like to share data between different stock charts, but there is no inherent way for this. Each indicator is inherited from the Indicator class, and, of course, each chart launches a unique instance of any applied indicators.

For example, I would like to be able to β€œsend” the current IBM price to an AAPL chart. Conceptually, on the β€œsend” diagram, I need to do something like:

static double IBM = 190.72;

however, when the user changes the chart ticket from IBM to DELL, for example, I now need something like:

static double DELL = 9.25;

On my "receiving" chart, I would like to do something like Print (DELL);

So, my tendency is to have a variable name that is assigned dynamically based on the ticker symbol that the user has selected for the chart, however, I know that this is not possible in C #. So, what is a good alternative approach to storing and retrieving data that needs to be indexed by a ticker when there is an almost unlimited set of potential ticker values?

+4
source share
2 answers

Why not use something like a dictionary? eg:.

var stocks = new Dictionary<string, double>(); stocks.Add("appl", 1234.56); Print(stocks["appl"]); 

You can dynamically add ticker names and values ​​to them as needed, give you a ticker search and a number of other useful functions. Any reason you need separate variables, not a collection?

+7
source

Create Object:

 public class Stock { public string Ticker{get; set;} public double Price {get; set;} } 

Then list all possible stocks in the list:

  List<Stock> allStocks = new List<Stock>(); allStocks.Add(new Stock() { Ticker = "Dell", Price = 9.25 }); allStocks.ForEach(stock => Print(string.Format("{0} : {1}", stock.Ticker, stock.Price) ); 
0
source

Source: https://habr.com/ru/post/1441909/


All Articles