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
February 20, 2016 03:55AM
awesome, thanks. will report back w/ completed code.
Re: Fun with ThinkScript
February 20, 2016 05:35AM
okay, i found the actual code for dmi/adx, which i have appended below, along with the alerts from baffled1, minus everything associated with adxr, because i haven't figure out how to add that to the mix yet (hint, hint).

i also attempted to add arrows when all conditions are met but so far I'm only getting arrows for DMI+ conditions, albeit the arrows are facing the wrong direction and changing down to up doesn't seem to change anything except for shifting the down arrows to the bottom of the screen.

i am going to visit my daughter today, else i would spend that time trying to figure this out myself. i'll get back to it but meanwhile, if anyone here wants to help out, well, ya know, have at it and thanks!

man, do i suck at this code stuff. brain is not as supple as it used to be and even then ...

# ADX and DIplus and DIminus

declare lower;

input length = 14;

def hidiff = high - high[1];
def lodiff = low[1] - low;

def dmip = if (hidiff < 0 and lodiff < 0) or lodiff >= hidiff then 0 else hidiff;

rec wdmip = CompoundValue("historical data" = Average(dmip, length), "visible data" = (dmip + wdmip[1] * (length - 1)) / length);
rec volty = CompoundValue("historical data" = Average(TrueRange(high, close, low), length), "visible data" = (TrueRange(high, close, low) + volty[1] * (length - 1)) / length);

plot "DI+" = 100 * wdmip / volty;
"DI+".SetDefaultColor(GetColor(1));

#declare lower;

def dmim = if (hidiff < 0 and lodiff < 0) or hidiff >= lodiff then 0 else lodiff;

rec wdmim = CompoundValue("historical data" = Average(dmim, length), "visible data" = (dmim + wdmim[1] * (length - 1)) / length);
rec volty2 = CompoundValue("historical data" = Average(TrueRange(high, close, low), length), "visible data" = (TrueRange(high, close, low) + volty2[1] * (length - 1)) / length);

plot "DI-" = 100 * wdmim / volty;
"DI-".SetDefaultColor(GetColor(8));

#declare lower;

plot ADX = DMI(length).ADX;
ADX.SetDefaultColor(GetColor(5));

def myLONG = "DI+" >20.0 and "DI-" <20.0 and ADX< "DI+" and ADX> "DI-";

def mySHORT = "DI-" >20.0 and "DI+" < 20.0 and ADX > "DI-" and ADX < "DI+" ;

alert(myLONG, "GO LONG", alert.BAR, sound.Ring);
alert(mySHORT, "GO SHORT", alert.BAR, sound.Ring);

def bSignalUp = mylong;
def bSignalDown = myshort;

plot up = if bSignalUp then high else double.NaN;
plot down = if bSignalDown then high else double.NaN;

up.SetPaintingStrategy(paintingStrategy.BOOLEAN_ARROW_up);
down.SetPaintingStrategy(paintingStrategy.BOOLEAN_ARROW_down);

up.setDefaultColor(color.MAGENTA);
down.setDefaultColor(color.CYAN);


xxxxxxxxxxxxxxxxxxxxxxxxxxxxx

p.s., i just found this from jamie pinto at the yahoo groups forum. it may be the formula for adxr, two variations thereof. will try to incorporate one of them into the above, unless someone gets to it first, ha ha.

To use the raw slope:
plot ADXslope = ADX - ADX[-1];
#plot smoothedADXslope = wildersAverage(ADXslope,5);

To use the smoothed slope:
def ADXslope = ADX - ADX[-1];
plot smoothedADXslope = wildersAverage(ADXslope,5);



Edited 1 time(s). Last edit at 02/20/2016 05:39AM by linter.
Re: Fun with ThinkScript
February 20, 2016 11:58AM
#ADXR = (ADX + ADX N periods ago) / 2

#try adding
input n = 10 ;
plot ADXR = ((ADX + ADX[n]) / 2);

I believe that the arrows will compress the chart



Edited 1 time(s). Last edit at 02/20/2016 12:37PM by Game0ver.
Re: Fun with ThinkScript
February 20, 2016 02:13PM
Linter,

Here is the full script including ADXR, but your chosen parameters give too many signals and lot of false signals. ADX is a lagging indicator and ADXR is even more lagging, so I don't think you will get useful signals based on your parameters. To plot the indicators select DMI and ADXR from the built in studies.


input length = 14;

def Diffhi = high - high[1];
def Difflo = low[1] - low;

def pDM = if Diffhi > Difflo and Diffhi > 0 then Diffhi else 0;
def mDM = if Difflo > Diffhi and Difflo > 0 then Difflo else 0;

def ATR = WildersAverage(TrueRange(high, close, low), length);
def DIp = 100 * WildersAverage(pDM, length) / ATR;
def DIm = 100 * WildersAverage(mDM, length) / ATR;

def DX = if (DIp + DIm > 0) then 100 * AbsValue(DIp - DIm) / (DIp + DIm) else 0;
def ADX = WildersAverage(DX, length);
plot ADXR = (ADX + ADX[length - 1]) / 2;

def BuySignal = DIp > 20 and DIm < 20 and ADX < DIp and ADX > DIm and ADXR > 20;
def SellSignal = DIp < 20 and DIm > 20 and ADX > DIp and ADX < DIm and ADXR > 20;

plot BS = if BuySignal then low else Double.NaN;
BS.SetPaintingStrategy(paintingStrategy.BOOLEAN_ARROW_UP);
BS.AssignValueColor(Color.GREEN);
BS.SetLineWeight(3);
plot SS = if SellSignal then high else Double.NaN;
SS.SetPaintingStrategy(paintingStrategy.BOOLEAN_ARROW_DOWN);
SS.AssignValueColor(Color.RED);
SS.SetLineWeight(3);

Alert(BuySignal, "GO LONG", alert.ONCE, sound.RING);
Alert(SellSignal, "GO SHORT", alert.ONCE, sound.RING);
NetVolume Indicator Inquiry,,,
February 21, 2016 02:49AM
Tanman,

First of all, I am returning to the "Fun with Thinkscript" thread after a somewhat protracted hiatus and I am very happy to see it thriving. When the OP, Robert, informed the community that he would be leaving for an indeterminate hiatus himself, it seemed, at the time, that the thread might be coming to an end. It is very good to know that is not the case...

As I have spent some time reading and catching up, I have come to realize the community has changed and definitely for the better. Whereas, in the past, it seemed that there were numerous participants coming and going, there were only a relatively small group of generous contributors providing potential answers to many requests. I must admit I was amongst the larger group of solution seekers, as I did not feel entirely confident in my scripting abilities at the time...

In any event, this post is not about the past, it is about the future. While I certainly hope to see Robert return somewhere along the way, I hope to rejoin this community and maybe even offer an appropriate contribution, if the opportunity to do so arises...

As I continue to read, I have come to realize and greatly appreciate the considerable quality of the information being provided by you and others. In my opinion, this is what makes this particular place special. I know there are other potential destinations out there, ThinkScripter comes to mind, but this thread not only survives, it thrives without the pressure of commercial underpinnings...

OK, since I have the tendency to get long-winded, I will get to the point. As a result of my reading, I came across your post regarding the potential perils of trying to properly read Volume and the very interesting solution you provided in response to the issue.

I have been a student and apprentice in the Markets for over ten years now. There are certain fundamental concepts I have come to understand. One is the critical importance of Price and Volume, the original leading "indicators". While I have not yet gotten around to Tape Reading, I have focused a fairly significant amount of my time and energy on Price and Volume. I will admit I went through the "Holy Grail" phase and still experiment with the many derivative indicators out there, but all roads seem to lead back to Price and Volume...

With that said, I am intrigued by your invention, the Net Volume Indicator. I have been working with it and have a couple of questions I'm hoping you might have a chance to answer. Consider the sample image below:



Given the four examples in the image above:

1. Without knowing Price was in the middle of a downtrend, with a large body, small wicks, closing near the low, above average Volume and Net Volume significantly above average, I believe the probabilities say the downtrend will continue.

> Does the size of the Net Volume bar confirm the continuation of the downtrend?
> Does the significant size of the Net Volume bar alone confirm the presence of many more Sellers than Buyers?
> Is there any correlation between the relative sizes of the Volume and Net Volume bars?

2. With the continuation of the downtrend, Price forms a Hammer candle, with substantially above average Volume and surprisingly average Net Volume. With the information presented, I would prefer to believe the probability that the downtrend has ended, but the lack of Net Volume confirmation renders me indecisive.

> Does the size of the Net Volume bar suggest weakness and the potential continuation of the downtrend?

3. With an anemic uptrend underway, Price surges (Fed influence), with a large body, small wicks, closing near the high, slightly above average Volume and significantly above average Net Volume. With the information displayed and the reaction of the Markets to the Fed Announcement, I would be wary of the sudden run-up in Price in the middle of a lackluster uptrend...possible "hidden" selling. The probabilities say the uptrend may be coming to an end.

> Does the size of the Net Volume bar suggest the potential presence of "hidden" selling and an upcoming reversal?

4. With the continuation of the current downtrend, Price forms what could be construed as a "bottoming"candle, although not quite a Hammer, with significantly above average Volume and average Net Volume. Once again the apparent lack of more Net Volume leaves me indecisive about the direction of Price. With that said, the probabilities say Price is more likely to reverse, due to the substantially above average Volume and Buyers coming in to buy the low.

> Does the smaller size of this Net Volume bar somehow suggest a potential upcoming reversal?
> When compared to Example #2, is it the buying off the low that fuels the reversal and resulting stronger uptrend in this case or is it more likely the strong Volume?
> Does Net Volume play any role in this or are there times when it will prove to be inconclusive?

I look forward to your reply if you have a chance. Thank you smiling smiley



Edited 1 time(s). Last edit at 02/21/2016 11:05AM by netarchitech.
Re: NetVolume Indicator Inquiry,,,
February 21, 2016 04:45AM
tanman, thanks mucho for that. works just right. and yes it throws off a lot of signals, a bunch of them fakeouts of one sort or another, but the indicator is part of a larger system that is ***supposed*** to cut down on the failures. see: [www.forexfactory.com]. i have my doubts, if only because skepticism of a system at that site leads to almost instant banning. but, i'm looking to see if i can find anything that makes sense to me.
i asked for the DMI alerts, so i can check things out in real time without having to stare at the chart all day. again, thanks.

btw / is it possible to make the audible alerts go off only when the first signal of any signal sequence fires?



Edited 1 time(s). Last edit at 02/21/2016 05:06AM by linter.
Re: Fun with ThinkScript
February 21, 2016 11:32PM
Netarchitech,

Thank you for the kind words and thought provoking post. Since it is a bit off topic, I took the liberty of replying in the following thread so it can benefit new traders also:

[www.researchtrade.com]

Look forward to your input



Edited 1 time(s). Last edit at 02/21/2016 11:50PM by tanman.
Re: Fun with ThinkScript
February 21, 2016 11:37PM
Linter,

You are welcome.

I don't think it is possible to make audible alerts go off only at the first signal, given your signal parameters. It will alert on every bar that is within your given parameters, unless you change your parameters to something that occurs only once, such as cross above 20 etc.
Re: Fun with ThinkScript
February 22, 2016 03:11AM
tanman: got it, thanks.
Re: Fun with ThinkScript
February 22, 2016 01:53PM
Watch this video about :

TTS CCI Ricoche Indicator

[www.youtube.com]


Re: Fun with ThinkScript
February 22, 2016 05:30PM
You may be looking for Pro_CCIDivergence_v1STUDY.zip ................from
[sites.google.com]
Re: Fun with ThinkScript
February 22, 2016 09:58PM
SARA, thanks. Watched it. Pricey, but looks like it works well on chart shown.
GameOver, thanks but files are password-protected.
EDIT~~ GameOver, direct download was PW protected, but went to link next to it (is that what you meant?). Might be the desired code, not sure yet. Link is this blog.

Gamblers roll the dice; Traders load the dice.



Edited 1 time(s). Last edit at 02/22/2016 10:06PM by baffled1.
Re: Fun with ThinkScript
February 22, 2016 10:43PM
The top studies are free for use, his pro studies, down close to the bottom you have to pay for.
Re: Fun with ThinkScript
February 23, 2016 12:02PM
bughatti Wrote:
-------------------------------------------------------
> The top studies are free for use, his pro studies,
> down close to the bottom you have to pay for.
==========================================
bughatti, thanks. That helps clarify.

Gamblers roll the dice; Traders load the dice.
Re: Fun with ThinkScript
February 25, 2016 07:02PM
Can Anyone help me to turn this script into a scannable script. I am only interested in turning the retrace dots portion into a scannable script

# BEGIN

input Hide_Lines = no;
input Hide_Dots = no;
input Dot_Size = 2;
input price = close;
input fastLength = 8;
input slowLength = 21;
input displace = 0;
plot fastAvg;
plot slowAvg;

################## Hide Lines ##################

if Hide_Lines
then {
    fastAvg = Double.NaN;
    slowAvg = Double.NaN;
} else {
    fastAvg = expAverage(price[-displace], fastLength);
    slowAvg = expAverage(price[-displace], slowLength);
}
################## Line Appearance ###############

fastAvg.AssignValueColor(Color.PINK );
fastAvg.SetStyle(Curve.SHORT_DASH);
slowAvg.AssignValueColor(Color.PINK );
slowAvg.SetStyle(Curve.POINTS);

#################### Signal Arrows ################

def fast = expAverage(price[-displace], fastLength);
def slow = expAverage(price[-displace], slowLength);
plot signaldown = Crosses(( slow > fast) != 0, 0.5, yes);
plot signalUp = Crosses(( slow < fast) != 0, 0.5, yes);
signaldown.SetPaintingStrategy (PaintingStrategy.BOOLEAN_ARROW_DOWN);
signaldown.AssignValueColor( Color.RED);
signalUp.SetPaintingStrategy (PaintingStrategy.BOOLEAN_ARROW_UP);
signalUp.AssignValueColor( Color.GREEN);

##################### Retrace Dots #########################

def MA8 = if Crosses(( high >= expAverage(price[-displace], fastLength)) != 0, 0.5, yes) or Crosses(( low <= expAverage(price[-displace], slowLength)) != 0, 0.5, yes) then expAverage(price[-displace], fastLength) else Double.NaN;

plot Ma8Dots = if Hide_Dots then Double.NaN else MA8;
Ma8Dots.SetDefaultColor(Color.WHITE);
Ma8Dots.SetStyle(Curve.POINTS);
Ma8Dots.SetLineWeight(Dot_Size);

# END
Re: Fun with ThinkScript
February 25, 2016 10:18PM
Can anyone come up with a code to use the hourly macD settings ( 6, 18, 3) on a daily time frame.
What is the best way to set up an ATR scan in TOS?
August 28, 2014 07:22AM
I need some guidance on the specifics of how to do this. I tried doing it but never could get any results. Thanks to all who help others on this board! smiling smiley
Re: What is the best way to set up an ATR scan in TOS?
August 28, 2014 08:01AM
Quote
JxnMStrader
I need some guidance on the specifics of how to do this. I tried doing it but never could get any results. Thanks to all who help others on this board!

Just stating that you'd like to scan for ATR is pretty general. It would help greatly if you would be more specific. Do you want to scan for all stocks with a 20 day ATR greater than 3? Or maybe, stocks that traded in a range that was greater than the 10 day ATR?

Please explain exactly what you are trying to scan for so that someone may better assist you.
Re: What is the best way to set up an ATR scan in TOS?
February 27, 2016 06:30PM
I would be curious to see if there is a way to create a custom scan and a watchlist in TOS that could show when a stock has traded at least 1/2 its ATR in a single 5 min candle?
Re: Fun with ThinkScript
February 28, 2016 04:10AM
I tried to do CCI divergence indicator as upper signals with alert by modifying another script for RSI divergence, but I'm sure there is a mess to be corrected :



declare lower;

input nCCI = 14;
input over_sold = -100;
input over_bought = 100;
input TrendLine = {EMA, SMA, default LRL, WMA, Hull}; 
input AlertOn = yes; 
def price = close + low + high /4;
def linDev = LinDev(price, nCCI);
plot CCI = if linDev == 0 then 0 else (price - Average(price, nCCI)) / linDev / 0.015;
plot OverBought = over_bought;
plot Midline = 0;
plot OverSold = over_sold;



def o = open;
def h = high;
def l = low;
def c = close;
def NetChgAvg = WildersAverage(c - c[1], nCCI);
def TotChgAvg = WildersAverage(AbsValue(c - c[1]), nCCI);
def ChgRatio = if TotChgAvg != 0 
then NetChgAvg / TotChgAvg 
else 0;

def lowestLow = if CCI > over_sold 
then 5 
else if CCI < over_sold and 
5 < lowestLow[1] 
then 5 
else lowestLow[1];
def lowestCCI = if CCI > MidLine 
then CCI 
else if CCI < MidLine and 
CCI < lowestCCI[1] 
then CCI 
else lowestCCI[1];
def divergentLow = if CCI < over_sold and 
5 <= lowestLow[1] and 
CCI > lowestCCI[1] 
then over_sold 
else Double.NaN;
plot DLow = divergentLow;
DLow.SetPaintingStrategy(PaintingStrategy.POINTS);
DLow.SetLineWeight(2);
DLow.SetDefaultColor(Color.YELLOW);

def highestHigh = if CCI < over_bought 
then h 
else if CCI > over_bought and 
h > highestHigh[1] 
then h 
else highestHigh[1];
def highestCCI = if CCI < MidLine 
then CCI 
else if CCI > MidLine and 
CCI > highestCCI[1] 
then CCI 
else highestCCI[1];
def divergentHigh = if CCI > over_bought and 
h >= highestHigh and 
CCI < highestCCI
then over_bought 
else if CCI < over_bought and 
c < o 
then Double.NaN 
else divergentHigh[1];

plot DHigh = divergentHigh ;
DHigh.SetPaintingStrategy(PaintingStrategy.POINTS);
DHigh.SetLineWeight(2);
DHigh.SetDefaultColor(Color.YELLOW);

CCI.DefineColor("OverBought", GetColor(5));
CCI.DefineColor("Normal", GetColor(7));
CCI.DefineColor("OverSold", GetColor(1)); 
OverSold.SetDefaultColor(Color.BLUE);
OverBought.SetDefaultColor(Color.BLUE);

# Alert(AlertCond1, "RSI crossed RSI Trend Line", Alert.Bar, Sound.Bell); 


Re: Fun with ThinkScript
February 28, 2016 03:15PM
SARA Wrote:
-------------------------------------------------------
> I tried to do CCI divergence indicator as upper
> signals with alert by modifying another script for
> RSI divergence, but I'm sure there is a mess to be
> corrected :
SARA, exactly what do you want this to Alert on? The DLow and DHigh or the crosses? Once we know that, then we can define what "AlertCond1" is. The code does run okay. You show it as "declare lower;" and it runs as lower study. I took that out to run as an upper study and had to Run on Left Axis but it displays fine that way too.

Gamblers roll the dice; Traders load the dice.
Re: Fun with ThinkScript
February 28, 2016 08:49PM
Hi Sara,

You wanted the daily signals on the hourly chart try and see if this works for you:

input Aggregate = { Hourly, default Daily, Weekly, Monthly} ;

def Agg = if Aggregate == Aggregate.Hourly then AggregationPeriod.Hour else
if Aggregate == Aggregate.Daily then AggregationPeriod.Day else
if Aggregate == Aggregate.Weekly then AggregationPeriod.WEEK else
if Aggregate == Aggregate.Monthly then AggregationPeriod.MONTH else AggregationPeriod.MIN;

input length = 5;
input capSpikesAt = 10;

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

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

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

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

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

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

plot dnArrow = if bearDiv then high(period=agg) * 1.003 else Double.NaN;
dnArrow.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
dnArrow.SetLineWeight(4);
dnArrow.SetDefaultColor(Color.RED);
Current Price Over/Below Yesterdays
February 29, 2016 11:54AM
I am new to think scripting I trying to put together a scan that will let me know if the current price is above or below yesterdays high or low. I can do everything else on the scan tab I need to do
Re: Fun with ThinkScript
March 01, 2016 02:40PM
baffled1 Wrote:
-------------------------------------------------------
> SARA Wrote:
> --------------------------------------------------
> -----
> > I tried to do CCI divergence indicator as upper
> > signals with alert by modifying another script
> for
> > RSI divergence, but I'm sure there is a mess to
> be
> > corrected :
> SARA, exactly what do you want this to Alert on?
> The DLow and DHigh or the crosses? Once we know
> that, then we can define what "AlertCond1" is. The
> code does run okay. You show it as "declare
> lower;" and it runs as lower study. I took that
> out to run as an upper study and had to Run on
> Left Axis but it displays fine that way too.

baffled1

I want a signal with alert when there is a "DLow" or " DHigh" on the Chat.
Re: Fun with ThinkScript
March 01, 2016 02:46PM
Lukhy911

Thank you a lot ... Yes, it is working perfectly . I appreciate your help .thumbs up



Edited 1 time(s). Last edit at 03/01/2016 02:48PM by SARA.
Re: Fun with ThinkScript
March 03, 2016 03:41PM
I have not bee able to get the alert to work. I'm hoping you can help me with this.

script OpenRange {
    input ORtime = 5;

    def FirstBar = GetDay() != GetDay()[1];
    def RangeTime = SecondsFromTime(0930) >= 0 and SecondsFromTime(0930) < 60 * ORtime;
    def Rhigh = if FirstBar then high else if RangeTime and high > Rhigh[1] then high else Rhigh[1];
    def Rlow = if FirstBar then low else if RangeTime and low < Rlow[1] then low else Rlow[1];

    plot h = if RangeTime then Double.NaN else Rhigh;
    plot l = if RangeTime then Double.NaN else Rlow;
}

def first30 = SecondsFromTime(0930) >= 0 and SecondsTillTime(1000) >= 0;
def today = GetLastDay() == GetDay();
def ATR = Average(TrueRange(high,  close,  low),  10);

plot yHigh = if !today then Double.NaN else high(period = "day" )[1];
yHigh.SetDefaultColor(Color.CYAN);
plot yLow = if !today then Double.NaN else low(period = "day" )[1];
yLow.SetDefaultColor(Color.PINK);

plot h5 = if !today then Double.NaN else if !first30 then Double.NaN else OpenRange(5).h;
h5.SetDefaultColor(Color.YELLOW);
plot l5 = if !today then Double.NaN else if !first30 then Double.NaN else OpenRange(5).l;
l5.SetDefaultColor(Color.YELLOW);
plot h30 = if !today then Double.NaN else OpenRange(30).h;
h30.SetDefaultColor(Color.YELLOW);
plot l30 = if !today then Double.NaN else OpenRange(30).l;
l30.SetDefaultColor(Color.YELLOW);

def lowConf = if first30 then Min(yLow, l5) - ATR else Min(yLow, l30) - ATR;
def highConf = if first30 then Max(yHigh, h5) + ATR else Max(yHigh, h30) + ATR;

plot lc1 = if first30 then lowConf else Double.NaN;
lc1.SetDefaultColor(Color.ORANGE);
plot lc2 = if !first30 then lowConf else Double.NaN;
lc2.SetDefaultColor(Color.ORANGE);
plot hc1 = if first30 then highConf else Double.NaN;
hc1.SetDefaultColor(Color.ORANGE);
plot hc2 = if !first30 then highConf else Double.NaN;
hc2.SetDefaultColor(Color.ORANGE);

def decisionL = if close > lowConf then Double.NaN else if close crosses below lowConf then low else decisionL[1];
def decisionH = if close < highConf then Double.NaN else if close crosses above highConf then high else decisionH[1];

plot dL = if !today then Double.NaN else decisionL;
dL.SetDefaultColor(Color.WHITE);
plot dH = if !today then Double.NaN else decisionH;
dH.SetDefaultColor(Color.WHITE);

def TL = CompoundValue(1, if IsNaN(dL) then Double.NaN else if !IsNaN(TL[1]) then TL[1] else if close crosses below dL then dL - 2 * ATR else Double.NaN, Double.NaN);
def SL = CompoundValue(1, if IsNaN(dL) then Double.NaN else if !IsNaN(SL[1]) then SL[1] else if close crosses below dL then dL + 2 * ATR else Double.NaN, Double.NaN);

plot Target1Low = if !today then Double.NaN else TL;
Target1Low.SetDefaultColor(Color.GREEN);
Target1Low.SetStyle(Curve.SHORT_DASH);
plot Stop1Low = if !today then Double.NaN else SL;
Stop1Low.SetDefaultColor(Color.RED);
Stop1Low.SetLineWeight(2);

AddChartBubble(IsNaN(TL[1]) and !IsNaN(TL), TL, "Target 1\n" + Round(TL, 2), Color.GREEN, no);
AddChartBubble(IsNaN(SL[1]) and !IsNaN(SL), SL, "Stop\n" + Round(SL, 2), Color.RED);

def TH = CompoundValue(1, if IsNaN(dH) then Double.NaN else if !IsNaN(TH[1]) then TH[1] else if close crosses above dH then dH + 2 * ATR else Double.NaN, Double.NaN);
def SH = CompoundValue(1, if IsNaN(dH) then Double.NaN else if !IsNaN(SH[1]) then SH[1] else if close crosses above dH then dH - 2 * ATR else Double.NaN, Double.NaN);

plot Target1High = if !today then Double.NaN else TH;
Target1High.SetDefaultColor(Color.GREEN);
Target1High.SetStyle(Curve.SHORT_DASH);
plot Stop1High = if !today then Double.NaN else SH;
Stop1High.SetDefaultColor(Color.RED);
Stop1High.SetLineWeight(2);

AddChartBubble(IsNaN(TH[1]) and !IsNaN(TH), TH, "Target 1\n" + Round(TH, 2), Color.GREEN);
AddChartBubble(IsNaN(SH[1]) and !IsNaN(SH), SH, "Stop\n" + Round(SH, 2), Color.RED, no);

def alertup = close[1] crosses above hc1 or close[1] crosses above hc2;
def alertdn = close[1] crosses below lc1 or close[1] crosses below lc2;
alert(alertup, getsymbol() + " UP", alert.bar, sound.bell);
alert(alertdn, getsymbol() + " DOWN", alert.bar, sound.bell);
Re: Fun with ThinkScript
March 06, 2016 10:08AM
Texas John,

I am surprised the alert didn't work in that script. Try replacing the def alertup and def alertdn lines (3rd and 4th lines from bottom) with the following:

def alertup = close[2] <= highConf and close[1] > highConf;
def alertdn = close[2] >= lowConf and close[1] < lowConf;

I can see you are using this script for breakout trading. Breakouts are more likely to be real breakouts when they occur on higher volume and more likely to be fake breakouts when they occur on lower volume. If you want to be alerted only when breakout happens on higher volume, then instead of above, replace the 3rd and 4th lines from the bottom of the original script with the following:

def VolUp = volume > average(volume, 6);
def alertup = close[2] <= highConf and close[1] > highConf and VolUp[1];
def alertdn = close[2] >= lowConf and close[1] < lowConf and VolUp[1];

To scan for price above or below yesterday's high or low, use the following:

plot beyond = close > high(period = "day" )[1] or close < low(period = "day" )[1];

To scan only for price above yesterday's high:

plot above = close > high(period = "day" )[1];

To scan only for price below yesterday's low:

plot below = close < low(period = "day" )[1];

I haven't checked these on TOS so if there is a bug let me know.
Re: Fun with ThinkScript
March 06, 2016 10:41PM
Tanman,

Thanks for the reply, I have been trading small until I get a little more comfortable with this system. but so far, its been very successful. It's too early to get some good stats yet after only about five days.

It's my understanding that you have to have the chart up for the alert to work, So my current plan is to have about 15 charts up on one screen. I will have more bullish or bearish symbols up depending on what the market is doing. Do you have any other suggestions along those lines.

Once again, Thanks loads.
Re: Fun with ThinkScript
March 08, 2016 08:51AM
Tanman,

The scan code is working well. The only strange thing is that if I use the scan in a watchlist, every once and a while, It show really strange results that have nothing to do with then scan and then goes back to what I assume the correct result. I test this by doing a the same scan in the scan tab.
Scanning for Hammers with current week low being the lowest of past 9 weeks
March 09, 2016 09:11AM
Hello,

I am new here and stumbled upon this site where a lot of really cool technical stuff is being shared on how to write scripts in TOS. I am currently struggling with the following problem and not very successful with getting the exact result that I am expecting.

The scan that I am trying to create is:
1. Stock/ETF should be trading above the 52 week SMA
2. The closing price at the end of the current week should be the lowest of the closing price of the past 9 weeks
3. The candle stick at the end of the current week should also be a Hammer with the tail being at least 2-3 times the size of the body. The upper shadow (if any) should not exceed more than 20% of the tail.
4. It doesn't matter if the candlestick is green or red as long as the tail (low) for the week happens to be lowest for the past 9 weeks.

My intention is to run the scan at the end of each week (during the weekend) to see which stock/ETF has a hammer candle stick pattern for that week and its closing price is the lowest for the past 9 weeks. I intend to use weekly charts to look at the scan output.

Any suggestions on how to program this into a query is very much appreciated.

Thanks smiling smiley
Sorry, only registered users may post in this forum.

Click here to login