Welcome! Log In Create A New Profile

Get Earnings and Seasonal Trends - Subscribe Today!

Advanced

Fun with ThinkScript

Posted by robert 
Re: Fun with ThinkScript
March 15, 2016 02:35PM
tanman, do you have a thinkscript for marking globex highs/lows?
also a daily open that shows the open at 9:30 even if on a extended session chart?
thanks for all your great scripts!
Re: Fun with ThinkScript
March 15, 2016 04:39PM
Rob Miller,

You are welcome. Here is the script:


input Market_Open_Time = 0930;

def OpeningBell = if SecondsTillTime(Market_Open_Time)[1] >= 0 and SecondsTillTime(Market_Open_Time) < 0 or (SecondsTillTime(Market_Open_Time)[1] < SecondsTillTime(Market_Open_Time) and SecondsTillTime(Market_Open_Time)[1] >= 0) then 1 else 0;
rec RunningOpen = CompoundValue(1, if Openingbell then open[1] else RunningOpen[1], open);
rec PrevOpen = CompoundValue(1, if OpeningBell then RunningOpen else PrevOpen[1], open);

plot PO = if SecondsFromTime(Market_Open_Time - 1) > 0 and SecondsFromTime(Market_Open_Time) < 60 then Double.NaN else PrevOpen;
PO.SetStyle(Curve.SHORT_DASH);
PO.SetDefaultColor(Color.CYAN);
PO.SetLineWeight(2);

plot Yhigh = high(period = "day" )[1];
Yhigh.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Yhigh.SetDefaultColor(Color.GREEN);
Yhigh.SetLineWeight(2);

plot Ylow = low(period = "day" )[1];
Ylow.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Ylow.SetDefaultColor(Color.RED);
Ylow.SetLineWeight(2);
Re: Fun with ThinkScript
March 15, 2016 06:15PM
Rob Miller,

This script will also plot the lowest low before regular trading hours in red dashed line, if below previous day low and highest high before regular trading hours in green dashed line, if above previous day high:

input Market_Open_Time = 0930;

def OpeningBell = if SecondsTillTime(Market_Open_Time)[1] >= 0 and SecondsTillTime(Market_Open_Time) < 0 or (SecondsTillTime(Market_Open_Time)[1] < SecondsTillTime(Market_Open_Time) and SecondsTillTime(Market_Open_Time)[1] >= 0) then 1 else 0;
rec RunningOpen = CompoundValue(1, if Openingbell then open[1] else RunningOpen[1], open);
rec PrevOpen = CompoundValue(1, if OpeningBell then RunningOpen else PrevOpen[1], open);

plot PO = if SecondsFromTime(Market_Open_Time - 1) > 0 and SecondsFromTime(Market_Open_Time) < 60 then Double.NaN else PrevOpen;
PO.SetStyle(Curve.SHORT_DASH);
PO.SetDefaultColor(Color.CYAN);
PO.SetLineWeight(2);

plot Yhigh = high(period = "day" )[1];
Yhigh.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Yhigh.SetDefaultColor(Color.GREEN);
Yhigh.SetLineWeight(2);

plot Ylow = low(period = "day" )[1];
Ylow.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Ylow.SetDefaultColor(Color.RED);
Ylow.SetLineWeight(2);

#Plot intraday high and intraday low
def h3 = high > Highest(high[-3], 3);
def dayHigh = if GetDay() != GetDay()[1] then high else if h3 and SecondsTillTime(Market_Open_Time) > 0 and high > dayHigh[1] then high else dayHigh[1];
def l3 = low < Lowest(low[-3], 3);
def dayLow = if GetDay() != GetDay()[1] then low else if l3 and SecondsTillTime(Market_Open_Time) > 0 and low < dayLow[1] then low else dayLow[1];

plot DH = if dayHigh > Yhigh then dayHigh else Double.NaN;
DH.SetStyle(Curve.SHORT_DASH);
DH.SetDefaultColor(Color.GREEN);
plot DL = if dayLow < Ylow then dayLow else Double.NaN;
DL.SetStyle(Curve.SHORT_DASH);
DL.SetDefaultColor(Color.RED);
Re: Fun with ThinkScript
March 15, 2016 07:02PM
Just curious, but has anyone ever tried or is there a script that will mark a bar when either a certain amount of contracts have been sold or just a high number of contracts sold? For example, paint a circle or something under a bar when 10 contracts have been sold?
Re: Fun with ThinkScript
March 15, 2016 07:15PM
Thank you tanman. The script works now.
Re: Fun with ThinkScript
March 17, 2016 07:57AM
bughatti,

This script will paint a dot below the current bar once 3000 contracts have been traded. You can change the number of contracts in edit properties.

input contracts = 3000;
plot traded = if volume >= contracts then low else Double.NaN;
traded.SetPaintingStrategy(PaintingStrategy.POINTS);
traded.AssignValueColor(Color.CYAN);
traded.SetLineWeight(3);



TomD,

You are welcome.
Re: Fun with ThinkScript
March 17, 2016 08:56AM
Thanks Tanman,. that was exactly what I needed. Next question I have is this, I am currently playing with Woodies CCI and there is a piece of the histogram that turns yellow after on the 4th bar indication a possible trend emergence. Is it possible to paint the bar a color or add another circle or square below or above the bar?

"CCI Hist represents CCI values in histogram format: red columns for downtrend, green columns for uptrend; gray/yellow columns mean that the trend has not established yet (six consequent bars above or below the zero line are needed). When trend is developing, CCIHist applies the colors to columns using the following algorithm: four bars after zero crossover are gray (early development), fifth is yellow (warning that trend might emerge), and starting from the sixth, columns are red or green, based on trend direction."

I would also like to see if I can add another circle or square above or below the bar when that bar is testing a previous high or low.

Thanks a million, in advance!
Re: Fun with ThinkScript
March 17, 2016 07:22PM
bughatti,

You are welcome. The Woodies CCI is too complicated and beyond my capabilities. The following script will mark a bar with green dot when testing previous day high or previous intraday high and mark a bar with red dot when testing previous day low or intraday low:

def yhigh = high(period = "day" )[1];
plot yhightest = if open < yhigh and high >= yhigh then low else double.nan;
yhightest.setpaintingstrategy(paintingstrategy.points);
yhightest.assignvaluecolor(color.green);
yhightest.setlineweight(3);

def ylow = low(period = "day" )[1];
plot ylowtest = if open > ylow and low <= ylow then high else double.nan;
ylowtest.setpaintingstrategy(paintingstrategy.points);
ylowtest.assignvaluecolor(color.red);
ylowtest.setlineweight(3);


def h3 = high > highest(high[-3], 3);
def dayhigh = if getday() != getday()[1] then high else if h3 and high > dayhigh[1] then high else dayhigh[1];
plot dhightest = if open < dayhigh and high >= dayhigh then low else double.nan;
dhightest.setpaintingstrategy(paintingstrategy.points);
dhightest.assignvaluecolor(color.green);
dhightest.setlineweight(3);


def l3 = low < lowest(low[-3], 3);
def daylow = if getday() != getday()[1] then low else if l3 and low < daylow[1] then low else daylow[1];
plot dlowtest = if open > daylow and low <= daylow then high else double.nan;
dlowtest.setpaintingstrategy(paintingstrategy.points);
dlowtest.assignvaluecolor(color.red);
dlowtest.setlineweight(3);
Re: Fun with ThinkScript
March 19, 2016 07:51AM
One more Question: eye rolling smiley

If my Signal Arrow appears after 2 bars , How can I code an Alert for that signal ?

This code will give alert sound on the current bar :
Alert(SellSignal, "Time to go SHORT", Alert.BAR, Sound.Ring);

But my Arrow will appear before two bars !!
Re: Fun with ThinkScript
March 19, 2016 10:59AM
SARA,

In the alert line, replace SellSignal with the variable that plots the arrow. You might have to add [1] to that variable. Try it both ways with and without [1].
Re: Fun with ThinkScript
March 19, 2016 01:56PM
Thank you Tanman ,

I just Change it as you said but I put [2] and the alert works perfectly smiling bouncing smiley

Alert(SellSignal[2], "Time to go SHORT", Alert.bar, Sound.Ring);
ATR Trailing Stop Cross Over
March 20, 2016 11:54AM
I am trying to set up an scan that will allow me to determine when price crosses above or below the 9, 2.9 ATR trailing stop. The condition wizard puts togeather the below code but it does not seem to work. Can anyone help me with this?

close is less than MovAvgExponential("length" = 8) and close is less than SimpleMovingAvg("length" = 21) and close crosses below ATRTrailingStop("atr period" = 9, "atr factor" = 2.9) or close is greater  than MovAvgExponential("length" = 8) and close is greater  than SimpleMovingAvg("length" = 21) and close crosses above ATRTrailingStop("atr period" = 9, "atr factor" = 2.9)
Re: ATR Trailing Stop Cross Over
March 20, 2016 03:32PM
I guess what I really need help with (if possible) I need to know when pric goes below t;he 9 EMA and the 21 SMA alone with going below the green ATR line as shown in the below URL.


ATR Trailing stop alert
Re: Fun with ThinkScript
March 22, 2016 06:27PM
Can anyone please help me with this indicator. Basically I would like an up or down arrow on the upper charts when conditions for the MacD occur
1. Down arrow when the Value line of the MacD Crosses below the zero line within one bar
2.) Up arrow when the value line of the MacD crosses above zeroline within one bar
3.) A White dot when Value line crosses up through Avg line only when a cross happens below zeroline, with a dashed line above price bar where Dot occured That only shows for the day
4.) A Red dot when Value line crosses down through Avg line only when above zeroline, with a dashed line below price bar where Dot occured That only shows for the day

Also if I can get help running a scannable script for the White and Red Dots.

I've Tried coding myself but I end up getting arrows everywhere instead of only one when conditions occur

Please if anyone can help me
Re: ATR Trailing Stop Cross Over
March 23, 2016 11:40AM
This is what I came up with. Seems to work OK.


alert(state != state[1], "ATR Trailing Stop Alert", alert.BAR, sound.ring);
assignBackgroundColor(if state != state[1] or state != state[2] then color.yellow else color.current);
Re: Fun with ThinkScript
March 24, 2016 02:33PM
Im looking for a TRIN indicator that will post in the lower studies, just simply a line graph that shows the TRIN. i also use sierra charts and found a custom TRIN study that looks good. I would also like it on my ToS. the only description on it I can find in sierra is "Better $TRIN indicator from e-miniwatch.com. Description: TRIN oscillator by Scottorama Discussion subtopic (Help with TRIN oscillator code)" . anyone ever heard of this or can help is much appreciated. Thanks


Also a commitment of traders ( COT ) thinkscript?
Re: Fun with ThinkScript
March 25, 2016 12:48AM
rob miller Wrote:
-------------------------------------------------------
> Im looking for a TRIN indicator that will post in
> the lower studies, just simply a line graph that
> shows the TRIN. i also use sierra charts and found
> a custom TRIN study that looks good. I would also
> like it on my ToS. the only description on it I
> can find in sierra is "Better $TRIN indicator from
> e-miniwatch.com. Description: TRIN oscillator by
> Scottorama Discussion subtopic (Help with TRIN
> oscillator code)" . anyone ever heard of this or
> can help is much appreciated. Thanks
>
>
> Also a commitment of traders ( COT ) thinkscript?


I have this script that will show it on the Chart :

# $ADVN - $DECN
input AD_Trend_Length = 1;
def AD = close("$ADVN" ) - close("$DECN" );
def ADtrend = Average(AD[1], 1);
AddLabel(AD > ADtrend, "A/D:  " + Round(AD, 2), Color.GREEN);
AddLabel(AD <= ADtrend, "A/D:  " + Round(AD, 2), Color.RED);

# $TICK
def tick = close("$TICK" );
def overbought = tick > 800;
def oversold = tick < -800;
AddLabel(overbought, "$TICK:  " + Round(tick, 2), Color.RED);
AddLabel(oversold, "$TICK:  " + Round(tick, 2), Color.GREEN);
AddLabel(!overbought and !oversold, "$TICK:  " + Round(tick, 2), Color.LIGHT_GRAY);

# TRAN
input TRAN_Trend_Length = 1;
def TRAN = close("TRAN" );
def TRANtrend = Average(TRAN[1], 1);
AddLabel(TRAN > TRANtrend, "TRAN:  " + Round(TRAN, 2), Color.GREEN);
AddLabel(TRAN <= TRANtrend, "TRAN:  " + Round(TRAN, 2), Color.RED);

# XLF
input XLF_Trend_Length = 1;
def XLF = close("XLF" );
def XLFtrend = Average(XLF[1], 1);
AddLabel(XLF > XLFtrend, "XLF:  " + Round(XLF, 2), Color.GREEN);
AddLabel(XLF <= XLFtrend, "XLF:  " + Round(XLF, 2), Color.RED);
Re: Fun with ThinkScript
March 25, 2016 10:09AM
Kamadi,

Here are the script and the scans:


input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;

def Value = ExpAverage(close, fastLength) - ExpAverage(close, slowLength);
def Avg = ExpAverage(Value, MACDLength);

plot ValueBelowZero = if Value crosses below 0 then high else Double.NaN;
ValueBelowZero.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
ValueBelowZero.AssignValueColor(Color.PINK);
ValueBelowZero.SetLineWeight(3);

plot ValueAboveZero = if Value crosses above 0 then low else Double.NaN;
ValueAboveZero.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
ValueAboveZero.AssignValueColor(Color.GREEN);
ValueAboveZero.SetLineWeight(3);

plot ValueAboveAvg = if Value crosses above Avg and Value < 0 then low - low*0.003 else Double.NaN;
ValueAboveAvg.SetPaintingStrategy(PaintingStrategy.POINTS);
ValueAboveAvg.AssignValueColor(Color.WHITE);
ValueAboveAvg.SetLineWeight(3);

plot ValueBelowAvg = if Value crosses below Avg and Value > 0 then high + high*0.003 else Double.NaN;
ValueBelowAvg.SetPaintingStrategy(PaintingStrategy.POINTS);
ValueBelowAvg.AssignValueColor(Color.RED);
ValueBelowAvg.SetLineWeight(3);



Scan #1

input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;

def Value = ExpAverage(close, fastLength) - ExpAverage(close, slowLength);
def Avg = ExpAverage(Value, MACDLength);

plot ValueAboveAvg = Value crosses above Avg and Value < 0;



Scan #2

input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;

def Value = ExpAverage(close, fastLength) - ExpAverage(close, slowLength);
def Avg = ExpAverage(Value, MACDLength);

plot ValueBelowAvg = Value crosses below Avg and Value > 0;
Re: Fun with ThinkScript
March 25, 2016 11:28AM
Rob Miller,

Here is TRIN as lower study. I have inverted it to make it easier to understand, so spike above 2 is overbought (plot will turn red) and spike below 0.5 is oversold (plot will turn green). It will draw horizontal line at 2 in pink and at 0.5 in light green to indicate overbought and oversold levels.

In regular TRIN it is upside down, spike above 2 is oversold and spike below 0.5 is overbought.



declare lower;

def UVOL = close(“$UVOL”);
def DVOL = close(“$DVOL”);
def ADV = close(“$ADVN”);
def DECL = close(“$DECN”);

plot TRIN = (DECL/ADV)/(DVOL/UVOL);
TRIN.SetStyle(Curve.FIRM);
TRIN.AssignValueColor(if TRIN >= 2 then Color.RED else if TRIN <= 0.5 then Color.GREEN else Color.GRAY);

plot ZeroLine = 1;
ZeroLine.AssignValueColor(Color.LIGHT_GRAY);

plot UB = 2;
UB.AssignValueColor(Color.PINK);

plot LB = 0.5;
LB.AssignValueColor(Color.LIGHT_GREEN);
Re: Fun with ThinkScript
March 25, 2016 12:39PM
Has anybody had a chance to take a look at this?

thanks


fvaldes Wrote:
-------------------------------------------------------
> hi Robert, thank you for all the amazing help you
> give in this forum.
>
> I wanted to ask you if you could code a divergence
> study/scan that identifies regular and hidden
> divergences in the MACD Histogram per the
> definition of Alexander Elder.
>
> He basically claims that a divergence only occurs
> if there is a cross of the zero line between the
> indicator pivots.
>
> Here is an image showing what I mean:
>
> [dl.dropboxusercontent.com]
> iv.jpg
>
> Here is a definiton of hidden and regular
> divergences.
>
> [dl.dropboxusercontent.com]
> g
>
> thanks
Re: Fun with ThinkScript
March 25, 2016 09:58PM
i need your help guys

i want this script to run in custom scan with alert

close > reference BollingerBands(length = 32, Num_Dev_Dn = -2.4, Num_Dev_up = 2.4).UpperBand

or this

if close > BollingerBandsSMA(length = 10, Num_Dev_Dn = -0.8, Num_Dev_up = 0.8).UpperBand then 1 else 0

thanks
JML
Re: Fun with ThinkScript
March 28, 2016 01:54PM
Tanman or anyone else...

here is a study with a cloud that I use to type in price levels in the settings.

Can you modify this to where I can change the color of the cloud in the settings?

Is it also possible to add the price value on top of the price level at the far left?
Thanks
JML

input M1 = 2024.00;
input M11 = 2025.00;
input M2 = 2101.75;
input M22 = 2090.25;
input M3 = 2081.75;
input M33 = 2075.50;
input M4 = 2070.50;
input M44 = 2067.00;
input M5 = 2064.25;
input M55 = 2062.25;
input M6 = 2059.50;
input M66 = 2056.00;

AddCloud(M1, M11,color.GRAY, color.GRAY);
AddCloud(M2, M22, color.GRAY, color.GRAY);
AddCloud(M3, M33, color.GRAY, color.GRAY);
AddCloud(M4, M44, color.GRAY, color.GRAY);
AddCloud(M5, M55, color.GRAY, color.GRAY);
AddCloud(M6, M66, color.GRAY, color.GRAY);
Re: Fun with ThinkScript
March 29, 2016 02:49PM
JML Wrote:
-------------------------------------------------------
> Tanman or anyone else...
>
> here is a study with a cloud that I use to type in
> price levels in the settings.
>
> Can you modify this to where I can change the
> color of the cloud in the settings?
>
> Is it also possible to add the price value on top
> of the price level at the far left?
> Thanks
> JML
>
> input M1 = 2024.00;
> input M11 = 2025.00;
> input M2 = 2101.75;
> input M22 = 2090.25;
> input M3 = 2081.75;
> input M33 = 2075.50;
> input M4 = 2070.50;
> input M44 = 2067.00;
> input M5 = 2064.25;
> input M55 = 2062.25;
> input M6 = 2059.50;
> input M66 = 2056.00;
>
> AddCloud(M1, M11,color.GRAY, color.GRAY);
> AddCloud(M2, M22, color.GRAY, color.GRAY);
> AddCloud(M3, M33, color.GRAY, color.GRAY);
> AddCloud(M4, M44, color.GRAY, color.GRAY);
> AddCloud(M5, M55, color.GRAY, color.GRAY);
> AddCloud(M6, M66, color.GRAY, color.GRAY);


JML - add this to the code.....change colors in quotations as needed.
Duplicate "Color1" to 3, 4, etc, if you need more colors....


input M1 = 2024.00;
input M11 = 2025.00;
input M2 = 2101.75;
input M22 = 2090.25;
input M3 = 2081.75;
input M33 = 2075.50;
input M4 = 2070.50;
input M44 = 2067.00;
input M5 = 2064.25;
input M55 = 2062.25;
input M6 = 2059.50;
input M66 = 2056.00;

input Color1 = {default "green", "red", "yellow"};
input Color2 = {"green",default "red", "yellow"};

defineGlobalColor("green",color.green);
defineglobalColor("red",color.red);
defineglobalColor("yellow",color.yellow);

AddCloud(M1, M11,GlobalColor(Color1),GlobalColor(Color2));
AddCloud(M2, M22, GlobalColor(Color1),GlobalColor(Color2));
AddCloud(M3, M33, GlobalColor(Color1),GlobalColor(Color2));
AddCloud(M4, M44, GlobalColor(Color1),GlobalColor(Color2));
AddCloud(M5, M55, GlobalColor(Color1),GlobalColor(Color2));
AddCloud(M6, M66,GlobalColor(Color1),GlobalColor(Color2));
JML
Re: Fun with ThinkScript
March 29, 2016 04:59PM
devildriver6 Wrote:
-------------------------------------------------------
> JML Wrote:
> --------------------------------------------------
> -----
> > Tanman or anyone else...
> >
> > here is a study with a cloud that I use to type
> in
> > price levels in the settings.
> >
> > Can you modify this to where I can change the
> > color of the cloud in the settings?
> >
> > Is it also possible to add the price value on
> top
> > of the price level at the far left?
> > Thanks
> > JML
> >
> > input M1 = 2024.00;
> > input M11 = 2025.00;
> > input M2 = 2101.75;
> > input M22 = 2090.25;
> > input M3 = 2081.75;
> > input M33 = 2075.50;
> > input M4 = 2070.50;
> > input M44 = 2067.00;
> > input M5 = 2064.25;
> > input M55 = 2062.25;
> > input M6 = 2059.50;
> > input M66 = 2056.00;
> >
> > AddCloud(M1, M11,color.GRAY, color.GRAY);
> > AddCloud(M2, M22, color.GRAY, color.GRAY);
> > AddCloud(M3, M33, color.GRAY, color.GRAY);
> > AddCloud(M4, M44, color.GRAY, color.GRAY);
> > AddCloud(M5, M55, color.GRAY, color.GRAY);
> > AddCloud(M6, M66, color.GRAY, color.GRAY);
>
>
> JML - add this to the code.....change colors in
> quotations as needed.
> Duplicate "Color1" to 3, 4, etc, if you need more
> colors....
>
>
> input M1 = 2024.00;
> input M11 = 2025.00;
> input M2 = 2101.75;
> input M22 = 2090.25;
> input M3 = 2081.75;
> input M33 = 2075.50;
> input M4 = 2070.50;
> input M44 = 2067.00;
> input M5 = 2064.25;
> input M55 = 2062.25;
> input M6 = 2059.50;
> input M66 = 2056.00;
>
> input Color1 = {default "green", "red",
> "yellow"};
> input Color2 = {"green",default "red", "yellow"};
>
> defineGlobalColor("green",color.green);
> defineglobalColor("red",color.red);
> defineglobalColor("yellow",color.yellow);
>
> AddCloud(M1,
> M11,GlobalColor(Color1),GlobalColor(Color2));
> AddCloud(M2, M22,
> GlobalColor(Color1),GlobalColor(Color2));
> AddCloud(M3, M33,
> GlobalColor(Color1),GlobalColor(Color2));
> AddCloud(M4, M44,
> GlobalColor(Color1),GlobalColor(Color2));
> AddCloud(M5, M55,
> GlobalColor(Color1),GlobalColor(Color2));
> AddCloud(M6,
> M66,GlobalColor(Color1),GlobalColor(Color2));

thanks
Re: Fun with ThinkScript
March 29, 2016 11:34PM
fvaldes,

The MACD Histogram divergence as per Alexander Elder's definition was tricky to code (also your reference links were not working so I had to search and read his book for the definition) and it took me many days but I finally solved it! To my knowledge, no one has coded the MACD Histogram divergence per Elder's definition in thinkscript before. The following script will plot large red arrows at potential regular bearish divergence, large green arrows at potential regular bullish divergence, small pink arrows at potential hidden bearish divergence and small lime arrows at potential hidden bullish divergence. The code will also alert you if arrow is plotted.

The reason I am using the word "potential" is because the code compares the high or low with the previous swing high or swing low to determine divergence (instead of comparing two swing highs or two swing lows), in order to get a fast signal before divergence is complete. Once you get the arrow(s) then wait for the MACD Histogram bar to become smaller for entry signal. If after the arrows are plotted, the histogram bars keep getting bigger to the point there is no divergence, then it is a no go.

I could have coded for divergence between two swing highs or two swing lows once the swing points have been determined, but it takes few smaller bars after the highest or lowest bar to determine swing high or swing low point, so plotting of the arrow will be delayed by few bars and by then price might already have moved a lot and signal might not be as useful. My code does not lag and arrow is plotted immediately if there is a potential divergence. You can change the number of bars used to determine the previous swing high or swing low in "edit properties" (default is 2).

This code is an upper study only and will not plot the MACD Histogram. Use the standard MACD Histogram in ToS as the lower study. In the Auto versus Manual pane in top right corner, make sure to uncheck all boxes except "fit study markers". Switching extended hours on or off will make a difference in divergences.




input bar = 2;

input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input averageType = AverageType.EXPONENTIAL;

plot Diff = MACD(fastLength, slowLength, MACDLength, averageType).Diff;

def SwingHigh = Diff > 0 and Diff >= highest(Diff[1], bar) and Diff >= highest(Diff[-bar], bar);
def SHprice = if SwingHigh then Diff else SHprice[1];
def SHBar = if SwingHigh then BarNumber() else SHBar[1];
def CrossBarL = if Diff crosses below 0 then BarNumber() else CrossBarL[1];

def SwingLow = Diff < 0 and Diff <= lowest(Diff[1], bar) and Diff <= lowest(Diff[-bar], bar);
def SLprice = if SwingLow then Diff else SLprice[1];
def SLBar = if SwingLow then BarNumber() else SLBar[1];
def CrossBarH = if Diff crosses above 0 then BarNumber() else CrossBarH[1];

def SHSP = if SwingHigh then high else SHSP[1];
def SLSP = if SwingLow then low else SLSP[1];

def BearDiv = Diff > 0 and CrossBarL[1] > SHBar[1] and Diff < SHprice[1] and high > SHSP[1] and SHprice[1] - Diff > 0.005;
def BullDiv = Diff < 0 and CrossBarH[1] > SLBar[1] and Diff > SLprice[1] and low < SLSP[1] and Diff - SLprice[1] > 0.005;
def HiddenBearDiv = Diff > 0 and Diff > SHprice[1] and high < SHSP[1] and Diff - SHprice[1] > 0.005;
def HiddenBullDiv = Diff < 0 and Diff < SLprice[1] and low > SLSP[1] and SLprice[1] - Diff > 0.005;

plot BearD = if BearDiv then high else Double.NaN;
BearD.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
BearD.AssignValueColor(Color.RED);
BearD.SetLineWeight(3);

plot BullD = if BullDiv then low else Double.NaN;
BullD.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
BullD.AssignValueColor(Color.UPTICK);
BullD.SetLineWeight(3);

plot HiddenBearD = if HiddenBearDiv then high else Double.NaN;
HiddenBearD.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
HiddenBearD.AssignValueColor(Color.PINK);
HiddenBearD.SetLineWeight(1);

plot HiddenBullD = if HiddenBullDiv then low else Double.NaN;
HiddenBullD.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
HiddenBullD.AssignValueColor(Color.LIME);
HiddenBullD.SetLineWeight(1);

Alert(BearDiv[1], "Short MACD divergence", Alert.BAR, Sound.Ring);
Alert(BullDiv[1], "Long MACD divergence", Alert.BAR, Sound.Ring);
Alert(HiddenBearDiv[1], "Short hidden MACD divergence", Alert.BAR, Sound.Ring);
Alert(HiddenBullDiv[1], "Long hidden MACD divergence", Alert.BAR, Sound.Ring);



Edited 1 time(s). Last edit at 03/29/2016 11:37PM by tanman.
Re: Fun with ThinkScript
March 31, 2016 10:51AM
thanks for the TRIN indicator tanman. Just what I was looking for.

I have another request. Im looking for a pre market volume chart label for making decisions on gap plays. Anyone have this?

Also a study to mark pivot/swing highs and lows so I can quickly identify when trend has changed direction. thanks in advance.
Re: Fun with ThinkScript
March 31, 2016 02:45PM
I have been trying figure out how to change the background color to yellow when I get an alert on the Stock brake out trade. I have been fiddling with some thinkscript to get the same effect but have not been successful. The first extract of from a study that works.

alert(state != state[1], "ATR Trailing Stop Alert", alert.BAR, sound.ring);
assignBackgroundColor(if state != state[1] or state != state[2] then color.yellow else color.current);

This is the Thinkscript that I want to background to turn yellow on alert just for the current bar.

def alertup = close[2] <= highConf and close[1] > highConf and VolUp[1]; 
def alertdn = close[2] >= lowConf and close[1] < lowConf and VolUp[1]; 
alert(alertup, getsymbol() + " UP", alert.bar, sound.ring;
alert(alertdn, getsymbol() + " DOWN", alert.bar, sound.ring);
Re: Fun with ThinkScript
March 31, 2016 04:30PM
Rob Miller,

You are welcome.

I don't know if pre market volume data is available for script purposes.

Here is the script for swing high and swing low. Swing high is marked by red arrow and swing low by green arrow. You can change it to dots by changing boolean_arrow_down to points. You can change the size by changing setlineweight to 2,3,4, or 5. The default setting to determine swing high/low is 3 candles but you can change that in edit properties.


input magnitude = 3;

# define and plot the most recent peak
def peak = high >= Highest(high[1], magnitude) and high >= Highest(high[-magnitude], magnitude);
def peakvalue = if BarNumber() < magnitude then Double.NaN else if peak then high else Double.NaN;
plot peakline = peakvalue;
peakline.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
peakline.SetDefaultColor(Color.DOWNTICK);
peakline.SetLineWeight(1);


# define and plot the most recent valley
def valley = low <= Lowest(low[1], magnitude) and low <= Lowest(low[-magnitude], magnitude);
def valleyvalue = if BarNumber() < magnitude then Double.NaN else if valley then low else Double.NaN;
plot valleyline = valleyvalue;
valleyline.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
valleyline.SetDefaultColor(Color.UPTICK);
valleyline.SetLineWeight(1);
Re: Fun with ThinkScript
March 31, 2016 04:37PM
Texas John,

AssignBackgroundColor(if alertup then Color.YELLOW else if alertdn then Color.YELLOW else Color.CURRENT);
Re: Fun with ThinkScript
March 31, 2016 06:57PM
Tanman, thanks again. I appreciate your help.
Sorry, only registered users may post in this forum.

Click here to login