Incorrect formula, was: AddToComposite

So the idea here is to find out what number of candles are red (open > close) and what number of candles are green (close > open) in the last 20 candles. Here is the AFL code for this

greenbars = Close > Open ;
redbars = Open > Close ;

sumgreenbars = Sum(greenbars,20);
sumredbars = Sum(redbars,20);

AddToComposite(sumgreenbars,"sumgreen","c");
sumgreenp = Foreign("sumgreen","c");
Buy = 0;
Filter = 0;
Plot(sumgreenp,"greenbars",colorGreen,styleLine);

AddToComposite(sumredbars,"sumred","c");
sumredp = Foreign("sumred","c");
Buy = 0;
Filter = 0;
Plot(sumredp,"redbars",colorRed,styleLine);

I am not getting the correct results with this code. Please help me

Filter = 0;
This will not filter anything.

Is your context AA or chart?

Defining multiple Filter doesn't help, only the last one will take effect.

Regarding the execution context, please note this statement from the Help file entry for AddToComposite():

AddToComposite function also detects the context in which it is run

(it works ONLY in scan mode, unless atcFlagEnableInBacktest or atcFlagEnableInExplore flags are specified) and does NOT affect composite ticker when run in Indicator or Commentary mode, so it is now allowed to join scan and indicator into single formula.

I am not filtering it. First I am scanning the afl and then insert it in the chart.

I am running the same scan mode and then use it on stocks

Your code is incorrect in so many places:

  1. Incorrect composite name: composite name SHOULD begin with tilde (it is required so it is added at the END of symbol list)
  2. Foreign used right after composite in the very same formula that attempts to create it- wrong. Foreign/Plot should be in CHART formula.
    You can't read composite right after AddToComposite because it is created/updated asynchronously. Foreign calls done during scan that creates composites would result in "partially updated" data. If you want synchronous stuff you need to use StaticVarAdd.
  3. Filter does not belong to Scan operation.
  4. Should turn on "pad and align".
2 Likes
  1. Yes have used " ~ " in the ticker section in add to composite function
  2. Just can't understand what you mean
  3. Filter is kept as 0 since I don't want to filter it. I am just interested in scanning it
  4. I have turned on pad and align. What do I fill in the reference symbol since I have 200 symbols on the basis of which I will create addtocomposite indicator

Can you please help me with this ? Also I watched the youtube video of DaveASXWatch for add to composite function.

  1. No, you didn't. You wrote "sumgreen" . There is no tilde. You should have written "~sumgreen"
  2. Read (or re-read) the manual http://www.amibroker.com/guide/a_addtocomposite.html
    Composites require two separate steps: a) creating composite b) reading composite. First step is usually done in Analysis/Scan, second can be done in any place but typically you display composite charts. Therefore the best is to have two formulas each doing its thing.
  3. That is not the point. Filter is a command for exploration, not scan. Having Filter = 0; does not have any sense because it works only for Exploration, it has no effect on Scan and Filter=0 in Exploration it says "don't display ANY results" http://www.amibroker.com/guide/h_exploration.html If you want to display exploration and "not filter anything" as you wrote, you should have written Filter=1; because that means don't filter (i.e. display ALL results).
  4. How did you turn pad and align without specifying reference ticker already? Did you read the manual: http://www.amibroker.com/guide/w_settings.html it explains "pad and align" option
1 Like