Re: need help following Janeczkoimplementing multiple time frame analysis in AmiBrok
Hi trash. I wanted to take the time to figure things out on my own before replying. I'm now starting to get a handle on this code (pasted below). For instance, I now understand (I think) the following:
IIf() creates an array that contains the value of the indicator at each bar number. Based on the periodicity in Backtester settings, each bar number corresponds to a date/time. AddToComposite() creates an artificial ticker symbol whose value at each time point is that of the indicator (i.e., it’s just the array created by IIf), and whose name is “~XYZ”+Name(), where Name() is the name of the symbol from which the indicator was created. I.e., it’s essentially a specialized export function, and as soon as you run the program you'll see the artificial ticker appear in the symbol list. Turning the array created by IIf() into an artificial ticker symbol allows it to be input into any other .afl file, via the use of Foreign(), which is essentially an import function. That way, you can import your indicator into, for instance, a plotting file: x=Foreign(…); Plot(x,…);. It’s called “Foreign” because it allows you to reference a ticker symbol different from (i.e., foreign to) the currently active one. [Personally, I think these would be far less cryptic if they were instead named "ExportArtTick" (or "CreateArtTick") and "ImportArtTick" (where ArtTick means artificial ticker). And I wish there were somewhere in the manual it had been explained in this fashion!]
Scan has to be run to create the indicator from the code. However, I have found it is NOT necessary to include any Buy statements to run Scan. All Buy=1 gives you is a listing, in the Scan window, of the value of the indicator at each time point (if you’re curious to see it) -- as expected, since it just tells you to Buy at every bar. But even if you don’t include Buy=1, the artificial ticker is still built when you run scan (as evidenced by its appearance in the symbol list, and the fact that you can plot it). Indeed, it even works if you set Buy=0. So I don’t understand what purpose Buy=1 serves, other than displaying the indicator values at each time point in the scan window. Of course, should you wish to backtest your code, you will need a Buy statement (for longs), but in that case you would use the actual Buy criterion, and not Buy=1. Curiously, nothing comes up when I search for “Buy” in Help, so I was unable to find detailed information about this reserved variable.
Now, returning to the outstanding issues from our previous exchange:
Regarding no. 1: Here’s the code that requires I hit “scan” twice before I see trades in the scan window:
**********
Count = 0;
result = 0;
for( i = 10; i <= 300; i++ )
{
TimeFrameSet( i * in1Minute );
m = MACD(12, 26 );
TimeFrameRestore();
m = IIf( TimeFrameExpand( m, i * in1Minute ) > 0, 1, -1 );
result = result + m;
Count++;
}
AddToComposite( result / Count, "~MACD"+Name(), "X" );
x = Foreign("~MACD"+Name(),"C");
Buy = Cross(x,-.5);
Sell= Cross(.5,x);
BuyPrice = SellPrice = Close;
*************
However, if I substitute this code after the closing bracket of the "for" loop, I only have to hit scan once:
x=result/Count;
Buy = Cross(x,-.5);
Sell= Cross(.5,x);
BuyPrice = SellPrice = Close;
So it seems it's the extra step of creating the artificial ticker, and then importing it back into the program, that accounts for the need to hit scan twice.
Regarding no 2: I can now explain the effect I was seeing: Let’s suppose you create the above composite indicator. Since it was built on a 1-minute periodicity, it has values for every minute. However, if you display it under a daily or weekly chart, you will only see its values at the close of each daily or weekly bar (i.e., its plotted value will only change once per day or week, respectively). That’s why, for these longer time frames, the frequency (rate of change per unit time) of the indicator decreased when I went from a daily to weekly view – because it went from changing once per day to once per week. However, we don’t see that effect at short periodicities (say, going from a 1-minute to a 5-minute bar display), because this is a slow indicator, and thus the time scale at which one sees changes is long relative to 1-minute or 5-minute bars. Hence changing the display from 1 minute to 5 minutes has no effect on the displayed frequency of the indicator. [In case this is not clear: suppose the true value of the indicator characteristically changes about once every few hours. Then, regardless of whether you display 1 minute or 5 minute bars, the displayed value of the indicator only will be seen to change once every few hours.]
So, in summary, at displayed time scales that are short relative to the characteristic true frequency of the indicator, changing the periodicity of the display has no effect on the displayed frequency of the indicator; however, at displayed time scales that are long relative the characteristic true frequency of the indicator, it is the displayed time scale that determines the displayed frequency of the indicator.