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
June 25, 2018 10:26PM
Does anyone know how to alter this script?

def storeLow = if X then open else storeLow[1];

Each time X occurs storeLow remembers the new value and the previous value is lost. Does anyone know how to write this so I can get storeLow to hold the value of the previous occurrence of X (that may have occurred 10 candles back) instead of the present value of X overriding the previous?
(else previous storeLow[X];
Thanks, JM.



Edited 4 time(s). Last edit at 06/26/2018 09:50AM by Ralph53.
Symbol volume compared to index volume
June 26, 2018 02:38PM
I have a plot of symbol price verses the SPX in TOS. I cannot figure out how to do the same for volume. I want to plot the volume of a symbol correlated to the chart symbol being viewed. If the chart is of GE then I want the lower study to show GE volume plot line and the SPX volume plot line on the same lower study. The two lines plotted could then be compared. If the chart symbol is changed to say F then the study should show F compared to SPX.
Hope this makes sense. If this is already available somewhere in TOS please point me to it.
Re: Symbol volume compared to index volume
June 26, 2018 04:28PM
trying to trade Wrote:
-------------------------------------------------------
Quote

I have a plot of symbol price verses the SPX in TOS.

As far as I know TOS doesn't plot volume of indexes (SPX,NDX,RUT,etc)



Edited 1 time(s). Last edit at 06/26/2018 04:29PM by rigel.
Re: Fun with ThinkScript
June 26, 2018 04:38PM
Ralph53
The question is not clear. the line:
def storeLow = if X then open else storeLow[1];

Does not store the value of X . It checks the condition X if positive then assign to storeLow the Open value of the bar, otherwise assign to storeLow its previous value.



Edited 1 time(s). Last edit at 06/26/2018 04:39PM by rigel.
Re: Fun with ThinkScript
June 26, 2018 06:31PM
My Thinkscript count formula breaks on the count condition. I want to count the number of times a ticker is above its 8EMA and then calculate the average of the distance of the price from the EMA

Here's the study:

input length = 8;
plot Ema = ExpAverage(close, length);
def crossing = close > ema;

##Method 1
def postCrossingRange = if crossing then 1 else 0;
def sumOfRanges = totalSum(postCrossingRange);
AddLabel(yes, Concat("Count: ", sumOfRanges), Color.ORANGE);

plot count = if crossing then sumOfRanges else double.nan;
Count.SetPaintingStrategy(PaintingStrategy.VALUES_ABOVE);

##Method 2
def count1 = if crossing then count1[1] + 1 else count1[1];
AddLabel(yes, Concat("Count: ", count1), Color.LIME);

The study returns a larger number for the count as the first bar that has a close > EMA starts at a higher number instead of 1. I tried a second method of counting but still get the incorrect results.

Thanks
Re: Fun with ThinkScript
June 27, 2018 10:32PM
Hello everyone!
I have this strategy that shows me during the current bar when to open a short position, however buy to cover appears in the chart two bars after it has passed and the sign appears in the prior bar .

#--------------------------here's where the order begins and ends-----------------------------------------#


def SHORT = EMA[-1] crosses below midline;
AddOrder(OrderType.SELL_TO_OPEN, short,name = "START", tradeSize = investment);

def COVER = [-3] crosses above midline ;
AddOrder(OrderType.BUY_TO_CLOSE,cover, name = "COVER" );

#------------------------------------------------------------------------------------------------------------------------#

NOTE:
when def COVER = [1 ] crosses above midline; it doesn't show any delays
I assume it has to do with how their script counts bars in order to show orders, but not sure

I'd appreciate if anyone could give me some of their time to figure this out since I'm new w TOSCRIPTS
have a good day
Re: Fun with ThinkScript
June 27, 2018 11:56PM
That's a good explanation.

If X triggers at 10:00 and again at 10:10 what I'm looking to do is calculate storelow(10:00) - storelow(10:10). Do you know a way to save the value of the previous(10:00) condition? Thanks again, JM.
Re: Fun with ThinkScript
June 28, 2018 11:08AM
Sherwin

your definition is not right:
def crossing = close > ema;

crossing will have all the times that the Close is higher than ema, not just when in crosses. That is why you are getting very big counts

You should use for example:

def crossing= close crosses above ema;
Re: Fun with ThinkScript
June 28, 2018 11:13AM
Quote
ralph53
If X triggers at 10:00 and again at 10:10 what I'm looking to do is calculate storelow(10:00) - storelow(10:10)

def mylows= if x then Low else milows[1];

def difference=mylows-mylows[1];
Re: Fun with ThinkScript
June 28, 2018 12:30PM
Thanks for your reply Rigel

I get what you’re saying and that will work. I should have used a better variable name than “crossing”.

Say I’m using the script on a 30 day: 1day chart, I really want the script to say “7 bars out of 22 bars closed above the 8 EMA” or “13 bars out of 22 bars closed between the 8 EMA and 21 EMA”. My script returns crazy results like “46 bars out of 22 bars closed above the 8 EMA”

rigel Wrote:
-------------------------------------------------------
> Sherwin
>
> your definition is not right:
>
> def crossing = close > ema;
>
>
> crossing will have all the times that the Close is
> higher than ema, not just when in crosses. That is
> why you are getting very big counts
>
> You should use for example:
>
> def crossing= close crosses above ema;
Re: Fun with ThinkScript
June 28, 2018 01:06PM
Thank you so much I'll test this.
Re: Fun with ThinkScript
June 28, 2018 03:06PM
alexguate

Please notice that when you use negative values like in EMA[-1] you are looking for one bar in the future... therefore your signals will appear late.

EMA[0] means ema at current bar.... EMA[1] is EMA of previous bar
Re: Fun with ThinkScript
June 28, 2018 09:26PM
Rigel,
Thank you for your quick response. Sadly I can't seem to make "[0] [1]" fit as I would like in my strategy, without having the signals trigger in the same bar or early in my position
Is there another wording in toscript to get the signals trigger by counting the amount of bars? Such as "If bar 4 greater than previous bar 3 then cover" and still managing to have EMA in the equation?

Thanks again
Re: Fun with ThinkScript
June 29, 2018 11:25PM
Not working yet. Anyone know how to write this;

rec ConditionValue = X - PreviousX;

There has to be a way to designate the previous occurrence in a sequence of random multiple occurrences. Thanks, JM.




Edited 1 time(s). Last edit at 06/30/2018 09:06PM by Ralph53.
Re: Fun with ThinkScript
July 01, 2018 07:45PM
Hi Rigel,

Could you please help me with script for this?

I am trying to make this script to alter bars' color to visualize trend condition as below:

1. Bullish trend is confirmed when price first close above 1 ATR from its EMA 21 and EMA 21 is rising.
After the bullish trend is confirmed, as long as EMA 21 is still rising, color all the bars green despite the closed price. If EMA 21 stop rising, switch color to dark blue.

2. Bearish trend is confirmed when price first close below 1 ATR from its EMA21 and EMA21 is falling.
After bearish trend is confirmed, as long as EMA21 is still falling, color all the bars red. If EMA 21 stop falling, switch color to cyan.

The problem with my script is that it only green those bars which meet the closed price condition, as soon as price closed within the ATR range, they all go blue.
I am a non-coder so I am really struggling with this.

Below is my whole script:

input length = 21;
input price = close;
input trueRangeAverageType = AverageType.WILDERS;

def EMA = ExpAverage(close, length);

def shift1 = 1 * MovingAverage(trueRangeAverageType, TrueRange(high, close, low), length);
def neg = EMA - shift1;
def pos = EMA + shift1;

def trigger = if(EMA > EMA[1] and close > pos) then 1
else if(EMA < EMA[1] and close < neg) then -1
else 0;


def bullish = trigger == 1 ;
def bearish = trigger == -1;

DefineGlobalColor("bullish", Color.UPTICK);
DefineGlobalColor("neutral", Color.BLUE);
DefineGlobalColor("bearish", Color.DOWNTICK);
AssignPriceColor( if bullish then GlobalColor("bullish"winking smiley else if bearish then GlobalColor("bearish"winking smiley else GlobalColor("neutral"winking smiley);
Re: Fun with ThinkScript
July 02, 2018 03:11AM
Hi

It is very difficult to help when the complete code is not posted. I have tried before to answer questions without code, only to hear that the answer didn't work.

There are several reasons for that, among them: the question is not clear enough or the logic of the code is wrong.

So I won't be able to answer questions when the code is missing.



Edited 3 time(s). Last edit at 07/14/2018 10:46AM by rigel.
Re: Fun with ThinkScript
July 02, 2018 07:17AM
Quote
solewind
The problem with my script is that it only green those bars which meet the closed price condition, as soon as price closed within the ATR range, they all go blue.

I see that in your logic there are two situations:

1.- Triggering of color change (you have that criteria right)
2.- Once the color change has been triggered, it needs to apply a second criteria (that is missing)

That is why the bars changed to blue again: because the first and only condition is not satisfied completely.

I think you need to have a second variable as a flag, that the 1st situation has happened then check the flag.
Give it a try smileys with beer

I'll come back later
Re: Fun with ThinkScript
July 02, 2018 04:11PM
solewind

Try this, I haven't tested it but seems doing the job with the flag

input length = 21;
input price = close;
input ATRs=1;
input trueRangeAverageType = AverageType.WILDERS;
def flag;

def EMA = ExpAverage(close, length);

def shift1 = ATRs * MovingAverage(trueRangeAverageType, TrueRange(high, close, low), length);
def neg = EMA - shift1;
def pos = EMA + shift1;
def TriggerBull= EMA > EMA[1] and close > pos; 
def TriggerBear=EMA < EMA[1] and close < neg;
#-----------------------------------------------------------
def trigger;
if TriggerBull then
 {
  trigger=1;
  flag = 1;
 }
 else if TriggerBear then
     {
      trigger=-1;
      flag = -1;
     }
  else 
  {
    trigger=0;
    flag = flag[1];#preserves previous flag value
  }
#-----------------------------------------------------------

def bullish = trigger== 1 or (flag==1 and  EMA > EMA[1]) ;
def bearish = trigger == -1 or (flag==-1 and EMA < EMA[1]);

DefineGlobalColor("bullish", Color.UPTICK);
DefineGlobalColor("neutral", Color.BLUE);
DefineGlobalColor("bearish", Color.DOWNTICK);
AssignPriceColor( if bullish then GlobalColor("bullish" ) else if bearish then GlobalColor("bearish" ) else GlobalColor("neutral" ));
 
Re: Fun with ThinkScript
July 02, 2018 09:59PM
Hi Rigel,

You are the best! The script works perfectly as I needed! Thank you so much for your time and kindness.

I have twist the color a bit for better visual, here is the chart on my screen.
Re: Fun with ThinkScript
July 06, 2018 05:50PM
Trying to create a stratghy to backtest but need help with a few parts. I'm gonna use aapl and Spy on this example. I can't code the part in bold red.

Buy Criteria
1. Aapl 5ema crosses 15 ema on 15 min Chart. (this was the easy part lol)
2. Spy currently trading above 10 ema on 15 min chart.

Exit Criteria
1. Aapl 5ema crosses below 15 ema on 15 minute chart (done)


input price = close;
input fastLength = 5;
input slowLength = 15;
input averageType = AverageType.EXPONENTIAL;

plot FastMA = MovingAverage(averageType, price, fastLength);
plot SlowMA = MovingAverage(averageType, price, slowLength);
FastMA.SetDefaultColor(GetColor(1));
SlowMA.SetDefaultColor(GetColor(2));

AddOrder(OrderType.BUY_AUTO, FastMA crosses above SlowMA, tickColor = GetColor(1), arrowColor = GetColor(1), name = "MovAvgTwoLinesStratLE"winking smiley;
AddOrder(OrderType.SELL_AUTO, FastMA crosses below SlowMA, tickColor = GetColor(2), arrowColor = GetColor(2), name = "MovAvgTwoLinesStratSE"winking smiley;




Edited 1 time(s). Last edit at 07/06/2018 05:51PM by Taz43.
ash
Re: Fun with ThinkScript
July 08, 2018 09:31AM
Hello,

I am new to thinkorswim.

I want to add a filter to scanning stock which is ([ATR(14)/close] greater than .0007).

Is this possible?

I would appreciate any help or advice you could provide.

Thank you!
ash
Re: Fun with ThinkScript
July 08, 2018 10:02AM
Hi,

I asked someone for some help and we came up with this:

input ATRLength = 14;

def ATR = Average(TrueRange(high, close, low), ATRLength);

def ATR2 = Average(TrueRange(high, close, low))/close;

def ATR3 = ATR2 is greater than .0007;

plot MinVolThresh=ATR3;

Not sure if that makes sense.

Would appreciate any input. Thank you!
TOS, get close or low of the highest high bar
July 09, 2018 05:39PM
Hi. Trying to script a scan in TOS. I'm able to get the highest high for past n number of days, need help in getting the close/low of this particular highest high. Thanks.
Re: Fun with ThinkScript
July 10, 2018 03:10AM
Quote
ash
I want to add a filter to scanning stock which is ([ATR(14)/close] greater than .0007).

Not sure what you are trying to achieve...

your code is:
plot pp=ATR(14)/close >0.0007;

Now, think about it, you are dividing the ATR by the price and that ratio has to be greater than a very small number... so practically any stock complies that, you will have a lot of "hits"

The other code somebody gave you makes no sense.
Re: Fun with ThinkScript
July 10, 2018 03:33AM
Quote
Taz
I can't code the part in bold red.

Buy Criteria
1. Aapl 5ema crosses 15 ema on 15 min Chart. (this was the easy part lol)
2. Spy currently trading above 10 ema on 15 min chart.

Assuming you are working with the chart of Aapl, then you need to have a reference to SPY to create the ema and the rest of your conditions.

First modify your input price=close; statement by this:
input price = FundamentalType.CLOSE;

Second add the reference and new variables

#---------------------------------
def secondaryPrice = Fundamental(price, "SPY" );
def emaspy = ExpAverage(secondaryPrice, 10);
def cond2 = secondaryPrice > emaspy;
#---------------------------------

Finally you will be able now to modify your buy order
AddOrder(OrderType.BUY_AUTO, FastMA crosses above SlowMA and cond2, tickcolor = GetColor(1), arrowcolor = GetColor(1), name = "MovAvgTwoLinesStratLE" );



Edited 2 time(s). Last edit at 07/10/2018 03:37AM by rigel.
Re: Fun with ThinkScript
July 10, 2018 11:57AM
Hello,

Anyone knows if there is a way to mark the price at which the "Impulse" study changes colors?

For example, if I add the Impulse study and it shows that the current bar is blue, is there any way to mark with a dot or something at which price it will become green and at which price red?

Any input is appreciated

Thank you!
Re: Fun with ThinkScript
July 11, 2018 06:48PM
Thank you! This works perfect!

I also did want to ask if we have any sort of an indicator on this thread that kind of signals when we are entering a consolidation phase or breaking out/down from one.
MVWAP
July 12, 2018 09:12PM
Looking to see if MVWAP is useful. Anyone experienced with it?
I wanted to play with it but cannot find it in ToS. Looks like you need to calculate the VWAP which ToS does then average it over say 10 days. As a 10 period MVWAP, they would simply wait for the first ten periods to elapse and then would average the first 10 VWAP calculations. This would provide the trader with the MVWAP that starts being plotted at period 10. To continue getting the MVWAP calculation, average the most recent 10 VWAP figures, include a new a VWAP from the most recent period and drop the VWAP from 11 periods earlier.
Would a aggregate()vwap 10 type of thinkscript do this?
Centered Moving Average (CMA)
July 13, 2018 10:11AM
Hoping someone might be able to help me out on a centered moving average. So the snippet portion of the code below is from a Hurst Channel indicator. Not interested in the Hurst but interested to know how the CMA is being plotted. It is unfortunately displaced 4 bars back. I have tried several different MA's to mimic the centered MA to no avail.

Could someone take a look and see if there is anyway to code how this CMA plots but yet not have it displaced. Again the code below is not the entire portion of the Hurst but I have included that portion that addresses the CMA.

Thank you.

#
# --- script begin ----
#

declare upper;

input price = hl2;
input length = 10;
input InnerValue = 1.6;
input OuterValue = 2.6;
input ExtremeValue = 4.2;
input showFlowPrice = NO;
input showPriceBar = YES;
input smooth = 1;

def displacement = (-length / 2) + 1;
def dPrice = price[displacement];

rec CMA = if !IsNaN(dPrice) then Average(dPrice, AbsValue(length)) else CMA[1] + (CMA[1] - CMA[2]);

plot CenteredMA = if !IsNaN(dPrice) then CMA else Double.NaN;
CenteredMA.SetDefaultColor(GetColor(1));
CenteredMA.SetLineWeight(2);
Re: Centered Moving Average (CMA)
July 14, 2018 10:34AM
to Chillic15

I guess the displacement is in the code not shown. So, you need too post the whole code.


To all:
I won't be able to answer questions when the code is missing.



Edited 1 time(s). Last edit at 07/14/2018 10:51AM by rigel.
Sorry, only registered users may post in this forum.

Click here to login