Welcome! Log In Create A New Profile

Get Earnings and Seasonal Trends - Subscribe Today!

Advanced

divergence idea needs a help smiling smiley

Posted by optiontrader101 
divergence idea needs a help smiling smiley
April 29, 2014 01:38PM
I have 2 indicators one is in upper chart and the other is down >

UP INDICATOR :

first one is with the top of candles ( if today high is higher than yesterday will make green line between the 2 highs. if it is lower will be red ).
the other one with the bottom of candles ( if today low is lower than yesterday will make red line between the 2 lows. if it is higher will be green )

#Script :

plot top= high;
top.AssignValueColor(if high > high[1] then color.green else if high < high[1] then color.red else color.yellow);

plot bot= low;
bot.AssignValueColor(if low > low[1] then color.green else if low < low[1] then color.red else color.yellow);


LOWER INDICATOR :

Value chart :

#script

#VALUE_CHART
declare lower;

input length = 5;
input capSpikesAt = 10;

def VarP = Round(length / 5);
def VarA = Highest(high, VarP) - Lowest(low, VarP);
def VarR1 = if VarA == 0 and VarP == 1 then AbsValue(close - close[VarP]) else VarA;
def VarB = Highest(high, VarP)[VarP + 1] - Lowest(low, VarP)[VarP];
def VarR2 = if VarB == 0 and VarP == 1 then AbsValue(close[VarP] - close[VarP * 2]) else VarB;
def VarC = Highest(high, VarP)[VarP * 2] - Lowest(low, VarP)[VarP * 2];
def VarR3 = if VarC == 0 and VarP == 1 then AbsValue(close[VarP * 2] - close[VarP * 3]) else VarC;
def VarD = Highest(high, VarP)[VarP * 3] - Lowest(low, VarP)[VarP * 3];
def VarR4 = if VarD == 0 and VarP == 1 then AbsValue(close[VarP * 3] - close[VarP * 4]) else VarD;

def VarE = Highest(high, VarP)[VarP * 4] - Lowest(low, VarP)[VarP * 4];
def VarR5 = if VarE == 0 and VarP == 1 then AbsValue(close[VarP * 4] - close[VarP * 5]) else VarE;
def LRange = ((VarR1 + VarR2 + VarR3 + VarR4 + VarR5) / 5) * 0.2;
def Var0 = if AbsValue(close - close[1]) > (high - low) then AbsValue(close - close[1]) else (high - low);
def LRange2 = if high == low then Average(AbsValue(close - close[1]), 5) * 0.2 else Average(Var0, 5) * 0.2;

def range = high + low;
def delta = high - low;
def median = range / 2;
def floatingAxis = Average(median, length);
def dynamicVolatilityUnit = if length <= 7 then LRange2 else LRange;
def relativeHigh = (high - floatingAxis) / dynamicVolatilityUnit;
def relativeLow = (low - floatingAxis) / dynamicVolatilityUnit;
def relativeOpen = (open - floatingAxis) / dynamicVolatilityUnit;
def relativeClose = (close - floatingAxis) / dynamicVolatilityUnit;

plot "High" = Min(relativeHigh, capSpikesAt);
"High".SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
"High".AssignValueColor(if "High" >= 8 then Color.RED else Color.BLACK);
"High".SetLineWeight(4);

plot "Low" = Max(relativeLow, -capSpikesAt);
"Low".SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
"Low".AssignValueColor(if "Low" <= -8 then Color.GREEN else Color.BLACK);
"Low".SetLineWeight(4);

plot backgroundColor = if "Low" > 0 then "Low" else if "High" < 0 then "High" else Double.NaN;
backgroundColor.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
backgroundColor.SetDefaultColor(Color.BLACK);
backgroundColor.SetLineWeight(4);

#plot "Open" = Min(AbsValue(relativeOpen), capSpikesAt) * relativeOpen / AbsValue(relativeOpen);
#"Open".SetStyle(Curve.POINTS);
#"Open".SetLineWeight(1);
#"Open".SetDefaultColor(Color.WHITE);
#"Open".Hide();

#plot "Close" = Min(AbsValue(relativeClose), capSpikesAt) * relativeClose / AbsValue(relativeClose);
#"Close".SetStyle(Curve.POINTS);
#"Close".SetLineWeight(1);
#"Close".AssignValueColor(if "Close" < "Open" then GetColor(5) else GetColor(6));
#"Close".Hide();

plot zero = 0;
zero.SetDefaultColor(Color.WHITE);

plot "Moderately O/B" = 4;
plot "Moderately O/S" = -4;
"Moderately O/B".SetDefaultColor(Color.YELLOW);
"Moderately O/S".SetDefaultColor(Color.YELLOW);

plot "Edge O/B" = 12;
plot "Edge O/S" = -12;
"Edge O/B".SetDefaultColor(Color.RED);
"Edge O/S".SetDefaultColor(Color.RED);

plot "Significantly O/B" = 8;
plot "Significantly O/S" = -8;
"Significantly O/B".SetDefaultColor(Color.RED);
"Significantly O/S".SetDefaultColor(Color.RED);

AddCloud("Moderately O/B","Moderately O/S", Color.Green, Color.Green);
AddCloud("Moderately O/B","Significantly O/B", Color.Yellow, Color.Yellow);
AddCloud("Moderately O/S","Significantly O/S", Color.Yellow, Color.Yellow);
AddCloud("Edge O/B","Significantly O/B", Color.RED, Color.RED);
AddCloud("Edge O/S","Significantly O/S", Color.RED, Color.RED);

Plot highline = if "High" > 0 then "High" else 0;
highline.assignValueColor(if "High" > "High"[1] then color.GREEN
else if "High" < "High"[1] then Color.RED
else Color.YELLOW);
highline.setLineWeight(1);
Plot lowline = if "low" < 0 then "low" else 0;
lowline.assignValueColor(if "low" > "low"[1] then color.GREEN
else if "low" < "low"[1] then Color.RED
else Color.YELLOW);
lowline.setLineWeight(1);



the chart will be like this :




_____________

What I want is that I realized a divergence when I compared between the upper indicator and the lower indicator (daily chart ) as shown :





CAN any one write a script to combine these two indicator to gives an arrow ( green - red ) or vertical line ( green - red ) when there is a divergence ?

NB: there are a clear -ve or +ve divergence and hidden +ve or -ve divergence


thanks ,,
Re: divergence idea needs a help smiling smiley
April 29, 2014 06:36PM
here you go.



input length = 5;
input capSpikesAt = 10;

def VarP = Round(length / 5);
def VarA = Highest(high, VarP) - Lowest(low, VarP);
def VarR1 = if VarA == 0 and VarP == 1 then AbsValue(close - close[VarP]) else VarA;
def VarB = Highest(high, VarP)[VarP + 1] - Lowest(low, VarP)[VarP];
def VarR2 = if VarB == 0 and VarP == 1 then AbsValue(close[VarP] - close[VarP * 2]) else VarB;
def VarC = Highest(high, VarP)[VarP * 2] - Lowest(low, VarP)[VarP * 2];
def VarR3 = if VarC == 0 and VarP == 1 then AbsValue(close[VarP * 2] - close[VarP * 3]) else VarC;
def VarD = Highest(high, VarP)[VarP * 3] - Lowest(low, VarP)[VarP * 3];
def VarR4 = if VarD == 0 and VarP == 1 then AbsValue(close[VarP * 3] - close[VarP * 4]) else VarD;

def VarE = Highest(high, VarP)[VarP * 4] - Lowest(low, VarP)[VarP * 4];
def VarR5 = if VarE == 0 and VarP == 1 then AbsValue(close[VarP * 4] - close[VarP * 5]) else VarE;
def LRange = ((VarR1 + VarR2 + VarR3 + VarR4 + VarR5) / 5) * 0.2;
def Var0 = if AbsValue(close - close[1]) > (high - low) then AbsValue(close - close[1]) else (high - low);
def LRange2 = if high == low then Average(AbsValue(close - close[1]), 5) * 0.2 else Average(Var0, 5) * 0.2;

def range = high + low;
def delta = high - low;
def median = range / 2;
def floatingAxis = Average(median, length);
def dynamicVolatilityUnit = if length <= 7 then LRange2 else LRange;
def relativeHigh = (high - floatingAxis) / dynamicVolatilityUnit;
def relativeLow = (low - floatingAxis) / dynamicVolatilityUnit;
def relativeOpen = (open - floatingAxis) / dynamicVolatilityUnit;
def relativeClose = (close - floatingAxis) / dynamicVolatilityUnit;

def "High" = Min(relativeHigh, capSpikesAt);
def "Low" = Max(relativeLow, -capSpikesAt);

def bullDiv = (low < low[1]) and (low < low[-1]) and ("Low" > "Low"[1]);
def bearDiv = (high > high[1]) and (high > high[-1]) and ("High" < "High"[1]);

plot upArrow = if bullDiv and !bullDiv[1] then low * 0.997 else Double.NaN;
upArrow.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
upArrow.SetLineWeight(4);
upArrow.SetDefaultColor(Color.GREEN);

plot dnArrow = if bearDiv and !bearDiv[1] then high * 1.003 else Double.NaN;
dnArrow.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
dnArrow.SetLineWeight(4);
dnArrow.SetDefaultColor(Color.RED);



Edited 1 time(s). Last edit at 04/29/2014 06:46PM by robert.
Re: divergence idea needs a help smiling smiley
April 29, 2014 09:25PM
WOW Robert , thank you very much , that's really a great job i appreciate it ,

it is really working perfectly but I don't know why it missed some divergence in the daily candles such as in

$MMM :





$MET :





Is it possible to be fixed ?
Re: divergence idea needs a help smiling smiley
April 30, 2014 12:01AM
Quote
optiontrader101
WOW Robert , thank you very much , that's really a great job i appreciate it ,

You're welcome.

Quote
optiontrader101
it is really working perfectly but I don't know why it missed some divergence in the daily candles

I was trying to filter out some of the extra arrows so that it only showed the ones which occurred at a turning point. If you want all the arrows, then use this code instead.



input length = 5;
input capSpikesAt = 10;

def VarP = Round(length / 5);
def VarA = Highest(high, VarP) - Lowest(low, VarP);
def VarR1 = if VarA == 0 and VarP == 1 then AbsValue(close - close[VarP]) else VarA;
def VarB = Highest(high, VarP)[VarP + 1] - Lowest(low, VarP)[VarP];
def VarR2 = if VarB == 0 and VarP == 1 then AbsValue(close[VarP] - close[VarP * 2]) else VarB;
def VarC = Highest(high, VarP)[VarP * 2] - Lowest(low, VarP)[VarP * 2];
def VarR3 = if VarC == 0 and VarP == 1 then AbsValue(close[VarP * 2] - close[VarP * 3]) else VarC;
def VarD = Highest(high, VarP)[VarP * 3] - Lowest(low, VarP)[VarP * 3];
def VarR4 = if VarD == 0 and VarP == 1 then AbsValue(close[VarP * 3] - close[VarP * 4]) else VarD;

def VarE = Highest(high, VarP)[VarP * 4] - Lowest(low, VarP)[VarP * 4];
def VarR5 = if VarE == 0 and VarP == 1 then AbsValue(close[VarP * 4] - close[VarP * 5]) else VarE;
def LRange = ((VarR1 + VarR2 + VarR3 + VarR4 + VarR5) / 5) * 0.2;
def Var0 = if AbsValue(close - close[1]) > (high - low) then AbsValue(close - close[1]) else (high - low);
def LRange2 = if high == low then Average(AbsValue(close - close[1]), 5) * 0.2 else Average(Var0, 5) * 0.2;

def range = high + low;
def delta = high - low;
def median = range / 2;
def floatingAxis = Average(median, length);
def dynamicVolatilityUnit = if length <= 7 then LRange2 else LRange;
def relativeHigh = (high - floatingAxis) / dynamicVolatilityUnit;
def relativeLow = (low - floatingAxis) / dynamicVolatilityUnit;
def relativeOpen = (open - floatingAxis) / dynamicVolatilityUnit;
def relativeClose = (close - floatingAxis) / dynamicVolatilityUnit;

def "High" = Min(relativeHigh, capSpikesAt);
def "Low" = Max(relativeLow, -capSpikesAt);

def bullDiv = (low <= low[1]) and ("Low" > "Low"[1]);
def bearDiv = (high >= high[1]) and ("High" < "High"[1]);

plot upArrow = if bullDiv then low * 0.997 else Double.NaN;
upArrow.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
upArrow.SetLineWeight(4);
upArrow.SetDefaultColor(Color.GREEN);

plot dnArrow = if bearDiv then high * 1.003 else Double.NaN;
dnArrow.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
dnArrow.SetLineWeight(4);
dnArrow.SetDefaultColor(Color.RED);
Re: divergence idea needs a help smiling smiley
April 30, 2014 08:35AM
I got it ,,

thanks alot smiling smiley
Re: divergence idea needs a help smiling smiley
May 06, 2014 05:20PM
Can't seem to get this working as a column signal.
Re: divergence idea needs a help smiling smiley
May 06, 2014 05:39PM
Quote
mtut
Can't seem to get this working as a column signal.

try this



def length = 5;
def capSpikesAt = 10;

def VarP = Round(length / 5);
def VarA = Highest(high, VarP) - Lowest(low, VarP);
def VarR1 = if VarA == 0 and VarP == 1 then AbsValue(close - close[VarP]) else VarA;
def VarB = Highest(high, VarP)[VarP + 1] - Lowest(low, VarP)[VarP];
def VarR2 = if VarB == 0 and VarP == 1 then AbsValue(close[VarP] - close[VarP * 2]) else VarB;
def VarC = Highest(high, VarP)[VarP * 2] - Lowest(low, VarP)[VarP * 2];
def VarR3 = if VarC == 0 and VarP == 1 then AbsValue(close[VarP * 2] - close[VarP * 3]) else VarC;
def VarD = Highest(high, VarP)[VarP * 3] - Lowest(low, VarP)[VarP * 3];
def VarR4 = if VarD == 0 and VarP == 1 then AbsValue(close[VarP * 3] - close[VarP * 4]) else VarD;

def VarE = Highest(high, VarP)[VarP * 4] - Lowest(low, VarP)[VarP * 4];
def VarR5 = if VarE == 0 and VarP == 1 then AbsValue(close[VarP * 4] - close[VarP * 5]) else VarE;
def LRange = ((VarR1 + VarR2 + VarR3 + VarR4 + VarR5) / 5) * 0.2;
def Var0 = if AbsValue(close - close[1]) > (high - low) then AbsValue(close - close[1]) else (high - low);
def LRange2 = if high == low then Average(AbsValue(close - close[1]), 5) * 0.2 else Average(Var0, 5) * 0.2;

def range = high + low;
def delta = high - low;
def median = range / 2;
def floatingAxis = Average(median, length);
def dynamicVolatilityUnit = if length <= 7 then LRange2 else LRange;
def relativeHigh = (high - floatingAxis) / dynamicVolatilityUnit;
def relativeLow = (low - floatingAxis) / dynamicVolatilityUnit;
def relativeOpen = (open - floatingAxis) / dynamicVolatilityUnit;
def relativeClose = (close - floatingAxis) / dynamicVolatilityUnit;

def "High" = Min(relativeHigh, capSpikesAt);
def "Low" = Max(relativeLow, -capSpikesAt);

def bullDiv = (low <= low[1]) and ("Low" > "Low"[1]);
def bearDiv = (high >= high[1]) and ("High" < "High"[1]);
def null = !bullDiv and !bearDiv;

AddLabel(bullDiv, "Bull Div" );
AddLabel(bearDiv, "Bear Div" );
AddLabel(null, " " );

AssignBackgroundColor(if bullDiv then Color.DARK_GREEN else if bearDiv then Color.DARK_RED else Color.CURRENT);



Edited 2 time(s). Last edit at 05/06/2014 05:48PM by robert.
Re: divergence idea needs a help smiling smiley
May 06, 2014 06:34PM
I am using your first version that shows fewer signals and I exchanged this code

def bullDiv = (low < low[1]) and (low < low[-1]) and ("Low" > "Low"[1]);
def bearDiv = (high > high[1]) and (high > high[-1]) and ("High" < "High"[1]);

and now the column never finishes loading. been waiting 30 minutes. Your new code loads right in.
Re: divergence idea needs a help smiling smiley
May 06, 2014 08:05PM
Quote
mtut
I am using your first version that shows fewer signals and I exchanged this code

def bullDiv = (low < low[1]) and (low < low[-1]) and ("Low" > "Low"[1]);
def bearDiv = (high > high[1]) and (high > high[-1]) and ("High" < "High"[1]);

and now the column never finishes loading. been waiting 30 minutes. Your new code loads right in.

Code written for a charting indicator has the luxury of seeing all the candles in a range; whereas the code for the watchlist columns can only look at the current candle and those before it.

def bullDiv = (low < low[1]) and (low < low[-1]) and ("Low" > "Low"[1]);

That line of code looks at three separate candles.

1) this candle -- low
2) the candle before this one -- low[1]
3) the candle after this one -- low[-1]

That logic works fine on a charting indicator because the indicator starts from the oldest candle, then works its way to the right until it reaches the current candle.

The watchlist column, however, can only look at the current candle and those behind it. So, using that line of code, it would first look at the current candle (low) which is ok. Then it would compare it to the candle before the current candle (low[1]) and that is still ok. Then it would try to compare the current candle to the one after it (low[-1]) which is no longer ok because the watchlist column can't look into the future.

If you wanted to use the same logic as that code, you'd have to rewrite it so that it was only backward looking.

def bullDiv = (low[1] < low[2]) and (low[1] < low) and ("Low"[1] > "Low"[2]); 
def bearDiv = (high[1] > high[2]) and (high[1] > high) and ("High"[1] < "High"[2]);

I hope that makes sense. My mind is sort of mush right now so I'm having a difficult time communicating clearly. confused smiley



Edited 1 time(s). Last edit at 05/06/2014 08:14PM by robert.
Re: divergence idea needs a help smiling smiley
May 07, 2014 09:21PM
Robert,

Just a quick question on the image below.



I checked the May 6 Daily charts for CSCO, FNMA and YHOO, and didn't see any divergence (at least the way it was taught in RTP class). Is there another time frame or underlying indicator being used to indicate divergence for these stocks? Just curious, thanks much!

RaleighTrader
Re: divergence idea needs a help smiling smiley
May 08, 2014 01:03AM
Quote
RaleighTrader
I checked the May 6 Daily charts for CSCO, FNMA and YHOO, and didn't see any divergence (at least the way it was taught in RTP class). Is there another time frame or underlying indicator being used to indicate divergence for these stocks? Just curious, thanks much!

RaleighTrader

This indicator has absolutely nothing to do with the GW material. Everything in this thread is in response to the special request from optiontrader101 who wanted arrows indicating when a divergence occurred based on the formulas he provided in his first posting which are non-standard. Then mtut asked for assistence in converting that code to a watchlist column.
Sorry, only registered users may post in this forum.

Click here to login