Australian (ASX) Stock Market Forum

Amibroker FAQ

I've attached a chart to better show what I'm trying to do. The blue linear regression line on the attached chart obviously fits the current trend much better than the red regression line - the standard error is lower for the blue line (those are standard error channels on the chart).

I want Amibroker to calculate the regression periods length that best fits the current trend for each stock/chart. My thoughts are that StdErr may be a good way to do this, as StdErr should be lowest for the best fit line.

Any thoughts?
 

Attachments

  • LinearReg.jpg
    LinearReg.jpg
    50.6 KB · Views: 4
Yes I see what you want now, I think. I'm pretty sure the shorter the period, the smaller the std error channel, the better the fit, in all but a few instances, no?
 
I'm pretty sure the shorter the period, the smaller the std error channel, the better the fit, in all but a few instances, no?

Hmmm, you may be right. Could possibly give a higher weighting to the longer periods???

Or is there a better way to do this? I suppose I want the linear regression line to start at the most recent change in trend. This is relatively easy to visualise when looking at a chart, but how do you translate that visual info in to a language that Amibroker can understand? Use the zig-zag indicator to identify the most recent trough perhaps?

Has anyone here done anything similar? Any ideas?
 
Yes I see what you want now, I think. I'm pretty sure the shorter the period, the smaller the std error channel, the better the fit, in all but a few instances, no?

Greetings --

If you are trying to fit a regression line through a price series, in general the fewer the data points, the better the fit and the less the residual error.

If you care about what happens beyond the data being fit, be wary of very short lookback periods, the regression line may not be fitting to a general condition, but rather to the variability that exists in that short data set.

In general, the lookback should be as short as is practical. That is, short enough to fit well and adapt to changes over time, and long enough to be fitting the signal rather than the noise.

There is no rule for how long that is. For example, the optimal lookback period when fitting a regression line to a money market fund will be different than when fitting a regression line to the price of gold. The only way to determine is to run some tests. After the tests have indicated the best lookback length, be sure to run walk forward tests and observe the goodness of the fit to the data immediately following the lookback period -- that is, run some out-of-sample tests.

Thanks,
Howard
 
alterego, the most recent change in trend would be best identified with zig-zag of say 5%, as you say. Get AB to note the date of the most recent zig trough/peak eg. trough (array, change, n=1), then draw your regression from there. Be careful with zig and make sure it's not referencing future data during backtests etc.
 
alterego, the most recent change in trend would be best identified with zig-zag of say 5%, as you say. Get AB to note the date of the most recent zig trough/peak eg. trough (array, change, n=1), then draw your regression from there. Be careful with zig and make sure it's not referencing future data during backtests etc.

Greetings --

ZigZag Always references future data. It is useful for searching for tradable ideas, but should not be used in backtests and cannot be used in live trading systems.

Thanks,
Howard
 
and cannot be used in live trading systems.

As against

Be careful with zig and make sure it's not referencing future data during backtests etc.

SPOT THE DIFFERENCE
 
What the hell is wrong with you tech/a? You message me twice telling me you're going to email me something, then send nothing, then you crap on like a little kid when you are actually WRONG about using zig zag in a backtest. You CAN use it - you just need to be careful to make sure that price has fallen down more than %change, as per: http://www.amibroker.com/members/traders/11-2003.html, a valid zig zag backtest method. And it CAN be used for live trading. Get your facts straight or STFU, twit.
 
What the hell is wrong with you tech/a? You message me twice telling me you're going to email me something, then send nothing, then you crap on like a little kid when you are actually WRONG about using zig zag in a backtest. You CAN use it - you just need to be careful to make sure that price has fallen down more than %change, as per: http://www.amibroker.com/members/traders/11-2003.html, a valid zig zag backtest method. And it CAN be used for live trading. Get your facts straight or STFU, twit.

Greetings all --

I'll try to add light to this topic without increasing the level of the heat. No flames, please.

Please be very careful in using the Zig function. It does look into the future and it cannot be used in a real trading system. Zig connects points that are higher or lower than the points that surround them by some percentage. When new data is added, that new data can change the location of a "Zig High" or a "Zig Low". You cannot be certain that any bar is really a Zig Low until later data confirms that.

One of the indications that an indicator looks into the future is that signals will change as new data is added to the series. AmiBroker has the "Bar Replay" feature that lets you watch the generation and display of indicator values as data is added. Try this:

Code up an indicator that is based on Zig. Just identifying a Zig Low is adequate.
// ZigIndicator.afl
zzz = Zig( C, 2 );
Plot( C, "C", colorBlack, styleCandle );
ZigLow = ( zzz < Ref( zzz, -1 ) ) && ( zzz < Ref( zzz, 1 ) );
// Ref(zzz,1) is a forward reference
ZigHigh = ( zzz > Ref( zzz, -1 ) ) && ( zzz > Ref( zzz, 1 ) );
shapes = ZigLow * shapeUpArrow + ZigHigh * shapeDownArrow;
shapecolors = IIf( ZigLow, colorGreen, colorRed );
PlotShapes( shapes, shapecolors );

Plot this.
Then run Bar Replay and watch as new data is added. The red and green arrows will appear, then sometimes disappear as new data is recognized. Or they will suddenly appear on a bar some number of bars in the past. An arrow is never shown at the current bar. It is always at least one bar late, sometimes several bars late.

If the code had been:
Buy = ZigLow;

Then there would have been some buy signals that later disappeared or buy signals that appeared late.

For example, after the close of trading on some day, the system tells me to enter a long position, which I do. A few days later, new data shows that that was not a ZigLow and the buy signal disappears. But I have a real long position taken with real money. The system will never show a Sell because it now has no record that there was ever a Buy.

Or -- On Tuesday I get no signal. On Wednesday I get no signal. On Thursday the signal to buy on Tuesday appears.

I call my broker and enter an order to buy at the closing price of Tuesday. She gets free drinks that evening as she tells all her colleagues about me and my trading system.

----------

From the AmiBroker User's Guide:

ZIG
- zig-zag indicator Basic price pattern detection
(AFL 1.1)


SYNTAX zig(ARRAY, change )
RETURNS ARRAY
FUNCTION Calculates the minimum % change Zig Zag indicator. Caveat: this function is based on Zig-Zag indicator and may look into the future - this means that you can get unrealistic results when back testing trading system using this indicator. This function is provided rather for pattern and trend recognition formulas.
EXAMPLE zig(close,5)

--------------

The User's Guide is being too generous. Zig ALWAYS needs future data before it can determine whether a given price is and always will be some percentage higher or lower than its neighbors.

As stated, it is an excellent tool for evaluating the features of an issue -- such as how much price moves and at what rate. But it is NOT USABLE for real time trading.

--------------

Thanks for listening,
Howard
 
What the hell is wrong with you tech/a? You message me twice telling me you're going to email me something, then send nothing, then you crap on like a little kid when you are actually WRONG about using zig zag in a backtest. You CAN use it - you just need to be careful to make sure that price has fallen down more than %change, as per: http://www.amibroker.com/members/traders/11-2003.html, a valid zig zag backtest method. And it CAN be used for live trading. Get your facts straight or STFU, twit.

Tried 3 times to send you the PDF but bounces back each time.
as for Zig Zag.
Go ahead and use it in Backtesting if you think its accurate.
I'm sick of arguing with fools they just beat you with experience everytime!
 

Attachments

  • Email.gif
    Email.gif
    7.6 KB · Views: 114
howard, I knew that about zigzag already. What can I do but repeat:

Look at: http://www.amibroker.com/members/traders/11-2003.html

This link, if you click it, shows how zig zag can be used reliably for backtesting and real trading, if you're careful. It was published in Stocks and Commodities Magazine, November 2003.

It references future data in it's calculation (of course, that's the very nature of zig zag), but not in its execution, because the execution is delayed, which you can see with the trade arrows.

Graham Kavanagh is an expert at Amibroker and i bet he would confirm what I'm saying here....if he still is an active member.

tech, stop coat-tailling off howard's comments. It's a bad look.
 
Think you had better checkout who Howard is and His association with Amibroker.

tech, stop coat-tailling off howard's comments. It's a bad look.

I was the first to bring it up and got flamed.(around 12 yrs ago) I keep bringing it to peoples attenion whenever I see it.
Howard was good enough to clarify.
I want to keep up my "look". But thanks for your concern.
 
Can someone please shed some light on what I'm doing wrong? I'm getting some very odd results. All I'm trying to do is find the number of bars since the most recent occurrence of the LLV(L,100)

I've tried:

Periods = LLVBars(L,100);

but if the lowest low was hit twice (or more) in the last 100 days, it seems to pick the 1st occurrence in the 100 days (ie. the oldest occurrence), not the most recent one. (Eg. AAK says 27 bars instead of 5)

so I tried:

Periods2 = BarsSince(L == LLV(L,100));

but this often returns periods of well over 200 or more days!! How could this be? :confused: (Eg. AAG says 275 bars when it should be 26)

I would've thought those 2 expressions above would produce the same result, but they don't. Run this exploration on the most recent day and see what I mean:

Periods = LLVBars(L,100);
Periods2 = BarsSince(L == LLV(L,100));

AddColumn(Periods,"Periods",1.0);
AddColumn(Periods2,"Periods2",1.0);

Filter = 1;


What's going on? What's the correct way to do this?

thanks
 
Periods is giving the LLV for L within 100 bars of the last bar (or selected bar). Periods2 is giving BarsSince the LLV from any bar in the array Periods. Try the following plot and I think you will see this:

x = LLV( L, 100 );
y = BarsSince( L == x ));//does not include current bar
Plot( x, "", colorRed );
Title = "x = " + x + " y = " + y;
 
Ok, I think I see what the BarsSince function is doing. That's not what I want.

But, Periods = LLVBars(L,100); should produce the result I'm after, shouldn't it? It works in most cases, but when there is more than one day that touched the lowest low (of last 100 bars from the most recent bar) it selects the oldest occurrence in the 100 day window rather than the most recent occurrence, as per my example above (AAK says 27 bars instead of 5). How can I get it to give me the most recent occurrence?

thanks
 
LLV() should give you the low of the 1st trough. I modified the price data for SPY so that the trough lows on 5/25/10 and 6/8/10 were the same. The previously posted "x,y" code returned the low and bars for the first trough looking backward from the current bar. So, as far as I can tell all is OK.
 
LLV() should give you the low of the 1st trough. I modified the price data for SPY so that the trough lows on 5/25/10 and 6/8/10 were the same. The previously posted "x,y" code returned the low and bars for the first trough looking backward from the current bar. So, as far as I can tell all is OK.

Hi Colin,

Your code above doesn't seem to give the correct value. See attached example. X is correct at 1.7, but Y is clearly wrong, as I count 23 bars from the last bar to the low of 1.7, not the 328 that it says on the screen.
 

Attachments

  • llv-test.jpg
    llv-test.jpg
    40.9 KB · Views: 2
However, if I replace your line of:

y = BarsSince( L == x );

with:

y = LLVBars(L,100);

it then gives the correct value of 23. See attached. HOWEVER, this code does not work if there are 2 or more days at that same low value. I'll upload another example of that in the next post.
 

Attachments

  • llv-test2.jpg
    llv-test2.jpg
    41.5 KB · Views: 1
Here's the example of more than one day on the same low.

As you can see, in this example, using the same formula as above:

y = LLVBars(L,100);

gives a value of 15. The most recent touching of the low was 10 bars ago, not 15. It was touched at 15 days ago, 14 days ago, and 10 days ago. It has selected the oldest one instead of the most recent.

However your formula of BarsSince( L == x ) gives the correct value of 10 in this instance, but gives the wrong value in the previous example in the last post. So maybe the solution is to use the lowest of the 2 formulas, ie. MIN( LLVBars(L,100), BarsSince( L == x ))
 

Attachments

  • llv-test3.jpg
    llv-test3.jpg
    48.6 KB · Views: 3
Top