AddToComposite multiplies Close by four

I want to create a new symbol called ~SilverGoldRatio to view in all my charts and layouts.

Unfortunately my formula multiplies the Gold and the Silver prices by four. Here are two lines which show the problem:

Silver = Foreign( "XAGFIX", "C" );
ADDTOCOMPOSITE(Silver, "~Silver", "X");

XAGFIX is the existing Silver security, with daily bars.
~Silver, which I hope would duplicate XAGFIX, instead shows the closing value multiplied by four. Switching "C" and "X" in either line does not fix the problem.

Using Amibroker Beta 6.39.1

Try swapping the lines about.

ADDTOCOMPOSITE(Silver, "~Silver", "X");
Silver = Foreign( "XAGFIX", "C" );

Thanks, but this does not work because you first need to define the array Silver.

What does that mean exactly?

  1. Do you mean that O H L C fields show same value?
  2. Or do you mean that e.g. you get value 4 instead of 1 in your cloned symbol's Close price field?

If it is 1. then do like this:

SetForeign( "XAGFIX" );
SilverO = Open;
SilverH = High;
SilverL = Low;
SilverC = Close;
RestorePriceArrays();

Buy = 0;
ADDTOCOMPOSITE(SilverO, "~Silver", "O");
ADDTOCOMPOSITE(SilverH, "~Silver", "H");
ADDTOCOMPOSITE(SilverL, "~Silver", "L");
ADDTOCOMPOSITE(SilverC, "~Silver", "C");

If 2. then set Apply to: Current in analysis tool bar.
23

Hello Fxshrat,
Both 1 and 2 occurred.
After using your code, 1 is fixed. ~Silver now contains different OHLC values.

However, all OHLC values have been multiplied by 4.

Thanks for your help.

Insert XAGFIX in ticker field and hit enter.
And run this code without Foreign.

Buy = 0;
ADDTOCOMPOSITE(O, "~Silver", "O");
ADDTOCOMPOSITE(H, "~Silver", "H");
ADDTOCOMPOSITE(L, "~Silver", "L");
ADDTOCOMPOSITE(C, "~Silver", "C");

23

(Turn off pad&align if you just want to create a clone).

Click Scan.

~Silver now draws a line with OHLC all set to 0.

The code works fine here.

Check your data of XAGFIX in quote editor!

You are doing things wrong. You should NOT be using Foreign.

You should select "XAGFIX" as CURRENT SYMBOL and run Analysis with Apply to set to "Current symbol". Also check your settings and disable "Pad and align to reference symbol".

If you use Foreign, then depending on your Analysis settings you may be adding the same thing multiple times (if Apply To is not set to single symbol, but to many), and/or you may be subject to padding (if your Apply To symbols have different (misaligned) timestamps than your foreign symbol.)

Proper AddToComposite usage is covered in the Users' Guide:
http://www.amibroker.com/guide/a_addtocomposite.html

Thank you Tomasz and Fxshrat.

The code is now working correctly and is able to produce a composite ~Silver which is an identical copy of the symbol XAGFIX.

After using Tomasz's suggestions, ~Silver still contained wrong values. Two more things were required. Volume needed to be included with OHLC in the AddToComposite. Secondly, all the fields must be initialised, and to OHLCV rather than 0.

Here is the code:

SilverO = Open;
SilverH = High;
SilverL = Low;
SilverC = Close;
SilverV = Volume;

ADDTOCOMPOSITE(SilverO, "~Silver", "O");
ADDTOCOMPOSITE(SilverH, "~Silver", "H");
ADDTOCOMPOSITE(SilverL, "~Silver", "L");
ADDTOCOMPOSITE(SilverC, "~Silver", "C");
ADDTOCOMPOSITE(SilverV, "~Silver", "V");

What are you talking about?

You do not need to initialize!
Once again if you do this

Then you get proper cloned symbol already.

(BTW, in analysis settings you should also set periodicity to base time interval of symbol to be cloned)


What did you do actually??

Did you do this?


O = 0;
C = 0;
H = 0;
L = 0;

Buy = 0;
ADDTOCOMPOSITE(O, "~Silver", "O");
ADDTOCOMPOSITE(H, "~Silver", "H");
ADDTOCOMPOSITE(L, "~Silver", "L");
ADDTOCOMPOSITE(C, "~Silver", "C");

Or

SilverO = 0;
SilverH = 0;
SilverL = 0;
SilverC = 0;
SilverV = 0;

Buy = 0;
ADDTOCOMPOSITE(SilverO, "~Silver", "O");
ADDTOCOMPOSITE(SilverH, "~Silver", "H");
ADDTOCOMPOSITE(SilverL, "~Silver", "L");
ADDTOCOMPOSITE(SilverC, "~Silver", "C");
ADDTOCOMPOSITE(SilverV, "~Silver", "V");

No one told you to do that! Where??

What a waste of valuable time again.

1 Like

To Fxshrat,
I have just run the code without initialisation that you suggested above, and it does not work. It produces ~Silver with all fields = 0.
All fields must be initialised correctly otherwise it will not work.
Thanks for your help.

Please upload your analysis project file with ENTIRE AFL that you run.

E.g. Google drive or elsewhere (also include XAGFIX of your database (latter one not really required but just to show you same price)).

It works out of the box without any 'initialization'. @fxshrat code works fine as well. Just follow precisely what was told you a number of times. Remember to run "SCAN" not any other mode because by default composites are meant to be used with scans. Recommended reading: Calculating multiple-security statistics with AddToComposite function

On a side note:
When posting the formula, please make sure that you use Code Tags (using </> code button) as explained here: How to use this site.

Using code button

Code tags are required so formulas can be properly displayed and copied without errors.

I have discovered what causes an error - selecting the ~composite you are creating as the current symbol. Unfortunately, when you are developing and testing a new formula the last symbol you usually look at is the new ~composite, so it remains selected.

It is actually possible to use any other symbol (except the ~composite being created), but unfortunately the newly created ~composite mirrors the dates of the bars in the selected current symbol, so if it has missing bars, is not up to date, or is a recent issue then the ~composite will be missing similar bars. This is why, as Thomasz says, one of the source symbols should be selected.

Another problem that sometimes occurs after running the scan is when I look at the chart of the newly-created ~composite, it is blank and the x axis shows no dates. Tapping the End key fixes it and everything displays correctly.

Here is my code to create a new composite silver/gold ratio from the silver and gold symbols XAGFIX and XAUFIX:

SilverO = Foreign( "XAGFIX", "O" );
SilverH = Foreign( "XAGFIX", "H" );
SilverL = Foreign( "XAGFIX", "L" );
SilverC = Foreign( "XAGFIX", "C" );
SilverV = Foreign( "XAGFIX", "V" );

GoldO = Foreign( "XAUFIX", "O" );
GoldH = Foreign( "XAUFIX", "H" );
GoldL = Foreign( "XAUFIX", "L" );
GoldC = Foreign( "XAUFIX", "C" );
GoldV = Foreign( "XAUFIX", "V" );

ADDTOCOMPOSITE(SilverO/GoldO, "~AG_AU", "O");
ADDTOCOMPOSITE(SilverH/GoldH, "~AG_AU", "H");
ADDTOCOMPOSITE(SilverL/GoldL, "~AG_AU", "L");
ADDTOCOMPOSITE(SilverC/GoldC, "~AG_AU", "C");
ADDTOCOMPOSITE(SilverV/GoldV, "~AG_AU", "V");

In your use case you should better create a watch list with one symbol for example "XAGFIX" and use Apply to: "Filter" and select that watch list. This will make your scan independent from currently selected symbol.

As to refresh, you can add

SetOption("RefreshWhenCompleted", True );

and it will refresh your composite without need to press any keys.

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.