Query Symbol quotes using dynamically formed Tickers

Hello Experts,

Understandably, it is ideal to use SetForeign or Foreign functions while referencing other symbol data when the Ticker is known beforehand. However, the issue differs in my case.

To keep it short, based on some conditions my code dynamically create strings that resembles actual symbols present in the same database. What I intend is to query those symbols bar-by-bar (since the string value might change anytime) and capture its "Close" dynamically in order to form a single array.

Here is a commented pseudo-snippet:

// Close of current Symbol on Chart i.e. 'Close' of current Name()
CurrClose = C;

Typ = IIf( CurrClose < AnotherArray, 1, IIf( CurrClose > AnotherArray, 0, 1 ) );
StrSymbol = Name() + WriteIf( Typ, "-I", "-II" ); // In its simplest form
// StrSymbol will have different value bar-by-bar and the conditions are such that I cannot fixate them using Params either

// Can I use Foreign or SetForeign?
MyArray = Foreign( StrSymbol, "C", 1 );
// Does not work here as the Plot of MyArray changes based on Bar Selection
Plot( MyArray, "MyArray", colorWhite );

// Is there a way such that MyArray would hold 'Close' of 'StrSymbol' being independent of the Selection

Not sure whether this is already covered or even possible.

Thank you

e.g.

CurrClose = C;

AnotherArray = MA(C, 50);

nm = Name();

sym1 = Foreign( nm + "-I", "C", 1 );
sym2 = Foreign( nm + "-II", "C", 1 );

MyArray = IIf( CurrClose < AnotherArray, sym1, sym2 );

Plot( MyArray, "MyArray", colorWhite );
3 Likes

Thank you @fxshrat for your quick reply, :slightly_smiling_face:

Problem is that I do not have the liberty to use sym1 or sym2. All symbol queries are dynamic, i.e. I do not know how many symbols are to be called, which are to be called beforehand.

The dynamic string symbol used for the query is more complex with dates and other things incorporated with it!

@Cougar you cannot have an array of strings, i.e. a string that changes on a bar-by-bar basis. Therefore, you have to use an approach similar to what @fxshrat already provided for you: first determine the different ticker symbols, second retrieve all data for those ticker symbols, and third combine the data into a single array.

1 Like

Are you referring to Futures or Options contracts? If that is the case, you would probably need to extract the "Root" symbol. My experience is that you have to do this by reading characters of a symbol from right-most to left-most. Then you can search all symbols that have that as the root symbol.

2 Likes

If you are using Norgate Data for futures, the following will extract the underlying symbol, expiry month and year.

//  Norgate Data - exctracting futures data 
//  Ex. DC-2021H
undr = StrExtract(Name(),0,'-');  // extracts the root symbol of the underlying
yr = StrMid( Name(), 3, 4 );  // extracts the year
mnth = StrRight(Name(),1);  // extracts the month```