Intra day volume for nifty 50 index

I trade futures / options for the NIFTY 50 Index - intra day volumes are available for futures and options.

Unfortunately intra day volumes are not available for the ACTUAL index !! So I have tried writing a code for this (using "Foreign"), the code is --

_SECTION_BEGIN("NIFTY VOLUME");

NIFTYVOL  =  Foreign("ADANIPORTS","V") + Foreign("ASIANPAINT","V") + Foreign("AXISBANK", "V") +    Foreign("BAJAJ_AUTO","V") +  Foreign("BAJFINANCE ",  "V") +    Foreign("BAJFINSV ",  "V") + Foreign(" BPCL", "V")+

 Foreign("BHARTIARTL ","V")  +    Foreign("INFRATEL","V")  +    Foreign("BRITANNIA", "V")  +    Foreign("CIPLA",  "V")  +    Foreign("COALINDIA", "V")  +  Foreign("DRREDDY",  "V")  +   Foreign("  EICHERMOT", "V")  +  
 
 Foreign("GAIL", "V") +  Foreign("GRASIM", "V") + Foreign("HCLTECH",  "V") +   Foreign("HDFCBANK ","V") +  Foreign("HEROMOTOCO","V") +   Foreign("HINDALCO","V") +      Foreign("HINDUNILVR","V") +   
 
 Foreign("HDFC ","V") +  Foreign("ICICIBANK","V")  +    Foreign("IBULHSGFIN", "V")  +    Foreign("IOC",  "V")  +    Foreign("INDUSINDBK", "V")  +   Foreign("INFY",  "V")  +   Foreign(" ITC","V")  +   Foreign(" JSWSTEEL","V")  +   
 
       Foreign("KOTAKBANK", "V")  +  Foreign("LT ", "V")  +    Foreign("  M_M   ","V")  +    Foreign("  MARUTI   ", "V")  +    Foreign("  NTPC   ",  "V")  +    Foreign("ONGC", "V")  +    Foreign("POWERGRID",  "V")  +   Foreign("RELIANCE", "V")  +
       
       Foreign("SBIN", "V")  +  Foreign("  SUNPHARMA ","V")  +    Foreign(" TCS   ","V")  +    Foreign(" TATAMOTORS   ", "V")  +    Foreign("  TATASTEEL  ",  "V")  +     Foreign("TECHM", "V")  +   Foreign("  TITAN  ",  "V")  + Foreign("ULTRACEMCO", "V")  +
       
       Foreign("UPL",  "V")  +     Foreign("VEDL", "V")  +   Foreign("  WIPRO   ","V")  +    Foreign("  YESBANK  ","V")  +    Foreign("  ZEEL   ", "V"); 

Plot( NIFTYVOL, "NIFTY VOLUME ",  colorBlack, styleHistogram | styleThick);

_SECTION_END();

Unfortunately with this code nothing plots - the plot pane is empty !!

I have checked and rechecked the "symbols" - all of them are same as per the data provider .

I will appreciate some help / guidance !

Thanks and regards

Actually, all of your symbols are not as per the data provider. I suggest you reformat your code similarly to this so that it is easier to find your errors:

NIFTYVOL  =  0;
NIFTYVOL  += Foreign("ADANIPORTS","V");
NIFTYVOL  += Foreign("ASIANPAINT","V");
NIFTYVOL  += Foreign("AXISBANK", "V");
NIFTYVOL  += Foreign("BAJAJ-AUTO","V");
NIFTYVOL  += Foreign("BAJFINANCE","V");
NIFTYVOL  += Foreign("BAJAJFINSV","V");
NIFTYVOL  += Foreign("BPCL","V");

This will also allow you to use the debugger to step through the calculations one at a time to see when NIFTYVOL changes from numeric values to NULL, which is your indication that AmiBroker was not able to retrieve data for the symbol as you specified it. A few errors that I found in the first set of symbols:

  1. You cannot include spaces before or after the symbol name, for example "BAJFINANCE "
  2. In my database, the symbol is "BAJAJ-AUTO", not "BAJAJ_AUTO"
  3. In my database, the symbol is "BAJAJFINSV", not "BAJFINSV " (two errors in this one)
1 Like

@JEETU,

you should not use multiple Foreign to sum up. First it is slow, secondly it wastes time writing code. What if you would have hundreds of symbols... would you really write hundreds of lines doing the same repetitive thing?

Instead you should move all your symbols to a watch list and then iterate that watch list.

So

wl_number = 0;// set your watchlist number here
ticker_list = CategoryGetSymbols(categoryWatchlist, wl_number );

NIFTYVOL = 0;
is_null = 0;
for (i = 0; (ticker = StrExtract(ticker_list, i)) != ""; i++) {
	SetForeign(ticker);
		NIFTYVOL += Nz(V);// Nz converts Null to zero
		is_null += IsNull(V);// Are there symbols returning Null (no data?)
	RestorePriceArrays();
}

// print sum of symbols returning Null (having no data)
printf("Sum of symbols returning Null: %g", is_null);

Plot( NIFTYVOL, "NIFTYVOL", colorGreen, styleHistogram, Null, Null, 0, 0, -60 );

6

As you can see in upper code another advantage is... you don't need to write down or copy any symbol names so avoiding any typos like spaces and what not.

BTW an even shorter and faster code would be doing it like in this one:

/// create composite via Analysis -> Scan
/// Set watchlist at "Apply to" -> "Filter"
/// derived from 
/// @link https://www.amibroker.com/guide/afl/staticvaradd.html
if ( Status( "action" ) == actionScan ) {
	Buy = Sell = Short = Cover = 0;
	if ( Status( "stocknum" ) == 0 ) 
		//remove any earlier composite values
		StaticVarRemove( "~NIFTYVOL" );
	StaticVarAdd( "~NIFTYVOL", V, KeepAll = True, storage = 0);
}

// To plot in chart pane
NIFTYVOL = StaticVarGet( "~NIFTYVOL" );
Plot( NIFTYVOL, "NIFTYVOL", colorGreen, styleHistogram, Null, Null, 0, 0, -60 );

You should still check your symbols in data base for having typos and not being inline with actual symbology of your data provider.

You can check for symbols having no data using this code:

wl_number = 0;// set your watchlist number here
ticker_list = CategoryGetSymbols(categoryWatchlist, wl_number );

if ( ParamTrigger( "Trace symbols having no data", "CLICK HERE" ) ) {
	AB = CreateObject("Broker.Application");
	null_ticker = "";
	for (i = 0; (ticker = StrExtract(ticker_list, i)) != ""; i++) {	
		stk = AB.Stocks(ticker);
		Quotes = Stk.Quotations;	
		bar_count = Quotes.Count;
		if( bar_count == 0 ) 
			null_ticker += ticker + ",";
	}
	_TRACE( "Symbols having no data: %s", Null_ticker );
}

Read here about how to see Trace output.

13 Likes

@mradtke - thanks a lot for your prompt reply, there was an error on my part with the symbol for "BAJAJFINSV" - BAJAJ_AUTO is OK.
Thanks for pointing out that there should be no spaces before or after the symbol name - I redid the code and it worked.

@fxshrat - what to say-you are a master at AFL - your codes are precise - some times difficult for a non software guy like me - but this one is a dream - short and sweet - thanks a lot - lots to learn !!

3 Likes
wl_number = 0;// set your watchlist number here
ticker_list = CategoryGetSymbols(categoryWatchlist, wl_number );

if ( ParamTrigger( "Trace symbols having no data", "CLICK HERE" ) ) {
	AB = CreateObject("Broker.Application");
	null_ticker = "";
	for (i = 0; (ticker = StrExtract(ticker_list, i)) != ""; i++) {	
		stk = AB.Stocks(ticker);
		Quotes = Stk.Quotations;	
		bar_count = Quotes.Count;
		if( bar_count == 0 ) 
			null_ticker += ticker + ",";
	}
	_TRACE( "Symbols having no data: %s", Null_ticker );
}

Can the above script be run from amibroker or we need to run it from outside ?

It is AFL. But there is a typo in upper code... it should be using _TRACEF but not _TRACE function since I was using format specifier and _TRACE does not allow format specifying.

So here is correction:

wl_number = 0;// set your watchlist number here
ticker_list = CategoryGetSymbols(categoryWatchlist, wl_number );

if ( ParamTrigger( "Trace symbols having no data", "CLICK HERE" ) ) {
	AB = CreateObject("Broker.Application");
	null_ticker = "";
	for (i = 0; (ticker = StrExtract(ticker_list, i)) != ""; i++) {	
		stk = AB.Stocks(ticker);
		Quotes = Stk.Quotations;	
		bar_count = Quotes.Count;
		if( bar_count == 0 ) 
			null_ticker += ticker + ",";
	}
	_TRACEF( "Symbols having no data: %s", null_ticker );
}
3 Likes

@fxshrt.... forgot to mention I need individual symbol volume, total sector symbols volume sumup along with plot on the chart pane option also in the AFL.

Thanks,

@dragon,

For posting questions on the forum, you are required to get "Verified". Please SEARCH for "Verified" or "Verified Badge" and follow the steps.

I forgot "verfiied" badge username. I will try to retrieve it and post it again.

Thanks

Why use multiple Usernames?

Previously I forgot the old username and don't know how to retrieve. Then created another one. Now after receiving badge verification notification and how to do the process, I checked in the amibroker site and in that I have found my username with badge verification.

Hope it clears...!

Hi @fxshrat Using your afl we can get cumulative volume of the symbols in the watchlist '0'. That's Great. But how can we get individual symbol volume along with the sumup volume from the same watchlist '0' using explore/scan option window.

we get volume alone from scan/explorer through afl. This afl is too good and great idea without entering each symbol in the afl that causes typo errors. I want to utilize in both the ways i.e., plotting on the chart pane in graphical form and also obtaining individual symbol volume along with cumulative volume in explore window for analysis.

I request you to can you please suggest modification in your afl to get the desired output. Otherwise I have to enter each symbol details in the afl like...

A1= Foreign( "ABC", "V" ) ;
A2= Foreign( "BCD", "V" ) ;
A3= Foreign( "DEF", "V" ) ;
A4= Foreign( "FGH", "V" ) ;
A5= Foreign( "HIJ", "V" ) ;

Total = A1+A2+A3+A4+A5;

addcolumn(Total, "Sector Volume", 1.2);

If I have hundreds of symbols it will be like this as you said..."you should not use multiple Foreign to sum up. First it is slow, secondly it wastes time writing code. What if you would have hundreds of symbols... would you really write hundreds of lines doing the same repetitive thing?"

Please do the needful.

Thanks with regards,

It is NOT allowed to use multiple accounts on forum. I have merged BBTrade with dragon and you will use your original user @dragon.

1 Like

@fxshrat, @portfoliobuilder or anyone please help me to get the desired output.

Thanks with regards,

If you have a list then use LIST (of symbols).

Either using category list

wl_number = 0;// set your watchlist number here
ticker_list = CategoryGetSymbols(categoryWatchlist, wl_number );

or in case of your 5 symbols you may create

ticker_list = "ABC,BCD,DEF,FGH,HIJ";

And then iterate that symbols list

ticker_list = "ABC,BCD,DEF,FGH,HIJ";

If ( Status("stocknum") == 0 ) {
	Total = 0;
	for (i = 0; (ticker = StrExtract(ticker_list, i)) != ""; i++) 
		Total += Nz(Foreign(ticker, "V"));	
	StaticVarSet("TotalVolume", Total);
}

AddColumn(StaticVarGet("TotalVolume"), "Sector Volume", 1.2);

Or use StaticVarAdd as posted in this very same thread.


I meant that if you call multiple arrays of same symbol via foreign then it is slower than SetForeign.
See manual

AFL Function Reference - SETFOREIGN

Single SetForeign( "ticker" ) call is equivalent to the following sequence:

C = Foreign( "ticker", "C" ); 
O = Foreign( "ticker", "O" ); 
H = Foreign( "ticker", "H" );
L = Foreign( "ticker", "L" ); 
V = Foreign( "ticker", "V" ); 
OI = Foreign( "ticker", "I" ); 
Avg = ( C + H + L )/3;

but 6x faster (SetForeign takes about the same time as single foreign).

1 Like

@fxshrat Thank you very much.

Hi @fxshrat.. Please look at the attached file. Is there any simple method to get each symbol volume from the watchlist given in the afl? Request you to give some inputs. Actually, yesterday I have given a reply but that post not published. testafl copy

@Tomasz - I'm using 5.40 version in Home laptop, and this analysis done in Unregistered Version 6 from the office. This is for your information, for not questioning.

Only versions 6.0 and higher are supported.

Unregistered versions are not supported in this section of the forum.
Unregistered versions are for 30 days use only.

1 Like

I have added colors to be bars on top of @fxshrat . Thanks @fxshrat

wl_number = 0;// set your watchlist number here
ticker_list = CategoryGetSymbols(categoryWatchlist, wl_number );

NIFTYVOL = 0;
is_null = 0;
for (i = 0; (ticker = StrExtract(ticker_list, i)) != ""; i++) {
	SetForeign(ticker);
		NIFTYVOL += Nz(V);// Nz converts Null to zero
		is_null += IsNull(V);// Are there symbols returning Null (no data?)
	RestorePriceArrays();
}

// print sum of symbols returning Null (having no data)
printf("Sum of symbols returning Null: %g", is_null);

//Colors for up down volume
//Base needs to be setup in the Symbols --> Categories (Base Indexes) to lets say ^NSEI
base = ParamStr("RS base ticker", GetBaseIndex());
SetForeign(base);
	finalColor = IIf(O<C, colorLime, IIf(O>C, colorRed, colorOrange));
RestorePriceArrays();

Plot( NIFTYVOL, "NIFTYVOL", finalColor, styleHistogram, Null, Null, 0, 0, -60 );