# Calculate composites tool automation

Dear AB Gurus,
I have NSE (National Stock Exchange, India) Index NIFTY50 with a set of 50 stocks, NSE doesnt provide volume information as other exchanges of DJIA, NASDAQ, or other European Exchanges.

I need the volume information of Index across all time frames.
So i was looking below solutions

Solution 1:
Using Foreign Symbol 1 to n
Foreign("Symbol1", "V", Fixup = 1);
.
.
Foreign("Symboln", "V", Fixup = 1);
plot the volume

``````_SECTION_BEGIN("VolMubV1");

V_AMBUJACEM = Foreign("N.AMBUJACEM", "V", Fixup = 1);
V_ASIANPAINT = Foreign("N.ASIANPAINT", "V", Fixup = 1);
V_AUROPHARMA = Foreign("N.AUROPHARMA", "V", Fixup = 1);
V_AXISBANK = Foreign("N.AXISBANK", "V", Fixup = 1);
V_BAJAJAUTO = Foreign("N.BAJAJ-AUTO", "V", Fixup = 1);
V_BAJFINANCE = Foreign("N.BAJFINANCE", "V", Fixup = 1);
V_BPCL = Foreign("N.BPCL", "V", Fixup = 1);
V_BHARTIARTL = Foreign("N.BHARTIARTL", "V", Fixup = 1);
V_INFRATEL = Foreign("N.INFRATEL", "V", Fixup = 1);
V_BOSCHLTD = Foreign("N.BOSCHLTD", "V", Fixup = 1);
V_CIPLA = Foreign("N.CIPLA", "V", Fixup = 1);
V_COALINDIA = Foreign("N.COALINDIA", "V", Fixup = 1);
V_DRREDDY = Foreign("N.DRREDDY", "V", Fixup = 1);
V_EICHERMOT = Foreign("N.EICHERMOT", "V", Fixup = 1);
V_GAIL = Foreign("N.GAIL", "V", Fixup = 1);
V_HCLTECH = Foreign("N.HCLTECH", "V", Fixup = 1);
V_HDFCBANK = Foreign("N.HDFCBANK", "V", Fixup = 1);
V_HEROMOTOCO = Foreign("N.HEROMOTOCO", "V", Fixup = 1);
V_HINDALCO = Foreign("N.HINDALCO", "V", Fixup = 1);
V_HINDPETRO = Foreign("N.HINDPETRO", "V", Fixup = 1);
V_HINDUNILVR = Foreign("N.HINDUNILVR", "V", Fixup = 1);
V_HDFC = Foreign("N.HDFC", "V", Fixup = 1);
V_ITC = Foreign("N.ITC", "V", Fixup = 1);
V_ICICIBANK = Foreign("N.ICICIBANK", "V", Fixup = 1);
V_IBULHSGFIN = Foreign("N.IBULHSGFIN", "V", Fixup = 1);
V_IOC = Foreign("N.IOC", "V", Fixup = 1);
V_INDUSINDBK = Foreign("N.INDUSINDBK", "V", Fixup = 1);
V_INFY = Foreign("N.INFY", "V", Fixup = 1);
V_KOTAKBANK = Foreign("N.KOTAKBANK", "V", Fixup = 1);
V_LT = Foreign("N.LT", "V", Fixup = 1);
V_LUPIN = Foreign("N.LUPIN", "V", Fixup = 1);
V_MM = Foreign("N.M&M", "V", Fixup = 1);
V_MARUTI = Foreign("N.MARUTI", "V", Fixup = 1);
V_NTPC = Foreign("N.NTPC", "V", Fixup = 1);
V_ONGC = Foreign("N.ONGC", "V", Fixup = 1);
V_POWERGRID = Foreign("N.POWERGRID", "V", Fixup = 1);
V_RELIANCE = Foreign("N.RELIANCE", "V", Fixup = 1);
V_SBIN = Foreign("N.SBIN", "V", Fixup = 1);
V_SUNPHARMA = Foreign("N.SUNPHARMA", "V", Fixup = 1);
V_TCS = Foreign("N.TCS", "V", Fixup = 1);
V_TATAMOTORS = Foreign("N.TATAMOTORS", "V", Fixup = 1);
V_TATASTEEL = Foreign("N.TATASTEEL", "V", Fixup = 1);
V_TECHM = Foreign("N.TECHM", "V", Fixup = 1);
V_UPL = Foreign("N.UPL", "V", Fixup = 1);
V_ULTRACEMCO = Foreign("N.ULTRACEMCO", "V", Fixup = 1);
V_VEDL = Foreign("N.VEDL", "V", Fixup = 1);
V_WIPRO = Foreign("N.WIPRO", "V", Fixup = 1);
V_YESBANK = Foreign("N.YESBANK", "V", Fixup = 1);
V_ZEEL = Foreign("N.ZEEL", "V", Fixup = 1);

V_NIFTY = V_ADANIPORTS + V_AMBUJACEM + V_ASIANPAINT + V_AUROPHARMA + V_AXISBANK  + V_BAJFINANCE + V_BPCL + V_BHARTIARTL + V_INFRATEL + V_BOSCHLTD + V_CIPLA + V_COALINDIA + V_DRREDDY + V_EICHERMOT + V_GAIL + V_HCLTECH + V_HDFCBANK + V_HEROMOTOCO + V_HINDALCO + V_HINDPETRO + V_HINDUNILVR + V_HDFC + V_ITC + V_ICICIBANK + V_IBULHSGFIN + V_IOC + V_INDUSINDBK + V_INFY + V_KOTAKBANK + V_LT + V_LUPIN + V_MM + V_MARUTI + V_NTPC + V_ONGC + V_POWERGRID + V_RELIANCE + V_SBIN + V_SUNPHARMA + V_TCS + V_TATAMOTORS + V_TATASTEEL + V_TECHM + V_UPL + V_ULTRACEMCO + V_VEDL + V_WIPRO + V_YESBANK + V_ZEEL;

Graph0 = Plot (V_NIFTY, "Vol", colorBlueGrey, styleHistogram);

_SECTION_END();
``````

The above solution freezes AB in realtime trading hours, non trading hours works good

Solution 2:
Using addtocomposite, its has to run on everytime frame

``````AddToComposite (V, "~NiftyComp", "V");
Graph0 = Plot (Foreign("~NiftyComp", "V"), "VolComp", colorBlueGrey, styleHistogram);
``````

Solution 3:
Using composite recalculation tool, once all the composite is calculated, re run the composite at intervals, all the time frame charts are updated automatically, which seems to me a good solution and less time consuming and in trading hours works good.

Issue with Solution 3:

1. Cannot schedule this task there is no write up available to scheduling (may be i would have missed it searched whole night)

2. Advance Declines do not update dynamic and advance declines numbers calculated are wrong and remain static across the timeframe

``````_SECTION_BEGIN("AD Adv");
Plot( AdvIssues(), _DEFAULT_NAME(), ParamColor( "Color", ColorCycle ) );
_SECTION_END();

Plot( DecIssues(), _DEFAULT_NAME(), ParamColor( "Color", ColorCycle ) );
_SECTION_END();
``````
1. Looking for how to schedule "composite recalculation tool: with options of Number of advancing/declining issues, volume for base index, copy volume to all indexes, date range, and markets options. refer to attached picture preferable from OLE or outside AB (scheduler/scan) using windows scheduler as it gives lot of flexibility 2. Custom time in mins date range as this will reduce the processing time (have not explored the bars option)

3. Dynamic change of advance and decline across the time frames. OR

is there any better solution other than the above.

@Tomasz @fxshrat @portfoliobuilder and other gurus all your help is appreciated.

Thanks

Instead of your "solution 1" use iteration of category and use setForeign plus some trigger (like recognation of new bar having arrived).

``````/// @link https://forum.amibroker.com/t/calculate-composites-tool-automation/6571/2
listnum = 0; // your category number (e.g. watchlist n)
_N( symlist = CategoryGetSymbols( categoryWatchlist, listnum ) );

trigger = ParamTrigger( "Update Volume", "CLICK HERE" );// or use any other trigger like autotrigger at new bar

persistency = True;
staticname = "NSE_Vol_" + Interval();
//StaticVarRemove(staticname);

if ( trigger OR LastValue(StaticVarGet(staticname) == 0 ) ) {

sumvol = 0;
for ( num = 0; ( symbol = StrExtract( symlist, num, ',' ) ) != ""; num++ )
{
SetForeign( symbol );
sumvol += Nz(V);
RestorePriceArrays();
}

StaticVarSet( staticname, sumvol, persistency );
}

sumvol = StaticVarGet( staticname );

Plot( sumvol, "Vol", colorRed, styleHistogram, Null, Null, 0, 0, -60 );
``````
4 Likes

@fxshrat Thanks for the quick response!!!
How will be the performance in realtime trading hours, my solution 1 freezes in real time.

I just tried your afl off market hours (now), volume is not peculated across the time frames. I ran this on 5 min, checked the 10 min volume is empty  Any thought on Solution 3

@mahmed4 I used @fxshrat's code as a starting point and then made an attempt StaticVarAdd. I don't have my Index data intraday so I looked to see Daily/Weekly/Monthly and it appears to be working. I have made no attempt to use real time implementation and persistence of static variables here (as I know nothing about the subject).

If you are looking to have multiple time frame information all at the same time you will need to use Multi-time Frame functions.

``````// Using StaticVarAdd and run EXPLORE

// pick watchlist in AA window
wlnum = GetOption( "FilterIncludeWatchlist" );
List = CategoryGetSymbols( categoryWatchlist, wlnum );

if( Status( "stocknum" ) == 0 )
{
// cleanup variables created in previous runs
StaticVarRemove( "~sumVol*" );

for( n = 0; ( Symbol = StrExtract( List, n ) )  != "";  n++ )
{

SetForeign( symbol );

RestorePriceArrays();
}
}

svSumvol = StaticVarGet( "~SumVol" );

Plot( svSumVol, "Vol", colorRed, styleHistogram, Null, Null, 0, 0, -40 );

``````

Daily Weekly Monthly 4 Likes

The code works on any time frame!
Try to understand what it is doing instead of just copy&pasting!

The code uses trigger!
You have to define your custom trigger yourself since paramtrigger is just example one.

That being said in if statement replace

``````if ( trigger OR LastValue(StaticVarGet(staticname) == 0 ) ) {
``````

by

``````if( trigger OR LastValue(StaticVarGet(staticname)) == 0 ) {
``````

First one is not error or bug. But second one just sets bracket where it was actually intended to be set. I quickly wrote in editor (since this is not "make a wish" show here).

"LastValue(StaticVarGet(staticname)) == 0" just makes initial auto update if there is no filled static var yet.

You could just as well create second static var for initial updates

``````if( trigger OR IsNull(StaticVarGet(staticname + "_initial" )) ) {
// ...

StaticVarSet( staticname + "_initial", 1 );
}
``````
``````/// @link https://forum.amibroker.com/t/calculate-composites-tool-automation/6571/4
/// alternative INITIAL auto-update
listnum = 0; // your Watchlist number
_N( symlist = CategoryGetSymbols( categoryWatchlist, listnum ) );

trigger = ParamTrigger( "Update Volume", "CLICK HERE" );// or use any other trigger like autotrigger at new bar

persistency = True;
staticname = "NSE_Vol_" + Interval();
//StaticVarRemove(staticname + "*");

if ( trigger OR IsNull(StaticVarGet(staticname + "_initial")) ) {

if ( Status("stocknum") == 0 ) {
sumvol = 0;
for ( num = 0; ( symbol = StrExtract( symlist, num, ',' ) ) != ""; num++ ) {
SetForeign( symbol );
sumvol += Nz(V);
RestorePriceArrays();
}
StaticVarSet( staticname + "_initial", 1 );
StaticVarSet( staticname, sumvol, persistency );
}
}

sumvol = StaticVarGet( staticname );

Plot( sumvol, "Vol", colorRed, styleHistogram, Null, Null, 0, 0, -60 );
``````

Again trigger variable you have to define yourself (e.g. update at each new bar).

As for "solution 3".. AFAIK, there is no possibility to access "Composite recalculation tool" to make it auto-updating.

2 Likes

@portfoliobuilder @fxshrat will study more about trigger and staticvar and build upon THANKS
btw am not a programmer but have some programming backgroud