# indie:lang_version = 5
from indie import indicator, Var, color
from indie.drawings import (
Label, LineSegment, AbsolutePosition, RelativePosition,
LabelRel, LabelAbs, vertical_anchor as va, horizontal_anchor as ha,
line_ending_style as les, extend_type as et,
)
from indie.algorithms import Highest, Lowest, SinceHighest, SinceLowest
from indie.color import rgba
from math import isnan
@indicator('Drawings example 2', overlay_main_pane=True)
def Main(self):
highest = Highest.new(self.high, 100)
lowest = Lowest.new(self.low, 100)
if self.bar_count % 100 == 0:
self.chart.draw(LineSegment(
AbsolutePosition(self.time[100], self.close[100]),
AbsolutePosition(self.time[0], highest[0]),
color=color.BLUE,
))
self.chart.draw(LineSegment(
AbsolutePosition(self.time[100], self.close[100]),
AbsolutePosition(self.time[0], lowest[0]),
color=color.BLUE,
))
if self.bar_count % 130 == 0:
self.chart.draw(LineSegment(
AbsolutePosition(self.time[0], self.close[0]),
AbsolutePosition(self.time[0], self.close[0] + 1.0),
extend_type=et.BOTH,
color=color.OLIVE(0.75),
line_width=7,
))
if self.bar_count % 50 == 0:
price = (highest[0] + lowest[0]) / 2
if isnan(price):
price = self.close[0]
r, g, b = rainbow_color((self.bar_count // 50) % 20, 20)
self.chart.draw(LabelAbs(
"WOW",
AbsolutePosition(self.time[0], price),
bg_color=rgba(r, g, b, 1.0),
))
stats_label = Var[LabelRel].new(LabelRel(
"text",
RelativePosition(
vertical_anchor=va.BOTTOM,
horizontal_anchor=ha.LEFT,
top_bottom_ratio=0.9,
left_right_ratio=0.1,
),
bg_color=color.NAVY(0.5),
text_color=color.YELLOW,
font_size=24,
))
stats_label.get().text = (
"Bar count: " + str(self.bar_count) +
"\nCurrent price: " + str(self.close[0]) +
"\nAn excellent indicator" +
"\nConvenient and informative" +
"\nBe profitable"
)
self.chart.draw(stats_label.get())
return
def hsv_to_rgb(h: float, s: float, v: float) -> tuple[int, int, int]:
i = int(h * 6)
f = h * 6 - i
p = v * (1 - s)
q = v * (1 - f * s)
t = v * (1 - (1 - f) * s)
i %= 6
r, g, b = 0.0, 0.0, 0.0
if i == 0:
r, g, b = v, t, p
elif i == 1:
r, g, b = q, v, p
elif i == 2:
r, g, b = p, v, t
elif i == 3:
r, g, b = p, q, v
elif i == 4:
r, g, b = t, p, v
else:
r, g, b = v, p, q
return int(r * 255), int(g * 255), int(b * 255)
def rainbow_color(i: int, total: int) -> tuple[int, int, int]:
hue = i / total
return hsv_to_rgb(hue, 1.0, 1.0)