Create my own composite

Hello AmiBroker Support Team,

i´m a very glad amibroker costumer. i wanna build my own stock composie with the symbols

JNJ
MO
BDX

i read, that i have to use the "addtocomposite'" function. but it doesnt work, i wanna just see, my own MyComposite in Chart
to analyze ist.

Please can you help me. Thank you so much.
AddToComposite("JNJ", "~MyComposite", "X" );
AddToComposite("MO", "~MyComposite", "X" );
AddToComposite("BDX", "~MyComposite", "X" );
graph0 = foreign("~MyComposite", "X");

First please read this: http://www.amibroker.com/guide/a_addtocomposite.html
Once you do, you will know that you need to run Scan (in Analysis window) to generate composite.
Then in indicator formula you can simply use PlotForeign.

@odin although I am not sure what your goal is, have you considered using StaticVarAdd ?

https://www.amibroker.com/guide/afl/staticvaradd.html

///@link https://forum.amibroker.com/t/create-my-own-composte/9372
// Using StaticVarAdd and run EXPLORE

List = "MO,JNJ,BDX"; 

if( Status( "stocknum" ) == 0 )
{
    // cleanup variables created in previous runs
        StaticVarRemove( "~sumClose*" );
		StaticVarRemove( "~SymbolCount*" );
		
    for( n = 0; ( Symbol = StrExtract( List, n ) )  != "";  n++ )
    {
        SetForeign( symbol );

        StaticVarAdd( "~sumClose",  Close );
        StaticVarAdd( "~SymbolCount", C != 0 );

        RestorePriceArrays();
    }
}

svSumClose = StaticVarGet( "~sumClose" );
svSymbolCount = StaticVarGet( "~SymbolCount" );
CompositeAveClose = svSumClose/svSymbolCount;

///////////////
// Explore
///////////////
Filter = Status( "stocknum" ) == 0;
SetOption("NoDefaultColumns",True);
AddColumn(DateTime(), "Date", formatDateTime);

AddColumn( svSumClose, "Sum of Closes", 1.2 );
AddColumn( svSymbolCount, "Symbol Count", 1.0 );
AddColumn( CompositeAveClose, "Composite Ave Close", 1.2 );

///////////////
// Chart
///////////////
Plot( CompositeAveClose, "CompositeAveClose", colorAqua, styleLine | styleThick);

image

Or a plot of the average of the 3 closes
image

6 Likes

thank you for all, answers.
i wanna test this, to run a scan.
@Tomasz is the Code right i have postet?

@portfoliobuilder thank you for this answer. is this then a realcomposite? whats is with stock splits?

@odin

I don't know what you consider a "real composite", as it is just an average of the stocks in the list. None of the major U.S. based Indexes are a plain average of their constituents (i.e. the DJIA, SP500, Nasdaq Composite etc).

As for stock splits, they are unrelated to this code which will calculate the Sum and the Average every bar for the data for those stocks in your database. If you have a data feed that supplies you with split adjusted data then the calculations will be split adjusted. If your data is different then the calculations will be different.

2 Likes

thank you so much! and sorry for late answer