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
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.
Incorrect composite name: composite name SHOULD begin with tilde (it is required so it is added at the END of symbol list)
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.
Yes have used " ~ " in the ticker section in add to composite function
Just can't understand what you mean
Filter is kept as 0 since I don't want to filter it. I am just interested in scanning it
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.
No, you didn't. You wrote "sumgreen" . There is no tilde. You should have written "~sumgreen"
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.
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).