experimental code: draw 2 rectangles, with start and stop dates/times, and price levels

halcyonguy

Moderator - Expert
VIP
Lifetime
if you are trying to manually draw 1 rectangle (partially) covering another, you may find that 1 of them covers up the other one.
this uses clouds , to draw 2 rectangles, that should allow both of them to be seen.

you pick the start and stop: dates, times, and prices.
the smaller rectangle is called the inner one
the bigger rectangle is called the outer one.

it doesn't check if the dates or times exist.
can choose the color for rectangles
can turn on labels to show dates/times
can show price levels
pick a $20 stock to see the default values drawn. i used T


Code:
#draw_rectangles

#https://usethinkscript.com/threads/tos-drawing-rectangles-in-a-rectangle.19332/
#TOS Drawing Rectangles in a Rectangle

# test with  T ,  a $19 stock

def na = double.nan;
def bn = barnumber();
def ymd = GetYYYYMMDD();

#DefineGlobalColor("outer", color.cyan);
#DefineGlobalColor("inner", color.magenta);
# GlobalColor("outer")
# GlobalColor("inner")

# color choose
#  this doesn't assign the color in quotes.
#  the location of the picked color , in the series, is changed to a number for getcolor()
input inner_color = {default "magenta", "cyan", "pink", "gray", "orange", "red", "green", "dark_gray", "tan", "white"};
input outer_color = {"magenta", default "cyan", "pink", "gray", "orange", "red", "green", "dark_gray", "tan", "white"};

# inner rect------------
input inner_rect_start_date_YYYYMMDD = 20240807;
#input inner_rect_stop_date_YYYYMMDD = 20240809;
input inner_rect_stop_date_YYYYMMDD = 20240813;
input times_are_EST = 0;
input inner_rect_start_time_HHMM = 1000;
input inner_rect_stop_time_HHMM = 1100;
input inner_rect_price_higher = 18.5;
input inner_rect_price_lower = 18.2;

def xin = if (ymd == inner_rect_start_date_YYYYMMDD and SecondsFromTime(inner_rect_start_time_HHMM) >= 0) then 1
 else if (ymd > inner_rect_start_date_YYYYMMDD and ymd < inner_rect_stop_date_YYYYMMDD) then 1
 else if (ymd == inner_rect_stop_date_YYYYMMDD and SecondsTillTime(inner_rect_stop_time_HHMM) > 0) then 1
 else 0; 

def cldin = if xin then inner_rect_price_higher else na;
#addcloud(cldin, inner_rect_price_lower, GlobalColor("inner"));
addcloud(cldin, inner_rect_price_lower, getcolor(inner_color));


# outer rect------------
input outer_rect_start_date_YYYYMMDD = 20240805;
input outer_rect_stop_date_YYYYMMDD = 20240812;
input outer_rect_start_time_HHMM = 1000;
input outer_rect_stop_time_HHMM = 1400;
input outer_rect_price_higher = 19.0;
input outer_rect_price_lower = 18.0;

def xout = if (ymd == outer_rect_start_date_YYYYMMDD and SecondsFromTime(outer_rect_start_time_HHMM) >= 0) then 1
 else if (ymd > outer_rect_start_date_YYYYMMDD and ymd < outer_rect_stop_date_YYYYMMDD) then 1
 else if (ymd == outer_rect_stop_date_YYYYMMDD and SecondsTillTime(outer_rect_stop_time_HHMM) > 0) then 1
 else 0; 

def cldout = if xout then outer_rect_price_higher else na;
#addcloud(cldout, outer_rect_price_lower, GlobalColor("outer"));
addcloud(cldout, outer_rect_price_lower, getcolor(outer_color));

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

# label calcs
def data1 = inner_rect_start_date_YYYYMMDD;
def year1 = Round(data1/10000, 0);
def month1 = Round((data1 % 10000) / 100, 0);
def day1 = (data1 % 100);

def data2 = inner_rect_stop_date_YYYYMMDD;
def year2 = Round(data2/10000, 0);
def month2 = Round((data2 % 10000) / 100, 0);
def day2 = (data2 % 100);

def data3 = outer_rect_start_date_YYYYMMDD;
def year3 = Round(data3/10000, 0);
def month3 = Round((data3 % 10000) / 100, 0);
def day3 = (data3 % 100);

def data4 = outer_rect_stop_date_YYYYMMDD;
def year4 = Round(data4/10000, 0);
def month4 = Round((data4 % 10000) / 100, 0);
def day4 = (data4 % 100);


input show_dates = yes;
addlabel(show_dates, " ", color.black);
addlabel(show_dates, "inner ", getcolor(inner_color));
addLabel(show_dates, month1 + "/" + day1 + "/" + AsPrice(year1), getcolor(inner_color));
addlabel(show_dates, asprice(inner_rect_start_time_HHMM), getcolor(inner_color));
addlabel(show_dates, " to ", color.white);
addLabel(show_dates, month2 + "/" + day2 + "/" + AsPrice(year2), getcolor(inner_color));
addlabel(show_dates, asprice(inner_rect_stop_time_HHMM), getcolor(inner_color));
addlabel(show_dates, " ", color.black);

addlabel(show_dates, "Outer", getcolor(outer_color));
addLabel(show_dates, month3 + "/" + day3 + "/" + AsPrice(year3), getcolor(outer_color));
addlabel(show_dates, asprice(outer_rect_start_time_HHMM), getcolor(outer_color));
addlabel(show_dates, " to ", color.white);
addLabel(show_dates, month4 + "/" + day4 + "/" + AsPrice(year4), getcolor(outer_color));
addlabel(show_dates, asprice(outer_rect_stop_time_HHMM), getcolor(outer_color));
addlabel(show_dates, " ", color.black);

input show_price_levels = yes;
plot xl1 = if xin and show_price_levels then inner_rect_price_higher else na;
plot xl2 = if xin and show_price_levels then inner_rect_price_lower else na;
xl1.setdefaultcolor(getcolor(inner_color));
xl2.setdefaultcolor(getcolor(inner_color));
plot xl3 = if xout and show_price_levels then outer_rect_price_higher else na;
plot xl4 = if xout and show_price_levels then outer_rect_price_lower else na;
xl3.setdefaultcolor(getcolor(outer_color));
xl4.setdefaultcolor(getcolor(outer_color));
#


ref , a question from this post
https://usethinkscript.com/threads/tos-drawing-rectangles-in-a-rectangle.19332/
 

Attachments

  • 00e-img1.JPG
    00e-img1.JPG
    74.4 KB · Views: 64

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Thread starter Similar threads Forum Replies Date
S Protect Source Code In ThinkOrSwim Playground 14

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
343 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top