AFL to clean and update watchlist from text files

I know how to create a watchlist based on another watchlist and also I know how to create text files from a watchlist but I don't know (not much programming experience) how to update a watchlist with symbols available in text files on the hard drive (for example in C:\data there are files like aapl.txt, amzn.txt, nflx.txt, etc.) but the files may change daily and I want the watchlist updated based on new ones only.)

Any help will be appreciated. I search this forum and I could not find the answer. Maybe I missed it.

This is the code I use for cleaning watchlists:

listnum = listnumber;

if ( Status( "stocknum" ) == 0 )
{
    oldlist = CategoryGetSymbols( categoryWatchlist, listnum );
    for ( i = 0; ( sym = StrExtract( oldlist, i ) ) != ""; i++ )
    {
        CategoryRemoveSymbol( sym, categoryWatchlist, listnum );
    }
}

what I miss is the code for import symbols from text files on hard drive after cleaning the watchlist.

1 Like

I tried a few different things but did not work. I need to important text files from hard drive to a watchlist after cleaning it. The text files have the symbol name and extension .txt, e.g. AAPL.txt
I need to do this daily because the files on hard drive change. Thanks in advance.

I meant "I need to import text (.txt) files from hard drive to a watchlist". Not good with programming but someone told me it cannot be done.

See Working with watch lists.

2 Likes

I know how to import any files manually to a watchlist. I have been doing that all along. I am looking for code that will do that automatically that is not available in that link.

Hi @makhar88,

AFL has File operations. From your description, I think fdir might be what you need.

Use fdir with CategoryAddSymbol, then create Batch to clear symbols from x watchlist (using batch 'Clear watch list' command) and to run script to populate x watchlist.

Watch lists ARE text files, so you don't actually need to import anything, because they are already in plain text format. Read carefully again https://www.amibroker.com/guide/h_watchlist.html

Note that watch lists files (those inside "Watchlists" subfolder) are read ONCE when you open database and saved ONCE when you close the database (or exit program). During all time in between they are kept in RAM.

2 Likes

Getting there I hope. This is my code:

listnum = 10;

    oldlist = CategoryGetSymbols( categoryWatchlist, listnum );
    for ( i = 0; ( sym = StrExtract( oldlist, i ) ) != ""; i++ )
    {
        CategoryRemoveSymbol( sym, categoryWatchlist, listnum );
    }

_N( list = fdir( "c:\\DATA\\ETF\\*.txt" , 1 ) );

for ( i = 0; ( filename = StrExtract( List, i ) ) != ""; i++ )
{
  CategoryAddSymbol( filename, categoryWatchlist, listnum ); 
   printf( filename + " " );
}

Two issues:

  1. The watchlist 10 is cleaned but it does not refresh. Is there a command to refresh watchlist (maybe close the file)?

  2. Although I get the correct files in the interpretation window, watchlist is not populated. Maybe there is some command to close the file and reopen before populating.

BTW I run this from Apply in AFL Formula Editor. Is there another way to run it which is faster?

Thank you.

@makhar88, did you not see this!

Yes, so what is the problem? I am importing text files. Does the specific text format (delimiters, etc.) play a role?

Re-read this statement.

@makhar88,

Is your "CategoryAddSymbol" grabbing the extension (.txt or .csv) of the file as well? Should it just be the symbol?
Also, would you need to shut down and restart AmiBroker to recognize the changes you made in the Watchlist?

Thanks to you and Tomasz I have solved part of problem. Files on hard drive have .txt extension and I eliminate that with script before I import to watchlist.

listnum = 10;

    oldlist = CategoryGetSymbols( categoryWatchlist, listnum );
    for ( i = 0; ( sym = StrExtract( oldlist, i ) ) != ""; i++ )
    {
        CategoryRemoveSymbol( sym, categoryWatchlist, listnum );
    }

_N( list = fdir( "c:\\DATA\\ETF\\*.*" , 1 ) );

for ( i = 0; ( filename = StrExtract( List, i ) ) != ""; i++ )
{
  CategoryAddSymbol( filename, categoryWatchlist, listnum ); 
   printf( filename + " " );
}

But now I have a more serious problem. In the watchlist I import from hard drive the symbols have no dividend adjustments, i.e., I need the data without dividend adjustment and only splits where it applies.

But since those same symbols already exist in the database as dividend adjusted, the new watchlist reverts back to them.

I tried to import the file to the watchlist with a modified name but it did not work. Probably they must be added to database first but I cannot find a way or function that does that.

listnum = 10;

    oldlist = CategoryGetSymbols( categoryWatchlist, listnum );
    for ( i = 0; ( sym = StrExtract( oldlist, i ) ) != ""; i++ )
    {
        CategoryRemoveSymbol( sym, categoryWatchlist, listnum );
    }

_N( list = fdir( "c:\\DATA\\ETF\\*.*" , 1 ) );

for ( i = 0; ( filename = StrExtract( List, i ) ) != ""; i++ )
{
  CategoryAddSymbol( filename+"-div", categoryWatchlist, listnum ); 
   printf( filename + " " );
}

Thanks. To me is big step because I am not programming expert.

1 Like

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.