0

I use the example Plugin.cpp from amibroker ADK. I make a directory 'ASCII' under the folder 'amibroker' and put all the data inside named *.AQI. But no data found in amibroker. Is there something I changed in function GetQuotesEx cause the problem?

PLUGINAPI int GetQuotesEx( LPCTSTR pszTicker, int nPeriodicity, int nLastValid, int nSize, struct Quotation *pQuotes, GQEContext *pContext  )
{
    char filename[256];
    FILE* fh;
    int  iLines = 0;

    // format path to the file (we are using relative path)
    sprintf_s(filename, "ASCII\\%s.AQI", pszTicker);

    // open file for reading
    fopen_s(&fh, filename, "r");

    // if file is successfully opened read it and fill quotation array
    if (fh)
    {
        char line[ 256 ];

        // read the line of text until the end of text
        // but not more than array size provided by AmiBroker
        while( fgets( line, sizeof( line ), fh ) && iLines < nSize )
        {
            // get array entry
            struct Quotation *qt = &pQuotes[ iLines ];
            char* pTmp = NULL;

            // parse line contents: divide tokens separated by comma (strtok) and interpret values

            // date and time first
            int datenum = atoi( strtok_s( line, ",", &pTmp) );  // YYMMDD
            int timenum = atoi( strtok_s( NULL, ",", &pTmp) );  // HHMM

            // unpack datenum and timenum and store date/time 
            qt->DateTime.Date = 0; // make sure that date structure is intialized with zero
            qt->DateTime.PackDate.Minute = timenum % 100;
            qt->DateTime.PackDate.Hour = timenum / 100;
            qt->DateTime.PackDate.Year = 2000 + datenum / 10000;
            qt->DateTime.PackDate.Month = ( datenum / 100 ) % 100;
            qt->DateTime.PackDate.Day = datenum % 100;

            // now OHLC price fields
            qt->Open = (float) atof( strtok_s( NULL, ",", &pTmp) );
            qt->High = (float) atof( strtok_s( NULL, ",", &pTmp) );
            qt->Low  = (float) atof( strtok_s( NULL, ",", &pTmp) );
            qt->Price = (float) atof( strtok_s( NULL, ",", &pTmp) ); // close price

            // ... and Volume
            qt->Volume = (float) atof( strtok_s( NULL, ",\n", &pTmp) );

            iLines++;
        }

        // close the file once we are done
        fclose( fh );

    }

    // return number of lines read which is equal to
    // number of quotes
    return iLines;   
}
  • 1
    You aren't checking the return code of`fopen_s` for success, so if that failed the whole thing would silently fail. Perhaps add some logging in the case of failure? – Jeff Foster Nov 01 '19 at 06:58
  • Did the example plugin work before you modified it? In that case, retrace your steps to find out which caused the problem. As a new user here, also take the [tour] and read [ask]. – Ulrich Eckhardt Nov 01 '19 at 07:09
  • Amplifying Jeff's comment, your code assumes a failure on `fopen_s` to open the file will hard-set the `FILE *fh` to NULL. No implementation nor documentation there-upon I've ever seen dictates that behavior. What they *all* dictate is what Jff said, an example from MS: *"Always check the return value to see if the function succeeded before you perform any further operations on the file. If an error occurs, the error code is returned and the global variable errno is set. "* The behavior your code is assuming (setting `fh` to null) is not specified whatsoever. – WhozCraig Nov 01 '19 at 07:15
  • Your code looks rather C-ish than C plus plussy. – nada Nov 01 '19 at 07:28
  • I have copied this function and run it separately. It works and print out data correctly. – Louis Wan Nov 01 '19 at 07:51
  • Try an absolute path in `fopen_s` typical issue is that the working directory is not what you think it is so the relative path does not work. Also note that the current working directory is per-process and can be changed by other (not in this case your) code in the process (3rd party shell plugins that interact with the open file dialog for example) so relative paths are issue prone. – Richard Critten Nov 01 '19 at 09:42

0 Answers0