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 03, 2016 06:22PM
tanman Wrote:
-------------------------------------------------------
> baffled1,
>
> The only way I can think of is as follows:
>
> High1 = max(variable1, variable2)
> High2 = max(High1, variable3)
> High3 = max(High2, variable4)
> High4 = max(High3, variable5)
>
> and so on and so forth
=========================================
Thanks, Tanman. Yep, I see how that works. Tried something like it- not quite as clean as yours- and ended up with 60 lines of code, which is what I estimate for yours too. But thanks for your help! Just seems like there should be a shorter way. Easily done in spreadsheet so kinda odd there's no simple way in thinkscript.
~EDIT- well, maybe not 60 lines with your code. . I'll give it a try. Thanks.

Gamblers roll the dice; Traders load the dice.



Edited 3 time(s). Last edit at 02/03/2016 06:28PM by baffled1.
Re: Fun with ThinkScript
February 03, 2016 06:46PM
ok, tanman, thanks. sorry i can't help you with your request. i am just terrible at this stuff. which brings me to round two of my questions, also dealing with code for a custom column. what i would like to do is color the box based on the color of the heikin ashi color, green or red. to get around the update lag, i'd like to do this for the most recently closed time-period bar and the one before that. iow, i don't need a column for the current HA candle.

here's code for a study that i tried, without success, to turn into the columns i'm looking for. i just couldn't figure out where to put the [1]s and [2]s and [3]s. can you or someone help?

#HEIKINASHISIGNALS
# [www.thinkscripter.com]
# thinkscripter@gmail.com
# Last Update 28 FEB 2009

def haclose = (open + high + low + close) / 4;
rec haopen = CompoundValue(1, (haopen[1] + haclose[1]) / 2, (open[1] + close[1]) / 2);
def diff = haclose - haopen;

plot HASignal = if diff > 0 and diff[1] <= 0 then low*0.999 else if diff < 0 and diff[1] >= 0 then high*1.001 else double.nan;

HASignal.assignValueColor(if diff >0 then color.green else color.red);
Re: Fun with ThinkScript
February 03, 2016 11:53PM
Linter,

I think it is not working because the last line should say:

HASignal.assignValueColor(if diff > 0 and diff[1] <= 0 then Color.GREEN else if diff < 0 and diff[1] >= 0 then Color.RED else Color.GRAY)

For previous bars, just change diff to diff[1] and diff[1] to diff[2] in last 2 lines.

I am still not optimistic because the watch list column colors lag even for previous bars. That's why I stopped coding watch list column colors.
Re: Fun with ThinkScript
February 04, 2016 06:59AM
thanks, tanman, i'll give it a shot. i'm not optimistic either, but we shall see. meanwhile, the metatrader program that forex people use is so much better at this kind of thing. see screenshot. it runs without a hitch and updates on any price change, i think. guess i'll have to run it in the background, because i like ToS so much better in other ways.

Re: Fun with ThinkScript
February 04, 2016 08:26AM
Linter,

TOS is not good for trading Forex. It is the best for options and good for stocks and OK for futures.
Re: Fun with ThinkScript
February 04, 2016 08:39AM
tanman Wrote:
-------------------------------------------------------
> Linter,
>
> TOS is not good for trading Forex. It is the best
> for options and good for stocks and OK for
> futures.
==========================================
=========================================
Agree. But for runnng algos H1 and longer for Forex is okay.

Gamblers roll the dice; Traders load the dice.
Re: Fun with ThinkScript
February 04, 2016 08:49AM
baffled1,

I tried the following on TOS and it worked! So potentially you can nestle the max's and put all variables in 1 line!

def variable1 = open;
def variable2 = close;
def variable3 = high;
def variable4 = low;
def High1 = max(max(variable1, variable2), max(variable3, variable4));
Re: Fun with ThinkScript
February 04, 2016 12:19PM
forex, i'm not trading it with ToS. on baffled1's suggestion, i'm doing it with oanda.
Re: Fun with ThinkScript
February 04, 2016 05:45PM
Quote
Tanman
> baffled1,
>
> I tried the following on TOS and it worked! So
> potentially you can nestle the max's and put all
> variables in 1 line!
>
> def variable1 = open;
> def variable2 = close;
> def variable3 = high;
> def variable4 = low;
> def High1 = max(max(variable1, variable2),
> max(variable3, variable4));
Tanman, many thanks. This looks very promising. I will try it shortly- my head was spinning from the problem so I backed off the last day or so.

Gamblers roll the dice; Traders load the dice.
Re: Directional Divergence Index ( DDI ) need a THINKORSWIM Script
February 04, 2016 06:43PM
Tanman
please can you delete histogram codes from this formula:


declare lower;

input N = 13;
input N1 = 3;
input M = 10;
input M1 = 5;

def TR = max(AbsValue(high - high[1]), AbsValue(low - low[1]));
def DMZ = if((high + low) <= (high[1] + low[1]), 0, TR);
def DMF = if((high + low) >= (high[1] + low[1]), 0, TR);
def DIZ = sum(DMZ, N)/(sum(DMZ, N) + sum(DMF, N));
def DIF = sum(DMF, N)/(sum(DMF, N) + sum(DMZ, N));

plot DDI = DIZ - DIF;
plot ZeroLine = 0;
DDI.AssignValueColor(Color.GRAY);
DDI.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
DDI.SetDefaultColor(Color.RED);
DDI.SetLineWeight(1);
DDI.AssignValueColor(if DDI >= 0 then Color.UPTICK else Color.DOWNTICK);
plot ADDI = MovingAverage(AverageType.SIMPLE, DDI[- N1], M);
plot AD = Average(ADDI, M1);
def UP = ADDI > AD;
AD.AssignValueColor(Color.DOWNTICK);
ADDI.AssignValueColor(if UP then Color.GREEN else Color.CYAN);



Thanks
Hanimusa
Re: Fun with ThinkScript
February 04, 2016 09:27PM
You know that you can just go and uncheck the plot



declare lower;

input N = 13;
input N1 = 3;
input M = 10;
input M1 = 5;

def TR = max(AbsValue(high - high[1]), AbsValue(low - low[1]));
def DMZ = if((high + low) <= (high[1] + low[1]), 0, TR);
def DMF = if((high + low) >= (high[1] + low[1]), 0, TR);
def DIZ = sum(DMZ, N)/(sum(DMZ, N) + sum(DMF, N));
def DIF = sum(DMF, N)/(sum(DMF, N) + sum(DMZ, N));

def DDI = DIZ - DIF;

plot ADDI = MovingAverage(AverageType.SIMPLE, DDI[- N1], M);
plot AD = Average(ADDI, M1);
def UP = ADDI > AD;
AD.AssignValueColor(Color.DOWNTICK);
ADDI.AssignValueColor(if UP then Color.GREEN else Color.CYAN);
Re: Fun with ThinkScript
February 05, 2016 03:31PM
tanman Wrote:
-------------------------------------------------------
> baffled1,
I tried the following on TOS and it worked! So
> potentially you can nestle the max's and put all
> variables in 1 line!
> def variable1 = open;
> def variable2 = close;
> def variable3 = high;
> def variable4 = low;
> def High1 = max(max(variable1, variable2),
> max(variable3, variable4));
=================================================
Tanman, I think that nested code of yours is going to work. But keeping track of how many parentheses is tough, so I tried to simplify (below) and it's a vast improvement over my 60 lines of before! With this, though, I only get 1 plot covering just 3 bars on the H1, but a lot of discontinuous plots for D1. So you were definitely on the right track- all I have to do now is extend your nesting idea properly the way you suggest. Thanks.
declare lower;
def A = (close(“eur/usd”)/close(“eur/usd”)[1])*100.00;
def B = (close(“nzd/usd”)/close(“nzd/usd”)[1])*100.00;
def C = (close(“eur/nzd”)/close(“eur/nzd”)[1])*100.00;
def D = (close(“gbp/nzd”)/close(“gbp/nzd”)[1])*100.00;
def E = (close(“aud/jpy”)/close(“aud/jpy”)[1])*100.00;
def F = (close(“gbp/jpy”)/close(“gbp/jpy”)[1])*100.00;
def G = (close(“usd/chf”)/close(“usd/chf”)[1])*100.00;
def H = (close(“nzd/chf”)/close(“nzd/chf”)[1])*100.00;
def I = (close(“eur/gbp”)/close(“eur/gbp”)[1])*100.00;
def J = (close(“nzd/cad”)/close(“nzd/cad”)[1])*100.00;
def K = (close(“usd/cad”)/close(“usd/cad”)[1])*100.00;
def L = (close(“gbp/usd”)/close(“gbp/usd”)[1])*100.00;

def High1 = max(max(A,B ),max(C,D));
def High2 = max(max(E,F ),max(G,H));
def High3 = max(max(I,J ),max(K,L));

def High4 = max(High1,High2);
def High5 = max(High2,High3);
def High6 = max(High4,High5);

plot High7 = High6;

Gamblers roll the dice; Traders load the dice.



Edited 1 time(s). Last edit at 02/05/2016 03:32PM by baffled1.
Re: Fun with ThinkScript
February 05, 2016 04:21PM
baffled1,

Looks like nesting doesn't work further than these levels on TOS sad smiley

You don't need 4 lines at the end, instead 2 lines should do:

def High4 = max(High1,High2);
plot High5 = max(High4,High3);

Then you can add the following at the end, so you can change color of plot in the code by changing cyan to something else:

High5.AssignValueColor(Color.CYAN);



Edited 2 time(s). Last edit at 02/05/2016 04:24PM by tanman.
Re: Fun with ThinkScript
February 05, 2016 05:08PM
tanman Wrote:
-------------------------------------------------------
> baffled1,
>
> Looks like nesting doesn't work further than these
> levels on TOS sad smiley
>
> You don't need 4 lines at the end, instead 2 lines
> should do:
>
> def High4 = max(High1,High2);
> plot High5 = max(High4,High3);
>
> Then you can add the following at the end, so you
> can change color of plot in the code by changing
> cyan to something else:
>
> High5.AssignValueColor(Color.CYAN);
================================================
Okay, thanks, Tanman. I can see what you mean- Was just so tired of it not working I took the long safe way out. I've also now configured it for "min" and I get that value too. Interesting- on D1 tight range means a continued downtrend (in EURUSD) and vice-versa. Accidental, as I was only looking for how to write code of this type, not to find an indicator.
EDIT~~ not sure what thread to put this in but heads-up for stock, option, emini traders... This article on how to use DAX premarket action to forecast SP500 premarket action very good. Turns out that premarket DAX "very often" is replicated by the LATER premarket SP500, almost bar-for-bar. But they take down their last month's articles (or pics) so if you want to see it should get it now. Basis for an interesting indicator, I think.
EDIT AGAIN~~ So if the DAX idea is useful to anybody, I think simple way is to advance DAX chart (offset) 7 hours(?). I had trouble finding DAX on ToS, closest I got was DAX.NV, $DWDE, $DWDENDT and can't remember which was best. Just sharing.

Gamblers roll the dice; Traders load the dice.



Edited 2 time(s). Last edit at 02/05/2016 05:29PM by baffled1.
Re: Fun with ThinkScript
February 05, 2016 06:06PM
baffled1,

Thanks for the article. I knew there was a correlation but firstly, the problem is that I don't think TOS allows trading FDAX or FDXM and secondly, the correlation is almost instant so not useful for trading purposes. I don't think there is a lag between FDAX and /ES which would be useful for trading. Even in the article he says to calculate an average for all European indices and then try to use that as an indicator for /ES and not just FDAX alone. I don't think even then there will be a lag otherwise all futures traders would take advantage of that and mint millions.

What he is saying in the article is that you can use the trend in FDAX to predict later trend in /ES during regular trading hours. This is true for regular /ES also because it will have the same pre-market trend as FDAX because they are both correlated. So you can use pre-market trend in /ES to predict trend in /ES during regular trading hours.

For more info please see:

[www.elitetrader.com]



Edited 2 time(s). Last edit at 02/05/2016 06:15PM by tanman.
Re: Fun with ThinkScript
February 06, 2016 03:28AM
Quote
TANMAN
> Thanks for the article. ...the correlation is almost instant so not
> useful for trading purposes...
> What he is saying in the article is that you can
> use the trend in FDAX to predict later trend in
> /ES during regular trading hours. This is true for
> regular /ES also because it will have the same
> pre-market trend as FDAX ,,,So you can use
pre-market trend in /ES to predict trend in /ES during
regular trading hours...
Tanman, thanks for clarifying and I read the link page too. Guess I misunderstood the article. So what I don't get is if the premarket /ES trend is all that's needed to predict regular market /ES, why bother with the DAX? Just curious- I mean, I don't even have a dog in this fight because my (forex) market is continuous but I'd still like to understand what his point is? No biggie, just wondering, if you get a minute.

Gamblers roll the dice; Traders load the dice.
Re: Fun with ThinkScript
February 06, 2016 09:05AM
baffled1,

DAX is useful for those traders who are night owls or wish to trade futures early in the morning before going to work (market open 3:30 EST). DAX is volatile at that time and moves a lot but requires higher margin than /ES to trade (EURO 10,000 for 1 contract). FDXM is the mini DAX just like /EZ is the mini S&P 500, and requires 1/5th the margin for DAX therefore it is traded more and therefore is more liquid than DAX. If you want to trade futures at night after work then the Asian futures are the best. In TOS FDXM is not available but the Asian Nikkei futures are available to trade /NKD.

The only problem with those futures is there are no market internals symbols available therefore more difficult to read. In /EZ market internals such as $VOLSPD, $ADD, $TICK are available. Knowing how to interpret market internals is the MOST IMPORTANT thing for any day trader and should be the FIRST thing they learn in the beginning.



Edited 1 time(s). Last edit at 02/06/2016 09:10AM by tanman.
Re: Fun with ThinkScript
February 06, 2016 01:29PM
Tanman, thanks for explaining. Helps me better understand why that author was saying that. Good point you also made, in my opinion, "Knowing how to interpret market internals is the MOST IMPORTANT thing for any day trader and should be the FIRST thing they learn in the beginning." So, seems like for /EZ you could code a proxy for at least Buy/Sell ratio? Wouldn't be perfect but usable. The others- ADX and put-call ratio- are obviously problematic. When I was doing stocks, a quick-and-dirty workaround we used was the L2s as a tell on internals (bearish if Ask sizes got too big too fast, bullish if Bid sizes got too big too fast; steady growth on Ask bullish, steady growth on Bid bearish). But I "assume" there's no L2 for Futures? I'm not disagreeing with you on use of internals (best if they all agree on overbought/oversold) but we both know that, for example, p/c ratio can stay irrational far longer than anybody can remain solvent. Thanks again for ALL the insight.


Gamblers roll the dice; Traders load the dice.
Re: Fun with ThinkScript
February 06, 2016 02:20PM
I'm thinking something like the below for BUY/SELL ratio. Crosses of 5ma and zeroline=0 on /ES are quite helpful on both H1 and 10m but better triggers might be found.
declare lower;
def myvolume = volume;
def negbar = close<open;
def posbar = close>open;
def buysellR = volume*hlc3;
def mycalc = if negbar then buysellR*(-1) else if posbar then buysellR else double.nan;
plot myplot = mycalc[1]/mycalc;
input AVG1 = 5;
plot myplotAVG = average(myplot,AVG1);
plot zeroline = 0;
Baffled1 comments on proxy code: "Kurtosis? What, me worry? I don't trade the stinkin' emini." Laughs. "Kurtosis- that's a disease, right?"
EDIT- FIXED CODE TO SET DEFAULT INPUT AS 5 NOT 1.

Gamblers roll the dice; Traders load the dice.



Edited 1 time(s). Last edit at 02/06/2016 02:30PM by baffled1.
Re: Fun with ThinkScript
February 06, 2016 03:25PM
Tanman
Thank you for your response and support

Is there any code can be add to MACD codes on TOS that give me two colors as below

MACD = can be changed when the direction goes up
and give the other signal color when the direction becomes down please

Hanimusa
Re: Fun with ThinkScript
February 06, 2016 04:53PM
hanimusa Wrote:
-------------------------------------------------------
> Tanman
> Thank you for your response and support
>
> Is there any code can be add to MACD codes on TOS
> that give me two colors as below
>
> MACD = can be changed when the direction goes up
> and give the other signal color when the direction
> becomes down please
>
> Hanimusa
==========================================================
Hanimusa, you can change the colors and line thicknesses on this from the Chart's EditStudy screen.
EDIT- You only wanted 2 colors but I gave you 4. You'll see the advantage. But set it any colors you want on EditStudy.
declare lower;

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

plot Value = MovingAverage(averageType, close, fastLength) - MovingAverage(averageType, close, slowLength);
plot Avg = MovingAverage(averageType, Value, MACDLength);

plot Diff = Value - Avg;
plot ZeroLine = 0;

Value.SetDefaultColor(GetColor(1));

Value.DefineColor("Positive and Up", Color.BLUE);
Value.DefineColor("Positive and Down", Color.PLUM);
Value.DefineColor("Negative and Down", Color.RED);
Value.DefineColor("Negative and Up", Color.YELLOW);
Value.AssignValueColor(if Value >= 0 then if Value > Value[1] then Value.color("Positive and Up" )
 else Value.color("Positive and Down" ) else if Value < Value[1] then Value.color("Negative and Down" )
 else Value.color("Negative and Up" ));
ZeroLine.SetDefaultColor(GetColor(0));

Avg.SetDefaultColor(GetColor(8));
Diff.SetDefaultColor(GetColor(5));
Diff.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
Diff.SetLineWeight(3);
Diff.DefineColor("Positive and Up", Color.GREEN);
Diff.DefineColor("Positive and Down", Color.DARK_GREEN);
Diff.DefineColor("Negative and Down", Color.RED);
Diff.DefineColor("Negative and Up", Color.DARK_RED);
Diff.AssignValueColor(if Diff >= 0 then if Diff > Diff[1] then Diff.color("Positive and Up" )
 else Diff.color("Positive and Down" ) else if Diff < Diff[1] then Diff.color("Negative and Down" )
 else Diff.color("Negative and Up" ));
ZeroLine.SetDefaultColor(GetColor(0));


Gamblers roll the dice; Traders load the dice.



Edited 1 time(s). Last edit at 02/06/2016 05:07PM by baffled1.
Swing low and higher low
February 06, 2016 08:56PM
Hello Robert,

I need to plot the lowest swing within last 55 bars and then the subsequent swing high and the swing low to see if that swing low is a higher low to the lowest swing. How do I do that? Would highly appreciate of you could post the code for the same

Thanks

SKumar
Re: Fun with ThinkScript
February 07, 2016 03:46PM
Hello Robert,

I think, I posted this in a wrong thread...so reposting it here

I need to plot the lowest swing within last 55 bars and then the subsequent swing high and the swing low to see if that recent swing low is a higher low to the lowest swing. How do I do that? Would highly appreciate of you could post the code for the same and I am willing to tip for that

Thanks

SKumar
Re: Fun with ThinkScript
February 07, 2016 09:07PM
if you read the thread, in which is demonstrated much genius, you'll find out that robert is on hiatus, length of time, unknown.
Re: Fun with ThinkScript
February 08, 2016 01:13PM
Anyone know how to tell the Think-script for an indicator to "round" to the nearest number so it will only show two numbers after the decimal point?

For example my MACD shows the bubble with the data in it, but it shows 4 or 5 digits after the decimal point. I'd like to use it for all my indicators if possible.
Re: Fun with ThinkScript
February 08, 2016 02:07PM
RichieRick Wrote:
-------------------------------------------------------
> Anyone know how to tell the Think-script for an
> indicator to "round" to the nearest number so it
> will only show two numbers after the decimal
> point?
>
> For example my MACD shows the bubble with the data
> in it, but it shows 4 or 5 digits after the
> decimal point. I'd like to use it for all my
> indicators if possible.
================================================
RichieRick, your new MACD code is below (mods in BOLD BLUE) . An article is here with some other approaches:
https://tlc.thinkorswim.com/center/charting/thinkscript/tutorials/Chapter-3---Mathematical-Functions.html
declare lower;
input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input averageType = AverageType.EXPONENTIAL;
plot Value = round(MovingAverage(averageType, close, fastLength)
 - MovingAverage(averageType, close, slowLength),2);
plot Avg = MovingAverage(averageType, Value, MACDLength);
plot Diff = Value - Avg;
plot ZeroLine = 0;
Value.SetDefaultColor(GetColor(1));
Avg.SetDefaultColor(GetColor(8));
Diff.SetDefaultColor(GetColor(5));
Diff.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
Diff.SetLineWeight(3);
Diff.DefineColor("Positive and Up", Color.GREEN);
Diff.DefineColor("Positive and Down", Color.DARK_GREEN);
Diff.DefineColor("Negative and Down", Color.RED);
Diff.DefineColor("Negative and Up", Color.DARK_RED);
Diff.AssignValueColor(if Diff >= 0 then if Diff > Diff[1] then Diff.color("Positive and Up" )
 else Diff.color("Positive and Down" ) else if Diff < Diff[1] then Diff.color("Negative and Down" )
 else Diff.color("Negative and Up" ));
ZeroLine.SetDefaultColor(GetColor(0));

Gamblers roll the dice; Traders load the dice.
Re: Fun with ThinkScript
February 08, 2016 02:24PM
RichieRick, you asked "how to" and I didn't really answer that, though it's obvious from the code.
1. Identify the value that you want rounded.
2. At the start of it, type ROUND, then a PAREN.
3. At the end of it, COMMA, then the NUMBER of places to right of decimal you want it rounded to, then a PAREN, and then the semicolon.

Gamblers roll the dice; Traders load the dice.
Re: Fun with ThinkScript
February 09, 2016 09:23AM
Awesome!!!!

It works great for that. Thanks a bunch for squaring me away smiling smiley

I used it on the Stoch RSI, and on the DMI, only problem with those two is, it won't accept the ,2) part of it. It's doesn't seem to matter much on the Stoch RSI, but on the DMI it round the numbers down to a single digit with no decimal. For example on the DMI it rounded the number 8.1588 to 8 even instead of 8.16. Not sure how to resolve that.


NEVERMIND!!!!
I got it figured out. I put the "Round" in the wrong place. It's sorted out now, and show the proper numbers on the DMI and Stoch RSI.


Thanks again for getting me squared away.
R



Edited 1 time(s). Last edit at 02/09/2016 09:46AM by RichieRick.
Re: Fun with ThinkScript
February 09, 2016 11:35AM
Hello Guys,

I've asked previously if anyone could help me incorporate Robert Reversal indicator into a watchlist nobody was able to respond.
Would like to know if someone could help me with a column watchlist with color change to red, pink and the yellow for the EntryCandle .

Thank you guys
Re: Fun with ThinkScript
February 09, 2016 01:04PM
RichieRick Wrote:
-------------------------------------------------------
> Awesome!!!!
...I used it on the Stoch RSI, and on the DMI, only
> problem with those two is, it won't accept the ,2)
> part of it.... Not sure how to resolve
> that.
... NEVERMIND!!!!
> I got it figured out....
===================================
ROFLMBO spinning smiley sticking its tongue out . Anytime, RR.

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

Click here to login