Main
entry pointMain
entry point. It could be written in one of two forms: as a Main
function
definition which is a form of syntactic sugar or as a Main
class definition (which is not).
Main
function definitionMain
as a function definition:
Main
decorated with @indicator
it transforms it into a Main
class definition (i.e. to the second form) according to a few simple rules:
Main
class definition which is inherited from indie.MainContext
base class;Main
function becomes Main.calc()
method;Main
class may optionally have __init__
constructor (only if it is needed).Main
entry point is considered to
be a syntactic sugar. In some cases when indicator needs greater control over what is happening in the code the second form of writing the indicator’s Main
entry point with classes syntax can be used explicitly.
Main
written as a class definitionMain
entry point which uses class definition syntax:
__init__
constructor method and place some one-time initialization code there. Therefore for
indicators with complex logic you probably would like to use the second form. Constructor __init__
is optional though.
@algorithm
@algorithm
are automatically transformed by Indie compiler into classes inherited from
indie.Algorithm
. For example:
Main
entry point,
the decorator-based form is preferred in most cases. Class form should be used only in more complex cases, where a
more fine-grained control is needed.
Decorator @algorithm
is also a syntactic sugar in Indie language. Here are the simple rules that Indie compiler uses
to process it:
@algorithm
is replaced with a class with the same name inherited from
indie.Algorithm
base class;calc
method of this class, function body does not change;__init__
constructor is added to the generated class which accepts ctx: indie.Context
parameter and forwards it to
the parent constructor;self.ctx
property of
indie.Context
type. For example to get access to close prices use self.ctx.close
expression.
The DoubleEma
algorithm from the example above could be created and used in two ways:
DoubleEma.new()
static method (new
is a method of parent class indie.Algorithm
, well… not exactly but
kind of). This is a syntactic sugar that allows to use any algorithm right in the place where it is needed in the
indicator (e.g. in def Main
function, or in the function body of some other algorithm).DoubleEma()
constructor in __init__
method of a corresponding MainContext
, SecContext
or other
Algorithm
. Then, in the calc
method of a corresponding MainContext
, SecContext
or other
Algorithm
you should explicitly call the DoubleEma
’s calc()
method.Algorithm.new()
methodAlgorithm.new()
is used to create and use algorithm objects in Indie. It looks like a static method, but
it behaves very differently because it is also a syntactic sugar of the language. When Indie compiler sees a call, e.g.
res = Ema.new(src, length)
, it uses the following rules to transform such a piece of code:
__init__
constructor of the enclosing MainContext
, SecContext
or other
Algorithm
. In our example it would be: self._ema1 = Ema(ctx)
(in case self
is a Context
) or
self._ema1 = Ema(self.ctx)
(in case self
is an Algorithm
).Ema.new
is replaced with self._ema1.calc
call. In our example it would be
res = self._ema1.calc(src, length)
.Algorithm.new()
works only inside methods of descendants of MainContext
, SecContext
or other Algorithm
classes (except the __init__
constructors). You cannot use this functionality in plain
Python-like functions.
Syntactic sugar of Algorithm.new()
method can be used with any Indie algorithm regardless of the form in which the
algorithm was written (the decorator-based form or the class-based form).
MutSeries[T].new()
methodMutSeries[T].new()
(or MutSeriesF.new()
where MutSeriesF
is an alias for MutSeries[float]
) is used to create and use mutable series objects in Indie. It looks like a static method, but
it behaves very differently because it is also a syntactic sugar of the language. When Indie compiler sees a call, e.g.
s = MutSeriesF.new((a + b) / 2)
, it uses the following rules to transform such a piece of code:
__init__
constructor of the enclosing MainContext
, SecContext
or
Algorithm
. In our example it would be: self._ms1 = self.new_mut_series_f()
(in case self
is a Context
) or self.ctx.new_mut_series_f()
(in case self
is an Algorithm
).MutSeriesF.new
is replaced with self._ms1.calc
call. In our example it would be
s = self._ms1.calc((a + b) / 2)
. Method MutSeriesF.calc
writes given expression (a + b) / 2
into the last
element of the mutable series object and returns a reference to the mutable series itself.@algorithm
and MutSeriesF.new()
call.
@sec_context
@sec_context
are used to create secondary entry-points (besides Main
) which are needed when
indicator requests data of additional instruments. And decorator @sec_context
is syntactic sugar, because every time
Indie compiler sees a function definition decorated with @sec_context
it transforms it into a class, inherited from
indie.SecContext
according to a few simple rules:
@sec_context
is replaced with a class with the same name inherited from
indie.SecContext
base class;calc()
method of the generated class;__init__
constructor (only if it is needed).SecHighLow
looks like this:
Context.calc_on
only from __init__
constructors.