logo
Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Options
Go to last post Go to first unread
zigzag  
#1 Posted : Saturday, July 18, 2009 9:50:07 AM(UTC)
zigzag

Rank: Advanced Member

Groups: Registered, Registered Users
Joined: 1/13/2009(UTC)
Posts: 42

Hello All Borrowing from the idea of a RSI indicator with extremes normalized for bull and bear phases (from June 2009 issue of TASC by Giorgos Siligardos, copyright 2009 Technical Analysis Inc, all rights reserved), I have attempted to code an RSI that would have it's extreme boundaries automatically adjusted according to four "phases" of an ATR-based indicator, as follows: indic:=ATR(21)/((H+L)/2)*100 The general formula for this normalized RSI is (rsi(prd)-rsi(prd) low boundary)/(rsi(prd) high boundary-rsi(prd) low boundary). Here is my attempt at the code: hi1:=Highest(ValueWhen(1,indic<=1,RSI(14))); hi2:=Highest(ValueWhen(1,1<indic<=2,RSI(14))); hi3:=Highest(ValueWhen(1,2<indic<=4,RSI(14))); hi4:=Highest(ValueWhen(1,indic>4,RSI(14))); lo1:=Lowest(ValueWhen(1,indic<=1,RSI(14))); lo2:=Lowest(ValueWhen(1,1<indic<=2,RSI(14))); lo3:=Lowest(ValueWhen(1,2<indic<=4,RSI(14))); lo4:=Lowest(ValueWhen(1,indic>4,RSI(14))); hilo1:=hi1-lo1; hilo2:=hi2-lo2; hilo3:=hi3-lo3; hilo4:=hi4-lo4; If(indic<=1,(RSI(14)-lo1)/hilo1,If(1<indic<=2,(RSI(14)-lo2)/hilo2,If(2<indic<=4,(RSI(14)-lo3)/hilo3,(RSI(14)-lo4)/hilo4))) The problem is that it only plots the latest "phase" of the ATR indicator. Would anybody like to take a swing at it? Many thanks. ZigZag
zigzag  
#2 Posted : Saturday, July 18, 2009 10:12:43 AM(UTC)
zigzag

Rank: Advanced Member

Groups: Registered, Registered Users
Joined: 1/13/2009(UTC)
Posts: 42

Somehow when I copy-paste the code it is not showing right in Forum. I apologize. The idea is to normalize RSI for each phase (when ATR(21)/(H+L)/2*100 is below 1, between 1 and 2, between 2 and 4 and above 4). Any help is appreciated.
johnl  
#3 Posted : Saturday, July 18, 2009 9:39:37 PM(UTC)
johnl

Rank: Advanced Member

Groups: Registered, Registered Users
Joined: 11/7/2005(UTC)
Posts: 602

Here is my stab at it. Might give you some ideas.

ti := 100;
p1:=ATR(21)/((H+L)/2)*100;
p1f := 100/(HHV(p1,ti)-LLV(p1,ti));
p1m := (p1-LLV(p1,ti))*p1f;
{----------------------------------------}
a1:=ValueWhen(1,p1m=0,RSI(14));
a2:=ValueWhen(1,Cross(p1m,25),RSI(14));
a3:=ValueWhen(1,Cross(p1m,50),RSI(14));
a4:=ValueWhen(1,Cross(p1m,75),RSI(14));
b1:=(RSI(14)-a1/(a2-a1));
b2:=(RSI(14)-a2/(a3-a2));
b3:=(RSI(14)-a3/(a4-a3));
b4:=(RSI(14)-a1/(a2-a1));
c1:=If((p1m<25),b1,
If((p1m>25) AND (p1m<50),b2,
If((p1m>50) AND (p1m<75),b3,
b4)));
If((c1>100),100,If((c1<0),0,c1))

zigzag  
#4 Posted : Monday, July 20, 2009 1:28:07 PM(UTC)
zigzag

Rank: Advanced Member

Groups: Registered, Registered Users
Joined: 1/13/2009(UTC)
Posts: 42

johnl

Thanks for your code - you are clearly a talented code-writer. What I actually had in mind was to have the following indicator plotted (sorry for having been unclear):

(rsi(c,14)-lowest(rsi(c,14)))/(highest(rsi(c,14))-lowest(rsi(c,14)))

using as the "lowest" and "highest" reference data the highest/lowest value of RSI(C,14) according to the range where the value of the formula below falls (ranges: x<=1, 1<x<=2, 2<x<=4 and x>4).

ATR(21)/((H+L)/2)*100

Not sure your code is doing that (I don't mean "not sure" as in "I don't think so"...).

My original (falied) attempt below:

indic:=ATR(21)/((H+L)/2)*100;

hi1:=Highest(ValueWhen(1,indic<=1,RSI(14)));

hi2:=Highest(ValueWhen(1,1<indic<=2,RSI(14)));

hi3:=Highest(ValueWhen(1,2<indic<=4,RSI(14)));

hi4:=Highest(ValueWhen(1,indic>4,RSI(14)));

lo1:=Lowest(ValueWhen(1,indic<=1,RSI(14)));

lo2:=Lowest(ValueWhen(1,1<indic<=2,RSI(14)));

lo3:=Lowest(ValueWhen(1,2<indic<=4,RSI(14)));

lo4:=Lowest(ValueWhen(1,indic>4,RSI(14)));

hilo1:=hi1-lo1;

hilo2:=hi2-lo2;

hilo3:=hi3-lo3;

hilo4:=hi4-lo4;

indic2:=If(indic<=1,(RSI(14)-lo1)/hilo1,If(1<indic<=2,(RSI(14)-lo2)/hilo2,If(2<indic<=4,(RSI(14)-lo3)/hilo3,(RSI(14)-lo4)/hilo4)));

Thanks again. Any help from Forum members is appreciated.

ZigZag

oztrader  
#5 Posted : Tuesday, July 21, 2009 2:02:52 AM(UTC)
oztrader

Rank: Advanced Member

Groups: Registered, Registered Users, Unverified Users
Joined: 7/12/2007(UTC)
Posts: 134
Location: Perth Western Australia

Hi ZigZag,

This code works as an Indicator:-

{Wabbit's "Division by Zero" Error Trap}
zero:=Power(10,-20);

data:=ATR(21)/MP()*100;

hi1:=Highest(ValueWhen(1,data<=1,RSI(14)));
hi2:=Highest(ValueWhen(1,data>1 AND data<=2,RSI(14)));
hi3:=Highest(ValueWhen(1,data>2 AND data<=4,RSI(14)));
hi4:=Highest(ValueWhen(1,data>4,RSI(14)));

lo1:=Lowest(ValueWhen(1,data<=1,RSI(14)));
lo2:=Lowest(ValueWhen(1,data>1 AND data<=2,RSI(14)));
lo3:=Lowest(ValueWhen(1,data>2 AND data<=4,RSI(14)));
lo4:=Lowest(ValueWhen(1,data>4,RSI(14)));

hilo1:=hi1-lo1;
hilo2:=hi2-lo2;
hilo3:=hi3-lo3;
hilo4:=hi4-lo4;

If(data<=1,(RSI(14)-lo1)/MAX(hilo1,zero),
If(data>1 AND data<=2,(RSI(14)-lo2)/Max(hilo2,zero),
If(data>2 AND data<=4,(RSI(14)-lo3)/Max(hilo3,zero),
(RSI(14)-lo4)/Max(hilo4,zero))));

I have used the the in-built Median Price Indicator which is the same as (H-L)/2.

Cheers,

oz

zigzag  
#6 Posted : Wednesday, July 22, 2009 11:14:53 AM(UTC)
zigzag

Rank: Advanced Member

Groups: Registered, Registered Users
Joined: 1/13/2009(UTC)
Posts: 42

Oztrader

Many thanks for your post. I actually have the same problem with your code that I have with mine (posted previously): the indicator only starts plotting after ATR(21)/MP()*100 crosses above 4 for the first time (on 10/6/08 on daily SPY, for instance). I wonder if you would have the same problem.

I can't seem to be able to figure out why this is happening...

Thanks again for the help.

ZigZag

oztrader  
#7 Posted : Thursday, July 23, 2009 2:46:17 AM(UTC)
oztrader

Rank: Advanced Member

Groups: Registered, Registered Users, Unverified Users
Joined: 7/12/2007(UTC)
Posts: 134
Location: Perth Western Australia

ZigZag,

Yes, although I hadn't noticed this until you raised the issue, but the indicator does not start plotting until all 4 of the range values for the "indic" have been established and I'm not sure why this happens.

Out of interest this code
"indic > 1 AND indic <= 2"
gives a different result than using this code
"1 < indic <=2"
when used in your formula!

This can be checked by displaying the following indicators on a chart:-

{Ind1}
data:=ATR(21)/((H+L)/2)*100;
If(data<=1,1,
If(data>1 AND data<=2,2,
If(data>2 AND data<=4,3,4)))

{Ind2}
indic:=ATR(21)/((H+L)/2)*100;
If(indic<=1,1,
If(1<indic<=2,2,
If(2<indic<=4,3,4)))

oz

zigzag  
#8 Posted : Thursday, July 23, 2009 11:59:31 AM(UTC)
zigzag

Rank: Advanced Member

Groups: Registered, Registered Users
Joined: 1/13/2009(UTC)
Posts: 42

Oz

thanks for the post. i haven't tried plotting but "indic > 1 AND indic <= 2" is mathematically the same as "1 < indic <=2". it is curious that they would plot differently...

i am really perplexed as to why the indicator is plotting the way it is.

would anybody else like to take a swing at it?

ZigZag

wabbit  
#9 Posted : Friday, July 24, 2009 12:18:37 AM(UTC)
wabbit

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers, Unverified Users
Joined: 10/28/2004(UTC)
Posts: 3,111
Location: Perth, Western Australia

Was thanked: 16 time(s) in 16 post(s)
zigzag wrote:
i haven't tried plotting but "indic > 1 AND indic <= 2" is mathematically the same as "1 < indic <=2". it is curious that they would plot differently...

Have a look in the MS Users Manual for the operator precedence. In the expression:
Code:
1 < indic <=2

the LT expression will be evaluated first, resulting in a binary 0 or 1; then the LTE expression will be evaluated, so it will be evaluated:
Code:
(1 < indic) <= 2;
{so, depending on user definition requirements, it's the same as saying something like}
(0 <= 2) AND (1 <= 2);

so the expression will always be true, and, is not the same as:
Code:
1 < indic AND indic <=2




wabbit [:D]

zigzag  
#10 Posted : Friday, July 24, 2009 1:11:28 PM(UTC)
zigzag

Rank: Advanced Member

Groups: Registered, Registered Users
Joined: 1/13/2009(UTC)
Posts: 42

Hi Wabbit

Thanks for your post - educational, as usual.

Would you care to take a look at the code for the indicator? It is not working as I expect and I cannot figure out why...

Thanks.

ZigZag

wabbit  
#11 Posted : Friday, July 24, 2009 9:03:22 PM(UTC)
wabbit

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers, Unverified Users
Joined: 10/28/2004(UTC)
Posts: 3,111
Location: Perth, Western Australia

Was thanked: 16 time(s) in 16 post(s)
Hi zz,

You will need to deal with the periods when ValueWhen() returns N/A. You could use a whole pile of calls to the forum.sum function but this would be rather unwieldy and probably quite slow; maybe even slow enough to warrant reverting to using a PREV based latch?? (Although I haven't looked too closely as to the specifics of how you'd employ it).

zigzag wrote:
It is not working as I expect and I cannot figure out why..

Can you please post an annotated chart and more details? Comments like this aren't much use in bug-hunting!



wabbit [:D]

zigzag  
#12 Posted : Saturday, July 25, 2009 8:14:36 PM(UTC)
zigzag

Rank: Advanced Member

Groups: Registered, Registered Users
Joined: 1/13/2009(UTC)
Posts: 42

thanks Wabbit the problem I am having is that the indicator doesn't start plotting until ATR(21)/MP()*100 first crosses above four. how would one post a chart in Forum? ZigZag
wabbit  
#13 Posted : Saturday, July 25, 2009 10:24:42 PM(UTC)
wabbit

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers, Unverified Users
Joined: 10/28/2004(UTC)
Posts: 3,111
Location: Perth, Western Australia

Was thanked: 16 time(s) in 16 post(s)
wabbit  
#14 Posted : Monday, July 27, 2009 8:22:30 AM(UTC)
wabbit

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers, Unverified Users
Joined: 10/28/2004(UTC)
Posts: 3,111
Location: Perth, Western Australia

Was thanked: 16 time(s) in 16 post(s)
ZZ,

Using the free forum.dll to eliminate the N/A from the ValueWhen() function calls:

Code:

data:=ATR(21)/MP()*100;
i:=RSI(14);

hi1:=Highest(ExtFml("forum.sum",ValueWhen(1,data<=1,i),1));
hi2:=Highest(ExtFml("forum.sum",ValueWhen(1,data>1 AND data<=2,i),1));
hi3:=Highest(ExtFml("forum.sum",ValueWhen(1,data>2 AND data<=4,i),1));
hi4:=Highest(ExtFml("forum.sum",ValueWhen(1,data>4,i),1));

lo1:=Lowest(ExtFml("forum.sum",ValueWhen(1,data<=1,i),1));
lo2:=Lowest(ExtFml("forum.sum",ValueWhen(1,data>1 AND data<=2,i),1));
lo3:=Lowest(ExtFml("forum.sum",ValueWhen(1,data>2 AND data<=4,i),1));
lo4:=Lowest(ExtFml("forum.sum",ValueWhen(1,data>4,i),1));

hilo1:=hi1-lo1;
hilo2:=hi2-lo2;
hilo3:=hi3-lo3;
hilo4:=hi4-lo4;

zero:=Power(10,-8);

100*
If(data<=1,(i-lo1)/Max(hilo1,zero),
If(data>1 AND data<=2,(i-lo2)/Max(hilo2,zero),
If(data>2 AND data<=4,(i-lo3)/Max(hilo3,zero),
(i-lo4)/Max(hilo4,zero))));



Hope this helps.


wabbit [:D]

wabbit  
#15 Posted : Wednesday, July 29, 2009 10:05:34 AM(UTC)
wabbit

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers, Unverified Users
Joined: 10/28/2004(UTC)
Posts: 3,111
Location: Perth, Western Australia

Was thanked: 16 time(s) in 16 post(s)
ZZ,

As much as yesterday's code showed how to eliminate the N/A by using the forum.sum functionality, I don't think the code achieved the aim? I haven't spent much time thinking about what the resultant curve should look like and as no chart has been posted, I hope you'll find this version is faster whilst still eliminating the ValueWhen() N/A problem and produces a curve more inline with what I think the objective should be?

Code:

ind:=RSI(14);
data:=200*ATR(21)/(H+L);

zero:=Power(10,-8);
i:=Cum(IsDefined(data))=1;

x:=data<=1;
hi:=HighestSince(1,i OR x=0,ind);
lo:=LowestSince(1,i OR x=0,ind);
y1:=x*(ind-lo)/Max(hi-lo,zero);

x:=data>1 AND data<=2;
hi:=HighestSince(1,i OR x=0,ind);
lo:=LowestSince(1,i OR x=0,ind);
y2:=x*(ind-lo)/Max(hi-lo,zero);

x:=data>2 AND data<=4;
hi:=HighestSince(1,i OR x=0,ind);
lo:=LowestSince(1,i OR x=0,ind);
y3:=x*(ind-lo)/Max(hi-lo,zero);

x:=data>4;
hi:=HighestSince(1,i OR x=0,ind);
lo:=LowestSince(1,i OR x=0,ind);
y4:=x*(ind-lo)/Max(hi-lo,zero);

100*(y1+y2+y3+y4);


I have been putting in 14-16 hour days on a contract this week so I haven't been able to spend much time looking too closely at anything else but my own stuff; some comments about what you think is working and what isn't will allow me to dedicate more time to this or other members, otherwise I will consider this thread resolved?


Hope this helps.

wabbit [:D]

Users browsing this topic
Guest (Hidden)
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.