Code Block 18th April Non-Series Indicators

Non-series Indicators Examples

Pivot Points

Code from IDE from Platform
# indie:lang_version = 5from indie import indicator, param, Optional, MainContextfrom indie.algorithms import SinceHighest, SinceLowestfrom indie.drawings import LabelAbs, AbsolutePosition, callout_position @indicator('Pivots High/Low by Ruslan S ', overlay_main_pane=True)@param.int('length_high_left', default=10)@param.int('length_low_left', default=10)@param.int('length_high_right', default=10)@param.int('length_low_right', default=10)class Main(MainContext): def __init__(self): none_label: Optional[LabelAbs] = None self._prev_high_pivot = self.new_var(none_label) self._prev_high_pivot_bar_index = self.new_var(0) self._prev_low_pivot = self.new_var(none_label) self._prev_low_pivot_bar_index = self.new_var(0) def calc(self, length_high_left, length_low_left, length_high_right, length_low_right): sh = SinceHighest.new(self.high, length_high_left + 1) sl = SinceLowest.new(self.low, length_low_left + 1) if sh[0] == 0: # we have a new high pivot candidate new_pivot_candidate_y = self.high[0] hp_ref: Optional[LabelAbs] = self._prev_high_pivot.get() if hp_ref is not None: # we have a previous high pivot, must decide which one is better if self.bar_index - self._prev_high_pivot_bar_index.get() > length_high_right: self._prev_high_pivot.set(None) # prev_high_pivot is too far away in history, forget about it elif hp_ref.value().position.price < new_pivot_candidate_y: self._update_high_pivot() # new pivot candidate is better, update previous pivot # else new pivot candidate is worse, keep the previous pivot if self._prev_high_pivot.get() is None: self._update_high_pivot() # create new low pivot if sl[0] == 0: new_pivot_candidate_y = self.low[0] lp_ref: Optional[LabelAbs] = self._prev_low_pivot.get() if lp_ref is not None: # we have a previous low pivot, must decide which one is better if self.bar_index - self._prev_low_pivot_bar_index.get() > length_low_right: self._prev_low_pivot.set(None) # prev_low_pivot is too far away in history, forget about it elif lp_ref.value().position.price > new_pivot_candidate_y: self._update_low_pivot() # new pivot candidate is better, update previous pivot # else new pivot candidate is worse, keep the previous pivot if self._prev_low_pivot.get() is None: self._update_low_pivot() # create new low pivot def _update_high_pivot(self) -> None: new_pivot_y = self.high[0] new_pivot_x = self.time[0] new_pivot_bar_index = self.bar_index if self._prev_high_pivot.get() is None: # create and draw self._prev_high_pivot.set(LabelAbs( str(new_pivot_y), AbsolutePosition(new_pivot_x, new_pivot_y), callout_position=callout_position.TOP_RIGHT, )) self._prev_high_pivot_bar_index.set(new_pivot_bar_index) else: # update field values and redraw hp_ref: Optional[LabelAbs] = self._prev_high_pivot.get() hp_ref.value().position = AbsolutePosition(new_pivot_x, new_pivot_y) hp_ref.value().text = str(new_pivot_y) self._prev_high_pivot_bar_index.set(new_pivot_bar_index) self.chart.draw(self._prev_high_pivot.get().value()) def _update_low_pivot(self) -> None: new_pivot_y = self.low[0] new_pivot_x = self.time[0] new_pivot_bar_index = self.bar_index if self._prev_low_pivot.get() is None: # create and draw self._prev_low_pivot.set(LabelAbs( str(new_pivot_y), AbsolutePosition(new_pivot_x, new_pivot_y), callout_position=callout_position.BOTTOM_LEFT, )) self._prev_low_pivot_bar_index.set(new_pivot_bar_index) else: # update field values and redraw hp_ref: Optional[LabelAbs] = self._prev_low_pivot.get() hp_ref.value().position = AbsolutePosition(new_pivot_x, new_pivot_y) hp_ref.value().text = str(new_pivot_y) self._prev_low_pivot_bar_index.set(new_pivot_bar_index) self.chart.draw(self._prev_low_pivot.get().value())
Code from Text Editor on Mac 
# indie:lang_version = 5from indie import indicator, param, Optional, MainContextfrom indie.algorithms import SinceHighest, SinceLowestfrom indie.drawings import LabelAbs, AbsolutePosition, callout_position@indicator('Pivots High/Low by Ruslan S ', overlay_main_pane=True)@param.int('length_high_left', default=10)@param.int('length_low_left', default=10)@param.int('length_high_right', default=10)@param.int('length_low_right', default=10)class Main(MainContext):    def __init__(self):        none_label: Optional[LabelAbs] = None        self._prev_high_pivot = self.new_var(none_label)        self._prev_high_pivot_bar_index = self.new_var(0)        self._prev_low_pivot = self.new_var(none_label)        self._prev_low_pivot_bar_index = self.new_var(0)    def calc(self, length_high_left, length_low_left, length_high_right, length_low_right):        sh = SinceHighest.new(self.high, length_high_left + 1)        sl = SinceLowest.new(self.low, length_low_left + 1)        if sh[0] == 0:            # we have a new high pivot candidate            new_pivot_candidate_y = self.high[0]            hp_ref: Optional[LabelAbs] = self._prev_high_pivot.get()            if hp_ref is not None:  # we have a previous high pivot, must decide which one is better                if self.bar_index - self._prev_high_pivot_bar_index.get() > length_high_right:                    self._prev_high_pivot.set(None)  # prev_high_pivot is too far away in history, forget about it                elif hp_ref.value().position.price < new_pivot_candidate_y:                    self._update_high_pivot()  # new pivot candidate is better, update previous pivot                # else new pivot candidate is worse, keep the previous pivot            if self._prev_high_pivot.get() is None:                self._update_high_pivot()  # create new low pivot        if sl[0] == 0:            new_pivot_candidate_y = self.low[0]            lp_ref: Optional[LabelAbs] = self._prev_low_pivot.get()            if lp_ref is not None:  # we have a previous low pivot, must decide which one is better                if self.bar_index - self._prev_low_pivot_bar_index.get() > length_low_right:                    self._prev_low_pivot.set(None)  # prev_low_pivot is too far away in history, forget about it                elif lp_ref.value().position.price > new_pivot_candidate_y:                    self._update_low_pivot()  # new pivot candidate is better, update previous pivot                # else new pivot candidate is worse, keep the previous pivot            if self._prev_low_pivot.get() is None:                self._update_low_pivot()  # create new low pivot    def _update_high_pivot(self) -> None:        new_pivot_y = self.high[0]        new_pivot_x = self.time[0]        new_pivot_bar_index = self.bar_index        if self._prev_high_pivot.get() is None:  # create and draw            self._prev_high_pivot.set(LabelAbs(                str(new_pivot_y),                AbsolutePosition(new_pivot_x, new_pivot_y),                callout_position=callout_position.TOP_RIGHT,            ))            self._prev_high_pivot_bar_index.set(new_pivot_bar_index)        else:  # update field values and redraw            hp_ref: Optional[LabelAbs] = self._prev_high_pivot.get()            hp_ref.value().position = AbsolutePosition(new_pivot_x, new_pivot_y)            hp_ref.value().text = str(new_pivot_y)            self._prev_high_pivot_bar_index.set(new_pivot_bar_index)        self.chart.draw(self._prev_high_pivot.get().value())    def _update_low_pivot(self) -> None:        new_pivot_y = self.low[0]        new_pivot_x = self.time[0]        new_pivot_bar_index = self.bar_index        if self._prev_low_pivot.get() is None:  # create and draw            self._prev_low_pivot.set(LabelAbs(                str(new_pivot_y),                AbsolutePosition(new_pivot_x, new_pivot_y),                callout_position=callout_position.BOTTOM_LEFT,            ))            self._prev_low_pivot_bar_index.set(new_pivot_bar_index)        else:  # update field values and redraw            hp_ref: Optional[LabelAbs] = self._prev_low_pivot.get()            hp_ref.value().position = AbsolutePosition(new_pivot_x, new_pivot_y)            hp_ref.value().text = str(new_pivot_y)            self._prev_low_pivot_bar_index.set(new_pivot_bar_index)        self.chart.draw(self._prev_low_pivot.get().value())

Just some text


Comments
Not authorized user image
No Comments yet image

Be the first to comment

Publish your first comment to unleash the wisdom of crowd.