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
May 16, 2014 08:15AM
Robert,

Excellent job on the code you have been able to develop. I know very little about TOS other than I want to throw the computer out the window every time I try to navigate the platform. If I ever take the time to review the tutorials and build a comfort level I will revisit this thread as there is some really good stuff. Typically I can read the code and quickly figure out what you are doing and how. I just get frustrated using the platform.

Quick thought about your S6 obstacle. Do you have the ability to compare day (calendar day)? I learned a long time ago that seasonality and S6 are areas to be aware of and not an exact science. Extremely good or bad news can delay or mute what one would expect to see. Having said that can you do something as follows (again, basic concept as I do not know thinkscript). Not sure if you have to hard code each month or if you can reference a table or array.


First Three If Day < 4 Then… (Month does not matter - highlight all days < 4 for each month)

Last Three IF Month = 1,3,5,7,8,10,12 AND Day > 28 Then… (Month necessary only if trying to dial into exact days)
If Month = 4,6,9,11 AND Day > 27 Then…
IF Month = 2 AND Day > 25 Then …


Since it is not an exact science and only something to be aware of then giving those that want it the ability to increase the EOM assumption (Day 4 as an input) so that they can pad a few days and be prepared would be great. I have to figure this type of stuff out in Excel all the time. Not sure what you have available to work with in TOS. Just a thought...

Happy Trading!

NCT
Re: Fun with ThinkScript
May 16, 2014 03:29PM
Quote
linter
Hi, Robert -- great stuff and i can't believe how adept at it you are. anyway, sorry for a request being my first post on the forum, but what can you do?

I am using part of your very helpful ADX study with the slow stochastics indicator as a set up. I use 11, 11 for the ADX and 8, 3 for sto. The idea is a signal is fired when the ADX rises from beneath 20 to above 20 at roughly the same time as the sto rises from beneath 80 to over 80. I'm wondering if there's not a way to combine the two ideas into one that i could use as a custom column on my watch list and/or get some other kind of alert, so i don't have to keep flipping through screens from stock to stock and wearing out my already frazzled eyeballs to see where things stand.
I've attached a screen shot of the set up. It's the red line to the right.
And, actually, for the ADX, it'd be awesome if I could adjust the crossover number up or down a little. For instance, 18 might be better than twenty, just so I have time to get to the chart and study things before the combo fires.
What do you think? Possible? And, if so, is this anything you might want to take a thwack at?
If you have any questions, fire away!
OTOH, something like this might already be in ToS. If so, forgive my ignorance.

Hey, linter.

I believe this'll do what you ask.



Chart Indicator
input DI_length = 11;
input ADX_length = 11;
input ADX_Crossover = 20;

def DX = if (DIPlus(DI_length) + DIMinus(DI_length) > 0) then 100 * AbsValue(DIPlus(DI_length) - DIMinus(DI_length)) / (DIPlus(DI_length) + DIMinus(DI_length)) else 0;
def ADX = WildersAverage(DX, ADX_length);

input over_bought = 80;
input over_sold = 20;
input KPeriod = 8;
input DPeriod = 3;
input smoothingType = {Default SMA, EMA};

def SlowK = reference StochasticFull(over_bought,over_sold,KPeriod,DPeriod,high,low,close,3,smoothingType).FullK;

def signalup = if ADX crosses above ADX_Crossover and SlowK > over_bought then 1 else if SlowK > over_bought and signalup[1] == 1 then signalup[1] else 0;
def signaldn = if ADX crosses above ADX_Crossover and SlowK < over_sold then 1 else if SlowK < over_sold and signaldn[1] == 1 then signaldn[1] else 0;

plot arrowUP = if signalup and !signalup[1] then low * 0.9985 else Double.NaN;
arrowUP.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
arrowUP.SetLineWeight(4);
arrowUP.SetDefaultColor(Color.GREEN);
plot arrowUP = if signalup and !signalup[1] then low * 0.9985 else Double.NaN;
arrowDN.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
arrowDN.SetLineWeight(4);
arrowDN.SetDefaultColor(Color.PINK);

Watchlist Column
input DI_length = 11;
input ADX_length = 11;
input ADX_Crossover = 20;

def DX = if (DIPlus(DI_length) + DIMinus(DI_length) > 0) then 100 * AbsValue(DIPlus(DI_length) - DIMinus(DI_length)) / (DIPlus(DI_length) + DIMinus(DI_length)) else 0;
def ADX = WildersAverage(DX, ADX_length);

input over_bought = 80;
input over_sold = 20;
input KPeriod = 8;
input DPeriod = 3;
input smoothingType = {Default SMA, EMA};

def SlowK = reference StochasticFull(over_bought,over_sold,KPeriod,DPeriod,high,low,close,3,smoothingType).FullK;

def signalup = if ADX crosses above ADX_Crossover and SlowK > over_bought then 1 else if SlowK > over_bought and signalup[1] == 1 then signalup[1] else 0;
def signaldn = if ADX crosses above ADX_Crossover and SlowK < over_sold then 1 else if SlowK < over_sold and signaldn[1] == 1 then signaldn[1] else 0;

def UP = signalup and !signalup[1];
def DOWN = signalup and !signalup[1];
addlabel(UP, " " );
addlabel(Down, " " );
addlabel(!UP and !DOWN, " " );
assignbackgroundcolor(if UP then color.green else if DOWN then color.red else color.current);

- robert


Professional ThinkorSwim indicators for the average Joe



Edited 1 time(s). Last edit at 05/16/2014 03:37PM by robert.
Re: Fun with ThinkScript
May 16, 2014 03:35PM
Quote
NCTrader
Robert,

Excellent job on the code you have been able to develop. I know very little about TOS other than I want to throw the computer out the window every time I try to navigate the platform.

Yep. TOS certainly has its quirks, but for the low price of FREE, I'm pretty pleased with the capabilities that it does offer. smiling smiley

As to your coding suggestion, thank you so very much for chiming in. Any and all help is most appreciated. However, I thought about something similar and unless I'm missing something, that strategy would identify the last X number of calendar days when what I'm trying to identify is the last X number of trading days. That is to say, it won't take into account weekends or holidays if they fall in the last X window.

On the other hand, it's entirely possible that I'm just not understanding what you are trying to spoon-feed me. eye rolling smiley
Re: Fun with ThinkScript
May 16, 2014 05:18PM
You are absolutely correct. I am suggesting if you can't get exactly what you want then a good compromise may have to do. I remember emailing someone back in 2007 with a question that centered around trading vs. calendar. In the response I was politely told to “stop being an engineer type – it doesn’t matter”. Some of the books I have read over the years suggest -3/+3 is general consensus. At least one suggested -1/+4 is better on some Nasdaq sectors and had all the data to support the recommendation. My own research by running charts back suggests that the timeframe in question is an area of opportunity to be aware of – nothing more. Not attempting to offer any recommendations on how someone should approach S6 or that my approach is the best as this is all personal preference. Just trying to help.
Re: Fun with ThinkScript
May 16, 2014 05:26PM
Thanks soooo much, Robert. I am having troubles, though.

The watchlist one returns an error and won't let me save. here tis:

com.devexperts.tos.thinkscript.runtime.ScriptAndData$TooComplexException: The complexity of the expression suggests that it may not be reliable with real-time data.

the main one returns this message:

Identifier Already Used: arrowUP at 23:6
No such variable: arrowDN at 24:1
Can not call SetPaintingStrategy on arrowDN at 24:1
No such variable: arrowDN at 25:1
Can not call SetLineWeight on arrowDN at 25:1
No such variable: arrowDN at 26:1
Can not call SetDefaultColor on arrowDN at 26:1
Already assigned: arrowUP at 23:6
Identifier Already Used: arrowUP at 23:6
No such variable: arrowDN at 24:1
Can not call SetPaintingStrategy on arrowDN at 24:1
No such variable: arrowDN at 25:1
Can not call SetLineWeight on arrowDN at 25:1
No such variable: arrowDN at 26:1
Can not call SetDefaultColor on arrowDN at 26:1
Already assigned: arrowUP at 23:6

Have any suggestions? Maybe I have another indicator in my collection that's conflicting with it.
Thanks again.
Re: Fun with ThinkScript
May 16, 2014 06:24PM
Quote
linter
com.devexperts.tos.thinkscript.runtime.ScriptAndData$TooComplexException: The complexity of the expression suggests that it may not be reliable with real-time data.

My fault for not testing the code first. I just typed it up quickly and pasted it here. The "too complex exception" is because the indicator code is using a recursive variable which is fine on the charts, but is not supported in the watchlist columns. Sorry, I forgot about that limitation.

As to the "arrowUP" error message you received in the charting indicator, copy paste error on my part. Again, my fault for rushing through my responses this afternoon. Thanks for bringing it to my attention.

Corrected code.
input DI_length = 11;
input ADX_length = 11;
input ADX_Crossover = 20;

def DX = if (DIPlus(DI_length) + DIMinus(DI_length) > 0) then 100 * AbsValue(DIPlus(DI_length) - DIMinus(DI_length)) / (DIPlus(DI_length) + DIMinus(DI_length)) else 0;
def ADX = WildersAverage(DX, ADX_length);

input over_bought = 80;
input over_sold = 20;
input KPeriod = 8;
input DPeriod = 3;
input smoothingType = {Default SMA, EMA};

def SlowK = reference StochasticFull(over_bought,over_sold,KPeriod,DPeriod,high,low,close,3,smoothingType).FullK;
def SlowD = reference StochasticFull(over_bought,over_sold,KPeriod,DPeriod,high,low,close,3,smoothingType).FullD;

def signalup = if ADX crosses above ADX_Crossover and SlowK > over_bought then 1 else if SlowK > over_bought and signalup[1] == 1 then signalup[1] else 0;
def signaldn = if ADX crosses above ADX_Crossover and SlowK < over_sold then 1 else if SlowK < over_sold and signaldn[1] == 1 then signaldn[1] else 0;

plot arrowUP = if signalup and !signalup[1] then low * 0.9985 else Double.NaN;
arrowUP.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
arrowUP.SetLineWeight(4);
arrowUP.SetDefaultColor(Color.GREEN);
plot arrowDN = if signaldn and !signaldn[1] then high * 1.0015 else Double.NaN;
arrowDN.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
arrowDN.SetLineWeight(4);
arrowDN.SetDefaultColor(Color.PINK);

- robert


Professional ThinkorSwim indicators for the average Joe



Edited 1 time(s). Last edit at 05/16/2014 06:29PM by robert.
Re: Fun with ThinkScript
May 16, 2014 06:27PM
okay, so that one is out, no biggie. what about the other one? have any idea what the problem is there?
Re: Fun with ThinkScript
May 16, 2014 06:38PM
Quote
linter
what about the other one? have any idea what the problem is there?

see above
Re: Fun with ThinkScript
May 16, 2014 07:47PM
that is simply beautiful. i can't thank you enough. now to go see if i can makes heads or tails of it!
Re: Fun with ThinkScript
May 18, 2014 01:03AM
Is it possible to have the area between 2 moving averages shaded like in this video?
Shaded MA's
Re: Fun with ThinkScript
May 18, 2014 08:32AM
Quote
Gaterz
Is it possible to have the area between 2 moving averages shaded like in this video?



input ShortAvgLength = 20;
input LongAvgLength = 50;

plot MA1 = Average(close, ShortAvgLength);
plot MA2 = Average(close, LongAvgLength);

AddCloud(MA1, MA2);
Re: Fun with ThinkScript
May 18, 2014 06:46PM
Do you mean this?

You can download his workspace off of the blog. Then you can edit the script and copy the code.

Thanks for the heads up on his site, I would rather trade futures than options.



plot Data = close;
input price = close;

plot ema21 = expAverage(price, 21);
ema21.SetDefaultColor(Color.magenta);
ema21.SetStyle(Curve.firm);

plot eMA8 = expAverage(price, 8);
eMA8.SetDefaultColor(Color.blue);
eMA8.SetStyle(curve.firm);



AddCloud(ema8, ema21);

def emaFst = ExpAverage(price, 8);
def emaSlw = ExpAverage(price, 21);
def Xup = emaFst[1] <= emaSlw[1] && emaFst > emaSlw;
def XDwn = emaFst[1] >= emaSlw[1] && emaFst < emaSlw;
Re: Fun with ThinkScript
May 18, 2014 07:47PM
But... after playing with the code, Robert's works just as good.
Re: Fun with ThinkScript
May 18, 2014 10:54PM
Thanks guys...that was rather quick. Awesome work!
Re: Fun with ThinkScript
May 20, 2014 08:32AM
okay, here's another request. i'm looking for an easy way to gauge risk/reward. maybe this idea is already in ToS but if so i haven't found it. the idea is this ...

a drawing plot where you, say, click on some price level, a horizontal price bar appears there. move the cursor up, a second bar moves up too, with a bubble telling you the percentage gain from your starting position. go the other way (i.e. toward your stop level) and the bubble tells you the percentage loss.

kind of a variation on the fib tool.

what do you think? possible?
Re: Fun with ThinkScript
May 20, 2014 09:58AM
Quote
linter
what do you think? possible?

Nope. Thinkscript doesn't allow for any user interaction as you are describing.
Re: Fun with ThinkScript
May 20, 2014 10:35AM
Hi Robert,

I have noticed that whenever there is a huge volume spike, an almost 100% sure trade follows in the same direction. For example Pandora (P) had a volume spike at 11:12 today just a few minutes ago. The trick is to notice that volume spike immediately. Is a code possible that can issue an audible and written alert whenever the volume of the last bar is say 3 times the volume average, more than 30 minutes after market open?

Tan



Edited 2 time(s). Last edit at 05/20/2014 10:44AM by tanman.
Fun with ThinkScript
May 20, 2014 12:01PM
Hello folks,
I have added an AvgVol column to a watchlist. I would like the current volume cell for each item in the watchlist to be background colored green when it is above the AvgVol. I seem to be confused about how to do this when the AvgVol is not being used on a chart but in a watchlist.

Thank you, amazing stuff elsewhere on this forum!
Re: Fun with ThinkScript
May 20, 2014 02:27PM
Hello Folks,
I have a watchlist with a custom column for average volume. I like to watch the 10 day average volume and compare it to the current day's volume. I would like to edit the column indication to make this comparison for me and change the background color when the current volume is above the 10 day average. I have tinkered with editing the average volume study, but I just screw it up. Any ideas how to edit the indicator?
Re: Fun with ThinkScript
May 20, 2014 03:58PM
Quote
Tan
Is a code possible that can issue an audible and written alert whenever the volume of the last bar is say 3 times the volume average, more than 30 minutes after market open?

Tan

input AverageLength = 10;
input VolumeMultiplier = 3;
input MinutesAfterOpen = 30;

def AvgVol = average(volume, AverageLength);
def VolX = (volume > AvgVol * VolumeMultiplier) and (secondsfromtime(0930) >= 60000 * MinutesAfterOpen);

Alert(VolX, Concat(GetSymbolPart(), " has a large volume spike." ), Alert.BAR, Sound.Bell);
Re: Fun with ThinkScript
May 20, 2014 04:06PM
Quote
mkaltenborn
I have a watchlist with a custom column for average volume. I like to watch the 10 day average volume and compare it to the current day's volume. I would like to edit the column indication to make this comparison for me and change the background color when the current volume is above the 10 day average. I have tinkered with editing the average volume study, but I just screw it up. Any ideas how to edit the indicator?

Watchlist Column
def AvgVol = average(volume, 10);
plot vol = volume;
assignbackgroundcolor(if vol > AvgVol then color.dark_green else color.current);
Re: Fun with ThinkScript
May 20, 2014 05:00PM
so how does this kind of thing work? do you have to have individual charts open for the alarm to sound on any one of them?



obert Wrote:
-------------------------------------------------------
> > Is a code possible that can issue an audible and
> written alert whenever the volume of the last bar
> is say 3 times the volume average, more than 30
> minutes after market open?
>
> Tan
>
>
>
> input AverageLength = 10;
> input VolumeMultiplier = 3;
> input MinutesAfterOpen = 30;
>
> def AvgVol = average(volume, AverageLength);
> def VolX = (volume > AvgVol * VolumeMultiplier)
> and (secondsfromtime(0930) >= 60000 *
> MinutesAfterOpen);
>
> Alert(VolX, Concat(GetSymbolPart(), " has a large
> volume spike." ), Alert.BAR, Sound.Bell);
>
Re: Fun with ThinkScript
May 20, 2014 06:05PM
Robert, Thank you very much smiling smiley

Linter, yes the individual chart has to be open for the alarm to sound. The way I do it is I open up multiple charts on the flexible grid to watch multiple symbols at the same time and then I detach the layout in a separate window and don't minimize the detached window, just make it a little smaller so I can see the tabs on the main TOS. Then even if I navigate to other tabs or charts in TOS, I still get the alerts. Sometimes I have 30 or more charts open at the same time in flexible grid in the detached window and I get alerted anytime the programmed condition is triggered in any one of those symbols. I add the code to assign background color to light gray if the condition is met, so when the alert goes off I click the detached window in the task bar and immediately know which of the 30 symbols has been triggered.

assignbackgroundcolor(if ..... then color.light_gray else color.current);
Re: Fun with ThinkScript
May 20, 2014 06:52PM
Hello Robert,
Thank you for helping. The trouble that remains is that the new study is not available in the customize column menu of the watchlist. Any ideas?
Re: Fun with ThinkScript
May 20, 2014 07:14PM
Quote
mkaltenborn
Hello Robert, Thank you for helping. The trouble that remains is that the new study is not available in the customize column menu of the watchlist. Any ideas?

Read this tutorial then substitute your own code.

[www.thinkscripter.com]
Re: Fun with ThinkScript
May 21, 2014 04:46AM
tanman: thanks very much for the explanation. i'm going to give it a go!
Re: Fun with ThinkScript
May 21, 2014 09:47AM
I have read all pages of this thread. This forum is an amazingly friendly and informative resource. Thank you once more for all the help.

TOS does not make Time & Sales data available to thinkscript. It doesn't really matter why. I like to trade futures intraday. The T&S data is refreshed every 20 minutes so the tracks of buyers and sellers is almost impossible using T&S. I like to watch TICK charts to show when the rate of activity is changing.

I have observed that when prices move higher rapidly shorts cover their positions as buy stops are reached. When large blocks lift the offer at these price extremes and are not followed by more buyers it often marks a price turning point. The reverse occurs as prices decline rapidly.

The On Balance Volume study attempts to indicate this phenomenon.

Have you ever written any code aimed at the futures markets?
Re: Fun with ThinkScript
May 21, 2014 10:21AM
Quote
mkaltenborn
Have you ever written any code aimed at the futures markets?

I barely understand stocks grinning smiley I've never even looked at futures.
Re: Fun with ThinkScript
May 21, 2014 11:01AM
Hi Robert,

I wasn't getting any alerts on the volume spike so I changed 60,000 to 60 and then it started working. I guess if it is secondsfromtime it should be 60 instead of 60,000.
Re: Fun with ThinkScript
May 21, 2014 12:37PM
Quote
tanman
I guess if it is secondsfromtime it should be 60 instead of 60,000.

Good catch. You are 100% correct. I had milliseconds on the brain from working with AggregationPeriod, but SecondsFromTime uses actual seconds (duh). smiling smiley
Sorry, only registered users may post in this forum.

Click here to login