Main
function (it must be named Main
to keep all things in a ‘standardized’ way).
The Main
function is a function that is called per every candle of a chart instrument from left to right and after
that it is continued to be called on every realtime update of the last (rightmost) candle. To sum up, it returns an
indicator value for every candle of the main instrument.
There is a self
parameter in the Main
function and it is an object of type
MainContext
. It represents the main instrument of the chart, for example it
gives access to OHLCV values of the instrument (e.g. self.close
). If you are
familiar with Python, then you probably know that self
is a reference to the instance of the class that is currently
being used. It has the exact same meaning in Indie, though there are no classes in the code (yet). The @indicator
decorator is a syntactic sugar that turns def Main
function into a class Main
. Read more about this in
How Indie’s syntactic sugar works section.
Context, another new term (represented by a self
parameter of Main
function) is a representation of a current instrument displayed on a candle chart, with all it’s prices (open, high, low, close,
volume, etc.), some state and any other possible attributes it may have. In our example we take a series of close prices self.close
and apply a square brackets operator to it, to get the last (most recent) value of that price:
self.close[0]
. Zero offset always corresponds to the last value (value on the last candle). Offset 1
corresponds to the value of the previous candle and so on.
Our Main
function is decorated with a required @indicator
decorator with a
required title
param (title
is the first param of this decorator, but you may write it in a kwarg form:
@indicator(title='Example 1')
). In general, decorators (things which names start with a @
character) provide
meta-information about indicators to TakeProfit’s runtime. Meta-information describes indicator in some way, we will
see a lot of this further.
Finally, please note, that we have to import
symbols like indicator
, because they are not a part of Indie’s
built-in symbols. For now, the list of things that are allowed to be imported in the indicator code is very limited.
But as the platform grows, the library will be expanded accordingly.
After you type (or better copy-paste) the above code into the Code Editor (which is a widget and it should be added
to your workspace first as More Widgets -> Indicators Code Editor) click on the button Add to Chart.
As you can see, the indicator was added on a separate pane, below the main pane with candle chart.
overlay_main_pane=True
param to the
@indicator
decorator:
Sma
algorithm from package indie.algorithms
and 2) invocation of Sma.new
in the indicator’s code: Sma.new(self.close, 12)
. Algorithms that process series of prices
(like self.close
) usually return series of numeric values (variable s
). More about algorithms in this chapter.
Let us make our indicator more interesting and calculate two SMAs with different lengths:
l[0]
and s[0]
, that is why it plots two lines.
@plot.line
decorator for such way of coloring:
Main
function not plain numbers, but plot.Line
objects:
42
and 12
) but instead use named
parameters. Named parameters are very convenient because they can be modified in the indicator’s Settings panel
without modifying the indicator’s source code. For this purpose there are a whole set of decorators, starting with
@param.
prefix, for example: @param.int
, @param.bool
, @param.string
and so on. Let us incorporate them in our
example indicator: