need help following Janeczkoimplementing multiple time frame analysis in AmiBroker

#1
need help following Janeczko's implementation of multiple time frame analysis

I tried asking this on another site and got no replies, so I thought I'd try here:).

I'm relatively new to Amibroker -- I'm currently evaluating the trial version -- and I'm trying (apparently unsuccessfully) to follow the instructions given by Tomasz Janeczko for implementing a "multiple time-frame MACD" at http://www.amibroker.com/docs/MTFIndicators.html. Specifically, he writes:
*******************
Formula 1. Multiple time-frame MACD. Note: in order to run this code and calculate multiple time-frame MACD, enter it into Automatic Analysis (AA) window, go to Settings window, make sure that Periodicity is set to 1-minute. Then press "Scan" button in the AA 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" );
Buy=1;
*****************
When you follow the instructions you're supposed to have your original price chart marked with buy and sell arrows, and to see a new pane that shows the composite indicator. In my case, there are no arrows on the original price chart, and the new pane that's supposed to contain the composite indicator is blank. Is there anything missing from his instructions I need to do? Or could it be that, because I'm evaluating the trial, something like this won't work? I've tried implementing a couple of other formulas, and they appear to work OK.

Here are the details, in case it helps:

I created a new database with a base period of 1 tick, and imported a month's worth of tick data (for a futures contract) using $TICKMODE 1; it imported without issue. I then generated a chart from this with 1-minute bars and clicked on it to activate it. I then copied the above code, clicked on Analysis->Formula Editor, pasted it there, saved it, changed the periodicity in "Backtester Settings" (I assume that's what he meant by "Settings") to 1 minute and hit "Scan."

Unfortunately, what I got was a table listing Buys only, one at the close of each and every minute (so clearly something isn't working here). When I click on the portfolio equity icon I get two panes: the upper one is the 1-minute bars of my data, but without any arrows marking trades. The lower one is blank and says, at the top:
"The formula 'Formulas\Drag-drop\_Portfolio_Equity.afl' is empty-no chart to display." When I click on the reports icon, I get a trade report which says there were 0 trades. Finally, when I click on "Charts" under the reports icon, it says, for each of the "Equity" boxes:
"Not enough data available. To plot any chart at least 3 data bars are needed, but there are only 0 bars in '~~ "
This of course doesn't make sense, since the main Analysis window lists the time and close of each and every minute bar -- so it does have the data.

I also repeated the above, this time importing minute data instead of tick data, and got the same result.

The only explanation I can offer of what's wrong is that it has something to do with the fact that I'm using the trial version:
Note that the trial version doesn't save databases. The message I get when the data is deleted is the same as that above ("...there are only 0 bars..."), suggesting that the problem is that the analysis isn't seeing any data (though this doesn't make sense, for the reason indicated above). So either I'm making a very basic mistake in implementing the above code (in which case I would greatly appreciate any assistance in correcting my error), OR the problems are a result of limitations inherent to the trial version (which would be unfortunate, since the trial is supposed to be fully-functional, to facilitate evaluation).
 
Last edited:

trash

Well-Known Member
#2
Re: need help following Janeczkoimplementing multiple time frame analysis in AmiBrok

And it's not because of trial. Before Scan make sure that you have set 1-minute interval in periodicity settings of AA window.

The arrows are not plotted because you have no shape plot. T.J. was just showing a picture.

So use this

Code:
x = Foreign("~MACD"+Name(),"C");
Buy= Cross(x, -0.5);
Sell = Cross(0.5,x);
BuyPrice = SellPrice = Close;

Buy = ExRem( Buy, Sell );
Sell = ExRem( Sell, Buy );

PlotShapes(IIf(Buy,shapeSmallUpTriangle,shapeNone),colorGreen,0,L,-15);
PlotShapes(IIf(Buy,shapeSmallCircle,shapeNone),colorWhite,0,BuyPrice,0);
PlotShapes(IIf(Sell,shapeSmallDownTriangle,shapeNone),colorRed,0,H,-15);
PlotShapes(IIf(Sell,shapeSmallCircle,shapeNone),colorWhite,0,SellPrice,0);
For plot of MTMACD in pane use this

Code:
x = Foreign("~MACD"+Name(),"C");

Plot( x, "MTMACD", colorBlue , styleThick ) ;

PlotGrid(0.5, colorGreen );
PlotGrid(-0.5, colorRed);
It's working. I've tested it.
 
#3
Re: need help following Janeczkoimplementing multiple time frame analysis in AmiBrok

Thanks once again, Trash! :clap:

The arrows code works beautifully. Also, the code is starting to make sense to me; for the arrows, for instance, you're using the definition of ~MACD established earlier to determine buy and sell signals, and then plotting arrows based on this.

For the scan, the reason I was getting strange results wasn't because of periodicity -- I had that set correctly. Rather, it was because I hadn't included buy and sell instructions. So I just borrowed from your arrows code, and added this to the end of TJ's Formula 1:

x = Foreign("~MACD"+Name(),"C");
Buy= Cross(x, -0.5);
Sell = Cross(0.5,x);
BuyPrice = SellPrice = Close;

Once I did that, the scan ran fine.
 
Last edited:

trash

Well-Known Member
#4
Re: need help following Janeczkoimplementing multiple time frame analysis in AmiBrok

Thanks once again, Trash! :clap:

The arrows code works beautifully. Also, the code is starting to make sense to me; for the arrows, for instance, you're using the definition of ~MACD established earlier to determine buy and sell signals, and then plotting arrows based on this.

For the scan, the reason I was getting strange results wasn't because of periodicity -- I had that set correctly. Rather, it was because I hadn't included buy and sell instructions. So I just borrowed from your arrows code, and added this to the end of TJ's Formula 1:

x = Foreign("~MACD"+Name(),"C");
Buy= Cross(x, -0.5);
Sell = Cross(0.5,x);
BuyPrice = SellPrice = Close;

Once I did that, the scan ran fine.

No ,the results are not strange

The original scan code by T.J. works too
Code:
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" );
Buy=1;
Since you are new you just don't know what's its purpose.
The buy column doesn't serve as a buy signal column. Buy = 1; is just there to get the scan running. So forget about the scan results. That code is just there to create the index. Nothing more nothing less.


To scan for signals, yes, you can use the following code.
Code:
x = Foreign("~MACD"+Name(),"C");
Buy= Cross(x, -0.5);
Sell = Cross(0.5,x);
BuyPrice = SellPrice = Close;
But that code doesn't create an index. That one is a signal or backtest code. The previous one is a creation code. Hope it's more clear now.
 

trash

Well-Known Member
#5
Re: need help following Janeczkoimplementing multiple time frame analysis in AmiBrok

BTW for shape plot you can also use this one without iif conditions

Code:
x = Foreign("~MACD"+Name(),"C");
Buy= Cross(x, -0.5);
Sell = Cross(0.5,x);
BuyPrice = SellPrice = Close;

Buy = ExRem( Buy, Sell );
Sell = ExRem( Sell, Buy );

PlotShapes(Buy*shapeSmallUpTriangle,colorGreen,0,L,-15);
PlotShapes(Buy*shapeSmallCircle,colorWhite,0,BuyPrice,0);
PlotShapes(Sell*shapeSmallDownTriangle,colorRed,0,H,-15);
PlotShapes(Sell*shapeSmallCircle,colorWhite,0,SellPrice,0);
 

trash

Well-Known Member
#6
Re: need help following Janeczkoimplementing multiple time frame analysis in AmiBrok

Buy = 1; means buy is true for every bar. So here it means calculate the composite for every bar.
 
#7
Re: need help following Janeczkoimplementing multiple time frame analysis in AmiBrok

Thanks Trash, your added explanations helped; it's becoming clearer now:
Formula 1 (with Buy=1) + "Scan" generates the composite only;
Formula 2 + 'Insert into blank pane' plots the composite;
Your arrows formula (with Buy and Sell criteria) + 'Insert into price pane' shows the trading points;
My modified Formula 1 (with Buy and Sell criteria) + "Scan" and "Backtest" generates trading signals for portfolio analysis.

The three bits of strangeness that remain are:

(1) When I use my modified Formula 1, I have to hit "Scan" twice to see the full list of trades (the first time it's blank or truncated).
(2) When I view the plot (Formula 2) on various intraday settings, as I go to longer time periods, it behaves as expected: I see the same plot, just more linearly compressed, because of the time compression. But when I go to a still longer time period -- the "daily" view -- I see a very different chart, with a much lower frequency of peaks and valleys per unit time.
(3) When I do a new analysis (say, with a modified Formula 1) and hit "Scan" and "Backtest," it automatically re-inserts the plot from the previous analysis into the chart (thus giving me a duplicate of the old plot in a new pane, which I then have to delete).

Perhaps I can get explanations of these behaviors from AmiBroker.
 
Last edited:

trash

Well-Known Member
#8
Re: need help following Janeczkoimplementing multiple time frame analysis in AmiBrok

regarding 1) please post your whole modified code. Otherwise I can not say anything about it.

regarding 2) that's because of x = Foreign("~MACD"+Name(),"C");
There you define the name of the symbol you wanna create via AddToComposite. And of course if there is new data it adds the new calculated values to that created index. The name of that symbol is defined in the formula.

BTW you can also use Exploration for creation of composite

just delete Buy = 1;

replace code with

Code:
barinrange = Status("barinrange");
Count = 0;
result = 0;

for( i = 10; i <= 300; i++ )
{
	TimeFrameSet( i * in1Minute );
	m =  IIf(barinrange, MACD(12, 26 ), 0);
	TimeFrameRestore();
	m = IIf( TimeFrameExpand( m, i * in1Minute ) > 0, 1, -1 );
	result = result + m;
	Count++;
}

if(Status("action") == actionExplore)
{
	AddToComposite( result / Count, "~MACD"+Name(), "X", atcFlagDefaults | atcFlagEnableInExplore);
	AddTextColumn("Index created", "Status", 1, colorLightGrey, colorDarkGreen, 90);
	Filter = Status("lastbarinrange");
}


then you can additionally control the length of the index via From-To and all other range settings in Analysis window because of m = IIf(barinrange, MACD(12, 26 ), 0);

Exploration is not different to Scan other than being a more advanced scan. You can add multiple columns there.


To the plot code you can additionally add refresh of 1 second. So you don't need to reload manually

Code:
RequestTimedRefresh(1);

x = Foreign("~MACD"+Name(),"C");

Plot( x, "MTMACD", colorBlue , styleThick ) ;

PlotGrid(0.5, colorGreen );
PlotGrid(-0.5, colorRed);
 
#9
Re: need help following Janeczkoimplementing multiple time frame analysis in AmiBrok

Thanks Trash -- will need a couple of days to reply.:)
 
#10
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.
 
Last edited:

Similar threads