Accessing internet

The lot size for futures are periodically reviewed on NSE India and a csv file is put on the website. Using AFL code how to download a csv file from internet and save it on, say, desktop?
the function InternetOpenURL (’‘http://url_to_your_web_resource’’) connects to net. But how to download a file located at the said url?
for example i want to download a csv file which is on the following link:- “https://www.nseindia.com/content/fo/fo_mktlots.csv”.

Further, with csv file opened using fopen(file location,“r”) , you will find that the symbols are located in the second column and the lotsizes of current month are in the third column. How to search a particular symbol (say DLF from column 2) and get its lotsize from column 3 and assign it to a variable say nLotsize?.

Please help.

It can be done using InternetOpenURL(), InterenetReadString(), [InternetClose()]((http://www.amibroker.com/f?internetclose). Everything is in the manual. But there are better ways to download files from internet to the file on your hard disk. Google for wget.

Thank you for your reply.
sir, I had referred the manual and tried the functions InternetOpenURL(), InterenetReadString(), InternetClose(). Are there any parameters for the function InternetOpenURL(), so that i give destination for saving the file.

Further, any help regarding:
reading a symbol from at column 2 in the csv file (not internet) after downloading , matching it with Name() and then retrieving the lotsize of particular symbol from column 3 (only if matched)

@rvdesh here you are happy day

the code reads from the Net and saves a new file after keeping only the ticker of the 2nd column and the lots of the 3rd column

// @link http://forum.amibroker.com/t/accessing-internet/701
PathToSave	= "C:\\";
FileName = "test.txt";
fo_mktlots = "https://www.nseindia.com/content/fo/fo_mktlots.csv";
PressToDownload = ParamTrigger( "Press to Start Download", "Press Once" );

if( PressToDownload )
{
    ih = InternetOpenURL( fo_mktlots );
    fhLots = fopen( PathToSave + FileName, "a" );

    if( ih && fhLots )
    {
        while( ( str = InternetReadString( ih ) ) != "" )
        {
            Symbol = StrExtract( str , 1 );

            if( StrFind( Symbol, "DLF" ) )  // if ticker found do something
            {
                printf( "The DLF Fund " );
                lots = StrExtract( str , 2 );  	// Extract the 3rd column
                printf( "lotsizes " + lots );
                line = Symbol + "," + lots;

                fputs( line, fhLots );	// save the Clean data file txt
            }

        }

        if( ih ) InternetClose( ih );

        if( fhLots ) fclose( fhLots );
    }
}
9 Likes

Thank you very much friend.
copied and made changes (expected) for file location.
Nothing is happening from PressToDownload onwards…
still struggling…(beginners have very basic challenges tooo!!)

Opps. sorry i did type that
you MUST change the “PathToSave and FileName” for file location

PathToSave	= "R:\\temp\\"; 
FileName = "test.txt";

and the code is runing fine - and tested

I changed drive letter to C: in the original code because otherwise we would have dozens of “does not work” replies here.

A note to beginners: do NOT blindly copy paste the codes. You need to read the code, line by line and try to understand each statement. Lookup the users guide.

The code that @PanoS provided is great coding example given to you for free. Appreciate that and learn from it. Do NOT be copy-paster. Be intelligent and try to understand what you are doing.

If you are beginner consider starting with simpler things.

4 Likes

Surely, thanked him (@PanoS) already and you too for this resourceful forum.
Regards

1 Like