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 29, 2015 09:45AM
I understand it shifts at midnight. The problem here, if I understand correctly, is from 0000 to 0930 the daily close is (will be) that of 1600. During this period there’s no data if our main aggregation period is less than a day. Is it possible to replace the daily close during 0000 ~ 0930 with 1600[1] (previous day close)?
Re: Fun with ThinkScript
June 29, 2015 09:49AM
Quote

I understand it shifts at midnight. The problem here, if I understand correctly, is from 0000 to 0930 the daily close is (will be) that of 1600. During this period there’s no data if our main aggregation period is less than a day. Is it possible to replace the daily close during 0000 ~ 0930 with 1600[1] (previous day close)?

I did. See example no. 2 above. That's exactly what it's doing. Let's suppose we are looking at Monday and Tuesday. The first example above remembers Monday's 1600 close up until midnight (2400) on Monday, then it shifts at 0000 to Tuesday's 1600 close. The second example remembers Monday's 1600 close until the market opens at 0930 on Tuesday, then it shifts to Tuesday's 1600 close.

- robert


Professional ThinkorSwim indicators for the average Joe
Re: Fun with ThinkScript
June 29, 2015 10:50AM
Oops sorry I didn't know you had the second example. Must have scrolled down too fast. You're the man. However I don't need to plot the close. I need to implement this dictated close to daily aggregation period in calculation. I might be able to use SecondsTillTime condition to play around. I'll post any updated code if I figure out. Thanks again Robert.
Re: Fun with ThinkScript
June 29, 2015 04:45PM
Quote
Palmer
I posted this some time ago and now getting back to it. Basically, it is a scan to be done during pre-market hours that compares the current price close during pre-market to the close at 1459 (USA cst) the previous day. It looks for a 3% change to the upside during pre-market; a gap up. I'm missing symbols according to some on-line pre-market gainers and it is also including symbols that are significantly lower during pre-market compared to the previous day's close.

If you are only going to run the scan in the morning, before the market opens, why not just keep the script simple? You could give this a try.

input price = close; 
input percent_change = 3.00; 

def closing_price = close(period = "day" )[1]; 
def afterhours_percent_change = 100 * (price / closing_price - 1); 
def meet_scan_criteria = afterhours_percent_change >= percent_change; 

plot scan = meet_scan_criteria;

- robert


Professional ThinkorSwim indicators for the average Joe
Re: Fun with ThinkScript
June 29, 2015 05:16PM
Quote
KevinR
Robert,

How would I reference only the most recent datapoint?

You are going to need to use what's called a recursive variable. What that means is that the variable refers to itself. The syntax goes like this:

def MyVariable = if {some condition is met} then {assign some value} else MyVariable[1];

The important bits are in red. Notice that the end of the line uses the same variable name (MyVariable) that was defined at the beginning. In english, what that line says is this:

If the right condition is met then assign some value to MyVariable, otherwise let MyVariable be equal to what it was on the last bar ( MyVariable[1] ).

Recursive variables can be confusing and it took me a while to wrap my mind around them. Here's an example expanding on the code we discussed earlier.

# ----- define a valley as any point which is lower than the three preceding lows and the three following lows
def Valley = low < Lowest(low[1], 3) and low < Lowest(low[-3], 3);

# ----- mark each valley with an up arrow -----
plot ArrowUP = Valley;
     ArrowUP.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
     ArrowUP.SetDefaultColor(Color.WHITE);
     ArrowUP.SetLineWeight(4);

# ----- draw a straight line connecting each valley -----
plot line = if ArrowUP then low else Double.NaN;
     line.EnableApproximation();
     line.SetDefaultColor(Color.LIME);
     line.SetLineWeight(2);

# ----- remember the most recent valley's low price -----
def ValleyLow = if Valley then low else ValleyLow[1];

# ----- plot then ValleyLow as a quick visual means of verifying that the low value is being remembered
plot VisualCheck = ValleyLow;
     VisualCheck.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
     VisualCheck.SetDefaultColor(Color.DARK_ORANGE);

In this case, the recursive variable says to assign the low price to ValleyLow if the conditions for a Valley are met, otherwise just keep ValleyLow at the same value as it was before—ValleyLow[1].

So, every time a valley occurs, ValleyLow will be assigned the low price at that time, then ValleyLow will remember that price until the next valley occurs.



- robert


Professional ThinkorSwim indicators for the average Joe
Re: Fun with ThinkScript
June 29, 2015 06:37PM
Thanks!...Will run it in the morning.

Does TOS count every minute on the clock from 7:30am to the previous close at 1459 as one bar, count bars where there is only data for, or count bars according to what the actual market trading hours are?

If it counts every bar, per the clock, then 730am pre-market to yesterday's close at 1459 (CST) is 990 (or 991) bars back on a 1 minute chart. Just curious.
Re: Fun with ThinkScript
June 29, 2015 09:29PM
Thanks Robert, but I'm actually looking to do something like this:

def ValleyLow = if Valley then LowOfValley else MostRecentCandleOnChart;

That way I can sort of "preview" the next connecting line before a valley is formed and it finalizes.


Also, I made a set of "gapless" indicators, but is there a display it as a line from open-close, then a new line from the next days open-close. I do not want it to plot between the close and next days open. Currently I just have the plot set to "DOTS", since it does not connect them.

Thanks,



Edited 2 time(s). Last edit at 06/30/2015 10:50AM by KevinR.
Re: Fun with ThinkScript
June 30, 2015 03:25PM
Robert,
About the discontinuous daily data, it turned out it's not because of its closing price. I included secondstilltime(0930) condition but still didn't work. I plotted FullK_Daily itself without defining FullK_4H and it gave me continuous plot with no problem.
def FullK_Daily = StochasticFull(over_bought, over_sold, KPeriod, DPeriod, RSI_Daily, RSI_Daily, RSI_Daily, slowing_period, averageType).FullK;

However when I defined both FullK_Daily and FullK_4H and try to plot both, it gave me good FullK_4H plot but discontinuous FullK_Daily.
def FullK_4H = StochasticFull(over_bought, over_sold, KPeriod, DPeriod, RSI_4H, RSI_4H, RSI_4H, slowing_period, averageType).FullK;
def FullK_Daily = StochasticFull(over_bought, over_sold, KPeriod, DPeriod, RSI_Daily, RSI_Daily, RSI_Daily, slowing_period, averageType).FullK;

I think somewhere I confused it when using two periods to reference the same script but I'm not sure.
Then I redefined FullK_Daily by rewriting its code it gave me a continuous plot.
def FullK_Daily = wildersaverage((close(period = AggregationPeriod.day) - Lowest(low(period = AggregationPeriod.day), KPeriod)) / (Highest(high(period = AggregationPeriod.day), KPeriod) - Lowest(low(period = AggregationPeriod.day), KPeriod)) * 100, slowing_period);

The updated code is below.
declare lower;

# 4H StochRSI 40 Xover and Daily StochRSI trend

input RSI_length = 14;
input over_bought = 80;
input over_sold = 20;
input cross_over = 40;
input RSI_average_type = AverageType.WILDERS;
def RSI_price_4H = close (period = AggregationPeriod.FOUR_HOURS);
input KPeriod = 14;
input DPeriod = 3;
input slowing_period = 1;
input averageType = AverageType.SIMPLE;

def RSI_4H = RSI(price = RSI_price_4H, length = RSI_length, averageType = RSI_average_type);

def FullK_4H = StochasticFull(over_bought, over_sold, KPeriod, DPeriod, RSI_4H, RSI_4H, RSI_4H, slowing_period, averageType).FullK;

def FullK_Daily = wildersaverage((close(period = AggregationPeriod.day) - Lowest(low(period = AggregationPeriod.day), KPeriod)) / (Highest(high(period = AggregationPeriod.day), KPeriod) - Lowest(low(period = AggregationPeriod.day), KPeriod)) * 100, slowing_period);

def C1 = if FullK_4H > 40 then 3 else -3;
def C4 = if (FullK_Daily >= FullK_Daily[1] and FullK_Daily[1] > FullK_Daily[2]) then 1 else if (FullK_Daily <= FullK_Daily[1] and FullK_Daily[1] < FullK_Daily[2]) then -1 else 0;

# 4H and Daily MACD trend

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

def Value_4H = MovingAverage(averageType2, close(period = AggregationPeriod.FOUR_HOURS), fastLength) - MovingAverage(averageType2, close(period = AggregationPeriod.FOUR_HOURS), slowLength);
def Avg_4H = MovingAverage(averageType2, Value_4H, MACDLength);

def Diff_4H = Value_4H - Avg_4H;

def Value_Daily = MovingAverage(averageType2, close(period = AggregationPeriod.DAY), fastLength) - MovingAverage(averageType2, close(period = AggregationPeriod.DAY), slowLength);
def Avg_Daily = MovingAverage(averageType2, Value_Daily, MACDLength);

def Diff_Daily = Value_Daily - Avg_Daily;

def C2 = if (Value_4H >= Value_4H[1] and Value_4H[1] > Value_4H[2]) then 1 else if (Value_4H <= Value_4H[1] and Value_4H[1] < Value_4H[2]) then -1 else 0;
def C5 = if (Value_Daily >= Value_Daily[1] and Value_Daily[1] > Value_Daily[2]) then 2 else if (Value_Daily <= Value_Daily[1] and Value_Daily[1] < Value_Daily[2]) then -2 else 0;

def C6 = if (Diff_Daily >= Diff_Daily[1] and Diff_Daily[1] > Diff_Daily[2]) then 2 else if (Diff_Daily <= Diff_Daily[1] and Diff_Daily[1] < Diff_Daily[2]) then -2 else 0;

# Plot Sum of weighted scores

def C = C1 + C2 + C4 + C5 + C6;
plot Ctotal = C;

# Draw vertical line to indicate call and put signals and when to get out

def triggerup = if FullK_4H > 40 and FullK_4H[1] <= 40 and C >= 3 then 1 else 0;
def unload = if triggerup then 0 else if unload == 0 and C > -2 then unload[1] else 1;
def unloadc = if unload and unload[1] == 0 then 1 else 0;

def triggerdown = if FullK_4H <= 40 and FullK_4H[1] > 40 and C <= -3 then 1 else 0;
def cover = if triggerdown then 0 else if cover == 0 and C < 2 then cover[1] else 1;
def coverc = if cover and cover[1] == 0 then 1 else 0;

AddVerticalLine(triggerup, "Up", Color.UPTICK);
AddVerticalLine(unloadc, "unload", Color.LIGHT_GREEN);
AddVerticalLine(triggerdown, "Down", Color.DOWNTICK);
AddVerticalLine(coverc, "cover", Color.LIGHT_RED);

The back test result of 1hr chart is now even better. But back test sometimes doesn't work for stocks that have aggregation period starts at 9:30. I don't know why but it worked great for any future that trades 24 hours. I have a general question tho. FullK_Daily is calculated with aggregation period of a day. Does FullK_Daily[1] on hourly chart mean it return FullK_Daily of previous hour candle or the value of previous day? I think it's going back the previous hour. How can I let it go back to previous day since it defeats the purpose of comparing daily values if it's just comparing intraday noise?

One last thing, is it possible to set alert to send a phone text when triggered even if I shut off the computer?

Here is my back test script if anyone is interested
# 4H StochRSI 40 Xover and Daily StochRSI trend

input RSI_length = 14;
input over_bought = 80;
input over_sold = 20;
input cross_over = 40;
input RSI_average_type = AverageType.WILDERS;
def RSI_price_4H = close (period = AggregationPeriod.FOUR_HOURS);
input KPeriod = 14;
input DPeriod = 3;
input slowing_period = 1;
input averageType = AverageType.SIMPLE;

def RSI_4H = RSI(price = RSI_price_4H, length = RSI_length, averageType = RSI_average_type);

def FullK_4H = StochasticFull(over_bought, over_sold, KPeriod, DPeriod, RSI_4H, RSI_4H, RSI_4H, slowing_period, averageType).FullK;

def FullK_Daily = wildersaverage((close(period = AggregationPeriod.day) - Lowest(low(period = AggregationPeriod.day), KPeriod)) / (Highest(high(period = AggregationPeriod.day), KPeriod) - Lowest(low(period = AggregationPeriod.day), KPeriod)) * 100, slowing_period);

def C1 = if FullK_4H > 40 then 3 else -3;
def C4 = if (FullK_Daily >= FullK_Daily[1] and FullK_Daily[1] > FullK_Daily[2]) then 1 else if (FullK_Daily <= FullK_Daily[1] and FullK_Daily[1] < FullK_Daily[2]) then -1 else 0;

# 4H and Daily MACD trend

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

def Value_4H = MovingAverage(averageType2, close(period = AggregationPeriod.FOUR_HOURS), fastLength) - MovingAverage(averageType2, close(period = AggregationPeriod.FOUR_HOURS), slowLength);
def Avg_4H = MovingAverage(averageType2, Value_4H, MACDLength);

def Diff_4H = Value_4H - Avg_4H;

def Value_Daily = MovingAverage(averageType2, close(period = AggregationPeriod.DAY), fastLength) - MovingAverage(averageType2, close(period = AggregationPeriod.DAY), slowLength);
def Avg_Daily = MovingAverage(averageType2, Value_Daily, MACDLength);

def Diff_Daily = Value_Daily - Avg_Daily;

def C2 = if (Value_4H >= Value_4H[1] and Value_4H[1] > Value_4H[2]) then 1 else if (Value_4H <= Value_4H[1] and Value_4H[1] < Value_4H[2]) then -1 else 0;
def C5 = if (Value_Daily >= Value_Daily[1] and Value_Daily[1] > Value_Daily[2]) then 2 else if (Value_Daily <= Value_Daily[1] and Value_Daily[1] < Value_Daily[2]) then -2 else 0;

def C6 = if (Diff_Daily >= Diff_Daily[1] and Diff_Daily[1] > Diff_Daily[2]) then 2 else if (Diff_Daily <= Diff_Daily[1] and Diff_Daily[1] < Diff_Daily[2]) then -2 else 0;

# Plot Sum of weighted scores

def C = C1 + C2 + C4 + C5 + C6;

# Draw vertical line to indicate call and put signals and when to get out

def triggerup = if FullK_4H > 40 and FullK_4H[1] <= 40 and C >= 3 then 1 else 0;
def unload = if triggerup then 0 else if unload == 0 and C > -2 then unload[1] else 1;
def unloadc = if unload and unload[1] == 0 then 1 else 0;

def triggerdown = if FullK_4H <= 40 and FullK_4H[1] > 40 and C <= -3 then 1 else 0;
def cover = if triggerdown then 0 else if cover == 0 and C < 2 then cover[1] else 1;
def coverc = if cover and cover[1] == 0 then 1 else 0;

AddOrder(OrderType.Buy_TO_OPEN, triggerup, close, arrowcolor = Color.GREEN);
AddOrder(OrderType.Sell_To_cloSE, unloadC, close, arrowcolor = Color.BLUE);
AddOrder(OrderType.Sell_To_OpEN, triggerdown, close, arrowcolor = Color.RED);
AddOrder(OrderType.Buy_TO_CLOSE, coverC, close, arrowcolor = Color.PINK);
Re: Fun with ThinkScript
June 30, 2015 08:21PM
@Howo3579 - Interesting concept... Love to try it..but can't get it to work...the updated code above keeps saying "trying to self initialize"? Any ideas to help?
Re: Fun with ThinkScript
July 01, 2015 02:24AM
Tampman, sorry I uploaded wrong one. I'm trying to let it stop showing exit signal when I don't have position but still couldn't figure out how. But it shouldn't matter when backtest. Here is the correct study.
declare lower;

# 4H StochRSI 40 Xover and Daily StochRSI trend

input RSI_length = 14;
input over_bought = 80;
input over_sold = 20;
input cross_over = 40;
input RSI_average_type = AverageType.WILDERS;
def RSI_price_4H = close (period = AggregationPeriod.FOUR_HOURS);
input KPeriod = 14;
input DPeriod = 3;
input slowing_period = 1;
input averageType = AverageType.SIMPLE;

def RSI_4H = RSI(price = RSI_price_4H, length = RSI_length, averageType = RSI_average_type);

def FullK_4H = StochasticFull(over_bought, over_sold, KPeriod, DPeriod, RSI_4H, RSI_4H, RSI_4H, slowing_period, averageType).FullK;

def FullK_Daily = wildersaverage((close(period = AggregationPeriod.day) - Lowest(low(period = AggregationPeriod.day), KPeriod)) / (Highest(high(period = AggregationPeriod.day), KPeriod) - Lowest(low(period = AggregationPeriod.day), KPeriod)) * 100, slowing_period);

def C1 = if FullK_4H > 40 then 3 else -3;
def C4 = if (FullK_Daily >= FullK_Daily[1] and FullK_Daily[1] > FullK_Daily[2]) then 1 else if (FullK_Daily <= FullK_Daily[1] and FullK_Daily[1] < FullK_Daily[2]) then -1 else 0;

# 4H and Daily MACD trend

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

def Value_4H = MovingAverage(averageType2, close(period = AggregationPeriod.FOUR_HOURS), fastLength) - MovingAverage(averageType2, close(period = AggregationPeriod.FOUR_HOURS), slowLength);
def Avg_4H = MovingAverage(averageType2, Value_4H, MACDLength);

def Diff_4H = Value_4H - Avg_4H;

def Value_Daily = MovingAverage(averageType2, close(period = AggregationPeriod.DAY), fastLength) - MovingAverage(averageType2, close(period = AggregationPeriod.DAY), slowLength);
def Avg_Daily = MovingAverage(averageType2, Value_Daily, MACDLength);

def Diff_Daily = Value_Daily - Avg_Daily;

def C2 = if (Value_4H >= Value_4H[1] and Value_4H[1] > Value_4H[2]) then 1 else if (Value_4H <= Value_4H[1] and Value_4H[1] < Value_4H[2]) then -1 else 0;
def C5 = if (Value_Daily >= Value_Daily[1] and Value_Daily[1] > Value_Daily[2]) then 2 else if (Value_Daily <= Value_Daily[1] and Value_Daily[1] < Value_Daily[2]) then -2 else 0;

def C6 = if (Diff_Daily >= Diff_Daily[1] and Diff_Daily[1] > Diff_Daily[2]) then 2 else if (Diff_Daily <= Diff_Daily[1] and Diff_Daily[1] < Diff_Daily[2]) then -2 else 0;

# Plot Sum of weighted scores

def C = C1 + C2 + C4 + C5 + C6;
plot Ctotal = C;

# Draw vertical line to indicate call and put signals and when to get out

def triggerup = if FullK_4H > 40 and FullK_4H[1] <= 40 and C >= 3 then 1 else 0;
def unload = if triggerup then 0 else if C > -2 then unload[1] else 1;
def unloadc = if unload and unload[1] == 0 then 1 else 0;

def triggerdown = if FullK_4H <= 40 and FullK_4H[1] > 40 and C <= -3 then 1 else 0;
def cover = if triggerdown then 0 else if C < 2 then cover[1] else 1;
def coverc = if cover and cover[1] == 0 then 1 else 0;

AddVerticalLine(triggerup, "Up", Color.UPTICK);
AddVerticalLine(unloadc, "unload", Color.LIGHT_GREEN);
AddVerticalLine(triggerdown, "Down", Color.DOWNTICK);
AddVerticalLine(coverc, "cover", Color.LIGHT_RED);

Strategy:
# 4H StochRSI 40 Xover and Daily StochRSI trend

input RSI_length = 14;
input over_bought = 80;
input over_sold = 20;
input cross_over = 40;
input RSI_average_type = AverageType.WILDERS;
def RSI_price_4H = close (period = AggregationPeriod.FOUR_HOURS);
input KPeriod = 14;
input DPeriod = 3;
input slowing_period = 1;
input averageType = AverageType.SIMPLE;

def RSI_4H = RSI(price = RSI_price_4H, length = RSI_length, averageType = RSI_average_type);

def FullK_4H = StochasticFull(over_bought, over_sold, KPeriod, DPeriod, RSI_4H, RSI_4H, RSI_4H, slowing_period, averageType).FullK;

def FullK_Daily = wildersaverage((close(period = AggregationPeriod.day) - Lowest(low(period = AggregationPeriod.day), KPeriod)) / (Highest(high(period = AggregationPeriod.day), KPeriod) - Lowest(low(period = AggregationPeriod.day), KPeriod)) * 100, slowing_period);

def C1 = if FullK_4H > 40 then 3 else -3;
def C4 = if (FullK_Daily >= FullK_Daily[1] and FullK_Daily[1] > FullK_Daily[2]) then 1 else if (FullK_Daily <= FullK_Daily[1] and FullK_Daily[1] < FullK_Daily[2]) then -1 else 0;

# 4H and Daily MACD trend

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

def Value_4H = MovingAverage(averageType2, close(period = AggregationPeriod.FOUR_HOURS), fastLength) - MovingAverage(averageType2, close(period = AggregationPeriod.FOUR_HOURS), slowLength);
def Avg_4H = MovingAverage(averageType2, Value_4H, MACDLength);

def Diff_4H = Value_4H - Avg_4H;

def Value_Daily = MovingAverage(averageType2, close(period = AggregationPeriod.DAY), fastLength) - MovingAverage(averageType2, close(period = AggregationPeriod.DAY), slowLength);
def Avg_Daily = MovingAverage(averageType2, Value_Daily, MACDLength);

def Diff_Daily = Value_Daily - Avg_Daily;

def C2 = if (Value_4H >= Value_4H[1] and Value_4H[1] > Value_4H[2]) then 1 else if (Value_4H <= Value_4H[1] and Value_4H[1] < Value_4H[2]) then -1 else 0;
def C5 = if (Value_Daily >= Value_Daily[1] and Value_Daily[1] > Value_Daily[2]) then 2 else if (Value_Daily <= Value_Daily[1] and Value_Daily[1] < Value_Daily[2]) then -2 else 0;

def C6 = if (Diff_Daily >= Diff_Daily[1] and Diff_Daily[1] > Diff_Daily[2]) then 2 else if (Diff_Daily <= Diff_Daily[1] and Diff_Daily[1] < Diff_Daily[2]) then -2 else 0;

# Plot Sum of weighted scores

def C = C1 + C2 + C4 + C5 + C6;

# Draw vertical line to indicate call and put signals and when to get out

def triggerup = if FullK_4H > 40 and FullK_4H[1] <= 40 and C >= 3 then 1 else 0;
def unloadc = if C <=-2 then 1 else 0;

def triggerdown = if FullK_4H <= 40 and FullK_4H[1] > 40 and C <= -3 then 1 else 0;
def coverc = if C>=2 then 1 else 0;

AddOrder(OrderType.Buy_TO_OPEN, triggerup, close, arrowcolor = Color.GREEN);
AddOrder(OrderType.Sell_To_cloSE, unloadC, close, arrowcolor = Color.BLUE);
AddOrder(OrderType.Sell_To_OpEN, triggerdown, close, arrowcolor = Color.RED);
AddOrder(OrderType.Buy_TO_CLOSE, coverC, close, arrowcolor = Color.PINK);
Re: Fun with ThinkScript
July 01, 2015 04:32AM
Quote
KevinR
Also, I made a set of "gapless" indicators, but is there a display it as a line from open-close, then a new line from the next days open-close. I do not want it to plot between the close and next days open. Currently I just have the plot set to "DOTS", since it does not connect them.

Compare these two examples.

plot DailyHigh = high(period = "day" );
plot DailyLow = low(period = "day" );



plot DailyHigh = high(period = "day" );
     DailyHigh.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
plot DailyLow = low(period = "day" );
     DailyLow.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);



- robert


Professional ThinkorSwim indicators for the average Joe
Re: Fun with ThinkScript
July 01, 2015 05:02AM
Quote
howo3579
I have a general question tho. FullK_Daily is calculated with aggregation period of a day. Does FullK_Daily[1] on hourly chart mean it return FullK_Daily of previous hour candle or the value of previous day? I think it's going back the previous hour. How can I let it go back to previous day since it defeats the purpose of comparing daily values if it's just comparing intraday noise?

It depends on how you do it.

Goal, check to see if FullK_Daily is greater than yesterday's FullK_Daily while on a hourly intra-day chart.

Example 1. (the wrong way)

def FullK_Daily = wildersaverage((close(period = AggregationPeriod.day) - Lowest(low(period = AggregationPeriod.day), KPeriod)) / (Highest(high(period = AggregationPeriod.day), KPeriod) - Lowest(low(period = AggregationPeriod.day), KPeriod)) * 100, slowing_period);

def Compare = FullK_Daily > FullK_Daily[1];

This method will not work because it is comparing the FullK_Daily from this hour to the FullK_Daily from last hour which is still the same day (unless it happens to be the very first candle of the day and is comparing it to the last candle from yesterday).

FullK_Daily is defined so that it looks up the value of today's Daily FullK at the time of this particular candle. So, if it's 12:30 when the Compare variable is called, it will first look up the value of FullK_daily for 12:30 and return today's FullK. Then, FullK_Daily[1] will look up the daily FullK value of the previous candle (11:30) which is still the same day so the two values would be the same.

Example 2. (the right way)

Goal, check to see if FullK_Daily is greater than yesterday's FullK_Daily while on a hourly intra-day chart.

def FullK_Daily = wildersaverage((close(period = AggregationPeriod.day) - Lowest(low(period = AggregationPeriod.day), KPeriod)) / (Highest(high(period = AggregationPeriod.day), KPeriod) - Lowest(low(period = AggregationPeriod.day), KPeriod)) * 100, slowing_period);

def Yesterddays_FullK_Daily = wildersaverage((close(period = AggregationPeriod.day)[1] - Lowest(low(period = AggregationPeriod.day)[1], KPeriod)) / (Highest(high(period = AggregationPeriod.day)[1], KPeriod) - Lowest(low(period = AggregationPeriod.day)[1], KPeriod)) * 100, slowing_period);

def Compare = FullK_Daily > Yesterdays_FullK_Daily;

In this example, FullK_Daily says to look up the value of today's daily FullK—wildersaverage((close(period = AggregationPeriod.day)...—at the time of this candle. While Yesterdays_FullK_Daily says to look up the value of the previous day's FullK—wildersaverage((close(period = AggregationPeriod.day) [1]...—at the time of this candle.

In this case, because the [1] is after AggregationPeriod.day in Yesterday_FullK_Daily, you are specifically telling the script to look at the previous day rather than the default previous hour.

- robert


Professional ThinkorSwim indicators for the average Joe
Re: Fun with ThinkScript
July 01, 2015 05:27AM
Quote
Palmer
Does TOS count every minute on the clock from 7:30am to the previous close at 1459 as one bar, count bars where there is only data for, or count bars according to what the actual market trading hours are?

TOS only plots a candle during the after hours market when a trade occurs. Because the after hours sessions are more thinly traded than during the day, the number of candles will vary.

By way of example, in the screenshot below, I plot the number of candles during market hours (cyan color) and during after-market hours (yellow color).

Notice that every day has the same number of candles during market hours (39) which makes sense. (6.5 hours)(60 minutes / hour) = 390 minutes --> therefore, 39 ten-minute candles.

However, for the after-hours session, a candle is only drawn during the times that trades actually occurred so the number of candles varies from day to day.



- robert


Professional ThinkorSwim indicators for the average Joe



Edited 1 time(s). Last edit at 07/01/2015 05:49AM by robert.
Re: Fun with ThinkScript
July 01, 2015 08:08AM
Is there anyway to do this for a non-horizontal line such as a moving average?

Quote

SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

And any idea on this one?

def ValleyLow = if Valley then LowOfValley else MostRecentCandleOnChart;
Re: Fun with ThinkScript
July 01, 2015 02:18PM
@How - Thanks... you have a snapshot of what the chart should look like?
Re: Fun with ThinkScript
July 01, 2015 08:18PM
@Howo3579 - Tried the new script one you posted.... don't understand the unload and cover signals? sometimes says unload but really should buy/go long..? Can you explain how to read the system? Otherwise ..the buy signals seem good..albeit missed a few.. Thanks for sharing..
Re: Fun with ThinkScript
July 02, 2015 12:00AM
Robert, this script works great and I was wondering if there's a way to set an end point to the line (say 15 candles)? Thanks again.


# 5 Cents Below Open #
def OpenNeg5 = (open("period" = AggregationPeriod.DAY) - .05);
def lastbar = HighestAll(if IsNaN(close) then 0 else BarNumber());
plot condition = if BarNumber() <= lastbar - 10 then GetValue(OpenNeg5, BarNumber() - lastbar) else Double.NaN;
AddChartBubble(BarNumber() == lastbar - 10, condition, "-5", Color.CYAN, 0);
Re: Fun with ThinkScript
July 02, 2015 03:20AM
Quote
Ralph53
Robert, this script works great and I was wondering if there's a way to set an end point to the line (say 15 candles)? Thanks again.

# 5 Cents Below Open # 
def OpenNeg5 = (open("period" = AggregationPeriod.DAY) - .05);
def lastbar = HighestAll(if IsNaN(close) then 0 else BarNumber());
plot condition = if BarNumber() <= lastbar - 10 and BarNumber() >= lastbar - 25 then GetValue(OpenNeg5, BarNumber() - lastbar) else Double.NaN;
AddChartBubble(BarNumber() == lastbar - 10, condition, "-5", Color.CYAN, 0);

- robert


Professional ThinkorSwim indicators for the average Joe
Re: Fun with ThinkScript
July 02, 2015 09:43AM
Tamp,
Yea I'm having trouble figuring out how to have unload or cover signal only after buy or short signal. The rules are simple in below.

Take position:
When FullK_4H crosses above 40, buy signal is triggered only if C (total of weighted score) is greater or equal to 3.
When FullK_4H crosses below 40, short signal is triggered only if C (total of weighted score) is less or equal to -3.

I use hourly chart so there might be 4 consecutive bars with buy or short signal because the triggering condition was based on 4 hour stochRSI. I’ll post screen shot of my chart when I get home tonight.

Exit position:
Unload only when C is less or equal to -2.
Cover only when C is greater or equal to 2.

My chart also show unload and cover randomly. You see the code under #Draw vertical line… of study script.
def unload = if triggerup then 0 else if C > -2 then unload[1] else 1;
def unloadc = if unload and unload[1] == 0 then 1 else 0;

I was trying to set the value of unload to 0 only when buy signal is trigger and as long as C is greater than -2. Then the C becomes 1 when C become equal or less than -2.
Then we can use the value of unload to determine the condition. unloadc is true only when unload = 1 (when C <= -2) and unload[1] = 0 (to indicate C crosses below 2).

I don’t know if this code is good to do what I intended? If you or anyone here can help fix the problem, it will be great.

Now if you look at strategy I only wrote
def unloadc = if C <=-2 then 1 else 0;

Because there won’t be Sell_to_close order added if we don’t have a position. Therefore I don’t have to worry about order gets added all the time.

Now I have 2 things I need to improve on the script.

1. Fix indicators so it only signals once when conditions are met until the next signal triggered..
2. Set study alert on watch list so it text me when the conditions are met
(not having luck with this. Kept giving me below text:
com.devexperts.tos.thinkscript.runtime.TooComplexException: The complexity of the expression suggests that it may not be reliable with real-time data.)



Edited 1 time(s). Last edit at 07/02/2015 09:45AM by howo3579.
Re: Fun with ThinkScript
July 02, 2015 03:09PM
Robert,
I've set the study alert when FullK_Day crosses 40. Once the alert is fired I have to go set it up again. Is there way to keep the alert condition without having to go in and set up every time? Kind of hard to do when I'm not with the platform during the day.
Re: Fun with ThinkScript
July 02, 2015 03:33PM
Quote
howo3579
Robert,
I've set the study alert when FullK_Day crosses 40. Once the alert is fired I have to go set it up again. Is there way to keep the alert condition without having to go in and set up every time? Kind of hard to do when I'm not with the platform during the day.

You may want to incorporate the "alert" function into your script. [tlc.thinkorswim.com]

Here are a couple of examples of its use.

[www.researchtrade.com]

[www.researchtrade.com]

- robert


Professional ThinkorSwim indicators for the average Joe
Re: Fun with ThinkScript
July 02, 2015 06:30PM
Robert,
Here is a twist. I would like a scan the looks for the companies with earnings for the current week and/or month that pulls all the information into the scan columns that shows up in the Eagle Eye earnings script.
Re: Fun with ThinkScript
July 02, 2015 08:05PM
Robert,
I wrote the alert manually but it seems in Study Alert it only recognizes alert that's set this way.


Sorry I'm a noob at navigating TOS. Am it missing something here?



Edited 1 time(s). Last edit at 07/02/2015 08:06PM by howo3579.
Re: Fun with ThinkScript
July 02, 2015 08:36PM
Add the alert code to the bottom of the same script that you are running on the charts.

something like.

Alert(FullK_Daily > 40, GetSymbol() + " FullK above 40", Alert.Bar, Sound.Bell);

- robert


Professional ThinkorSwim indicators for the average Joe
Re: Fun with ThinkScript
July 02, 2015 09:10PM
Howo3579 - Thanks.... wish i could help..but I'm not a thinkscripter.. Please keep me updated on any new fixes for your study... Thanks!
Re: Fun with ThinkScript
July 02, 2015 10:24PM
Robert, I know how to have alert on the study on the chart opened. However this alert doesn't send me text message and I wouldn't know if I turn off the computer or away. I'd like to have alert on watch list which can notify me even when tos is not running. But it seems there's a limitation on what the script can do on study alert.
Re: Fun with ThinkScript
July 03, 2015 05:41AM
How3579 - don't forget to post a view of the chart... I have it set I think..but would like to make sure to compare to yours. I was thinking maybe you are trying to do too much with the script as far as having cover/unload signals... May I suggest that you try only using signals for long/buy or short/sell. For example, you go long at Buy signal and hold until the opposite Sell signal appears that cancels out the Buy .. then you go short on the Sell signal..until another Buy signal cancels it out.. Sometimes simple is better... my 2cents.. If you do manage to re-write the script as suggested please post.. I think that's all you need for trading futures.. which is what I trade.. Thanks!
Re: Fun with ThinkScript
July 03, 2015 08:55AM
Tampman,
Sorry I forgot to post screen shot of the chart. Below is what it looks like. However I haven't got to trade using this strategy because I have a day job and am not in from of TOS station. I also created 2 new columns on watch list. If I'm in front of the computer all day I can just pay attention to the numbers of the columns once every hour.


As for unload and cover until next sell or buy signals which means you have position taken all the time. it works but risk/reward is not good. When we exit when C is -2 or 2, we might miss out on some potential profit but most of time we avoid loss of profit. However when C is -3 or 3, it doesn't mean it's about time to get in a trade. That's why we have to make sure StochRSI is turning down or up. One thing I learn is market can stay oversold or overbought longer than we think. You can play around with the condition of C value and see which one suits best. i did back test and found long when C=3 or 2 and unload when C=2 or 1, and vise versa for short, produces the most profitable result. Back tests on all the futures on my watch list have shown this method blow all the other swing trade strategies I've done out of water. Anyway happy 4th of july.
Re: Fun with ThinkScript
July 03, 2015 12:39PM
How - Thanks for the response. I am apparently confused as I did not realize you were using the 4hr Stoch study also... Only code I saw to copy was the "New-Study - the Plan" that is on your chart. Is the 4hr Stoch study code posted somewhere as I missed it? Also, none of the stuff on the price area of the chart is showing on my screen? where is the C score posted? I'd show you what my chart looks like but can't figure out how to attach it here......
Re: Fun with ThinkScript
July 03, 2015 01:00PM
How - I tried inputting the "Strategy" code you posted above (re-posted below)..but nothing shows on my screen... and get message saying -"Add Order function should not be used.." Any ideas?

Strategy:
# 4H StochRSI 40 Xover and Daily StochRSI trend

input RSI_length = 14;
input over_bought = 80;
input over_sold = 20;
input cross_over = 40;
input RSI_average_type = AverageType.WILDERS;
def RSI_price_4H = close (period = AggregationPeriod.FOUR_HOURS);
input KPeriod = 14;
input DPeriod = 3;
input slowing_period = 1;
input averageType = AverageType.SIMPLE;

def RSI_4H = RSI(price = RSI_price_4H, length = RSI_length, averageType = RSI_average_type);

def FullK_4H = StochasticFull(over_bought, over_sold, KPeriod, DPeriod, RSI_4H, RSI_4H, RSI_4H, slowing_period, averageType).FullK;

def FullK_Daily = wildersaverage((close(period = AggregationPeriod.day) - Lowest(low(period = AggregationPeriod.day), KPeriod)) / (Highest(high(period = AggregationPeriod.day), KPeriod) - Lowest(low(period = AggregationPeriod.day), KPeriod)) * 100, slowing_period);

def C1 = if FullK_4H > 40 then 3 else -3;
def C4 = if (FullK_Daily >= FullK_Daily[1] and FullK_Daily[1] > FullK_Daily[2]) then 1 else if (FullK_Daily <= FullK_Daily[1] and FullK_Daily[1] < FullK_Daily[2]) then -1 else 0;

# 4H and Daily MACD trend

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

def Value_4H = MovingAverage(averageType2, close(period = AggregationPeriod.FOUR_HOURS), fastLength) - MovingAverage(averageType2, close(period = AggregationPeriod.FOUR_HOURS), slowLength);
def Avg_4H = MovingAverage(averageType2, Value_4H, MACDLength);

def Diff_4H = Value_4H - Avg_4H;

def Value_Daily = MovingAverage(averageType2, close(period = AggregationPeriod.DAY), fastLength) - MovingAverage(averageType2, close(period = AggregationPeriod.DAY), slowLength);
def Avg_Daily = MovingAverage(averageType2, Value_Daily, MACDLength);

def Diff_Daily = Value_Daily - Avg_Daily;

def C2 = if (Value_4H >= Value_4H[1] and Value_4H[1] > Value_4H[2]) then 1 else if (Value_4H <= Value_4H[1] and Value_4H[1] < Value_4H[2]) then -1 else 0;
def C5 = if (Value_Daily >= Value_Daily[1] and Value_Daily[1] > Value_Daily[2]) then 2 else if (Value_Daily <= Value_Daily[1] and Value_Daily[1] < Value_Daily[2]) then -2 else 0;

def C6 = if (Diff_Daily >= Diff_Daily[1] and Diff_Daily[1] > Diff_Daily[2]) then 2 else if (Diff_Daily <= Diff_Daily[1] and Diff_Daily[1] < Diff_Daily[2]) then -2 else 0;

# Plot Sum of weighted scores

def C = C1 + C2 + C4 + C5 + C6;

# Draw vertical line to indicate call and put signals and when to get out

def triggerup = if FullK_4H > 40 and FullK_4H[1] <= 40 and C >= 3 then 1 else 0;
def unloadc = if C <=-2 then 1 else 0;

def triggerdown = if FullK_4H <= 40 and FullK_4H[1] > 40 and C <= -3 then 1 else 0;
def coverc = if C>=2 then 1 else 0;

AddOrder(OrderType.Buy_TO_OPEN, triggerup, close, arrowcolor = Color.GREEN);
AddOrder(OrderType.Sell_To_cloSE, unloadC, close, arrowcolor = Color.BLUE);
AddOrder(OrderType.Sell_To_OpEN, triggerdown, close, arrowcolor = Color.RED);
AddOrder(OrderType.Buy_TO_CLOSE, coverC, close, arrowcolor = Color.PINK);
Sorry, only registered users may post in this forum.

Click here to login