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

[amibroker] Re: Request for Code Review


  • Date: Fri, 12 Mar 2010 10:34:59 -0000
  • From: "Mike" <sfclimbers@xxxxxxxxx>
  • Subject: [amibroker] Re: Request for Code Review

PureBytes Links

Trading Reference Links

Some very quick thoughts for you:

1. If you want to continue down this road, it would be much much more efficient to have one file per symbol rather than one file per bar. For example assume you made at least one trade per day; 

20 symbols over 250 bars (1 year)
- 20 * 250 fopen operations (once per symbol per bar)
- 20 * 250 feof operations (once per symbol per bar)
- minimum 250 fgets operations (one per trade)
- 20 * 250 fclose operations (once per symbol per bar)

vs.

20 symbols over 250 bars (1 year)
- 20 fopen operations (once per symbol)
- 20 feof operations (once per symbol)
- minimum 250 fgets operations (one per trade)
- 20 fclose operations (once per symbol)

2. Once you start including other data, you'll probably want to use StrExtract instead of StrFind.

3. You have not considered same bar exit.

4. You have not considered scale in/out.

5. You have not considered fgets maximum buffer length.

6. It is pointless to say "Buy[i] = 1 AND Something", just say "Buy[i] = Something" since "true AND Something" will always equal "Something".

7. You probably want to set position size to -100/MaxPositions (percent equity evenly divided by max positions) rather than a fixed value based on initial equity. If your capital ever fell below that fixed value, you would never be able to take another trade.

8. No point setting base file name and extension inside the loop (i.e. once per bar), they're never going to change. Set before start of loop then use new variable built up from base name, unique name, and extension.

9. No point calling Name() for each bar. Call it once before entering loop.

10. No point initializing strDate to "" only to immediately overwrite it.

11. No point setting Sell bar by bar. Just set the entire array to 0 before or after the loop.

12. Personal preference, but I find it cleaner to use break statement to exit loop rather than including "AND ! DateCheckReturn" in loop control.

In answer to your question, no you do not need loops. Ultimately, you will probably find it easier to open a symbol specific file, parse each line to see if it's a Buy vs. Sell, the date of the operation, the number of shares, and the price. Using the Date read combined with ValueWhen, figure out the bar index and then set the respective arrays directly (e.g. Buy[index], BuyPrice[index], PositionSize[index]).

Mike

--- In amibroker@xxxxxxxxxxxxxxx, "longt3rm" <longt3rm@xxx> wrote:
>
> Oops, I forgot the big question: Is there a way to read one file and load the contents of the file into the array for each ticker in the file?
> 
> Bill
> 
> --- In amibroker@xxxxxxxxxxxxxxx, "longt3rm" <longt3rm@> wrote:
> >
> > Hello,
> > 
> > I'm slowly grasping AFL; really slowly.  ;-)
> > 
> > The code below looks for a text file for the date of the current bar, if it finds the ticker, it goes long.
> > 
> > Would anyone have comments / suggestions on improvements? Specifically, I am wondering if traversing the bars is necessary or if there is a way to skip the "for" loop.
> > 
> > Thank you!!
> > 
> > Bill
> > 
> > ----------------------------------------------------
> > /*
> > This is NOT a strategy but rather a learning exercise.
> > The purpose is to study / learn how to open an external file to test whether a ticker is contained in that file; next I will add the number of shares and price to use.
> > */
> > 
> > function DateCheck(myTicker,filename){
> > //See if the filename contains myTicker
> > DateCheckReturn = False;
> > fh = fopen(filename ,"r");
> > 
> > if(fh)
> > {
> > 	while(! feof(fh) AND ! DateCheckReturn) //Have we reached end of file OR found our ticker? 
> > 	{
> > 		Line = fgets(fh);
> > 		DateCheckReturn = IIf(StrFind(Line,myTicker), True, False);
> > 		if (DateCheckReturn)
> > 		{
> > 			// display information in DebugView when we find a match
> > 			_TRACE("filename=" + filename); 
> > 			_TRACE("myTicker=" + myTicker+ " / DateCheckReturn=" + DateCheckReturn);
> > 		}
> > 	}
> > 	fclose(fh);
> > }
> > return DateCheckReturn;
> > }
> > 
> > MaxPositions = 20;
> > SetOption("InitialEquity",50000); 
> > SetOption("MaxOpenPositions", MaxPositions );
> > 
> > SetPositionSize(GetOption("InitialEquity")/GetOption("MaxOpenPositions"),spsValue);
> > 
> > aDay = Day();
> > aMonth = Month();
> > aYear = Year();
> > for(i=0; i<BarCount; i++)
> > {
> > //Set the base for the file name
> > filename = "C:\\data\\signal_";
> > //Set the extension for the file
> > fileext = ".txt";
> > 
> > strDate="";
> > strDate = StrFormat( "%4.f%02.f%02.f", aYear[i],aMonth[i],aDay[i]); //get the date of the current bar in yyyymmdd format.
> > filename = filename + strDate + fileext; //Will be something like  c:\data\signal_20100311.txt
> > Buy[i] = 1 AND DateCheck(Name(),filename);
> > 
> > Sell[i] = 0;
> > }
> >
>




------------------------------------

**** 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/

Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/amibroker/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http://groups.yahoo.com/group/amibroker/join
    (Yahoo! ID required)

<*> To change settings via email:
    amibroker-digest@xxxxxxxxxxxxxxx 
    amibroker-fullfeatured@xxxxxxxxxxxxxxx

<*> To unsubscribe from this group, send an email to:
    amibroker-unsubscribe@xxxxxxxxxxxxxxx

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/