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

RE: [amibroker] Enhancement Request - "Playback" Function



PureBytes Links

Trading Reference Links

Actually Dale,
	I did know how, I was just too bloody lazy to do it!  But it always worked on 
multiple tickers, just run it on a watchlist with *at least* all the tickers 
mentioned in the file.  Doesn't matter if there are others, just slows it 
down a bit.

In fairness it was only intended as an example for other people to extend 
(like your DateToDateNum function).

Hovever I have:
* tidied up your latest mod to the code
* added position sizing
* put the main body in a function
* added 'local' declarations, so the functions can be generally used without 
worrying about clobbering globals
* added SetOption calls for the required settings

I did, however, restrain myself from 'fixing' the inside-out date format and 
the hungarian notation in your DateToDateNum function! :-)

The latest version is both below *AND* attached as an attachment, so that 
those of you who get individual emails don't have to worry about Yahoo's line 
wrapping etc.

	Nigel

On Sun, 25 Jan 2004 13:14, dingo wrote:
> I know you asked for it a while back - a number of people have asked for it
> over the last several years. I just saw what Nigel had done and it piqued
> my curiosity.
>
> The position size is left as and exercise for the student.  (Until I or
> someone else can figure out how to implement it.  I strongly suspect that's
> why Nigel decided not to finish it.  8-)  ).
>
> As to just the "current stock" that was to make it easy to run the example.
>
> It "should" be able to work on multple tickers, etc.
>
> Looking for suggestions from anyone!!!
>
> d
>

// Inspired by a request for a 'playback' feature from Don Upton,
//
// initial code by Nigel Rowe,
// with contributions by Ruddy Turnstone Trading, LLC  (dingo)
//
//
// Input file is comma seperated text.
// Fields are:
//	0.	Symbol        -- without quotes
//	1.  	Long Or Short -- L or S, without quotes
//	2.	Number of shares
//	3.  	Entry date    -- in mm/dd/yyyy format.  ie xmas day 2003 is 12/25/2003
//	4.	Entry price
//	5.	Exit date     -- mm/dd/yyyy  format, or blank for no exit
//	6.	Exit Price

 
function DateToDateNum(sMMDDYYYY) // date in format mm/dd/yyyy
{
 
/*-------------------------------------------------------------
  This function will accept a string in the MM/DD/YYY format
  and convert it into Amibroker's datenum.
 
  nDateNum = 10000 * (year - 1900) + 100 * month + day
 
  It assumes that mm dd and yyyy are reasonable. It does check
  for the presence of 2 "/" characters and if not there
  will return a 0.
- -------------------------------------------------------------*/
 	local nDateNum, sWrk, nPosn, nYr, nMth, nDay;

	nDateNum = 0;
    	sWrk = sMMDDYYYY;
    	nPosn = StrFind(sWrk, "/");
    	if (nPosn > 0) {
       	nMth = StrToNum(StrLeft(sWrk, nPosn-1));
        	sWrk = StrRight(sWrk, StrLen(sWrk) - nPosn);
        	nPosn = StrFind(sWrk, "/");
        	if (nPosn > 0) {
            		nDay = StrToNum(StrLeft(sWrk, nPosn-1));
            		sWrk = StrRight(sWrk, StrLen(sWrk) - nPosn);
            		nYr = StrToNum(sWrk);
            		nDateNum = 10000 * (nYr - 1900) + (100 * nMth) + nDay;
		}
	}
    	return nDateNum;
}
 
 

function DateToBar(dn) 
{
	return LastValue(ValueWhen(DateNum()==dn, BarIndex()));
}
 
 
 
 
function playback(filename) 
{ 
	local f, Line, sym;
	local LorS, numShrs;
	local entryDate, entryPrice, exitDate, exitPrice;
	local bar;

	global Buy, Sell, BuyPrice, SellPrice;
	global Short, Cover, ShortPrice, CoverPrice;
	global PositionSize;

	f = fopen(filename, "r");
	while( f && (! feof(f))) {
		Line = fgets(f);
		sym = StrExtract(Line, 0);
		if( sym == Name() ) {
			LorS = StrExtract(Line, 1);
			numShrs = StrToNum(StrExtract(Line,2));
			entryDate = DateToDateNum(StrExtract(Line,3));
			entryPrice = StrToNum(StrExtract(Line,4));
			exitDate = DateToDateNum(StrExtract(Line,5));
			exitPrice = StrToNum(StrExtract(Line,6));

			bar = DateToBar(entryDate);

			// The extra (entryPrice/2) is to avoid rounding errors.
			PositionSize[bar] = (numShrs*entryPrice) + (entryPrice/2);
 
			if (LorS == "L") {
				Buy[bar] = True;
				BuyPrice[bar] = entryPrice;
	      			if( exitdate ) {
	                   		bar = DateToBar(exitdate);
	                   		Sell[bar] = True;
	                   		SellPrice[bar] = exitPrice;
	            		}
			} else {
	      	         	Short[bar] = True;
	              	ShortPrice[bar] = entryPrice;
	              	if( exitDate ) {
	                  		bar = DateToBar(exitDate);
	                   		Cover[bar] = True;
	                   		CoverPrice[bar] = exitPrice;
	              	}
	        	}
	    	}
	}
	if(f) fclose(f);
}
 

SetFormulaName("Playback from file");
 
// Enforce required settings
SetOption("InitialEquity", 10000000);	 
SetOption("MaxOpenPositions", 10000);
SetOption("MinShares", 1);
SetOption("AllowPositionShrinking", False);
SetOption("FuturesMode", False);
SetTradeDelays(0,0,0,0);
ApplyStop(stopTypeLoss,	  stopModeDisable, 0, 0);
ApplyStop(stopTypeTrailing,stopModeDisable, 0, 0);
ApplyStop(stopTypeProfit,	  stopModeDisable, 0, 0);
ApplyStop(stopTypeNBar,    stopModeDisable, 0, 0);


// NB!! you will have to manually set Positions and Periodicity !!!!!
//
//The following SetOption are NOT valid, mentioned here for completeness
//SetOption("Positions", "Long and Short");
//Setoption("Periodicity", "Daily");

Buy = False;
Sell = False;
BuyPrice = Close;
SellPrice = Close;
Short = False;
Cover = False;
ShortPrice = Close;
CoverPrice = Close;
PositionSize = 0;
 
// and the next line does all the work!
playback("playback.txt"); 
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)

iD8DBQFAEzjABbmcM2pfckkRAkjMAKCV9NBW9/0g2zHkwvoYnO123ADCsACfbe1P
htBgUifFn5zE8aoCggQ4nl0=
=qVyg
-----END PGP SIGNATURE-----

Send BUG REPORTS to bugs@xxxxxxxxxxxxx
Send SUGGESTIONS to suggest@xxxxxxxxxxxxx
-----------------------------------------
Post AmiQuote-related messages ONLY to: amiquote@xxxxxxxxxxxxxxx 
(Web page: http://groups.yahoo.com/group/amiquote/messages/)
--------------------------------------------
Check group FAQ at: http://groups.yahoo.com/group/amibroker/files/groupfaq.html 

------------------------ Yahoo! Groups Sponsor ---------------------~-->
Buy Ink Cartridges or Refill Kits for your HP, Epson, Canon or Lexmark
Printer at MyInks.com. Free s/h on orders $50 or more to the US & Canada.
http://www.c1tracking.com/l.asp?cid=5511
http://us.click.yahoo.com/mOAaAA/3exGAA/qnsNAA/GHeqlB/TM
---------------------------------------------------------------------~->

Yahoo! Groups Links

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

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/ 


// Inspired by a request for a 'playback' feature from Don Upton,
//
// initial code by Nigel Rowe,
// with contributions by Ruddy Turnstone Trading, LLC  (dingo)
//
//
// Input file is comma seperated text.
// Fields are:
//	0.	Symbol        -- without quotes
//	1.  	Long Or Short -- L or S, without quotes
//	2.	Number of shares
//	3.  	Entry date    -- in mm/dd/yyyy format.  ie xmas day 2003 is 12/25/2003
//	4.	Entry price
//	5.	Exit date     -- mm/dd/yyyy  format, or blank for no exit
//	6.	Exit Price

 
function DateToDateNum(sMMDDYYYY) // date in format mm/dd/yyyy
{
 
/*-------------------------------------------------------------
  This function will accept a string in the MM/DD/YYY format
  and convert it into Amibroker's datenum.
 
  nDateNum = 10000 * (year - 1900) + 100 * month + day
 
  It assumes that mm dd and yyyy are reasonable. It does check
  for the presence of 2 "/" characters and if not there
  will return a 0.
-------------------------------------------------------------*/
 	local nDateNum, sWrk, nPosn, nYr, nMth, nDay;

	nDateNum = 0;
    	sWrk = sMMDDYYYY;
    	nPosn = StrFind(sWrk, "/");
    	if (nPosn > 0) {
       	nMth = StrToNum(StrLeft(sWrk, nPosn-1));
        	sWrk = StrRight(sWrk, StrLen(sWrk) - nPosn);
        	nPosn = StrFind(sWrk, "/");
        	if (nPosn > 0) {
            		nDay = StrToNum(StrLeft(sWrk, nPosn-1));
            		sWrk = StrRight(sWrk, StrLen(sWrk) - nPosn);
            		nYr = StrToNum(sWrk);
            		nDateNum = 10000 * (nYr - 1900) + (100 * nMth) + nDay;
		}
	}
    	return nDateNum;
}
 
 

function DateToBar(dn) 
{
	return LastValue(ValueWhen(DateNum()==dn, BarIndex()));
}
 
 
 
 
function playback(filename) 
{ 
	local f, Line, sym;
	local LorS, numShrs;
	local entryDate, entryPrice, exitDate, exitPrice;
	local bar;

	global Buy, Sell, BuyPrice, SellPrice;
	global Short, Cover, ShortPrice, CoverPrice;
	global PositionSize;

	f = fopen(filename, "r");
	while( f && (! feof(f))) {
		Line = fgets(f);
		sym = StrExtract(Line, 0);
		if( sym == Name() ) {
			LorS = StrExtract(Line, 1);
			numShrs = StrToNum(StrExtract(Line,2));
			entryDate = DateToDateNum(StrExtract(Line,3));
			entryPrice = StrToNum(StrExtract(Line,4));
			exitDate = DateToDateNum(StrExtract(Line,5));
			exitPrice = StrToNum(StrExtract(Line,6));

			bar = DateToBar(entryDate);

			// The extra (entryPrice/2) is to avoid rounding errors.
			PositionSize[bar] = (numShrs*entryPrice) + (entryPrice/2);
 
			if (LorS == "L") {
				Buy[bar] = True;
				BuyPrice[bar] = entryPrice;
	      			if( exitdate ) {
	                   		bar = DateToBar(exitdate);
	                   		Sell[bar] = True;
	                   		SellPrice[bar] = exitPrice;
	            		}
			} else {
	      	         	Short[bar] = True;
	              	ShortPrice[bar] = entryPrice;
	              	if( exitDate ) {
	                  		bar = DateToBar(exitDate);
	                   		Cover[bar] = True;
	                   		CoverPrice[bar] = exitPrice;
	              	}
	        	}
	    	}
	}
	if(f) fclose(f);
}
 

SetFormulaName("Playback from file");
 
// Enforce required settings
SetOption("InitialEquity", 10000000);	 
SetOption("MaxOpenPositions", 10000);
SetOption("MinShares", 1);
SetOption("AllowPositionShrinking", False);
SetOption("FuturesMode", False);
SetTradeDelays(0,0,0,0);
ApplyStop(stopTypeLoss,	  stopModeDisable, 0, 0);
ApplyStop(stopTypeTrailing,stopModeDisable, 0, 0);
ApplyStop(stopTypeProfit,	  stopModeDisable, 0, 0);
ApplyStop(stopTypeNBar,    stopModeDisable, 0, 0);


// NB!! you will have to manually set Positions and Periodicity !!!!!
//
//The following SetOption are NOT valid, mentioned here for completeness
//SetOption("Positions", "Long and Short");
//Setoption("Periodicity", "Daily");

Buy = False;
Sell = False;
BuyPrice = Close;
SellPrice = Close;
Short = False;
Cover = False;
ShortPrice = Close;
CoverPrice = Close;
PositionSize = 0;
 
// and the next line does all the work!
playback("playback.txt");