• Get $30 off VIP with code SUMMER30. Ends July 27. Happy trading!

Change chart's background based on thinkscript condition?

evanevans

Active member
Can the background color of a chart be changed under conditions of a ThinkScript? Or perhaps a full window cloud? It would be great to be able to do this, so I could know which of 100s of charts I monitor simultaneously all day, need my attention.

Something like this:

SJhgVT4.png


I want to create a ThinkScript that can color the background of the whole chart under a particular current condition. I already got this far:

AFyqxt3.png
 
Last edited by a moderator:

BenTen

Administrative
Staff
VIP
@evanevans I don't think so. You can change the background of your chart by going to Settings > Appearance.

A1wvgvv.png


You're trying to change the entire background of your chart based on a thinkscript condition, which is not possible (at least to my knowledge). The screenshot you posted above is somewhat different than what you're asking for.

Maybe this script below will be a better starting place for what you're trying to achieve:

Trade Zones just puts up either a red or green box around any time you designate in the study properties. There is a selection for “NOTRADE” – translation red box, or “REVERSAL” – translation green box. Just pick one and put the starting and ending time Eastern and it will appear on your chart every day.

Code:
# TS_TRADEZONES
# http://www.thinkscripter.com
# [email protected]
# Last Update 30 JUN 2009

input zoneStart = 1500;
input zoneEnd = 1615;
input type = {default NOTRADE, REVERSAL};
plot highBar;
plot lowBar;

switch (type){

case NOTRADE:
highBar = if secondsTillTime(zoneStart) <= 0 and secondsTillTime(zoneEnd) >= 0 then highestAll(high) else double.nan;
lowBar = if secondsTillTime(zoneStart) <= 0 and secondsTillTime(zoneEnd) >= 0 then lowestAll(low) else double.nan;

case REVERSAL:
lowBar = if secondsTillTime(zoneStart) <= 0 and secondsTillTime(zoneEnd) >= 0 then highestAll(high) else double.nan;
highBar = if secondsTillTime(zoneStart) <= 0 and secondsTillTime(zoneEnd) >= 0 then lowestAll(low) else double.nan;
}
highBar.assignValueColor(if highBar>lowBar then color.dark_red else color.green);
lowBar.assignValueColor(if highBar>lowBar then color.dark_red else color.green);
addCloud(lowBar, highBar);
#---------------- End Of Code --------------------

Z6JUXaa.png
 
Last edited:

rad14733

Well-known member
VIP
I haven't figured out a way to control AssignBackgroundColor() within any type of conditional statement as I've tried multiple times and multiple different logic scenarios... I don't think it can be done... The function needs to be on a line all by itself... :cautious:
 

evanevans

Active member
I haven't figured out a way to control AssignBackgroundColor() within any type of conditional statement as I've tried multiple times and multiple different logic scenarios... I don't think it can be done... The function needs to be on a line all by itself... :cautious:
Damn, that's not encouraging. Well, I think I'll keep trying the "addcloud" full chart method. Thanks @BenTen for that code. I'll test it out Sunday.
 

horserider

Well-known member
VIP
No code listed so here is a line from a vwap study. Maybe it will give you a clue.

assignBackgroundColor(if VWAP > VWAP[1] then GlobalColor("Up") else if VWAP is equal to VWAP[1] then Color.GRAY else GlobalColor("Down"));
 

rad14733

Well-known member
VIP
Odd how the documentation doesn't include any reference to conditionals "within" the AssignBackgroundColor() function... If a conditional statement can be used that's great news...!!! I thought I had seen a script that allowed background changes but couldn't remember where I saw it...

Edited to add: I guess I should have tried the conditional even though it was never documented... I can confirm that it works... That's about the only thing I hadn't tried... Sometimes solutions are simpler than we make them out to be... (y)
 
Last edited:

evanevans

Active member
I must admit, I am not a professional programmer, and I am way too new to ThinkScript. Can somebody help me figure out how to make a script which can change the background color of the current price is above or below a SMA? Here's what I was doing. I'm probably over working it.

When line 17 didn't work, I wrote lines 8-15. Just seem to be boxing myself in a corner.

Any help much appreciated! Thanks

gWWwp1p.png
 

Pensar

Well-known member
VIP
@evanevans According to the pic, it looks like you want the background color to change based on the relationship between current price and the 20 day SMA. Check if this is right -

Code:
input length = 20;
input avg_type = averagetype.simple;
input agg = aggregationperiod.day;

def c = close(period = agg);
def sma = movingaverage(avg_type,c,length);

DefineGlobalColor("above",GetColor(1));
DefineGlobalColor("below",Color.BLACK);

AssignBackgroundColor(if close > sma
                      then GlobalColor("above")
                      else GlobalColor("below"));


When I started learning thinkscript, I went down many dead-end roads, and still do so frequently. Usually I'm overthinking the code.
 

evanevans

Active member
Awesome guys, thanks for all your help. I'm sure happy with this code, which I have completed:

Code:
#ChartAlert_SMA
#EvanEvans
#V1.0

input aggregationPeriod = AggregationPeriod.DAY;
input SMAPeriod = 20;
input SuperSMAPeriod = 50;
input averageType = AverageType.SIMPLE;
input AlertWhenPercentAboveSMA = 0.050;
input SuperAlertWhenPercentAboveSMA = 0.100;

def c = close(period = aggregationPeriod);
def sma = movingaverage(averageType,c,SMAperiod);
def SuperSMA = movingaverage(averageType,c,SuperSMAperiod);

DefineGlobalColor("SuperAbove",Color.Green);
DefineGlobalColor("Above",Color.Dark_Green);
DefineGlobalColor("Below",Color.Dark_Red);
DefineGlobalColor("SuperBelow",Color.Red);
DefineGlobalColor("default",Color.Current);

AssignBackgroundColor(if close > ((SuperAlertWhenPercentAboveSMA+1) * sma)
                      then GlobalColor("SuperAbove") else if close > ((AlertWhenPercentAboveSMA+1) * sma) then GlobalColor("Above")
                      else if close < SuperSMA then GlobalColor("SuperBelow") else if close < SMA then GlobalColor("Below") else GlobalColor("default"));

If price is 5% above the 20 sma chart changes to dark green
If price is 10% above the 20 sma chart changes to bright green
If price is below the 20 sma chart changes to dark red
If price is below the 50 sma chart changes to bright red

Example in action:
uT1aOE0.png
 

atlanticshark

New member
Is this doable by use of GetDayOfWeek and AssignBackgroundColor on a 15min interval chart? Basically I want to color the background for Monday for the entire area of the 15min intervals (contiguously).
 

Similar threads

Top