A chart that represents the sum of two symbols, was: Adding two scripts

Hi,

If somebody can advice any function in Amiboker to join two scrips (add the values) to show in a single price chart, a line chart on closing prices would be good enough.

Thanks in advance.. Regards

Reading docs does not hurt
http://www.amibroker.com/guide/h_dragdrop.html

1 Like

Thank You Tomasz, for the suggested readings from the guide. It has greatly helped me in my chart layouts with complex indicator overlays especially by “StyleOwnScale setting”.

But still I am unable to find the solution to my initial query that pertains to adding the prices of two different stocks and showing it in single price chart.

If you can help me form a chart (say for e.g. stk1_Price (100) + stk2_Price (50) = Combine_stk1_stk2_Price(150).
Thank you once again. Regards…

You have to use Foreign function.
https://www.amibroker.com/guide/afl/foreign.html

stck1 = C; // price of selected symbol;

symbol2 = "Type_Your_Other_Symbol_Here";
stck2 = Foreign( symbol2 , "C" ); // foreign symbol's price data

combine = stck1 + stck2;
2 Likes

Your original question talked about “scrips” which could only be interpreted as mistyped “script”.
Script is just another word for program, formula. But apparently (after reading your second post) you did not mean script but SYMBOL (stock, future, what ever other tradable instrument).
That is why it is extremely important to spell the words correctly and use correct words. If you wanted to add prices of two symbols and draw them as single chart you need to do what @fxshrat wrote.

But this is for every new poster: make you questions CLEAR and properly spelled.

1 Like

Thank you very much Fxshrat for the solution, although I don’t have a programming knowledge but I hope I would be able to utilise the code provided by you properly.

I will try it out it now.

Regards

The Knowledge Base contains at least 2 articles dealing with that:
http://www.amibroker.com/kb/2007/03/26/how-to-chart-spreads/
http://www.amibroker.com/kb/2016/01/26/how-to-show-price-ratio-between-two-symbols/

the only difference is that instead of subtracting you are adding prices.

is there another way where it is a live composite index withought running scan …or running a batch where composites are calculated when u have foreign tickers data backfilling

workarounds are there but tradingview provides this

Along this topic, I'd like to combine two bar charts from two different equities into a single bar chart. The open price, should be the addition of the two different open prices, the high, the addition of the two highs, the low, the addition of the two lows and the close, the addition of the two closes. In the AFL example below, I can get the exploration to show my combined open, high, low and close prices. I thought that I could use the PlotOHLC function to do that for a chart, but I don't get the results that I'm looking for. Does anyone have an idea how I might be able to do this?

SetForeign("NUGT");
Open1 = Open;
High1 = High;
Low1 = Low;
Close1 = Close;
RestorePriceArrays();

SetForeign("DUST"); 
Open2 = Open;
High2 = High;
Low2 = Low;
Close2 = Close;
RestorePriceArrays();

CombinedOpen = Open1 + Open2;
CombinedHigh = High1 + High2;
CombinedLow = Low1 + Low2;
CombinedClose = Close1 + Close2;

//Exploration
Filter = 1;
AddColumn(Open1,"Open1",1.2);
AddColumn(Open2,"Open2",1.2);
AddColumn(CombinedOpen,"CombinedOpen",1.2);
AddColumn(High1,"High1",1.2); 
AddColumn(High2,"High2",1.2); 
AddColumn(CombinedHigh,"CombinedHigh",1.2); 
AddColumn(Low1,"Low1",1.2);
AddColumn(Low2,"Low2",1.2);
AddColumn(CombinedLow,"CombinedLow",1.2);
AddColumn(Close1,"Close1",1.2);
AddColumn(Close2,"Close2",1.2);
AddColumn(CombinedClose,"CombinedClose",1.2);

//Charting
SetChartOptions(1, chartShowArrows | chartShowDates); 
_N(Title = EncodeColor(colorAqua)+"Combined" +EncodeColor(colorWhite)+StrFormat("  {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}} ", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
PlotOHLC( CombinedOpen, CombinedHigh, CombinedLow, CombinedClose, "Combined", colorDefault , styleCandle); 

I was able to correct it myself. In the second last line, I changed O,H,L,C to CombinedOpen, CombinedHigh, CombinedLow, CombinedClose

The original code was doing exactly what I wanted it to do, except that the prices on the chart title were not reading correctly. Here is my corrected code:

SetForeign("NUGT");
Open1 = Open;
High1 = High;
Low1 = Low;
Close1 = Close;
RestorePriceArrays();

SetForeign("DUST"); 
Open2 = Open;
High2 = High;
Low2 = Low;
Close2 = Close;
RestorePriceArrays();

CombinedOpen = Open1  + Open2;
CombinedHigh = High1 + High2;
CombinedLow = Low1 + Low2;
CombinedClose = Close1 + Close2;

//Exploration
Filter = 1;
AddColumn(Open1,"Open1",1.2);
AddColumn(Open2,"Open2",1.2);
AddColumn(CombinedOpen,"CombinedOpen",1.2);
AddColumn(High1,"High1",1.2); 
AddColumn(High2,"High2",1.2); 
AddColumn(CombinedHigh,"CombinedHigh",1.2); 
AddColumn(Low1,"Low1",1.2);
AddColumn(Low2,"Low2",1.2);
AddColumn(CombinedLow,"CombinedLow",1.2);
AddColumn(Close1,"Close1",1.2);
AddColumn(Close2,"Close2",1.2);
AddColumn(CombinedClose,"CombinedClose",1.2);

//Charting
SetChartOptions(1, chartShowArrows | chartShowDates); 
_N(Title = EncodeColor(colorAqua)+"Combined" +EncodeColor(colorWhite)+StrFormat(" {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}} ", CombinedOpen, CombinedHigh, CombinedLow, CombinedClose, SelectedValue( ROC( C, 1 ) ) ));
PlotOHLC( CombinedOpen, CombinedHigh, CombinedLow, CombinedClose, "Combined", colorDefault , styleCandle); 
2 Likes

It was kindly pointed out to me that it wouldn't be accurate to add the high and low values of the two different equities. Even though I'm using daily bars, the two different highs could be occurring at different times of the day as well as the two different lows. The open and close happen at the same time each day for both equities, so you could combine them. This is my corrected AFL where I only chart the added open and close for the two equities:

SetForeign("NUGT");
Open1 = Open;
Close1 = Close;
RestorePriceArrays();

SetForeign("DUST"); 
Open2 = Open;
Close2 = Close;
RestorePriceArrays();

CombinedOpen = Open1  + Open2;
CombinedClose = Close1 + Close2;

//Exploration
Filter = 1;
AddColumn(Open1,"Open1",1.2);
AddColumn(Open2,"Open2",1.2);
AddColumn(CombinedOpen,"CombinedOpen",1.2);
AddColumn(Close1,"Close1",1.2);
AddColumn(Close2,"Close2",1.2);
AddColumn(CombinedClose,"CombinedClose",1.2);

//Charting
SetChartOptions(1, chartShowArrows | chartShowDates); 
_N(Title = EncodeColor(colorAqua)+"Combined" +EncodeColor(colorWhite)+StrFormat(" {{INTERVAL}} {{DATE}} Open %g, Close %g (%.1f%%) {{VALUES}} ", CombinedOpen, CombinedClose, SelectedValue( ROC( C, 1 ) ) ));
PlotOHLC( CombinedOpen, CombinedOpen, CombinedClose, CombinedClose, "Combined", colorDefault , styleCandle); 

I want to just combine closing price of multiple scripts into one close line chart.I am not able to achieve it using above AFl code.

With the above code, you can just remove everything that refers to the open price and you end up with this to combine close prices:

SetForeign("NUGT");
Close1 = Close;
RestorePriceArrays();

SetForeign("DUST"); 
Close2 = Close;
RestorePriceArrays();

CombinedClose = Close1 + Close2;

//Exploration
Filter = 1;
AddColumn(Close1,"Close1",1.2);
AddColumn(Close2,"Close2",1.2);
AddColumn(CombinedClose,"CombinedClose",1.2);

//Charting
SetChartOptions(1, chartShowArrows | chartShowDates); 

Plot( CombinedClose, "Combined", colorDefault , styleLine); 

Another method would be to use AddtoComposite, you can learn about that here:
http://www.asxmarketwatch.com/2015/11/amibroker-q-a-how-to-create-the-52-week-highlow-index-with-addtocomposite/

2 Likes