AddToComposite returns double value

Hello,

I am trying to add values to composite ticker using this simple code:

value=0;
for( i = 1; i < BarCount; i++ ) 
{ 
value[i]=1;
}

AddToComposite( value, "~Test", "C");

It creates the composite ticker "~Test", but instead value "1" there is value "2".

composite

If I put different value into the formula, I always get double in composite. Any tip what can be wrong?

Thank you.

Apparently you've run your code twice (or for TWO ticker list) so AddToCompostite added 1 TWICE to the composite. 1+1 equals 2.

Read the manual:
http://www.amibroker.com/guide/a_addtocomposite.html
and
http://www.amibroker.com/f?addtocomposite

So, AddToComposite as it name says, does not "create" composite. It ADDS to composite.

It is worth noting that code is executed potentially many times (for example when you press "Verify formula" or "Apply formula" when you run the code in chart, etc) and for every symbol in Analysis.

Besides, your looping code does not make sense. Instead of what you are doing you should just write:

value = 1; // ALL that is needed

Or if you want zero for first bar:

value = IIF( BarIndex() == 0, 0, 1 ); // all that is needed

or shorter

value = BarIndex() != 0;
2 Likes

Hello,

It is weird but I experience the same and I am almost sure that I am not running twice.

The most interesting thing is that it sometimes does not double the array while copying. 5-10% of time. So couldn't catch in the screen video.

You can find below the screen video link and AFL file link. The quality of the video has been low, sorry. I can record a new one if needed.

You can notice in the video that "current symbol" and "last 1 bar" is selected to prevent multiple runs.

By the way, the AFL is prepared to merge the equities of two or more backtest results. Anyone can make use of it.

Link to AFL file: https://drive.google.com/file/d/1qBA3zxYAoDZaL1lZiPNN9qGPNiJJ_IoQ/view?usp=sharing
Screen video: https://youtu.be/-8MmaAsf4Ng

Regards,

You can't be "almost sure". Either you are sure or you are not. And because "almost" word, you are not sure. To be sure use debug techniques, especially TRACE see: How do I debug my formula?

Also, "last 1 bar" setting for composites is plain wrong.

1 Like

Thank you for your philosophical approach. I agree with you on I am not sure and not agree with you that I can't be "almost sure". You are thinking digitally. May be this is a side effect of being a very good coder. To me, there are infinite number of "not sure" situations and some of them are closer, some are more close to being sure. "Almost sure" means a closer one.

Thank you also for your ssuggestion. I will check with TRACE. Please also let me know if you also have a shortcut solution idea for my problem.