[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[amibroker] Re: Code needed - fopen fget - use csv file as input ?



PureBytes Links

Trading Reference Links



Hi,

I took a look at my original post (now that I have AmiBroker in front of me). I mistakenly reversed the arguments to StrFind, which you have corrected. I also indicated in my notes that the code would work for multiline csv files, but did not actually include any handling for the newline characters.

Whereas my original code worked for a single line of multiple IDs separated by commas, but not multiple lines. Your proposed revision works for multiple lines of single entries but not for lines with multiple IDs.

If you plan to have a single ID per line in your file, then you can get rid of the whole building up of the ids string altogeather. The only reason for prepending the initial "," concatenating the results and appending the final ","  was to be able to do a generic StrFind using comma delimiters. e.g. given "10,5,24" would end up with ",10,5,24," and could generically search for ",<any value>,".

Below is a corrected version of my original that passes the StrFind arguments in the right order and has been corrected to strip any newline characters found. Note that the last line of a file might not have a newline. This version delivers on my original claim to handle any number of comma seperated values on each of any number of lines.

function IncludeIndustry( id )
{
    
local
ids;
    
local
fh;
    
local
line;

    ids =
","
;
    fh =
fopen( "C:\\temp\\filter.csv", "r"
);

    
if
( fh )
    {
        
while ( !feof
( fh ) )
        {
            line =
fgets
( fh );

            
if ( StrFind( line, "\n"
) )
            {
                line =
StrLeft( line, StrLen( line ) - 1
);
            }

            ids += line;
            ids +=
","
;
        }
    }

   
_TRACE
(ids);
    
return StrFind( ids, "," + id + ","
);
}

Filter = IncludeIndustry( IndustryID
() );

AddColumn( IndustryID(), "Industry"
);

A couple of additional notes on your last submission:

  • Your usage of Match = IIf(RID > 0,1,0); is redundent. Filter will accept any non zero value. Just set Filter = RID. Better yet, Filter = IncludeIndustry(IndustryID()) as per the original code, unless you specifically wanted to keep the return value for something. Either way, get rid of Match.
  • Try to avoid calling the same function repeatedly. You've already stored the value of IndustryID() in IID, so just use IID from that point on instead of calling IndustryID() again.
  • Your assumption that ever line in the file will end with a "\n" is false. The last line might not.
  • ids is declared as local in the function, therefore it is not available to your trace statements in the main script. The IDS declaration that you added above the function is not the same variable as what is used inside the function.

Just for kicks, if you wanted to only ever run the file reading once per AmiBroker session, you could use a static variable to hang on to the values read and skip the redundent reading on any subsequent calls. This would not be a good idea if you plan to modify the .csv file while AmiBroker is still running. But, it will speed up your exploration if you don't plan to change the .csv file.

function GetIndustries()
{
    
local
ids;
    
local
fh;
    
local
line;

    ids =
StaticVarGetText( "StaticIDs"
);

    
if ( StrLen( ids ) == 0
)
    {
        
_TRACE( "Inside static block"
);
        fh =
fopen( "C:\\temp\\filter.csv", "r"
);

        
if
( fh )
        {
            ids =
","
;

            
while ( !feof
( fh ) )
            {
                line =
fgets
( fh );

                
if ( StrFind( line, "\n"
) )
                {
                    line =
StrLeft( line, StrLen( line ) - 1
);
                }

                ids += line;
                ids +=
","
;
            }
        }

        
StaticVarSetText( "StaticIDs"
, ids );
    }

    
return
ids;
}

function
IncludeIndustry( id )
{
    
return StrFind( GetIndustries(), "," + id + ","
);
}

Filter = IncludeIndustry( IndustryID
() );

AddColumn( IndustryID(), "Industry" );

Mike


--- In amibroker@xxxxxxxxxxxxxxx, "gmorlosky" <gmorlosky@xxx> wrote:
>
> One slight edit. In the following line I needed to change the "," to "\n", else a ,1 is equal to ,10 or ,100. working Great
>
> return StrFind( ids, "," + id + "\n," );
>
>
> amibroker@xxxxxxxxxxxxxxx, "gmorlosky" gmorlosky@ wrote:
> >
> > Hi Mike;
> > I figured it out, THANKS to all your help. The creation of the ids file has a format that includes an end of line on each line and it is counted as a position, SO if I expect token 4 it is actually finding 13 as the position, when I reset the StrFind code to not include the last ",", because that found nothing. Therfore I coded the filter for > 0 (meaning the number was found), then it works
> >
> > return StrFind( ids, "," + id /* + "," */ ); // removed last comma
> > ,0 // 0,1,2
> > ,1 // 3,4,5
> > ,2 // 6,7,8
> > ,3 // 9,10,11
> > ,4 // 12,13
> >
> > I then tested against the following and it worked great.
> > ,0
> > ,2
> > ,20
> > ,200
> >
> > Below is the working code:
> >
> > _SECTION_BEGIN("ReadingCSV Function");
> > Title = "ReadingCSV Function";
> > IndID = 0;
> > IID = 0;
> > ids = "";
> > function IncludeIndustry( id )
> > {
> > local ids;
> > local fh;
> > ids = ",";
> > fh = fopen( "C:\\AmiBroker CSV\\ABTest.csv", "r" );
> > if ( fh )
> > {
> > while ( !feof( fh ) )
> > {
> > ids += fgets( fh );
> > ids += ",";
> > }
> > fclose( fh );
> > printf( ids );
> > }
> > // return StrFind( "," + id + "," , ids); // original code
> > return StrFind( ids, "," + id /* + "," */ );
> > }
> > //Filter = IncludeIndustry( IndustryID() ); // original code
> > IID = IndustryID();
> > RID = IncludeIndustry( IndustryID() );
> > Match = IIf(RID > 0,1,0);
> > Filter = Match;
> > Check = IndustryID() == RID;
> > Buy = Close >= 0;
> > Sell = Close == 0;
> > AddColumn ( Buy,"Buy" );
> > AddColumn ( Sell,"Sell" );
> > AddColumn ( Close,"Close" );
> > AddTextColumn( FullName(),"FullName" );
> > AddColumn( IndustryID(), "IndId" );
> > AddColumn( Check, "Check" );
> > _TRACE ("IID: "+ IID +" RID: "+ RID +" IDS: "+ IDS+" Match: "+ Match);
> > _SECTION_END();
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> > --- In amibroker@xxxxxxxxxxxxxxx, "gmorlosky" <gmorlosky@> wrote:
> > >
> > > I started using _TRACE and find that the only number that comes back from "IndustryID() == IncludeIndustry( IndustryID() )" is 0, therefore IndustryID == 0 works, but all others fail.
> > > Wondering if the problem is with the format of the created IDs file or the read of the IDs file ???
> > >
> > > Any thoughts
> > >
> > > --- In amibroker@xxxxxxxxxxxxxxx, "gmorlosky" <gmorlosky@> wrote:
> > > >
> > > > Here is the latest code, but it still is displaying in an Explore only IndustryID() == 0.
> > > > I did make 2 changes to the code:
> > > > 1) flipped the StrFind parameters
> > > > 2) expanded the Filter to include IndustryID() ==
> > > > 3) Commentary display of printf(ID) looks like this:
> > > >
> > > > ,,0
> > > > ,1
> > > > ,2
> > > > ,3
> > > > ,4
> > > > ,5
> > > > ,6
> > > > ,7
> > > > ,8
> > > > ,9
> > > > ,10
> > > > ,,
> > > >
> > > >
> > > >
> > > > _SECTION_BEGIN("ReadingCSV Function");
> > > > Title = "ReadingCSV Function";
> > > > id = 0;
> > > > function IncludeIndustry( id )
> > > > {
> > > > local ids;
> > > > local fh;
> > > >
> > > > ids = ",";
> > > > fh = fopen( "C:\\AmiBroker CSV\\ABTest.csv", "r" );
> > > >
> > > > if ( fh )
> > > > {
> > > > while ( !feof( fh ) )
> > > > {
> > > > ids += fgets( fh );
> > > > ids += ",";
> > > > printf( ids );
> > > > }
> > > > fclose( fh );
> > > > }
> > > > // return StrFind( "," + id + "," , ids); // original code
> > > > return StrFind( ids, "," + id + "," );
> > > > }
> > > >
> > > > //Filter = IncludeIndustry( IndustryID() ); // original code
> > > > Filter = IndustryID() == IncludeIndustry( IndustryID() );
> > > >
> > > > Buy = Close >= 0;
> > > > Sell = Close == 0;
> > > > AddColumn ( Buy,"Buy" );
> > > > AddColumn ( Sell,"Sell" );
> > > > AddColumn ( Close,"Close" );
> > > > AddTextColumn( FullName(),"FullName" );
> > > > AddColumn( id, "Id" );
> > > > _SECTION_END();
> > > >
> > >
> >
>



__._,_.___


**** IMPORTANT PLEASE READ ****
This group is for the discussion between users only.
This is *NOT* technical support channel.

TO GET TECHNICAL SUPPORT send an e-mail directly to
SUPPORT {at} amibroker.com

TO SUBMIT SUGGESTIONS please use FEEDBACK CENTER at
http://www.amibroker.com/feedback/
(submissions sent via other channels won't be considered)

For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
http://www.amibroker.com/devlog/





Your email settings: Individual Email|Traditional
Change settings via the Web (Yahoo! ID required)
Change settings via email: Switch delivery to Daily Digest | Switch to Fully Featured
Visit Your Group | Yahoo! Groups Terms of Use | Unsubscribe

__,_._,___