Download software Tutorial videos
Subscription & data-feed pricing Class schedule


New account application Trading resources
Margin rates Stock & option commissions

Attention: Discussion forums are read-only for extended maintenance until further notice.
Welcome Guest, please sign in to participate in a discussion. Search | Active Topics |

Zig Zag Rate this Topic:
Previous Topic · Next Topic Watch this topic · Print this topic ·
thnkbigr
Posted : Wednesday, November 4, 2009 1:48:28 PM
Platinum Customer Platinum Customer

Joined: 3/31/2006
Posts: 3,207
Bruce,

I recently have started to take some time to go through the topics posted by other users in the forum to get ideas and mainly learn the realcode language with all the other codes you write.

I have read a few topics that the user is trying to define uptrend and downtrends and I think this will do it. I am very interested to see if this is possible since it would allow me to count the Waves.

The Zig Zag % indicator zig zags when price reverses by X %. The % input that I might choose such as 10% can be too big on some stocks and too small on others.

For example plot a 10% zig zag on LVS from the March lows or even before it doesn't define anything since it's too small comparing to the stocks volatility while at the same time 10% can be too big on others.

I am trying to see if we can create an indicator that will zig zag when the stocks reverses at some multiple of its ATR. 

If the stock drops by let say 3 * ATR from it High to Low the indicator will reverse down and when the stock rises from Low to High by 3 * ATR it will reverse back up. This way you are letting the character of the stock define its trend rather your input. 

I tried to do this myself and there are two things I just can't define MinLow at the last reversal Down and MaxHigh at the last reversal Up. I have an idea how to do the Lowsince and MaxSince from your code for Auto LR realcode but I am not there yet.

Please let me know what I am missing?

thx


'# Cumulative
'# Multiplier = userinput.integer = 3
Static ZZ As Single
Static ATR As Single
Static TR As Single
Static sumWeight As Single
Static termRatio As Single = 13 / 14
Static LowSince As Single
Static HighSince As Single
If isFirstBar Then
 sumWeight = 1
 TR = Price.High - Price.Low
 LowSince = Price.Low
 HighSince = Price.High
Else
 TR = System.Math.Max(Price.High, Price.Last(1)) - _
  System.Math.Min(Price.Low, Price.Last(1))
 LowSince = System.Math.Min(LowSince, Price.Low)
 HighSince = System.Math.Max(LowSince, Price.High)
End If 
If CurrentIndex >= 15 Then
 Dim Weight As Single = 1 / sumWeight
 ATR = ATR * (1 - Weight) + Weight * TR
 sumWeight = sumWeight * termRatio + 1
End If
If Price.High > (Multiplier * ATR + ZZ.Value(Low at the last reversal))
 ZZ = Price.High
Else
 If Price.Low < (Multiplier * ATR - ZZ.Value(High at the last reversal))
  ZZ = Price.Low
 End If
 Plot = ZZ
Bruce_L
Posted : Wednesday, November 4, 2009 2:39:55 PM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138
The following RealCode Indicator is based on the RealCode in my Tuesday, September 30, 2008 12:22:12 PM ET post in ATR stop revisited:

'# period = UserInput.Single = 14
'# factor = UserInput.Single = 3
Static TR As Single
Static ATR As Single
Static termRatio As Single
static Weight As Single
Static sumWeight As Single
Static extreme As Single
Static tstop As Single
Static state As Boolean
If isFirstBar Then
    TR = Price.High - Price.Low
    termRatio = (period - 1) / period
    ATR = TR
    sumweight = termratio + 1
    weight = 1 / sumweight
    If Price.High(-1) - System.Math.Min(Price.Low, Price.Low(-1)) < _
        System.Math.Max(Price.High, Price.High(-1)) - Price.Low(-1) Then
        extreme = Price.Low
        tstop = System.Math.Max(Price.High, extreme + factor * ATR)
        state = False
    Else
        extreme = Price.High
        tstop = System.Math.Min(Price.Low, extreme - factor * ATR)
        state = True
    End If
    Plot = Single.NaN
Else
    TR = (Price.High - Price.Low + _
        System.Math.Abs(Price.High - Price.Last(1)) + _
        System.Math.Abs(Price.Last(1) - Price.Low)) / 2
    ATR = ATR * (1 - weight) + weight * TR
    sumweight = sumweight * termratio + 1
    weight = 1 / sumweight
    If state
        If Price.Low < tstop Then
            AddToOutput(Price.DateValue, extreme)
            state = False
            extreme = Price.Low
            tstop = System.Math.Max(Price.High, extreme + factor * ATR)
        End If
    Else
        If Price.High > tstop Then
            AddToOutput(Price.DateValue, extreme)
            state = True
            extreme = Price.High
            tstop = System.Math.Min(Price.Low, extreme - factor * ATR)
        End If
    End If
    If state
        extreme = System.Math.Max(extreme, Price.High)
        tstop = System.Math.Max(tstop, extreme - factor * ATR)
    Else
        extreme = System.Math.Min(extreme, Price.Low)
        tstop = System.Math.Min(tstop, extreme + factor * ATR)
    End If
End If
If isLastBar Then AddToOutput(Price.DateValue, extreme)
Plot = Single.NaN

-Bruce
Personal Criteria Formulas
TC2000 Support Articles
thnkbigr
Posted : Wednesday, November 4, 2009 3:45:36 PM
Platinum Customer Platinum Customer

Joined: 3/31/2006
Posts: 3,207
Bruce,

Thanks 

1 issue with this how can I push this back to overlay price just like the Zig Zag indicator?

I just shared a chart under Zig Zag so you can see what I am talking about.

Look at LVS it hit a low on 3/9 but the indicators low is on 3/24

Bruce_L
Posted : Wednesday, November 4, 2009 3:50:55 PM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138
Are you going to backtest the Indicator or use it in BackScanner? If so, don't even think about using the following (or the regular Zig Zag for that matter since they both rely on future data):

'# period = UserInput.Single = 14
'# factor = UserInput.Single = 3
Static TR As Single
Static ATR As Single
Static termRatio As Single
static Weight As Single
Static sumWeight As Single
Static extreme As Single
Static tstop As Single
Static state As Boolean
Static extDate As Date
If isFirstBar Then
    TR = Price.High - Price.Low
    termRatio = (period - 1) / period
    ATR = TR
    sumweight = termratio + 1
    weight = 1 / sumweight
    extDate = Price.DateValue
    If Price.High(-1) - System.Math.Min(Price.Low, Price.Low(-1)) < _
        System.Math.Max(Price.High, Price.High(-1)) - Price.Low(-1) Then
        extreme = Price.Low
        tstop = System.Math.Max(Price.High, extreme + factor * ATR)
        state = False
    Else
        extreme = Price.High
        tstop = System.Math.Min(Price.Low, extreme - factor * ATR)
        state = True
    End If
    Plot = Single.NaN
Else
    TR = (Price.High - Price.Low + _
        System.Math.Abs(Price.High - Price.Last(1)) + _
        System.Math.Abs(Price.Last(1) - Price.Low)) / 2
    ATR = ATR * (1 - weight) + weight * TR
    sumweight = sumweight * termratio + 1
    weight = 1 / sumweight
    If state
        If Price.Low < tstop Then
            AddToOutput(extDate, extreme)
            state = False
            extreme = Price.Low
            extDate = Price.DateValue
            tstop = System.Math.Max(Price.High, extreme + factor * ATR)
        End If
    Else
        If Price.High > tstop Then
            AddToOutput(extDate, extreme)
            state = True
            extreme = Price.High
            extDate = Price.DateValue
            tstop = System.Math.Min(Price.Low, extreme - factor * ATR)
        End If
    End If
    If state Then
        If Price.High >= extreme Then
            extreme = Price.High
            extDate = Price.DateValue
        End If
        tstop = System.Math.Max(tstop, extreme - factor * ATR)
    Else
        If Price.Low <= extreme Then
            extreme = Price.Low
            extDate = Price.DateValue
        End If
        tstop = System.Math.Min(tstop, extreme + factor * ATR)
    End If
End If
If isLastBar Then AddToOutput(extDate, extreme)
Plot = Single.NaN

-Bruce
Personal Criteria Formulas
TC2000 Support Articles
thnkbigr
Posted : Wednesday, November 4, 2009 6:59:17 PM
Platinum Customer Platinum Customer

Joined: 3/31/2006
Posts: 3,207

This looks amazing thanks 

See what I mean by letting the character of the stock define its trend. I set the ATR multiplier to 4 on LVS and you have to set the Zig Zag indicator to 30% to get similar results to the indicator above from the 2007 peak to present meanwhile if you look at 2005-2006 the 30% Zig Zag was too wide. Even the 30% Zig Zag wasn't wide enough from 8/2008 to 3/2009 lows it still had too many false zig zags.

As far as back testing yes and no. I think I know what you mean by why this and the zig zag indicator can not be backtested.

I want to constantly plot the extreme values. For example on 5/5 LVS hit a high of 11.84 which is the Extreme High. I like to say If in the next 10 days price doesn't break above that Extreme High then plot that value

If Price.MaxHigh(10) < The Last Extreme High Then
Plot = Last Extreme High

Or after a extreme Low is defined If the next 10 days Price doesn't break that low then plot that value 

If Price.MinLow(10) > The Last Extreme Low Then
Plot = Last Extreme Low

So on 5/19 the indicator will constantly plot the Extreme High value which was on 5/5 of 11.84 until it's broken by price.

There is one thing about the indicator above that is confusing me. Usually when you write an indicator at the end you have to say Plot = Something but you are saying Plot = Single.NaN which from my understanding means don't plot anything.

So how is it plotting the Extreme High and Low? 

thnkbigr
Posted : Thursday, November 5, 2009 12:09:20 AM
Platinum Customer Platinum Customer

Joined: 3/31/2006
Posts: 3,207
I forget how to paste a chart in here I know jas0501 knows how so please if you see this let me know once again how you paste pictures in here. thx

Bruce,

I marked the AAPL chart in the chart I just shared under Zig Zag 2 just to show you what I am trying to accomplish.

I changed all the Highs and Lows in the indicator above (Auto Zig Zag) to Close since from what I have seen so far it reduces a lot whipsaws. I have the indicator in Megenta. 

If you bring up the AAPL chart from the mid 2007 to present you should see what I mean.

On 8/14 the indicator made a Lower High and on 9/9 price closed below the most recent Extreme Low this would be your Short signal. 

Now other way around on 3/9 the indicator made a Higher Low and on 3/23 price closed above the most recent Extreme High this would be the Buy signal.

So if we can have an indicator that plots the extreme values just like the trend lines I have drawn we should be able to say If the extreme high is lower than the prior extreme high and also price closes below the most recent extreme low Short and other way around to go Long.

Many Thanks
thnkbigr
Posted : Thursday, November 5, 2009 2:36:16 AM
Platinum Customer Platinum Customer

Joined: 3/31/2006
Posts: 3,207
I went through a lot of charts in the last few hours and I think I understand what you mean by not using this for backtesting.

That is if I am saying If price crosses above it Buy and Sell when it crosses below it. Since the angle that the line is drawn changes depending on the future data, the crosses that you see now may not be the same as real time.

But I don't see any reason why we shouldn't be able to do what I explained in my last 2 posts.

Please let me know if there is anything else I need to be aware of. Other than that I think this could be very good strategy from what I have seen by going through a few hunderd charts in the last few hours. 

Thanks to SF I barely sleep anymore.
Bruce_L
Posted : Thursday, November 5, 2009 10:47:53 AM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138
QUOTE (thnkbigr)
I forget how to paste a chart in here I know jas0501 knows how so please if you see this let me know once again how you paste pictures in here. thx

The best topic of which I am aware on posting images in the forums is Inserting A Chart into A Worden post.

QUOTE (thnkbigr)
There is one thing about the indicator above that is confusing me. Usually when you write an indicator at the end you have to say Plot = Something but you are saying Plot = Single.NaN which from my understanding means don't plot anything.

So how is it plotting the Extreme High and Low?

It's using AddToOutput() to Plot the Values. The technique would probably be better implemented in the Class tab using AutoLoop = False, but that was not available when the ATR Stop was originally written (and I really didn't want to re-write the entire thing just to use a different technique).

QUOTE (thnkbigr)
I went through a lot of charts in the last few hours and I think I understand what you mean by not using this for backtesting.

That is if I am saying If price crosses above it Buy and Sell when it crosses below it. Since the angle that the line is drawn changes depending on the future data, the crosses that you see now may not be the same as real time.

But I don't see any reason why we shouldn't be able to do what I explained in my last 2 posts.
...
Look at LVS it hit a low on 3/9 but the indicators low is on 3/24

It doesn't show until 3/24 in the first version because it hasn't been identified as a low until 3/24. There is no way for you to know that is going to be a low until 3/24. Retroactively drawing the low on 3/9 may allow you to visualize things better when you look at the chart later, but it is cheating. There is no way to get valid backtesting results when using it.

You can draw the most recent extreme using something similar to the following RealCode Indicator (changing the last line to Plot = extreme(0) will draw a line at the next potential extreme instead):

'# period = UserInput.Single = 14
'# factor = UserInput.Single = 3
Static TR As Single
Static ATR As Single
Static termRatio As Single
static Weight As Single
Static sumWeight As Single
Static extreme(1) As Single
Static tstop As Single
Static state As Boolean
If isFirstBar Then
    TR = Price.High - Price.Low
    termRatio = (period - 1) / period
    ATR = TR
    sumweight = termratio + 1
    weight = 1 / sumweight
    extreme(1) = Single.NaN
    If Price.High(-1) - System.Math.Min(Price.Low, Price.Low(-1)) < _
        System.Math.Max(Price.High, Price.High(-1)) - Price.Low(-1) Then
        extreme(0) = Price.Low
        tstop = System.Math.Max(Price.High, extreme(0) + factor * ATR)
        state = False
    Else
        extreme(0) = Price.High
        tstop = System.Math.Min(Price.Low, extreme(0) - factor * ATR)
        state = True
    End If
    Plot = Single.NaN
Else
    TR = (Price.High - Price.Low + _
        System.Math.Abs(Price.High - Price.Last(1)) + _
        System.Math.Abs(Price.Last(1) - Price.Low)) / 2
    ATR = ATR * (1 - weight) + weight * TR
    sumweight = sumweight * termratio + 1
    weight = 1 / sumweight
    If state
        If Price.Low < tstop Then
            extreme(1) = extreme(0)
            state = False
            extreme(0) = Price.Low
            tstop = System.Math.Max(Price.High, extreme(0) + factor * ATR)
        End If
    Else
        If Price.High > tstop Then
            extreme(1) = extreme(0)
            state = True
            extreme(0) = Price.High
            tstop = System.Math.Min(Price.Low, extreme(0) - factor * ATR)
        End If
    End If
    If state
        extreme(0) = System.Math.Max(extreme(0), Price.High)
        tstop = System.Math.Max(tstop, extreme(0) - factor * ATR)
    Else
        extreme(0) = System.Math.Min(extreme(0), Price.Low)
        tstop = System.Math.Min(tstop, extreme(0) + factor * ATR)
    End If
End If
Plot = extreme(1)

You could have the extremes before that by just extending how many you track. For example if you increased the size of the array, you could change:

extreme(1) = extreme(0)

To:

extreme(3) = extreme(2)
extreme(2) = extreme(1)
extreme(1) = extreme(0)

-Bruce
Personal Criteria Formulas
TC2000 Support Articles
thnkbigr
Posted : Thursday, November 5, 2009 6:00:40 PM
Platinum Customer Platinum Customer

Joined: 3/31/2006
Posts: 3,207
Sorry Bruce

What's my array?

I don't understand this.

You could have the extremes before that by just extending how many you track. For example if you increased the size of the array, you could change:

extreme(1) = extreme(0)

To:

extreme(3) = extreme(2)
extreme(2) = extreme(1)
extreme(1) = extreme(0)
Bruce_L
Posted : Friday, November 6, 2009 9:31:08 AM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138
I variable that stores more than value is called an array. So you would need to change the line where this array is declared from:

Static extreme(1) As Single

To:

Static extreme(3) As Single

And both instances of:

extreme(1) = extreme(0)

To:

extreme(3) = extreme(2)
extreme(2) = extreme(1)
extreme(1) = extreme(0)

So a version that output all four values might look something like:

'# period = UserInput.Single = 14
'# factor = UserInput.Single = 3
Static TR As Single
Static ATR As Single
Static termRatio As Single
static Weight As Single
Static sumWeight As Single
Static extreme(3) As Single
Static tstop As Single
Static state As Boolean
If isFirstBar Then
    TR = Price.High - Price.Low
    termRatio = (period - 1) / period
    ATR = TR
    sumweight = termratio + 1
    weight = 1 / sumweight
    extreme(1) = Single.NaN
    If Price.High(-1) - System.Math.Min(Price.Low, Price.Low(-1)) < _
        System.Math.Max(Price.High, Price.High(-1)) - Price.Low(-1) Then
        extreme(0) = Price.Low
        tstop = System.Math.Max(Price.High, extreme(0) + factor * ATR)
        state = False
    Else
        extreme(0) = Price.High
        tstop = System.Math.Min(Price.Low, extreme(0) - factor * ATR)
        state = True
    End If
    Plot = Single.NaN
Else
    TR = (Price.High - Price.Low + _
        System.Math.Abs(Price.High - Price.Last(1)) + _
        System.Math.Abs(Price.Last(1) - Price.Low)) / 2
    ATR = ATR * (1 - weight) + weight * TR
    sumweight = sumweight * termratio + 1
    weight = 1 / sumweight
    If state
        If Price.Low < tstop Then
            extreme(3) = extreme(2)
            extreme(2) = extreme(1)
            extreme(1) = extreme(0)
            state = False
            extreme(0) = Price.Low
            tstop = System.Math.Max(Price.High, extreme(0) + factor * ATR)
        End If
    Else
        If Price.High > tstop Then
            extreme(3) = extreme(2)
            extreme(2) = extreme(1)
            extreme(1) = extreme(0)
            state = True
            extreme(0) = Price.High
            tstop = System.Math.Min(Price.Low, extreme(0) - factor * ATR)
        End If
    End If
    If state
        extreme(0) = System.Math.Max(extreme(0), Price.High)
        tstop = System.Math.Max(tstop, extreme(0) - factor * ATR)
    Else
        extreme(0) = System.Math.Min(extreme(0), Price.Low)
        tstop = System.Math.Min(tstop, extreme(0) + factor * ATR)
    End If
End If
OpenValue = extreme(3)
HighValue = extreme(2)
LowValue = extreme(1)
Plot = extreme(0)

-Bruce
Personal Criteria Formulas
TC2000 Support Articles
thnkbigr
Posted : Wednesday, November 11, 2009 5:00:57 PM
Platinum Customer Platinum Customer

Joined: 3/31/2006
Posts: 3,207

Bruce, 

My apologies if this is long but I think I did this without any cheating. This is pretty much explaining what I did and errors that I think can be resolved with realcode.


I fully understand what you mean by below. We won't know the extremes until the trend has reversed. Therefore I am using your 1st version of this indicator.

It doesn't show until 3/24 in the first version because it hasn't been identified as a low until 3/24. There is no way for you to know that is going to be a low until 3/24. Retroactively drawing the low on 3/9 may allow you to visualize things better when you look at the chart later, but it is cheating. There is no way to get valid backtesting results when using it.

I just shared a Layout under Zig Zag 3. Please bring up the chart of AAPL for the last 2 years. I did it under Layout so it comes with the back scanner for the signals to be plotted.

I have the 1st version of the indicator plotted in Aqua (Auto Zig Zag Close) I changed all the High and Lows to Close so it Zig Zags if the reversal is 4 * ATR from Close to Close rather than High to Low or Low to High.

I also have Extreme(1) plotted in Red and Extreme(2) plotted in Yellow based on your indicator from your Friday, November 06, 2009 9:31:08 AM.

So since we are not pushing any of these indicators back and they plot when the reversals actually happens I don't think there is any cheating involved. For example on 2/23/2009 we would have known that the 2/9/2009 closing high is an Extreme high since by 2/23 price had reversed down or dropped by 4 * ATR from the 2/9 closing high. 

As far as the Extremes I know that Extreme(1) plots the last Extreme and Extreme(2) plots the one before that. I call Extreme(1) E1 and Extreme(2) E2 in my rules.

So look at my Reversal Up Rule which is a Sequence of 4 steps. I am looking for a Lower Low, Lower High, Higher Low and a break above the Lower High (Step 2)  will be a Higher High which is the Breakout. 

First I am looking for a Downtrend which is defined by  E1.Value < E1.Value(1). This means that we just put in a Lower Extreme than where it was at the last Extreme. I probably can say E1.Value < E2.Value as well. This was defined on 2/4/2009 on AAPL since by 2/4 price had increased already by 4 * ATR from the 1/20 closing low therefore on 2/4 we have a new E1. 

2) Now we are looking for an Up leg and E1.Value <= E2.Value(1) which defines a Lower High. This was defined on 2/23 since price had dropped by 4 * ATR from 2/9 closing high. 

3) Now we are looking for a Down leg as long as we make a Higher Low E1.Value >= E2.Value(1). This was defined on 3/17 on AAPL since price had gained 4 * ATR from the 3/9 Closing Low.

4) Finally a break above Extreme(2) which now is plotting 2/9 value would signal a change in trend since we put in a Higher High. Please take a look at my Cross Up E2 and Dn E2 rules. I just couldn't say Price cross E2 since as I went through charts I realized that a signal gets generated when price crosses any E2 not just the last one, so I tried to lock in the breakout point in the cross rules as E2F. 

Lastly sometimes Step 3 and 4 happens the same day so I created a 2nd Buy and Short reversal rules with only 3 steps having step 3 and 4 as a combo rule. 

There are few errors with this that I think it can be resolved with realcode.

1) As you can see I have this as a Sequence with a look back of a 100 bars and sometimes the reversals take longer than 100 bars to complete and therefore we get no signal. If I increase the time frame to 200 or even higher it creates more of error 2.

2) When my look back is 100 bars as long as these 4 step happen within the last 100 bars even if other things has happened in between them it generates a signal. These steps need to happen one after another without any other up or down legs happening in between them. Let me give you an example.

Tale a look at CHRW in 2004-2005 please. My focus is the buy signal on 7/11/2005.

On 1/13/05 we had a reversal Dn signal which marked the closing high of 12/28/04 as the Extreme High. Then the reversal Dn was completed by 2/25/05 since price had reversed back up by > 4 * ATR and this marked the 1/28/05 closing low as the Extreme low. The leg up that followed ended by 4/6/05 and this extreme high was below the prior extreme high and therefore a break below the last extreme low will generate a Short signal and it did which is accurate. We had a Higher High, Higher Low, Lower High and then a break below the last Higher Low means you have a Lower Low therefore the trend has changed. 

But the Buy signal on 7/11/2005 is not accurate because the 4 steps had 2 other legs happening in between them.

In order to go long you need a Lower Low, Lower High, Higher Low and then a Higher High. So if you look at the hash marks on the bottom you need a Red one 1st, Green one 2nd, Magenta one 3rd and a Yellow one 4th (3 and 4 can happen on the same day).  So you have a Red one on 2/25/05, Green one on 4/6, Magenta and Yellow one both on 7/11, they all happened within 100 bars but between the Green one and the Magenta one you had two more legs (a down leg completed on 5/2 and an up leg completed on 6/10).

If we can write a realcode that looks for these steps to happen one after another without any other legs in between these 4 steps it will resolve both issues. I was thinking of dragging and dropping these 4 rules in the realcode editor and have the realcode say which rule needs to be true 1st which one 2nd and so on and add a piece that checks for no other legs happening in between.

Can we write a realcode that would do this?

 

Thanks a lot

thnkbigr
Posted : Wednesday, December 2, 2009 1:49:09 PM
Platinum Customer Platinum Customer

Joined: 3/31/2006
Posts: 3,207

Bruce,

I have played with this for a long time and I have tried to do this myself but I haven't been able to resolve this. There is only 1 code that you have written in the past for me that is a sequence in realcode and that's the FT day (I have the code pasted below). This is the only code that I can refer to to learn.

Can we put my Reversal UP Combo rule in a realcode sequence and check to make sure that the Zig Zag indicator doesn't have any other legs happening in-between these 4 steps? I don't think I am doing any cheating in here. I have the steps listed below.

E1 is Extreme 1 and E2 is Extreme 2

Step 1) 
If E1.Value < E1.Value(1) Then Pass

Step 2) 
If E1.Value > E1.Value(1) AndAlso _
                 E1.Value <= E2.Value(1) Then Pass

Step 3) 
If E1.Value < E1.Value(1) AndAlso _
                 E1.Value >= E2.Value(1) Then Pass

Step 4) 
Static E2F As Single
If isFirstBar Then
 E2F = Single.NaN
Else If E1.Value < E1.Value(1) Then
 E2F = E2.Value
End If
If Price.Close > E2F AndAlso _
 Price.Close(1) <= E2F Then Pass

I need these 4 steps to happen one after another but without the indicator having any other zig zags in-between them.

My Layout is shared under ZIG ZAG 3 if you need to refer to it.

Thanks

FT realcode

'# Cumulative

Static State As Integer

Static Base As Single

Static ATR As Single

Static TR As Single

Static sumWeight As Single

Static termRatio As Single = 13 / 14

Static AvgV As Single

If isFirstBar Then

                State = 0

                sumWeight = 1

                TR = Price.High - Price.Low

                AvgV = 0

Else

                TR = System.Math.Max(Price.High, Price.Last(1)) - _

                                System.Math.Min(Price.Low, Price.Last(1))

                AvgV += Volume.value(1) / 30

End If

If CurrentIndex >= 31 Then AvgV -= Volume.value(31) / 30

Dim Weight As Single = 1 / sumWeight

ATR = ATR * (1 - Weight) + Weight * TR

sumWeight = sumWeight * termRatio + 1

If CurrentIndex >= 30 Then

                If State > 0 AndAlso Price.Low < Base Then State = 0

                If State >= 2 AndAlso State <= 4 Then

                                State += 1

                                '        Label = State

                End If

                If State = 1 AndAlso Price.Last > Price.Last(1) Then

                                State = 2

                                '        Label = State

                End If

                If Price.Low < Price.MinLow(22, 1) Then

                                Base = Price.Low

                                If Price.Last - Price.Low >= .10 * (Price.High - Price.Low) Then

                                                State = 2

                                Else

                                                State = 1

                                End If

                                '        Label = State

                End If

                If State >= 5 AndAlso _

                                Volume.value > Volume.value(1) * 1.01 AndAlso _

                                Volume.value(1) > AvgV * .60 AndAlso _

                                Price.Last - Price.Last(1) >= ATR Then

                                State = 6

                                '        Label = State

                End If

End If

If State > 0 Then

                '    Plot = Base

Else

                '    Plot = Single.NaN

End If

If State = 6 Then

                State = 0

                Pass

End If

Bruce_L
Posted : Wednesday, December 2, 2009 2:31:32 PM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138
I must be missing something because I don't see why you wouldn't just use the RealCode that tracks the past Values, add a extreme(4) and check for Price.Last < extreme(1) AndAlso Price.Last(1) >= extreme(1) AndAlso extreme(1) < extreme(2) AndAlso extreme(2) > extreme(3) AndAlso extreme(3) < extreme(4) (or output these all four Values as part of an Indicator and reference them in a RealCode Rule):

'# period = UserInput.Single = 14
'# factor = UserInput.Single = 3
Static TR As Single
Static ATR As Single
Static termRatio As Single
static Weight As Single
Static sumWeight As Single
Static extreme(4) As Single
Static tstop As Single
Static state As Boolean
If isFirstBar Then
    TR = Price.High - Price.Low
    termRatio = (period - 1) / period
    ATR = TR
    sumweight = termratio + 1
    weight = 1 / sumweight
    extreme(4) = Single.NaN
    extreme(3) = Single.NaN   
    extreme(2) = Single.NaN
    extreme(1) = Single.NaN
    If Price.High(-1) - System.Math.Min(Price.Low, Price.Low(-1)) < _
        System.Math.Max(Price.High, Price.High(-1)) - Price.Low(-1) Then
        extreme(0) = Price.Low
        tstop = System.Math.Max(Price.High, extreme(0) + factor * ATR)
        state = False
    Else
        extreme(0) = Price.High
        tstop = System.Math.Min(Price.Low, extreme(0) - factor * ATR)
        state = True
    End If
Else
    TR = (Price.High - Price.Low + _
        System.Math.Abs(Price.High - Price.Last(1)) + _
        System.Math.Abs(Price.Last(1) - Price.Low)) / 2
    ATR = ATR * (1 - weight) + weight * TR
    sumweight = sumweight * termratio + 1
    weight = 1 / sumweight
    If state
        If Price.Low < tstop Then
            extreme(4) = extreme(3)
            extreme(3) = extreme(2)
            extreme(2) = extreme(1)
            extreme(1) = extreme(0)
            state = False
            extreme(0) = Price.Low
            tstop = System.Math.Max(Price.High, extreme(0) + factor * ATR)
        End If
    Else
        If Price.High > tstop Then
            extreme(4) = extreme(3)
            extreme(3) = extreme(2)
            extreme(2) = extreme(1)
            extreme(1) = extreme(0)
            state = True
            extreme(0) = Price.High
            tstop = System.Math.Min(Price.Low, extreme(0) - factor * ATR)
        End If
    End If
    If state
        extreme(0) = System.Math.Max(extreme(0), Price.High)
        tstop = System.Math.Max(tstop, extreme(0) - factor * ATR)
    Else
        extreme(0) = System.Math.Min(extreme(0), Price.Low)
        tstop = System.Math.Min(tstop, extreme(0) + factor * ATR)
    End If
End If
If Price.Last > extreme(2) AndAlso _
    Price.Last(1) <= extreme(2) AndAlso _
    extreme(1) < extreme(2) AndAlso _
    extreme(2) > extreme(3) AndAlso _
    extreme(3) < extreme(4) Then Pass

-Bruce
Personal Criteria Formulas
TC2000 Support Articles
Users browsing this topic
Guest-1

Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.