# experimental , draw an oval / circle in ThinkOrSwim

#### halcyonguy

##### Well-known member
VIP
this is 1 of my works in progress. not sure when i will do more with it, so i am sharing it as it is.
--------------------------------

draw 2 ovals.

define 2 points, by entering a date and time for each.
draw a line connecting those points. this line will be the diameter and will be diagonal.
calculate a horizontal diameter. (the oval will be wider than the 2 points)
draw 2 arcs, for the top half and bottom half of the oval.
this will be the outer oval.
the center is marked with a yellow square.

a 2nd smaller oval is drawn.
it will have a width of the time span between the 2 points.
the arc lines are not drawn on the date-time bars. (i don't remember why)

i used different colors on the arcs, to show how they are drawn.

i set the 2 dates as 5/20 and 5/31.
set a chart to 30 days. mine is at 15min 30days

Ruby:
``````# circle_2points_0e

# Drawing a Circle on the chart

#-------------------------------------------
#https://www.math.net/circle-formula
#Given that point (x, y) lies on a circle with radius r centered at the origin of the coordinate plane, it forms a right triangle with sides x and y, and hypotenuse r. This allows us to use the Pythagorean Theorem to find that the equation for this circle in standard form is:

#x^2 + y^2 = r^2

#  r^2 = x^2 + y^2

# calc y
#  y^2 = r^2 - x^2
#  y = sqrt ( r^2 - x^2 )

#-------------------------------------------
#https://jshingler.github.io/TOS-and-Thinkscript-Snippet-Collection/TOS%20&%20Thinkscript%20Collection.html#c-draw-a-line-between-two-price-points
# jshingler web site
#C-DRAW A LINE BETWEEN TWO PRICE POINTS
#Hint: Draws a line between to points defined by dates, times and price type
#Developed by RCG3 and StanL on the ThinkScript Lounge 1/2/14
#-------------------------------------------

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

def bn1_date = if bn == 1 then GetYYYYMMDD() else bn1_date;

input time1 = 1130; #hint time1: EST , Time of first point using the 24-hour clock
input date1 = 20220520; #hint date1:Date of the first point in YYYYMMDD format
input time2 = 1300; #hint time2: EST , Time of second point using the 24-hour clock
input date2 = 20220531; #hint date2: Date of the first point in YYYYMMDD format
input price = close; #hint price: Select the price type desired

def date1ok = (date1 < bn1_date);
input show_dates = no;
addlabel(show_dates and date1ok, "1st date is off the chart", (if date1ok then color.red else color.gray));
#addlabel(show_dates, "1st bar date " + bn1_date, color.yellow);
addlabel(show_dates, "1st date " + date1, color.yellow);
addlabel(show_dates, "2nd date " + date2, color.yellow);

def date= getyyyymmdd();
def x1= if secondsfromtime(time1) == 0 and date1==date then barnumber() else x1;
def y1= if secondsfromtime(time1) == 0 and date1==date then price else y1;
def x2= if secondsfromtime(time2) == 0 and date2==date then barnumber() else x2;
def y2= if secondsfromtime(time2) == 0 and date2==date then price else y2;
def x11= highestall(x1);
def y11= highestall(y1);
def x22= highestall(x2);
def y22= highestall(y2);
def slope= (y22-y11)/(x22-x11);

plot line = if x1!=0 and x2==0 then y1+((barnumber()-x1)*slope) else double.nan;

#def  Angle_deg = ATan(slope) * 180 / Double.Pi;

#addLabel(yes, "Slope = " +  Angle_deg + "degrees", color.CYAN);#Angle printout has no consistency at various aggs
#alert(price crosses line, "Price crossed trendline", alert.once,sound.chimes);# uncomment this line if used as a trendline

#  ----------------------------------
# width of 2 points
def point_width = x22 - x11;

# horz width between 2 points
def point_width_half = floor(point_width/2);

#  ----------------------------------
# height of 2 points
def maxy = max(y11, y22);
def miny = min(y11, y22);

def point_height = maxy - miny;
def point_height_half = round(point_height/2, 2);

#  ----------------------------------
# center point of circle
def centerx = (x11 + point_width_half);
def centery = (miny + point_height_half);

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

#addlabel(1, "bars |" + x1 + "|" + width + "|" + x22 , color.yellow);
#addlabel(1, "\$\$ |" + y1 + "|" + height + "|" + y22 , color.yellow);

addlabel(1, "bars " + x22 + " - " + x11 + " = " + point_width + " |  1/2 = " + (point_width/2), color.yellow);
addlabel(1, "\$\$ " + maxy + " - " + miny + " = " + point_height, color.yellow);

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

input height_factor = 13.0;
#def height0 = halfheight * height_factor;
def point_yfac = point_height_half * height_factor;

# use center x,y point and one x,y point, into pathagorean therum to calc hypot = radius
def radz = floor(sqrt( point_width_half*point_width_half + point_yfac*point_yfac));

addlabel(1, "x " + point_width_half + " |  y " + point_yfac, color.white);

#  ----------------------------------
#////////////////////////////////////////////

# the bars within the circle
#def circle_bars3 = if ( bn >= floor(x11 - radz) and bn <= floor(x22 + radz)) then 1 else 0;
def circle_bars3 = if ( bn >= floor(centerx - radz) and bn <= floor(centerx + radz)) then 1 else 0;

def barx3 = if !circle_bars3 then na else floor(absvalue(centerx - bn));

input test4 = no;
bn + "\n" +
x11 + " x1\n" +
x22 + " x2\n" +
circle_bars3 + "\n" +
centerx + " c\n" +
, (if circle_bars3 then color.yellow else color.gray),no);

# y = sqrt ( r^2 - x^2 )

def circle_y3 = if !circle_bars3 then na else sqrt( (radz * radz) - (barx3 * barx3) );

input yfactor3 = .08;
#input yfactor = .04;
plot ytop3 =  centery + (circle_y3 * yfactor3);
plot ybot3 =  centery - (circle_y3 * yfactor3);
ytop3.SetDefaultColor(Color.cyan);
ybot3.SetDefaultColor(Color.blue);

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

#input y_factor = 0.5;
def bnsmall = x11 - rad_fac_num;
def bnbig = x22 + rad_fac_num;

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

#def bnsmall = x11 - rad_fac_num;
#def bnbig = x22 + rad_fac_num;

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

addlabel(0, "small bns " + bnsmall + " " + x11, color.cyan);
addlabel(0, "big bns " + x22 + " " + bnbig, color.cyan);

#def circle_bars = if ( bn >= x11 and bn <= x22) then 1 else 0;
def circle_bars = if ( bn >= floor(x11*(1-rad_factor)) and bn <= floor(x22*(1+rad_factor))) then 1 else 0;

# -----------------------------------------
# def y = (pt1price + pt2price ) / 2;
#calc x and y center

#calc
#calc y
# y^2 = r^2 - x^2
# y = sqrt ( r^2 - x^2 )

def rad1 = centerx - x11;
#def rad2 = floor((x11 + x22)/2);

addlabel(1, "point_x " + point_width_half, color.yellow);

# 2 pt height diff  used to make factor ??

# if bars >= circle bars <=  ,   centerx - bn
def barx = if !circle_bars then na else floor(absvalue(centerx - bn)*rad_factor);

# y = sqrt ( r^2 - x^2 )
# circle_bars
#def circley = if !circle_bars then na else sqrt( (rad0 * rad0) - (barx * barx) );
def circley = if !circle_bars then na else sqrt( (radx * radx) - (barx * barx) );

input yfactor = .08;
#input yfactor = .04;
plot ytop =  centery + (circley * yfactor);
plot ybot =  centery - (circley * yfactor);
ytop.SetDefaultColor(Color.magenta);
ybot.SetDefaultColor(Color.violet);

addlabel(1, "center x " + centerx, color.yellow);
addlabel(1, "center y " + centery, color.yellow);

# center point
plot ctr = if bn == centerx then centery else na;
ctr.SetPaintingStrategy(PaintingStrategy.SQUARES);
ctr.SetDefaultColor(Color.yellow);
ctr.setlineweight(4);
#ctr.hidebubble();

# --------------------------------
# test codes

input test2 = no;
bn + "\n" +
centerx + "\n" +
centery
, ( if ( bn == centerx) then color.yellow else color.gray), no);

input test1 = no;
x1 + "\n" +
y1 + "\n" +
x2 + "\n" +
y2 + "\n" +
x22 + "\n" +
y22 + "\n" +
slope
, color.yellow, no);

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

# ref codes

#from tos chat - mobius
def data = getYYYYMMDD();
def year = Round(data/10000, 0);
def month = Round((data % 10000) / 100, 0);
def day = (data % 100);
addLabel(1, "date: " + month + "/" + day + "/" + AsPrice(year), color.white);
#``````

AMZN 15min 5/2022 -------------------------------

it doesn't check if the dates are valid trading days.

could adjust a date with something like this.
to look for a day of week (dow) = 6 or 7. then set a new date var to be a friday. (or monday)

def dow1 = GetDayofWeek(date1);
# calc an offset to friday or original date
def date_adj = if dow1 == 6 then date1 - 1 else if dow1 == 7 then date1 - 2 else 0;

i didn't test what happens if the date is the 1st of month and 1 is subtracted from the date.

------------------------------
reference stuff

arc formulas
https://www.math.net/circle-formula
https://saylordotorg.github.io/text...en in the form (x,smaller is the minor radius.

diagonal line
https://jshingler.github.io/TOS-and...n.html#c-draw-a-line-between-two-price-points
hal_arc

Last edited:
• MerryDay

### Not the exact question you're looking for?

87k+ Posts
212 Online ## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

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?