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
January 16, 2019 03:40PM
Anyone here know how to write a script that will show a stock by individual yearly quarters?

Example Jan-Mar for 2015
Moving from lower study to price chart
January 20, 2019 06:24PM
Hey everyone, new member here. I joined to ask if it's possible to move something from the below study (where RSI usually sits at) and move an indicator from the bottom to the actual chart with prices in it?

I'm trying to move this indicator https://usethinkscript.com/d/47-volume-spread-analysis-vsa-reversal-indicator-for-thinkorswim to the upper study but I'm not sure which settings to activate that.
Need help for syntax errror (TOS script)
January 19, 2019 11:01PM
I'm having trouble making my slope line change color (syntax error).

The color line for SLOPE 3-period ahead (not back) won't turn to green if the current price is greater than the SLOPE 3-consecutive period ahead (not back), it only shows the current SLOPE is green when the current price is greater than the current SLOPE. (e.g.: current price is $10, which is greater than the current SLOPE at $8 then the color of current SLOPE line is green, yet the SLOPE for 1~3 period later(ahead not back) is under $8 but they don't turn green.

Can you help me make the color of SLOPE 3-period later (1st, 2nd, 3rd-period) turn green(or red) if the current price is greater(less) than the SLOPE 3-period ahead (later)? Thanks a million!

if current price is greater than slope [0] (which is the present slope) - turn green otherwise red
if current price is greater than slope [-1] (which is the slope 1-bar future) - turn green otherwise red
if current price is greater than slope [-2] (which is the slope 2-bar future) - turn green otherwise red
if current price is greater than slope [-3] (which is the slope 3-bar future) - turn green otherwise red
------------------------------------------------------------------------------------------------------------------------
input price=close;

declare upper;
plot SLOPE = 2*close[3] - close[9];
SLOPE.AssignValueColor(if price[0] > SLOPE[0], price[0]>SLOPE[-1], price[0]> SLOPE[-2], price[0] SLOPE[-3] then CreateColor(0, 153, 0) else CreateColor(153, 0, 51));
Moving Avg crossover code needed
January 19, 2019 10:40AM
Need help!!

Looking for a two part signal that involves the crossing of a moving avg twice before firing

example: (1) price crosses "below" ma50. Record this fact and the low of that candle. (2) After price has crossed "above" ma50 and then recross down, the signal would come when price breaches the "low" of the first cross (1).

Reversed is first cross is "above" ma50

Thanks for any help!!
Re: Moving from lower study to price chart
January 21, 2019 05:14AM
Pilotui

You only need to delete the declaration:
declare lower;

But you will not see the study as you like it. The reason is that you will have conflicting units: the price of the symbol you are looking at, and the units of your indicator which are completely different.
Re: I'm having trouble making my slope line change color
January 21, 2019 05:31AM
Sumihiko

I understand you are learning TOS scripting... so a piece of general advice:

try to define your conditions one by one to help in your thought process:

input price=close; 

declare upper; 
plot SLOPE = 2*close[3] - close[9]; 


def Cond1= price>slope; 
def Cond2= price>slope[1]; 
def Cond3= price>slope[2]; 
def Cond4= price>slope[3]; 

#slope will be green when ALL the conditions are TRUE otherwise it will be RED
slope.assignvaluecolor( if Cond1 AND cond2 AND cond3 AND cond4 then color.green else color.red); # AND "joins" the conditions

Re: I'm having trouble making my slope line change color
January 23, 2019 02:39AM
Thanks a million for the advice...

Actually, it didn't work on the future price.

price > slope [-1] → current (today's) price is greater than tomorrow's slope
price > slope [-2] → current (today's) price is greater than the day after tomorrow's slope.

-------------------------------------------------------------------------------------------------
I was looking for the current (today's) price is greater than tomorrow's slope.



Edited 1 time(s). Last edit at 01/23/2019 02:49AM by sumihiko.
Re: I'm having trouble making my slope line change color
January 25, 2019 07:19AM
Any indicator that "works" with future candles is a re-paint type, therefore their signals are unreliable. Shouldn't be nice to know the future?

If you add, using AND, "future" conditions to past conditions obviously you will not get any signals as the test of future candles will be false. In other words, ask yourself, what is the program to tell you to the question: is today's price higher than the slope tomorrow (or after tomorrow) ?



Edited 1 time(s). Last edit at 01/25/2019 07:22AM by rigel.
Re: Moving average watchlist with colors and trend
January 27, 2019 11:01PM
Hi Everyone,

I have been following this forum for a couple weeks. Some amazing things in here.
I decided to try some Think Scripting myself.

I'm basically trying to have a dynamic watchlist that shows in which direction the 200SMA is pointing, trending, for various time-frames.

Here is the watchlist I've built so far and the code. Nothing fancy.

But I need help, please?

1. I would like to have the 200SMA price show in the same column as the column that show it is up trending in green, or red for down trending. Currently it colors the column, but only shows a 1 for up, or 0 for down trend. I tried adding the 200SMA script but it keeps getting an error:


#200SMA price Column

plot simple = simpleMovingAvg(close,200);


Here is the color column for up or down trend:

plot trendup = SimpleMovingAvg(length = 200) > SimpleMovingAvg(length = 200)[1];

trendup.AssignValueColor(if trendup then Color.UPTICK else Color.DOWNTICK);
AssignBackgroundColor(if trendup then Color.UPTICK else Color.DOWNTICK);


2. Anyone know of a script or code to have all of the 200SMA I have added together? Example if there were 5 columns, each with the 200SMA on a different time-frame, and 2 where up trending it would show 40% and color it say orange.

Any help is much appreciated.

Thank you all
Re: Fun with ThinkScript
January 27, 2019 11:12PM
I'm trying to convert a Tradestation ELS into Thinkscript. Here is the Tradestation code:

inputs:
Length(20);

vars:
srsi(0),
srsin(0),
k(0);

srsin = 0;
For k = 0 to Length - 1
Begin
Value1 = absvalue(close[k] - open[k];
Value2 = absvalue(high[k] - low[k];
If value2 = 0 then value3 = 0
Else
Value3 = value1/value2;
srsin = srsin + value3;
End;

srsi = srsin/length;

plot1(0.38);
plot2(srsi);
plot3(-0.38);
plot4(0);
If srsi > 0 then Setplotcolor(2,green);
If srsi < 0 then Setplotcolor(2,red);
If srsi >= -0.05 and srsi <= 0.05 then Setplotcolor(2,yellow);

Setplotcolor(1,darkred);
Setplotcolor(3,darkgreen);
Setplotcolor(4,yellow);


Here is my attempt at converting it to Thinkscript:

declare lower;

input Length = 20;

def srsi = 0;
def srsin = 0;
def k = 0;

plot iteration = fold k = 0 to Length - 1;

def Value1 = absvalue(close[k] - open[k]);
def Value2 = absvalue(high[k] - low[k]);
def Value = if value2 == 0 then value3 == 0 else value3 == value1 / value2;

def srsin = srsin + value3;

def srsi = srsin / Length;

plot line1 = 0.38;
line1.SetDefaultColor(Color.DARK_RED);
line1.HideTitle();
line1.HideBubble();

plot line2 = srsi;
line2.AssignValueColor(if srsi > 0 then Color.GREEN 
else if srsi < 0 then Color.RED 
else if srsi >= -0.05 and srsi <= 0.05 then Color.YELLOW 
else Color.GRAY);
line2.SetLineWeight(2);
line2.HideTitle();

plot line3 = -0.38;
line3.SetDefaultColor(Color.DARK_GREEN);
line3.HideTitle();
line3.HideBubble();

plot ZeroLine = 0;
ZeroLine.SetDefaultColor(Color.ORANGE);
ZeroLine.HideTitle();
ZeroLine.HideBubble();

The Thinkscript compiler says the fold statement is invalid. This is the first time I've attempted to work with a fold statement.

Any assistance would be greatly appreciated. Thanks in advance...
error
January 28, 2019 08:32AM
deleted



Edited 1 time(s). Last edit at 01/28/2019 09:22AM by rigel.
Watchlist with price and trend of simplemoving average
January 27, 2019 10:59PM
Hi Everyone,

I have been following this forum for a couple weeks. Some amazing things in here.
I decided to try some Think Scripting myself.

I'm basically trying to have a dynamic watchlist that shows in which direction the 200SMA is pointing, trending, for various time-frames.

Here is the watchlist I've built so far and the code. Nothing fancy.

But I need help, please?

1. I would like to have the 200SMA price show in the same column as the column that show it is up trending in green, or red for down trending. Currently it colors the column, but only shows a 1 for up, or 0 for down trend. I tried adding the 200SMA script but it keeps getting an error:


#200SMA price Column

plot simple = simpleMovingAvg(close,200);


Here is the color column for up or down trend:

plot trendup = SimpleMovingAvg(length = 200) > SimpleMovingAvg(length = 200)[1];

trendup.AssignValueColor(if trendup then Color.UPTICK else Color.DOWNTICK);
AssignBackgroundColor(if trendup then Color.UPTICK else Color.DOWNTICK);


2. Anyone know of a script or code to have all of the 200SMA I have added together? Example if there were 5 columns, each with the 200SMA on a different time-frame, and 2 where up trending it would show 40% and color it say orange.

Any help is much appreciated.

Thank you all
Re: Convertion of Tradestation ELS
January 28, 2019 09:19AM
netarchitech try this

# Convertion of Tradestation ELS into Thinkscript
# By Rigel , Jan 2019

declare lower;

input Length = 20;


def srsi=(fold K=0 to length-1 with srsim=0 do (if absvalue(high[k] - low[k])==0 then 0 else absvalue(close[k] - open[k])/ absvalue(high[k] - low[k]))+srsim)/Length;


plot line1 = 0.38;
line1.SetDefaultColor(Color.DARK_RED);
line1.HideTitle();
line1.HideBubble();

plot line2 = srsi;
line2.AssignValueColor(if srsi > 0 then Color.GREEN 
else if srsi < 0 then Color.RED 
else if srsi >= -0.05 and srsi <= 0.05 then Color.YELLOW 
else Color.GRAY);
line2.SetLineWeight(2);
line2.HideTitle();

plot line3 = -0.38;
line3.SetDefaultColor(Color.DARK_GREEN);
line3.HideTitle();
line3.HideBubble();

plot ZeroLine = 0;
ZeroLine.SetDefaultColor(Color.ORANGE);
ZeroLine.HideTitle();
ZeroLine.HideBubble();
Re: Watchlist with price and trend of simplemoving average
January 28, 2019 09:22AM
For your first question, try this


#200SMA price Column 

plot simple = simpleMovingAvg(close,200); 

def trendup = simple > simple[1]; 

AssignBackgroundColor(if trendup then Color.UPTICK else Color.DOWNTICK);

I don't quite understand your second question
Re: Watchlist with price and trend of simplemoving average
January 29, 2019 12:06AM
@ Rigel,

Thank you very much. It worked and did exactly what I needed.

Any way to change the color of the price, from white to say Black? Been trying but addLabel or assignpricecolor didn't do it.

In regards to my 2nd question:

I basically want to assign a % out of 100 to a column to indicate that the 200SMA is trending up on a specific Time Frames. Per example:

If we have 5 columns and each shows the value of the 200SMA on a different Time frame, say 1min, 3min, 5min, 10min, and 15min, how many of these Time Frames is the 200SMA trending up? Then assign a score of 3, or 3 out of 5, or 60% for 3 out of 5.

Hope that clarifies it.

Again, thank you for any help in advance. Very much appreciated.
Re: Conversion of Tradestation ELS
January 29, 2019 02:42AM
Thank you, Rigel... I really appreciate your efforts to assist me... Unfortunately, despite numerous attempts to successfully convert the Tradestation code, it still doesn't work properly.

With that said, I am thankful to have made some progress. I plugged in the code you forwarded and received no errors from the compiler, but the output was not what it should of been.

As a result, I scoured the code looking for something that may have possibly been overlooked, made some changes, but the output remained pretty much the same.



declare lower;

input Length = 20;

def srsin = 0;

plot srsi = (fold k = 0 to length - 1 do (if absvalue(high[k] - low[k]) == 0 then 0 else srsin + absvalue(close[k] - open[k]) / absvalue(high[k] - low[k]) + srsin / Length));
srsi.SetLineWeight(2);
srsi.HideTitle();
srsi.AssignValueColor(if srsi > 0 then Color.GREEN else if srsi < 0 then Color.RED else if srsi >= -0.05 and srsi <= 0.05 then Color.YELLOW else Color.GRAY);

plot line1 = 0.38;
line1.SetDefaultColor(Color.DARK_RED);
line1.HideTitle();
line1.HideBubble();

plot line3 = -0.38;
line3.SetDefaultColor(Color.DARK_GREEN);
line3.HideTitle();
line3.HideBubble();

plot ZeroLine = 0;
ZeroLine.SetDefaultColor(Color.ORANGE);
ZeroLine.HideTitle();
ZeroLine.HideBubble();


Not to be deterred, I then went back to the drawing board and proceeded to "bare metal" rewrite the code from scratch. I must admit, I'm not a coder or a scripter per se, but I was buoyed by the experience, if not the results. Unfortunately, this time around while I received no errors from the compiler, there was no output on the screen either sad smiley



declare lower;

input length = 20;

def o = open;
def c = close;
def h = high;
def l = low;
def k = 0;

def v1 = (absvalue(c[k] - o[k]));
def v2 = (absvalue(h[k] - l[k]));
def v3 = (absvalue(c[k] - o[k])) / (absvalue(h[k] - l[k]));

def data = (fold i = 0 to length - 1 with s do s + (if v2 == 0 then v3 == 0 else v3));

def srsin = srsin + v3;

plot srsi = srsin / length;
srsi.SetLineWeight(2);
srsi.HideTitle();
srsi.AssignValueColor(if srsi > 0 then Color.GREEN else if srsi < 0 then Color.RED else if srsi >= -0.05 and srsi <= 0.05 then Color.YELLOW else Color.GRAY);

plot line1 = 0.38;
line1.SetDefaultColor(Color.DARK_RED);
line1.HideTitle();
line1.HideBubble();

plot line3 = -0.38;
line3.SetDefaultColor(Color.DARK_GREEN);
line3.HideTitle();
line3.HideBubble();

plot ZeroLine = 0;
ZeroLine.SetDefaultColor(Color.ORANGE);
ZeroLine.HideTitle();
ZeroLine.HideBubble();

Any thoughts? Looking forward to your reply...

Thanks again!



Edited 1 time(s). Last edit at 01/29/2019 02:47AM by netarchitech.
Re: Watchlist with price and trend of simplemoving average
January 29, 2019 06:20AM
Neoisback
To change the color of the values add this at the end:

use the colors you like most
simple.assignValueColor(if trendup then color.black else color.white );

For your second question, each of those columns would be independent, so there is no way to check across columns to assign the score you like
Re: Conversion of Tradestation ELS
January 29, 2019 06:27AM
Netarchitec

Look for the help of Fold statement, and see the examples.

Your fold instruction has two problems: It has incorrect syntax and anyway, you are not using the variable data in the rest of your calculation.

What is the name of the indicator you are trying to convert ? is there a link to the formula?



Edited 1 time(s). Last edit at 01/29/2019 06:34AM by rigel.
Re: Conversion of Tradestation ELS
January 29, 2019 08:04AM
Hi Rigel,

I have been studying the Fold help file intensively for the last couple of days. I am well acquainted with the examples, but I continue to refer to it on an ongoing basis.

As for the two problems with my current code, apparently I foolishly thought the syntax issue would be flagged by the compiler and I will look further into the variable data situation.

The name of the file is SRSI (Sell Relative Strength Index). There is no link to a formula as the conversion file I posted is the only information provided from the article I read in a trading magazine.

I think the problem might possibly be due to the author supplying a ELS (EasyLanguage Storage file) instead of a ELD (EasyLanguage Document file), but I'm just guessing based on the research I've done so far...I am far from proficient when it comes to TradeStation or fold statements...

Thanks again for your time and attention to this matter. I really do appreciate it smiling smiley
Re: Conversion of Tradestation ELS
January 29, 2019 10:45AM
try this:

#intepreted by vimes
declare lower;
input len = 20;

def srsin;
def value1 = close - open; #code in the article had the absvalue here but then it would never oscillate around 0
def value2 = high - low;
def value3 = if value2 == 0 then 0
else value1 / value2;

srsin = Sum(value3, len);
plot srsi = srsin / len;
plot l1 = 0.38 ; #limits from the article, seem arbitrary?
plot l2 = -0.38; #limits from the article, seem arbitrary?
plot zline = 0;

srsi.assignValueColor(if srsi > 0.05 then Color.GREEN
else if srsi<-0.05 then color.red
else color.yellow);
l1.SetDefaultColor(Color.DARK_RED);
l2.SetDefaultColor(Color.DARK_GREEN);
zline.SetDefaultColor(Color.YELLOW);
Re: Watchlist with price and trend of simplemoving average
January 29, 2019 02:59PM
@ Rigel,

Thank you! This worked well. Thank you

As for this: "For your second question, each of those columns would be independent, so there is no way to check across columns to assign the score you like", could it not all be done in one script, and show in just one column?

Do the calculation in one script, and then plot the % result in one single column?

Thank you again!
Re: Conversion of Tradestation ELS
January 29, 2019 10:02PM
@jakesdad:

#intepreted by vimes
declare lower;
input len = 7; #changed from the setting of 20 per the instructions of the original author

def srsin;
def value1 = close - open; #code in the article had the absvalue here but then it would never oscillate around 0
def value2 = high - low;
def value3 = if value2 == 0 then 0
else value1 / value2;

srsin = Sum(value3, len);
plot srsi = srsin / len;
plot l1 = 0.38 ; #limits from the article, seem arbitrary?
plot l2 = -0.38; #limits from the article, seem arbitrary?
plot zline = 0;

srsi.assignValueColor(if srsi > 0.05 then Color.GREEN
else if srsi<-0.05 then color.red
else color.yellow);
l1.SetDefaultColor(Color.DARK_RED);
l2.SetDefaultColor(Color.DARK_GREEN);
zline.SetDefaultColor(Color.YELLOW);

That worked! Funny how the Fold statement wasn't even necessary smiling smiley

FYI to anyone planning on taking the indicator out for a test drive, I have contacted the original author and he informed me that the length (len) property should be set to 7 instead of 20.

Thank you for your time and critical contribution to this matter, @jakesdad. I really appreciate it!
Re: Watchlist with price and trend of simplemoving average
January 30, 2019 09:00AM
Quote
Neoisback
could it not all be done in one script, and show in just one column?

Do the calculation in one script, and then plot the % result in one single column?

Unfortunately not, because the scripts for columns need to specify only one working time (yellow arrow), so your internal script in multi-times wouldn't work
Re: Fun with ThinkScript
January 30, 2019 10:56AM
http://www.screencast.com/t/r9cCQh5i4Greetings guys and girls I purchased the champion reversal indicator from thinkscript and I would like to know if someone could code an alert for the arrow that appears prior to entry signal. It would be greatly appreciated.




Edited 1 time(s). Last edit at 01/30/2019 10:58AM by trade2winz.
Re:alert for indicator
January 30, 2019 12:13PM
Sent you a PM



Edited 1 time(s). Last edit at 01/30/2019 12:14PM by rigel.
Re: Re:alert for indicator
February 01, 2019 10:58AM
Hi guys,

I'm working a script, and these two lines are butting heads.


def RSI = reference RSI (price = RSI_price, length = RSI_length);

def RSI = 50 * (ChgRatio + 1);


I understand that both lines are attempting to define RSI, but I'm not sure how to resolve this.

Can anyone help me understand the second line more clearly? What does "50 * (ChgRatio + 1)" tell me?


Thanks
R
Re: Re:alert for indicator
February 01, 2019 11:17AM
@RichieRick,

Try this:

def averageTypeRSI = AverageType.WILDERS;
def lengthRSI = 14;

def NetChgAvg = MovingAverage(averageTypeRSI, close - close[1], lengthRSI);
def TotChgAvg = MovingAverage(averageTypeRSI, AbsValue(close - close[1]), lengthRSI);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;

plot RSI = 50 * (ChgRatio + 1);

Hope this helps smiling smiley
Re: Re:alert for indicator
February 01, 2019 12:21PM
Awesome!!! Thanks for posting that.

I want to understand what "def RSI = 50 * (ChgRatio + 1);" is doing and what it's telling me. I don't even know if I even need it.

I have to two separate scripts, and both are using RSI as part of the code. I'd like to combine them into one script that I can use in a Watch list column. I want to use StochRSI, but I'm not even sure the line of code above is even part of that. smiling smiley

Boy, I hope that made sense. smiling smiley hehehe.

R
double.NaN alternative
February 01, 2019 12:24PM
Is there a way to combine these 2 statements so N/A doesn't show up in the label?


AddLabel(yes,if buy then ((close[length]-open[length])*.25)+open[length] else double.NaN,color.lighT_GREEN);
AddLabel(yes,if sell then ((open[length]-close[length])*.75)+close[length] else double.NaN,color.pink);
Re: double.NaN alternative
February 01, 2019 12:58PM
Have you tried " " in place of double.NaN???

AddLabel(yes,if buy then ((close[length]-open[length])*.25)+open[length] else " ",color.lighT_GREEN);
AddLabel(yes,if sell then ((open[length]-close[length])*.75)+close[length] else " ",color.pink);

Keep in mind that there is a blank space between the " marks. smiling smiley


Or just have a space there instead of the " " marks. Sometimes that works for me depending on the complexity of the script.



Edited 1 time(s). Last edit at 02/01/2019 12:59PM by RichieRick.
Sorry, only registered users may post in this forum.

Click here to login