AFL functions/methods to get Contract Specification information (Point Value, Tick Size, Margin Deposit, Round Lot Size)

Hi,

I want to export Contract Specification from Amibroker database to file. I would like to access what is visible in “Information” “window/pane” under “Contract Specification” section. How do I access (using which function(s) and parameter(s)):

  • Round Lot Size
  • Margin Deposit
  • Tick size
  • Point Value

I looked at GetFnData but no such information can be retrieved using this function.

Thanks.

How about built in variables: RoundLotSize, MarginDepost, TickSize, PointValue

1 Like

Thanks.

I can get data for RoundLotSize, TickSize, PointValue.

Unfortunately PoinTvalue is wrong. For VX futures I can see PointValue being equal to 1000 in information window but the number that gets exported to csv file equals 1.

Also, I can not use MarginDeposit since I get an error saying variable is not initialized… Is this a feature or a bug?

So here we have another Sherlock who found a “bug”.
How about checking manual first:
http://www.amibroker.com/guide/h_futbacktest.html

Should you read that you would know that you need to enable futures mode in the Settings.

PointValue is correct but you must turn on FUTURES mode. In Stocks mode it is always 1.

MarginDeposit is only defined in FUTURES mode.

SetOption("FuturesMode", True );
_TRACE("PV = " + PointValue );
_TRACE("MD = " + MarginDeposit );

Buy = 1;
Sell=0;
2 Likes

Thanks for your answer.

I am trying to export some data I can only get (at the moment) via AmiBroker. I don’t intend to become an AmiBroker or afl expert. Just getting the data export job done. It looked like a simple task when I started but turned out I lack sufficient functional literacy to do it only with help files. AmiBroker is huge and would require from me years of using it to master it to the level not to ask obvious/stupid questions.

I am curious and it makes me wonder how well will it serve you in the future calling people who want to do a seemingly simple task names like Sherlock? How do you know they were not RTFM for days trying to figure this out? Well, I mean, maybe you don’t. Maybe the task is not so simple and documentation is to huge/sparse to be able to carry on this task just based on the docs in reasonable time. Is this the problem of the Sherlock or the author of documentation? This is a task for Watson to figure out…

1 Like

Below is the final result that does the job. Hope it helps someone else. The only question is whether there are some other “switches” I should turn on since data for splits and dividends is not exported correctly at all.

// Outputs Norgate NDU data in csv format to be used outside of AmiBroker
// Only possible to produce this script with help from AmiBroker comunity forum and e-mail exchange with AmiBroker users.

#include_once "Formulas\Norgate Data\Norgate Data Functions.afl"  // use NDU plugin for historical index constituents

// PointValue and MarginDeposit is only defined in FUTURES mode. Use the below to switch it on.
SetOption("FuturesMode", True );
_TRACE("PV = " + PointValue );
_TRACE("MD = " + MarginDeposit );

Buy = 1;
Sell=0;

outputStatic = True;
outputSeries = True;

if (outputStatic) 
{
	// create folder for exporting static data purposes
	fmkdir( "D:\\DataExportStatic\\" );

	// open file for writing
	// file name depends on currently processed ticker
	fh = fopen( "D:\\DataExportStatic\\" + Name() + ".txt", "w" );

	// proceed if file handle is correct
	if ( fh )
	{
		NorgateID = NorgateAssetID();
		
		// Sectors
		// AmiBroker Sectors are populated by the Norgate Data plugin with information from the Thomson Reuters Business Classification scheme (TRBC).
		// The Sectors field has two levels - Group and Industry.
		//		- Group corresponds to TRBC level 1 (Economic Sector).
		//		- Industry corresponds to TRBC level 4 (Industry).

		// Schemes supported are: TRBC and GICS
		// For example, to obtain the name the Thomson Reuters Business Classification for a given stock you could use the following:
		trbccode = NorgateClassification("TRBC"); // Provides a string that shows the current classification of the current symbol for the given classification scheme
		trbcname = NorgateClassificationName("TRBC"); // Provides a string that shows the current classification name of the current symbol for the given classification scheme
		//trbcdescription = NorgateClassificationDescription("TRBC"); // Provides a string that shows the current classification description of the current symbol for the given classification scheme

		gicscode = NorgateClassification("GICS");
		gicsname = NorgateClassificationName("GICS");
		//gicsdescription = NorgateClassificationDescription("GICS");

		indexsymbol1 = NorgateCurrentIndustryIndexSymbol("$SP1500",1,"PR");
		indexsymbol2 = NorgateCurrentIndustryIndexSymbol("$SP1500",2,"PR");
		indexsymbol3 = NorgateCurrentIndustryIndexSymbol("$SP1500",3,"PR");
		indexsymbol4 = NorgateCurrentIndustryIndexSymbol("$SP1500",4,"PR");
		
		// write header line
		fputs( "Ticker,FullName,NorgateID,IsIndex,MarketID,Market,GroupID,Group,IndustryID,Industry,GicsID,GicsCategoryName,IcbID,IcbCategoryName,trbccode,trbcname,gicscode,gicsname,indexsymbol1,indexsymbol2,indexsymbol3,indexsymbol4,DelistingDate,PointValue,MarginDeposit,RoundLotSize,TickSize\n", fh );

		qs =  Name() + "," + FullName() + "," + NorgateID + "," + IsIndex() + "," + MarketID() + "," + MarketID(1) + "," + GroupID() + "," + GroupID(1) + "," + IndustryID() + "," + IndustryID(1) + "," + GicsID(0) + "," + GicsID(1) + "," + IcbID(0) + "," + IcbID(1);               
		fputs( qs, fh );
		qs =  "," + trbccode + "," + trbcname + "," + gicscode + "," + gicsname + "," + indexsymbol1 + "," + indexsymbol2 + "," + indexsymbol3 + "," + indexsymbol4 + "," + NumToStr(GetFnData("DelistingDate"), formatDateTime) + "," + PointValue + "," + MarginDeposit + "," + RoundLotSize + "," + TickSize;
		fputs( qs, fh );

		// close file handle
		fclose( fh );
	}
}

if (outputSeries) 
{
	// create folder for exporting time series data purposes
	fmkdir( "D:\\DataExport\\" );

	// open file for writing
	// file name depends on currently processed ticker
	fh = fopen( "D:\\DataExport\\" + Name() + ".txt", "w" );


	// check for membership
	// US Historical Index Constituents
	// Index Name					Symbol	Constits Start	Index Summary
	// Russell 1000					$RUI	Jul 1990		Top 1000 US companies by market cap, reconstituted annually, delisted stocks replaced annually, new IPOs added quarterly
	// Russell 2000					$RUT	Jul 1990		Next 2000 US companies by market cap, reconstituted annually, delisted stocks replaced annually, new IPOs added quarterly
	// Russell 3000					$RUA	Jul 1990		Top 3000 US companies by market cap, reconstituted annually, delisted stocks replaced annually, new IPOs added quarterly
	// Russell MidCap				$RMC	Jul 2008		Smallest approximately 800 companies of the Russell 1000 by market cap plus the next smallest eligible securities by market cap, reconstituted annually, delisted stocks replaced annually, new IPOs added quarterly
	// Russell MicroCap				$RUMIC	Jul 2008		Smallest 1000 of the Russell 2000 by market cap plus the next smallest eligible securities by market cap, reconstituted annually, delisted stocks replaced annually, new IPOs added quarterly
	// S&P 100						$OEX	Sep 1989		Top 100 US companies by market cap, reconstituted quarterly, delisted stocks replaced immediately
	// S&P 500						$SPX	Jan 1967		Top 500 US companies by market cap, reconstituted quarterly, delisted stocks replaced immediately
	// S&P MidCap 400				$MID	Jun 1991		Next 400 US companies by market cap, reconstituted quarterly, delisted stocks replaced immediately
	// S&P SmallCap 600				$SML	Oct 1994		Next 600 US companies by market cap, reconstituted quarterly, delisted stocks replaced immediately
	// S&P 1500						$SP1500	Nov 1994		Top 1500 US companies by market cap, reconstituted quarterly, delisted stocks replaced immediately
	// Dow Jones Industrial Average	$DJI	Jan 1950		Selected large cap 30 stocks, reconstituted irregularly, delisted stocks replaced immediately
	// NASDAQ 100					$NDX	Jan 1995		Top 100 NASDAQ companies by market cap, reconstituted annually, delisted stocks (and listing transfers) replaced immediately

	MemberOfSPX    = NorgateIndexConstituentTimeSeries("$SPX");
	MemberOfRUI    = NorgateIndexConstituentTimeSeries("$RUI");
	MemberOfRUT    = NorgateIndexConstituentTimeSeries("$RUT");
	MemberOfRUA    = NorgateIndexConstituentTimeSeries("$RUA");
	MemberOfRMC    = NorgateIndexConstituentTimeSeries("$RMC");
	MemberOfRUMIC  = NorgateIndexConstituentTimeSeries("$RUMIC");
	MemberOfOEX    = NorgateIndexConstituentTimeSeries("$OEX");
	MemberOfMID    = NorgateIndexConstituentTimeSeries("$MID");
	MemberOfSML    = NorgateIndexConstituentTimeSeries("$SML");
	MemberOfSP1500 = NorgateIndexConstituentTimeSeries("$SP1500");
	MemberOfDJI    = NorgateIndexConstituentTimeSeries("$DJI");
	MemberOfNDX    = NorgateIndexConstituentTimeSeries("$NDX");

	// Depending on your plugin configuration settings for Price & Volume Adjustment, the price and volume data will be adjusted to account for the effect of capital events and dividends.
	// If your trading system needs to reference the original unadjusted closing price of a security, use the function shown below.
	oc = NorgateOriginalCloseTimeSeries();
	// Likewise, if you need to reference the unadjusted volume, use:
	ov = NorgateOriginalVolumeTimeSeries();
        
        // This dividend business doesn't seem to work
	OrdinaryDividends = NorgateAllDividendsTimeSeries("Ordinary Dividends");
	SpecialDividends = NorgateAllDividendsTimeSeries("Special Dividends");
	TotalDividends = NorgateAllDividendsTimeSeries("Total Dividends");

	// proceed if file handle is correct
	if ( fh )
	{
		dt = DateTime();

		// write header line
		fputs( "Date,Open,High,Low,Close,Volume,OI,Aux1,Aux2,oc,ov,MemberOfSPX,MemberOfRUI,MemberOfRUT,MemberOfRUA,MemberOfRMC,MemberOfRUMIC,MemberOfOEX,MemberOfMID,MemberOfSML,MemberOfSP1500,MemberOfDJI,MemberOfNDX,OrdinaryDividends,SpecialDividends,TotalDividends\n", fh );
		
		// iterate through all the bars

		for ( i = 0; i < BarCount; i++ )
		{
			// write date/time information
			fputs( DateTimeToStr( dt[ i ] ) + ",", fh );

			//write quotations and go to the next line
			qs = StrFormat( "%g,%g,%g,%g,%g,%g,%g,%g,%g,%g", O[ i ], H[ i ], L[ i ], C[ i ], V[ i ], OI[ i ], Aux1[ i ], Aux2[ i ], oc[ i ], ov[ i ]);
			fputs( qs + "," , fh );
			qs = StrFormat( "%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,%g", MemberOfSPX[i], MemberOfRUI[i], MemberOfRUT[i], MemberOfRUA[i], MemberOfRMC[i], MemberOfRUMIC[i], MemberOfOEX[i], MemberOfMID[i], MemberOfSML[i], MemberOfSP1500[i], MemberOfDJI[i], MemberOfNDX[i] );
			fputs( qs + "," , fh );
			qs = StrFormat( "%g,%g,%g\n", OrdinaryDividends[i], SpecialDividends[i], TotalDividends[i] );        
			fputs( qs, fh );
		}
		// close file handle
		fclose( fh );
	}
}
// line required by SCAN option
Buy = 0;

You were called Sherlock because your post was offending since you were assuming bug on our end (you may read “When something is bugging you” section at: http://www.amibroker.com/kb/2014/10/09/efficient-support/). If you worked on the program for 20 years as I did and put so much effort, you would know how offending such statements are. So let me tell you straight. At your level of familiarity with AB there are ZERO bugs.
You may find one after 5+ years with the software if you are lucky

If you are new, a good idea is to assume your own errors.

Because all you asked was in the TUTORIAL about Futures Backtesting.
Really, it does not require searching.

Tutorial is must-read. It is small and does not require “years”, but just few hours to read.

I could understand if it was somewhere else, but in “tutorial”?

I know that some people nowadays expect IPod simplicity but AmiBroker does much more things than just play music and go to next/prev song, so it requires more buttons than one.

2 Likes

I am very sorry you find question that ask whether something is a feature or a bug insulting and highly offending. The beauty is in the eye of the observer. As said if this serves you by all means do feel even more of it.

You are assuming I was using AmiBroker for bakcteesting. Which makes sense since AmiBroker is a great tool for doing that and a lot more. That is why I purchased premium edition years ago. After many years I just wanted to export some data without any knowledge of AFL hence my questions…

Thanks for all the answers. As noted above I finished the task. Almost. I still can not get any data in Aux2 (which is suppose to export dividend data) and columns OrdinaryDividends, SpecialDividends and TotalDividends. I assume these are the issues of NDU AmiBroker plugin.

This “feature or bug” has clear negative conotation since we all know that the “feature” in this sentence is just another name for undesired thing. If somebody asked “are you stupid or lazy?” would that insult you? That is precisely what you did in your post. The fact of using question form does not change anything.

1 Like

Oh, come on, man, there are people, like myself, who can not imagine in their wildest dreams that someone would get insulted and feel offended by a sincere question. Again, my intention was getting the job done and hence I asked the questions. Without a trace of any intention to emotionally affect anyone. This is not my problem. As would not be yours if I would feel offended or insulted by anybody asking me whether I am stupid and(!)/or lazy. My feelings are my responsibility not the people around me. It is my job to create them, find/invent/imagine the “reason” or person to blame I have them and rationalize the whole thing and then let them (these feelings) grow and therefore live with them.

This is not productive anymore.

Thanks again for your answers and the great software. Have a great day.

1 Like

Sincere question would be “what I am doing wrong” or “am I missing something?”

3 Likes