PlotForeign Using Watchlist as a Variable Parameter

Dear Team,

I am trying to code a plot wherein I can draw chart of Foreign Tickers one at a time (Read members of a watchlist) in the form of a parameter. The Watchlist is getting updated through Exploration as part of the Scheduler. So once the watchlist is updated, I wish to use it as a dropdown in the parameter and the corresponding symbol gets ploted against the symbol in the search box, with same style with an auto axis mode.

// Get your WL numeric number, OR find it 
WLNumber = 0;
// OR
// WLNumber = CategoryFind( "MyWatch List 1", categoryWatchlist );

listComma = CategoryGetSymbols( categoryWatchlist, WLNumber );
listPipe  = StrReplace( listComma, ",", "|" ); // format , to |

Fticker = ParamList( "WL Ticker", listPipe );  // param field

PlotForeign( Fticker, Fticker, colorDefault, styleOwnScale );

You should post your code effort!

1 Like

Thanks @nsm51 the code worked as expected. For plotting the base ticker have added Plot of Close.

Just a quick modification needed in the first part to make it user friendly
@nsm51

// Get your WL numeric number, OR find it 
WLNumber = 0;
// OR
// WLNumber = CategoryFind( "MyWatch List 1", categoryWatchlist );

Can we make the Watchlist Names too variable (ParamStr). Something on this sort

AllWLNamesComma = CategoryGetNames( categoryWatchlist );
AllWLNamesPipe = StrReplace( AllWLNamesComma, ",", "|" );
WLName = ParamList( "Select Watchlist", AllWLNamesPipe, "" );
WLNumber = CategoryFind( WLName, categoryWatchlist );

Any help in the above?

// from 0 to n, or use a matrix for selected lists
Total_WLS = 10;
category  = categoryWatchlist;


strWLS = "";
for( i=0; i < Total_WLS-1; ++i )
	strWLS += CategoryGetName( category, i ) + "|";
strWLS += CategoryGetName( category, Total_WLS-1 );


WLNumber = CategoryFind( ParamList( "WL Names", strWLS, 0 ), category );

// printf( "WLNumber=%0.f", WLNumber );	// test output

Thanks a lot @nsm51
How to put his ON auto refresh? Refering the attached screenshot, When I select watchlist as Param 1, I wish to see the corresponding tickers in that watchlist in Param2

I happened to prefix the code to WLNumber. How to Refresh to the new constituents of list selected in Param1

// WL Configuration

Total_WLS = 64;
category  = categoryWatchlist;


strWLS = "";
for( i=0; i < Total_WLS-1; ++i )
	strWLS += CategoryGetName( category, i ) + "|";
strWLS += CategoryGetName( category, Total_WLS-1 );


WLNumber = CategoryFind( ParamList( "WL Names", strWLS, 0 ), category );

// Get your WL numeric number, OR find it 
//WLNumber = 0;

listComma = CategoryGetSymbols( categoryWatchlist, WLNumber );
listPipe  = StrReplace( listComma, ",", "|" ); 

// Parameter Field
Fticker = ParamList( "WL Ticker", listPipe );

// --- Base Plotting Logic  ---
Plot(C,Name(), colorBlue,styleDashed | styleOwnScale); 

// 1. Get the Close price of the Foreign Ticker
ForeignClose = Foreign( Fticker, "Close" );

// 2. Calculate the Ratio
RatioPlot = Close / ForeignClose;

// 3. Plot the Foreign Ticker (Original Request)
PlotForeign( Fticker, Fticker, colorRed, styleDashed | styleOwnScale);

// 4. Plot the Ratio
Plot( RatioPlot, "Ratio " + Name() + " / " + Fticker, colorBlack, styleDots | styleOwnScale);
_SECTION_END();

Any help on this?

As far as I know, there’s no way to accomplish exactly what you’re trying to do, which is to modify the content of one dropdown list in real time based on the selection that is made in another dropdown list. The reason is that while the Parameters window is being displayed, your AFL is not being executed every time a parameter value changes. Your AFL only runs after you’ve closed the Parameters window AND you’ve taken another action that requires AmiBroker to run your code.

As a workaround, I think you could select your watchlist, exit the Parameters window, and then open it again. This would force your AFL to run, and the symbol dropdown would contain the symbols for the previously selected watchlist. That’s a little awkward from a user standpoint though.

2 Likes