Candles colored in relation to the 20sma


Category:
0
0

Hey traders. so I wanted to make this study where the bars are colored in GREEN above the 20sma, and colored in RED below the 20sma. So I contacted thinkorswim customer service and the guy told me that I could use this study LBR_PaintBars and so i configured the inputs for // hl length=20 // atr length=9 // factor=2,5 // and it kinda worked but I was wondering if any of you guys know another way to do this with more precision, because as you can see in the image below, through this LBR approach we got some candles that was not painted And some that were painted outside the specified conditions

And also how I could config TOS to give me alerts, whenever the monitored stock changes from below the 20sma to above, and from above to below as well.  Preferably on the 5min chart

Cheers

Attachments:
RESOLVED
Marked as spam
Posted by (Questions: 1, Answers: 1)
Asked on March 26, 2017 11:35 am
2871 views
1
Private answer

I see that we have a two part question. I’ll begin by addressing part 1. In order to dynamically adjust the color of the candles we use a function called AssignPriceColor(). You can read more about that here: http://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Look---Feel/AssignPriceColor.html

The function takes a single argument, Color. We will use an if/then statement to set the color based on the position of the close in relation to the simple moving average.

plot sma1 = Average(close, 20);
AssignPriceColor(if close > sma1 then Color.GREEN else Color.RED);

That’s it. Nothing more is needed.

Now on to the second part of the question. The request is for an alert that will trigger whenever the color changes from one color to the other. In this case we will add a function called Alert(). You can read more about that here: http://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Others/Alert.html

Before we add this, we’ll need to define the conditions. We’ll do this using a statement that will check if the previous bar is below the sma, while the current bar is above it. Then we’ll do the reverse to capture the other change. Then we’ll use these two conditions as the trigger for each of two alerts. So we’ll be adding these four additional lines to the two original lines shown above.

def crossAbove = close[1] < sma1[1] and close > sma1;
def crossBelow = close[1] > sma1[1] and close < sma1;
Alert(crossAbove, "Close Crossed Above SMA", Alert.BAR, Sound.RING);
Alert(crossBelow, "Close Crossed Below SMA", Alert.BAR, Sound.RING);

And we’re done. Screenshot below shows the results. Be sure to up-vote any answers that best solve your question!

Attachments:
Marked as spam
Posted by (Questions: 37, Answers: 4087)
Answered on March 26, 2017 12:29 pm
1
Private answer

In response to the second question about adding alerts based on two conditions.

It is always easiest to begin with the full list of specifications and write the code using all the facts. The reason is that very often the additional specifications cause us to disregard lines of code that were previously written. You cannot simply combine two sets of conditions without regard for how they interact on the chart.

Case in point. For your set of rules, you did not specify if the close above the SMA should always occur before the color change in the SMA. Or perhaps the SMA should change color first, followed by the close above the SMA. So you can’t just take a cross of the SMA and use that in combination with the change in color of the SMA. You have to take them both into consideration and write the code so that both events are properly accounted for. Unless you only want one of them accounted for.

This code provides for both events occurring.

plot sma1 = Average(close, 20);
AssignPriceColor(if close > sma1 then Color.GREEN else Color.RED);
#def crossAbove = close[1] < sma1[1] and close > sma1;
#def crossBelow = close[1] > sma1[1] and close < sma1; #Alert(crossAbove, "Close Crossed Above SMA", Alert.BAR, Sound.Chimes); #Alert(crossBelow, "Close Crossed Below SMA", Alert.BAR, Sound.ding); sma1.AssignValueColor(if sma1 > sma1[1] then Color.CYAN else Color.DARK_ORANGE);
def closeAboveSMA = close > sma1;
def closeBelowSMA = close < sma1; def smaUP = sma1 > sma1[1];
def smaDOWN = sma1 < sma1[1];
def alertUP = (smaUP and closeAboveSMA and closeBelowSMA[1]) or (closeAboveSMA and smaUP and smaDOWN[1]);
def alertDOWN = (smaDOWN and closeBelowSMA and closeAboveSMA[1]) or (closeBelowSMA and smaDOWN and smaUP[1]);
Alert(alertUP, "Up Trend", Alert.BAR, Sound.CHIMES);
Alert(alertDOWN, "Down Trend", Alert.BAR, Sound.DING);

Marked as spam
Posted by (Questions: 37, Answers: 4087)
Answered on March 29, 2017 10:17 am
0

thanks for the support Pete, We are almost there
I’m wondering how to change this so it appears just like that lower subgraph I sketched in the screenshot,
Like, for it to change only between these 3 scenarios [+1], [0] and [-1]
is that even possible?

( at March 29, 2017 11:16 am)
0

plot signal = if alertUp then 1 else if alertDown then -1 else 0;

( at March 29, 2017 11:50 am)
0
Private answer

Nice answer Pete !
The other part of this strategy I’m trying to build would be to define when the 20sma moving average is angled uptrend or angled downtrend.

Like this image below. The problem is that I tried to config this on TOS and gave this problem “no such variable: ave”

Do you see where I’m going with this? After that, the final scanner will alert me when the moving average is directed upwards and with candles above the 20sma    FOR LONGS
And for the short side, The final scanner will alert me when the moving average is directed downwards and with candles below the 20sma.       All in the 5min chart

Attachments:
Marked as spam
Posted by (Questions: 1, Answers: 1)
Answered on March 26, 2017 2:42 pm
0

Easy fix. The function named AssignValueColor() can only be applied to a plot. You only have one plot statement which is SMA. Not sure why you are trying to apply it to a nonexistent variable named ave. But that’s what we’re here for.
SMA.AssignValueColor(if SMA > SMA[1] then Color.CYAN else Color.DARK_RED);

( at March 26, 2017 4:12 pm)
0

hey Pete, Do you know if Thinkorswim can differentiate the angle of inclination of the 20sma?
I mean, to avoid the alerts to throw me in the middle of some choppy action, where the 20sma tends to be almost horizontal, And price action takes no trend at all

( at March 27, 2017 6:42 pm)
0

Angles can be computed, it’s just math. But the low tech approach is the use two moving averages. When the short ma is above the long ma, the long ma should be turning upward. Another indicator you can use is the DMI, which measures the strength of a trend. There a some posts here in the Q&A forum about the DMI/ADX. We also recently published a video on installing this in a watchlist.

( at March 27, 2017 7:28 pm)
0
Private answer

Mr Hahn, I had to log in through twitter because through facebook not working out

Im already using this study where the bars are colored in GREEN above the 20sma, and colored in RED below the 20sma.

and also This other script, when the 20sma is angled uptrend the line becomes BLUE and when angled downtrend the line becomes ORANGE

so as i told you before, I’m trying to finish this study To combine these two features, I’ve even made this image below so you can better understand what we’re looking for here

 

CONDITION 1 [plus 1]= will alert me when the moving average is directed upwards and with candles above the 20sma    FOR LONGS (green candles and the sma colored in blue)

 

CONDITION 0 [zero] = (green candle and 20sma in orange) OR (red candle and 20sma in blue)
CONDITION -1 [minus 1]= for the short side, will alert me when the moving average is directed downwards and with candles below the 20sma.      (red candles and the 20sma colored in orange )

 

Attachments:
Marked as spam
Posted by (Questions: 7, Answers: 13)
Answered on March 28, 2017 12:52 pm
0

Alerts are among the most simple things to implement in the Thinkscript language. I see in your screenshot you have a lower subgraph called ”NewStudy0”. That right there contains everything you need to build your alert.
In human terms:
if current bar of NewStudy0 equals +1 but previous bar of NewStudy0 is less than +1 then ALERT
If current bar of NewStudy0 equals -1 but previous bar of NewStudy0 is greater than -1 then ALERT
But you didn’t post your code for NewStudy0. So you’ll have to work that part out on your own.

( at March 28, 2017 6:15 pm)
0

I edited that screenshot on microsoft paint so you can better understand what we’re looking for.
I do not have the final code yet, thats why I’m here for your support. All Im using is this script below, see if it’s easier to help with this. These studies are working but not alerting in accordance with the conditions I have described above

plot sma1 = Average(close, 20);
AssignPriceColor(if close > sma1 then Color.GREEN else Color.RED);
def crossAbove = close[1] < sma1[1] and close > sma1;
def crossBelow = close[1] > sma1[1] and close < sma1; Alert(crossAbove, "Close Crossed Above SMA", Alert.BAR, Sound.Chimes); Alert(crossBelow, "Close Crossed Below SMA", Alert.BAR, Sound.ding); sma1.AssignValueColor(if sma1 > sma1[1] then Color.CYAN else Color.DARK_ORANGE);

( at March 29, 2017 5:05 am)
0
Private answer

exactly
The only problem now is that it must remain in condition +1, during the period that condition +1 is being respected

and remain in condition -1, during the period that condition -1 is being respected

See this screenshot below

Attachments:
Marked as spam
Posted by (Questions: 7, Answers: 13)
Answered on March 29, 2017 12:28 pm
0

So then you really don’t want alerts at all. you just want to plot a lower study that captures your trend direction? If we configure it the way you are requesting now, the alerts would trigger for every single bar where the lower study is either +1 or -1. I’m sure you don’t want alerts that trigger that way. So here is a line of code to plot what you are requesting. Just don’t try using it to trigger alerts.

plot signal = if close > sma1 and sma1 > sma[1] then 1 else if close < sma1 and sma1 < sma[1] then -1 else 0;

( at March 29, 2017 2:07 pm)
0

exactly
You know way more than the Tech Support at the thinkorswim website.
thank you for the quick answers Pete
I will continue enriching your website’s database if you dont mind
Cheers

( at March 29, 2017 2:57 pm)
0
Private answer

Hey Pete,  See this image below

For this final tweak on the code,

FOR THE LONG SIDE, the color of the line on that lower subgraph should turn green every time if forms At least 3 candles in [condition 1], and will lose the green color As soon as it loses [condition 1]

AND FOR THE SHORT SIDE, the color of the line on that lower subgraph should turn magenta every time if forms At least 3 candles in [condition -1], and will lose the magenta color As soon as it loses [condition -1]

Also, It Should indicate in the chart Just above the candle, the price of entry and exit for the period the line was green, and the price of entry and exit the period when that line was magenta

def sma1 = Average(close, 20);

plot signal = if close > sma1 and sma1 > sma1[1] then 1 else if close < sma1 and sma1 < sma1[1] then -1 else 0;

For you who that have great experience with thinkorswim, would quickly solve this with some command lines.
But for us who are learning, is not that simple,
so I thank you again for the cooperation

Trade safe !

Attachments:
Marked as spam
Posted by (Questions: 0, Answers: 3)
Answered on March 31, 2017 11:49 am
0

Building on your previous code, this should do the trick for the price bubbles placed at the locations you specified:

def sma1 = Average(close, 20);
plot signal = if close > sma1 and sma1 > sma1[1] then 1 else if close < sma1 and sma1 < sma1[1] then -1 else 0; def sum = sum(signal, 4); def plotBubble = (sum == 4 and sum[1] < 4) or (sum == -4 and sum[1] > -4);
AddChartBubble(plotBubble, high, AsText(close, NumberFormat.TWO_DECIMAL_PLACES), Color.WHITE, yes);

( at April 4, 2017 8:04 am)
0

最小リソースのインテリジェントな使用による最大限の精度

( at April 5, 2017 2:48 pm)
0
Private answer

Cheers!

Marked as spam
Posted by (Questions: 37, Answers: 4087)
Answered on April 5, 2017 3:07 pm