BullCharts Forum Homepage
Forum Home Forum Home > BullCharts > BullScript
  New Posts New Posts RSS Feed: RSI Divergence
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

RSI Divergence

 Post Reply Post Reply
Author
Message Reverse Sort Order / View First Unread Post
etrader View Drop Down
Regular
Regular
Avatar

Joined: 18 Dec 2004
Posts: 63
Post Options Post Options   Quote etrader Quote  Post ReplyReply Direct Link To This Post Topic: RSI Divergence
    Posted: 17 Aug 2005 at 12:09am

Wow  !! you have exceled Tim, I really appreciate the time and effort you have put in to explain things, and this will definately help me and others I am sure, to work through some practical Bullscript. I'll study this and no doubt will gain some useful knowledge for future reference. 

cheers etrader.

Back to Top
Tim Allen View Drop Down
BullCharts Staff
BullCharts Staff
Avatar

Joined: 10 Sep 2004
Location: Australia
Posts: 53
Post Options Post Options   Quote Tim Allen Quote  Post ReplyReply Direct Link To This Post Posted: 16 Aug 2005 at 4:05pm
Originally posted by etrader

I did try and copy and paste but got this error initially.

I'm not quite sure how MetaStock handles these things, but BullCharts expects that an indicator will contain a single formula, unless it encounters a semi-colon. "5 7" is a non-sensical mathematical expression, and so putting two if statements together as your MetaStock formula does is just as confusing.

The simple solution is: put a semi-colon after the first expression, like this:
{RSI(9) DIVERGENCE BUY:}
If(RSI(9) >= HHV(RSI(9),19) AND CLOSE <HHV(CLOSE,19), 1, 0)
OR
If(CLOSE <= LLV(CLOSE,19) AND RSI(9) > LLV(RSI(9),19), 1, 0);

{RSI(9) DIVERGENCE SELL:}
If(CLOSE >= HHV(CLOSE,19) AND RSI(9)<HHV(RSI(9),19), 1, 0)
OR
If(RSI(9) <= LLV(RSI(9),19) AND CLOSE > LLV(CLOSE,19), 1, 0);

Originally posted by etrader

I tried this also below which returned no errors, but not sure what the 0 is at the end. I would have expected maybe more returns ??

{RSI(9) DIVERGENCE BUY:}

IF (rsi(close,9) >= HHV(RSI(close,9),19) AND CLOSE < HHV(CLOSE,19),1,0) OR

IF (CLOSE <= LLV(CLOSE,19) AND RSI(9) > LLV(RSI(close,9),19), 1,0);



That's the same as the first half of what I got, so you're doing alright there. However, when I add that indicator to a chart of, say, BHP or TLS, I get a steady, unwavering line at 0, indicating that this particular signal never went off.

When I look at a big, complicated expression like this, I can't imagine how it works all in my head at the same time, so I'm going to break it up into smaller components and see how those work.
  • I'm going to delete the SELL half of the indicator to simplify things, because it's basically just the inverse of the BUY half - whatever makes the BUY half work should make the SELL half work as well.
  • I'm going to comment-out the BUY half, because I know it always returns 0, so it'll just get in the way. I won't delete it because I want to copy-and-paste things from it.
  • When I look at the BUY indicator, the first thing I notice is that it's got two conditions, linked by an OR: "cond1 OR cond2". So it would be a good idea to investigate each of these conditions separately before putting them together.
  • The first condition is silly, since it looks like this: "if(cond3, 1, 0)". Basically, if cond3 is true, the if() function returns true. If cond3 is false, the if() function returns false. Therefore "if(cond3, 1, 0)" is exactly equivalent to "(cond3)". So let's look at cond3.
  • cond3 contains two expressions, linked with an AND: "RSI(9) >= HHV(RSI(9),19) AND CLOSE <HHV(CLOSE,19)". Let's look at both halves of the indicator individually, and see what happens.
Our indicator so far:
{RSI(9) DIVERGENCE BUY:}
{
If(RSI(9) >= HHV(RSI(9),19) AND CLOSE <HHV(CLOSE,19),1,0)
OR
If(CLOSE <= LLV(CLOSE,19) AND RSI(9) > LLV(RSI(9),19),1,0);
}

RSI(9) >= HHV(RSI(9),19);
CLOSE < HHV(CLOSE,19);

Hmm... this indicator fires signals all over the place, so something's going on there. However, since all I can see is blue, I'm not sure which bits are which expression. Let's add some colours to the indicator so we can tell the lines apart:

[color=red]
RSI(9) >= HHV(RSI(9),19);
[color=blue]
CLOSE < HHV(CLOSE,19);

Hmm.. all blue, no red. I think there must be something wrong with the first expression, although it looks simple enough... Looking in the Script Helper (tick 'Show script helper' at the bottom of the indicator window), I see that the RSI function is supposed to take two parameters - an expression and a period. Let's change the first expression to include CLOSE:

[color=red]
RSI(CLOSE, 9) >= HHV(RSI(CLOSE, 9),19);
[color=blue]
CLOSE < HHV(CLOSE,19);

Ah, now we've got a dancing blue line and a dancing red line. If I combine both of those back into cond3:

RSI(CLOSE, 9) >= HHV(RSI(CLOSE, 9),19) AND CLOSE < HHV(CLOSE,19);

...then I get a line that spikes only occasionally, which is more like what I expected an indicator to do. So let's put the BUY and SELL signals back together, with everything we've learned:

{RSI(9) DIVERGENCE BUY:}
(RSI(CLOSE, 9) >= HHV(RSI(CLOSE, 9), 19) AND CLOSE < HHV(CLOSE, 19))
OR
(CLOSE <= LLV(CLOSE, 19) AND RSI(CLOSE, 9) > LLV(RSI(CLOSE, 9), 19));

{RSI(9) DIVERGENCE SELL:}
(CLOSE >= HHV(CLOSE, 19) AND RSI(CLOSE, 9) < HHV(RSI(CLOSE, 9), 19))
OR
(RSI(CLOSE, 9) <= LLV(RSI(CLOSE, 9), 19) AND CLOSE > LLV(CLOSE, 19));

I don't know if that's exactly the output you were expecting, but it certainly seems a lot more informative than it did to start with.

Originally posted by etrader

I understand what divergence is, and that we are looking for a higher RSI value after 19 bars and a lower close after 19 bars [and the opposite for the LLV], but the problem I have is my programming iliteracy I guess, and how to make this totally effective with markers etc.


Markers are really simple, once you've got an indicator that does more or less what you want (as we have here). For details, see the Attribute Reference in the BullScript Help, but to start you off here's the same formula as above, but with attributes added to turn the lines into markers:

[target=Price]

{RSI(9) DIVERGENCE BUY:}
[name=Type1; linestyle=marker; marker=type1; tooltip="Decide for yourself what you should do in this situation"]
(RSI(CLOSE, 9) >= HHV(RSI(CLOSE, 9), 19) AND CLOSE < HHV(CLOSE, 19))
OR
(CLOSE <= LLV(CLOSE, 19) AND RSI(CLOSE, 9) > LLV(RSI(CLOSE, 9), 19));

{RSI(9) DIVERGENCE SELL:}
[name=Type2; linestyle=marker; marker=type2; tooltip="Decide for yourself what you should do in this situation"]
(CLOSE >= HHV(CLOSE, 19) AND RSI(CLOSE, 9) < HHV(RSI(CLOSE, 9), 19))
OR
(RSI(CLOSE, 9) <= LLV(RSI(CLOSE, 9), 19) AND CLOSE > LLV(CLOSE, 19));



Back to Top
etrader View Drop Down
Regular
Regular
Avatar

Joined: 18 Dec 2004
Posts: 63
Post Options Post Options   Quote etrader Quote  Post ReplyReply Direct Link To This Post Posted: 15 Aug 2005 at 8:52pm

I did try and copy and paste but got this error initially.

I am not a programmer so am trying to understand the IF's and buts. I tried this also below which returned no errors, but not sure what the 0 is at the end. I would have expected maybe more returns ??

I understand what divergence is, and that we are looking for a higher RSI value after 19 bars and a lower close after 19 bars [and the opposite for the LLV], but the problem I have is my programming iliteracy I guess, and how to make this totally effective with markers etc.

{RSI(9) DIVERGENCE BUY:}

IF (rsi(close,9) >= HHV(RSI(close,9),19) AND CLOSE < HHV(CLOSE,19),1,0) OR

IF (CLOSE <= LLV(CLOSE,19) AND RSI(9) > LLV(RSI(close,9),19), 1,0);

Back to Top
Tim Allen View Drop Down
BullCharts Staff
BullCharts Staff
Avatar

Joined: 10 Sep 2004
Location: Australia
Posts: 53
Post Options Post Options   Quote Tim Allen Quote  Post ReplyReply Direct Link To This Post Posted: 15 Aug 2005 at 10:35am
Before I get too far in, what BullScript did you come up with, and what did it do that you didn't expect?
Back to Top
etrader View Drop Down
Regular
Regular
Avatar

Joined: 18 Dec 2004
Posts: 63
Post Options Post Options   Quote etrader Quote  Post ReplyReply Direct Link To This Post Posted: 12 Aug 2005 at 12:43pm

I would appreciate if you could help with this conversion from Metastock to bullscript Tim. I thought i could work it out, but got stuck.

RSI Divergence

{RSI(9) DIVERGENCE BUY:}
If(RSI(9) >= HHV(RSI(9),19) AND CLOSE <HHV(CLOSE,19), 1,0) OR
If(CLOSE <= LLV(CLOSE,19) AND RSI(9) > LLV(RSI(9),19), 1,0)

{RSI(9) DIVERGENCE SELL:}
If(CLOSE >= HHV(CLOSE,19) AND RSI(9)<HHV(RSI(9),19),1,0) OR
If(RSI(9) <= LLV(RSI(9),19) AND CLOSE > LLV(CLOSE,19),1,0)

{You can substitute any formula for the "RSI(9)"}

cheers etrader



Edited by etrader
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down

Bulletin Board Software by Web Wiz Forums® version 9.69
Copyright ©2001-2010 Web Wiz