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 12, 2016 09:43PM
Davetrader,

Thank you for the kind words. After 5 more hours of intense thought and experimentation, I was finally able to figure out how to plot solid horizontal lines at the earnings highs without any breaks in them and only for the last 2 years! Here is the script:


def Earnings = if GetYear() > GetLastYear() - 3 and HasEarnings() then 1 else 0;
def Before = HasEarnings(EarningTime.BEFORE_MARKET);
def After = HasEarnings(EarningTime.AFTER_MARKET);
def During = HasEarnings() and !Before and !After;
def EHigh = if Earnings and Before then high else if Earnings and After then high[-1] else if Earnings and During then high else EHigh[1];

def count = if !Earnings then 1 else count[1] + 1;
plot ext = GetValue(EHigh, count);
ext.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ext.SetDefaultColor(Color.CYAN);

def count1 = if Earnings then 1 else count1[1] + 1;
plot ext1 = GetValue(EHigh, count1);
ext1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ext1.SetDefaultColor(Color.CYAN);

def count2 = if Earnings then 1 else count2[1] + 1;
plot ext2 = GetValue(EHigh[80], count2);
ext2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ext2.SetDefaultColor(Color.CYAN);

def count3 = if Earnings then 1 else count3[1] + 1;
plot ext3 = GetValue(EHigh[140], count3);
ext3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ext3.SetDefaultColor(Color.CYAN);

def count4 = if Earnings then 1 else count4[1] + 1;
plot ext4 = GetValue(EHigh[200], count4);
ext4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ext4.SetDefaultColor(Color.CYAN);

def count5 = if Earnings then 1 else count5[1] + 1;
plot ext5 = GetValue(EHigh[280], count5);
ext5.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ext5.SetDefaultColor(Color.CYAN);

def count6 = if Earnings then 1 else count6[1] + 1;
plot ext6 = GetValue(EHigh[350], count6);
ext6.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ext6.SetDefaultColor(Color.CYAN);

def count7 = if Earnings then 1 else count7[1] + 1;
plot ext7 = GetValue(EHigh[420], count7);
ext7.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ext7.SetDefaultColor(Color.CYAN);

def count8 = if Earnings then 1 else count8[1] + 1;
plot ext8 = GetValue(EHigh[490], count8);
ext8.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ext8.SetDefaultColor(Color.CYAN);


Game0ver,

You are welcome and thank you!


baffled1,

Thank you!



Edited 5 time(s). Last edit at 02/13/2016 12:10PM by tanman.
Re: Fun with ThinkScript
February 13, 2016 11:49AM
All readers,

I have modified and edited the script in my previous post to draw horizontal lines at earnings highs without any line breaks and only for last 2 years. That is the final cleaned up version.
Re: Fun with ThinkScript
February 13, 2016 12:10PM
Tanman,

Thank you for all of your help. Who would have ever thought it would be so difficult to plot some straight lines? Trust me, I have been chasing this white whale for some time now. You have been the closest to crack it so far. The code looks great. Thanks again for all the time and effort you have put into this. I appreciate it.
Re: Fun with ThinkScript
February 13, 2016 12:33PM
Davetrader,

I just discovered that the script posted above has a bug and mysteriously doesn't work on some symbols, for example GPRO. Please use this code instead which should work on all symbols:


def Earnings = if GetYear() > GetLastYear() - 3 and HasEarnings() then 1 else 0;
def Before = HasEarnings(EarningTime.BEFORE_MARKET);
def After = HasEarnings(EarningTime.AFTER_MARKET);
def During = HasEarnings() and !Before and !After;
def EHigh = if Earnings and Before then high else if Earnings and After then high[-1] else if Earnings and During then high else Ehigh[1];

def count = if !Earnings then 1 else count[1] + 1;
plot ext = GetValue(EHigh, count);
ext.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ext.SetDefaultColor(Color.CYAN);

def count1 = if Earnings then 1 else count1[1] + 1;
plot ext1 = GetValue(EHigh, count1);
ext1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ext1.SetDefaultColor(Color.CYAN);

def count2 = if Earnings then 100 else count2[1] + 1;
plot ext2 = GetValue(EHigh, count2);
ext2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ext2.SetDefaultColor(Color.CYAN);

def count3 = if Earnings then 150 else count3[1] + 1;
plot ext3 = GetValue(EHigh, count3);
ext3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ext3.SetDefaultColor(Color.CYAN);

def count4 = if Earnings then 200 else count4[1] + 1;
plot ext4 = GetValue(EHigh, count4);
ext4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ext4.SetDefaultColor(Color.CYAN);

def count5 = if Earnings then 250 else count5[1] + 1;
plot ext5 = GetValue(EHigh, count5);
ext5.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ext5.SetDefaultColor(Color.CYAN);

def count6 = if Earnings then 300 else count6[1] + 1;
plot ext6 = GetValue(EHigh, count6);
ext6.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ext6.SetDefaultColor(Color.CYAN);

def count7 = if Earnings then 350 else count7[1] + 1;
plot ext7 = GetValue(EHigh, count7);
ext7.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ext7.SetDefaultColor(Color.CYAN);

def count8 = if Earnings then 400 else count8[1] + 1;
plot ext8 = GetValue(EHigh, count8);
ext8.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ext8.SetDefaultColor(Color.CYAN);
Re: Fun with ThinkScript
February 13, 2016 02:01PM
Tanman,

It looks good with one exception. The highs that are plotted are the next days candle not the actual earnings date candle. Looking at your script, you have before and after earnings plotted. I don't know if that is required to make the code work but if it could be omitted that would be great. The before and after has absolutely no bearing in my world of trading. I am just interested in the high of the day that the earnings symbol is on the chart. I apologize if I am being difficult or not making sense.
In a nutshell, the day the earnings symbol appears on the chart, that is the candle or bar that I want the high line drawn from. The way it is drawn now throws everything off because it is the next days which is either higher of lower.

Again thanks for your help.
Re: Fun with ThinkScript
February 13, 2016 02:05PM
Tanman,

After looking at a few tickers, your code it working nicely. There were just a few that did not track properly. Disregard the previous post. It looks great. Thanks.
Re: Fun with ThinkScript
February 13, 2016 10:26PM
Nevermind!



Edited 3 time(s). Last edit at 02/14/2016 11:00PM by RichieRick.
JML
Re: Fun with ThinkScript
February 14, 2016 05:20AM
Has anyone seen this indicator or can duplicate it? It complements the TTM squeeze from John Carter.

The indicator itself compliments the standard Squeeze indicator available on most platforms. Green bars indicate that the squeeze is preparing to fire. The remaining price bars match the squeeze momentum indicator but will change colors earlier as the rate of momentum change begins to diminish. The combination gives you both an early entry and early exit cue.

[simplerindicators.com]

squeeze indicator #2 on image ( already on TOS)
[2.bp.blogspot.com]


Thanks
Jack L
Re: Fun with ThinkScript
February 14, 2016 06:07AM


for those who may be able to compare



Edited 1 time(s). Last edit at 02/14/2016 04:04PM by Game0ver.
JML
Re: Fun with ThinkScript
February 17, 2016 10:20PM
Does anyone know how to make a moving average value on daily chart show on another time frame say like a 5 minute chart?

For example, at the close today the 21 ema on a daily chart of the SPY was 189.78
I would like that value of the 21 ema of the daily chart to show on a 5 minute chart or any other time frame like 15 min, 1 hr ect...

can that be done?

Thanks

JML
Re: Fun with ThinkScript
February 18, 2016 01:50AM
JML

Try This:

input agg = AggregationPeriod.day;
input length = 21;
input avg = AverageType.EXPONENTIAL;
plot movavg = MovingAverage(avg, close(period = agg), length) ;
Re: Fun with ThinkScript
February 18, 2016 10:31AM
Good Morning ,,

Could someone modify this code for this Indicator to give Signals only when the crossover happened in the Oversold or Overbought area ?



Indicator name : AroonIndicator

it is already in Thinkorswim platform studies


#
# TD Ameritrade IP Company, Inc. (c) 2007-2016
#

declare lower;

input length = 5;

Assert(length > 0, "'length' must be positive: " + length);

plot Up = (length - 1 - GetMaxValueOffset(high, length)) * 100.0 / (length - 1);
plot Down = (length - 1 - GetMinValueOffset(low, length)) * 100.0 / (length - 1);
plot OverBought = 70;
plot OverSold = 30;

Up.SetDefaultColor(GetColor(1));
Down.SetDefaultColor(GetColor(5));
OverBought.SetDefaultColor(GetColor(8));
OverSold.SetDefaultColor(GetColor(8));

Re: Fun with ThinkScript
February 18, 2016 11:54AM
Can anyone help me with this alert code. Im using the Ehlers Simple Decycler and would like to be alerted once the high price is within the range of 6 pips or closer to the upperband and the low price being at 6 pips or closer to the lowerband. Im using 10 min bars on GBP/AUD. Any help is much appreciated.
JML
Re: Fun with ThinkScript
February 18, 2016 12:17PM
Lukhy911 Wrote:
-------------------------------------------------------
> JML
>
> Try This:
>
> input agg = AggregationPeriod.day;
> input length = 21;
> input avg = AverageType.EXPONENTIAL;
> plot movavg = MovingAverage(avg, close(period =
> agg), length) ;

thanks LUkhy911, works great!!
Re: Fun with ThinkScript
February 18, 2016 01:24PM
Does anyone know if we can access any external URLs within thinkscript???

Thanks

SKumar
JML
Re: Fun with ThinkScript
February 18, 2016 01:26PM
Lukhy911 Wrote:
-------------------------------------------------------
> JML
>
> Try This:
>
> input agg = AggregationPeriod.day;
> input length = 21;
> input avg = AverageType.EXPONENTIAL;
> plot movavg = MovingAverage(avg, close(period =
> agg), length) ;

Lukhy911,
one more thing...
can you set that to where I can select the MA values from either daily or weekly, hourly to show lets say on a 5 minute?

JML
Re: Fun with ThinkScript
February 18, 2016 01:53PM
SARA Wrote:
-------------------------------------------------------
> Good Morning ,,
>
> Could someone modify this code for this Indicator
> to give Signals only when the crossover happened
> in the Oversold or Overbought area ?
> Indicator name : AroonIndicator
> it is already in Thinkorswim platform studies
===============================================
FINAL EDIT... SARA, my apologies. I tried many times and failed. AroonIndicator is code from the DARKSIDE. Best I can explain the problem is this: Aroon forces digital rendering, i.e., straight lines between multiple datapoints, based on its MaxValue and MinValue across certain lengths. "Normal" plots use something "closer" to analog rendering, what I call "laminar" rendering, which makes it possible to cite and specify crosses occurring above/below specific values. There might be a way, though, perhaps by tampering with Aroon itself to get rid of MaxValueOffset and MinValueOffset. But my head is tired right now.

p.s.- did you sit up all night inventing this problem? spinning smiley sticking its tongue out

Gamblers roll the dice; Traders load the dice.



Edited 5 time(s). Last edit at 02/18/2016 03:38PM by baffled1.
Re: Fun with ThinkScript
February 18, 2016 03:48PM
SARA, closer, but still no cigar. Ripped out part of AroonIndicator. Arrows now about 75% accurate (except for color).
#
# TD Ameritrade IP Company, Inc. (c) 2007-2016
#
declare lower;
input length = 25;
Assert(length > 0, "'length' must be positive: " + length);

plot Up = (GetMaxValueOffset(high, length)) * 100.0 / (length - 1);
plot Down = (GetMinValueOffset(low, length)) * 100.0 / (length - 1);
plot OverBought = 70;
plot OverSold = 30;

Up.SetDefaultColor(GetColor(1));
Down.SetDefaultColor(GetColor(5));
OverBought.SetDefaultColor(GetColor(8));
OverSold.SetDefaultColor(GetColor(8));

plot Above = Up crosses Down and Down<30;
Above.SetDefaultColor(Color.green);
Above.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);

plot Below =  Up crosses Down and Down>70;
Below.SetDefaultColor(Color.red);
Below.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
plot zeroline = 0;

Gamblers roll the dice; Traders load the dice.
Re: Fun with ThinkScript
February 18, 2016 08:35PM
Here you go JML:

input Aggregate = { Hourly, default Daily, Weekly, Monthly} ;
input length = 21;
input avg = AverageType.EXPONENTIAL;

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;

plot movavg = MovingAverage(avg, close(period = agg), length) ;
Re: Fun with ThinkScript
February 19, 2016 01:46AM
baffled1

Thank you for your time , I'll try it thumbs up
Re: Fun with ThinkScript
February 19, 2016 02:16AM
Lukhy911 ,

The Aggregation code that you posted looks great. I tried to add it to this script but I don't know why it didn't work ..?
I just wanted to see the daily signals on the smaller time frames, 1 or 2 hours time frame .. etc ..



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, 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: Fun with ThinkScript
February 19, 2016 01:14PM
Never mind once again. I figured it out on my own.... again.

Challenging but it is rewarding when I'm able to solve an issue on my own. smiling smiley

R



Edited 6 time(s). Last edit at 02/19/2016 06:35PM by RichieRick.
Re: Fun with ThinkScript
February 19, 2016 04:20PM
well, i've taken a sudden liking to Wilder's ADX indicator. one problem with ToS is it doesn't have the ADXr line, only the DMI+, DMI-, and ADX. i was wondering if someone here could code up the ADX with ADXr included? Definition of ADXr is below.

thanks!

***also*** I would ***love*** to get alerts when the following conditions are met:

Long:

1. DMI+ should be above 20 level.
2. DMI- should be below 20 level.
3. ADX should be between DMI+ & DMI-.
4. ADXr should be above 20 level.

Short:

1. DMI- should be above 20 level.
2. DMI+ should be below 20 level.
3. ADX should be between DMI- & DMI+.
4. ADXr should be above 20 level.

ADXr:

The Average Directional Movement Index Rating (ADXR) measures the strength of the Average Directional Movement Index (ADX). It's calculated by taking the average of the current ADX and the ADX from one time period before (time periods can vary, but the most typical period used is 14 days).
Like the ADX, the ADXR ranges from values of 0 to 100 and reflects strengthening and weakening trends. However, because it represents an average of ADX, values don't fluctuate as dramatically and some analysts believe the indicator helps better display trends in volatile markets.
Calculations

Sample Chart View Graphic
This smoothing step results in the ADXR being slightly less responsive than the ADX. Where the ADXR shines is its ability to compensate for the variance of excessive tops and bottoms.
JML
Re: Fun with ThinkScript
February 19, 2016 04:38PM
Lukhy911 Wrote:
-------------------------------------------------------
> Here you go JML:
>
> input Aggregate = { Hourly, default Daily, Weekly,
> Monthly} ;
> input length = 21;
> input avg = AverageType.EXPONENTIAL;
>
> 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;
>
> plot movavg = MovingAverage(avg, close(period =
> agg), length) ;

Works great! Thanks Lukhy911
Re: Fun with ThinkScript
February 19, 2016 05:50PM
linter Wrote:
-------------------------------------------------------
> well, i've taken a sudden liking to Wilder's ADX
> indicator. one problem with ToS is it doesn't
> have the ADXr line...
=========================================
Linter, I'm confused. My platform's got ADXR (also plots ADX)... So, ummm???

Gamblers roll the dice; Traders load the dice.
Re: Fun with ThinkScript
February 19, 2016 06:20PM
well, thanks, i just went and looked and it seems that right you are. but to get the DMIs and the ADXs together, you have to put one on top of the other, right? ok, can do. like i said, thanks! and, about those alerts ....smiling smiley
Re: Fun with ThinkScript
February 20, 2016 12:17AM
Jesus - I've been reading this single post for weeks now, and there is no shortage of absolute GOLD contained within.
Good job everyone!!
Re: Fun with ThinkScript
February 20, 2016 12:29AM
hello.

i would like help to make the following script....

would like to calculate the minimum distance between two ema's of equal periodicity and with constant vertical distance between them.

in other words this is the distance of the shortest line connecting the two ema's at any point in time. and it is not the perpendicular because the perpendicular to one ema will not be perpendicular to the other ema where it hits it. one ema is shifted vertically by a constant distance ... say 10 points.

most indicators are constructed to measure the vertical distance between curves or ema's ... but this is completely useless.
so both ema's are say 50 period and the vertical distance between them is 10 price points.

thank you in advance to anyone who can help with this.

i believe that it is a calculus problem concerning minimum distance between two curves.

i found something suggesting the following
.............. shortest distance b/w two curves is the distance along their common normal.....so find the equation of common normal at a point of any curve and then solve it with another curve to get two points of intersection,,,,now calculate distance using distance formula.
Re: Fun with ThinkScript
February 20, 2016 12:56AM
Manny Wrote:
-------------------------------------------------------
> Can anyone help me with this alert code. Im using
> the Ehlers Simple Decycler and would like to be
> alerted once the high price is within the range of
> 6 pips or closer to the upperband and the low
> price being at 6 pips or closer to the lowerband.
> Im using 10 min bars on GBP/AUD. Any help is much
> appreciated.
=============================================
Manny, this should work. Copy the Ehlers and paste this onto the bottom of it. Sound options are Bell, Chimes, Ding, Ring, you choose.
def mySHORT = close>=(UpperBand-0.0006);
def myLONG = close<=(LowerBand+0.0006);

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

Gamblers roll the dice; Traders load the dice.



Edited 1 time(s). Last edit at 02/20/2016 12:57AM by baffled1.
Re: Fun with ThinkScript
February 20, 2016 01:24AM
linter Wrote:
-------------------------------------------------------
> well, thanks, i just went and looked and it seems
> that right you are. but to get the DMIs and the
> ADXs together, you have to put one on top of the
> other, right? ok, can do. like i said, thanks!
> and, about those alerts ....smiling smiley
=========================================
Okay, but not sure about DMI terms. I used your terms for DMI+ and DMI-. But Robert, in a post at http://www.researchtrade.com/forum/read.php?7,2258,2258#msg-2258, used "DIplus" and "DIminus" for one of his studies. Also, below, I put the terms in parens so as not to confuse the system with the + and - signs. He also used "" signs. Anyway, this code shows how to set it up once you have the right terms.
def myLONG = (DMI+)>20.0 and (DMI-)<20.0 and ADX<(DMI+) and ADX>(DMI-) and ADXr>20.0;
def mySHORT = (DMI-)>20.0 and (DMI+)<20.0 and ADX>(DMI-) and ADX<(DMI+) and ADXr>20.0;
alert(myLONG, "GO LONG", alert.BAR, sound.Ring);
alert(mySHORT, "GO SHORT", alert.BAR, sound.Ring);

Gamblers roll the dice; Traders load the dice.
Sorry, only registered users may post in this forum.

Click here to login