Iterate between __init__ and next ?
-
Hi all,
i'm new to this platform, and am stuck implementing following part of a strategy. i have experimented quite a few things, and read the online help available, but its not working out. any help is welcome !Strategy algorithm snippet : (fixed data feed size being used : 1000 periods)
- start with an EMA (say, EMA of 200 periods)
- generate the EMA line
- generate the EMA crossings w.r.t. datas.close
- count number of crossings created in 3)
- if count is < 10
5a) decrease EMA period by 1
5b) goto step 2)
else
5c) print out final EMA period and use it for rest of the strategy.
While implementing above, i create EMA of x period in __init__ of strategy, something like:
self.current_ema = bt.indicators.ExponentialMovingAverage(self.datas[0].close, period=self.emasize)
i can generate the crossings line in 3) either in __init__ (line processing) or in next (element processing)
however, the count of crossings can be generated only in next (or so i believe !)
The problem is, once i generate the count and reach 5b), how do i create the next EMA in 2) ? the control flow wont go back to __init__ for creating a new indicator.
any ideas/ help welcome !
-
You seem to be NEW with Python, since you ask for an impossibility. Take your time.
In any case, you cannot modify the period of the created indicators, they are simply not meant to work like that. Your best bet is to create as many
EMA
s with different periods as you deem necessary ... and then switch amongst them until you find the one you want to stick to. -
:-) well, thats right - indeed i'm new to Python as well !
i had thought of creating lots of EMAs upfront ... but wondered there must be a better way to achieve the goal. till then, i'll implement with lots of EMAs.
thanks a lot ! -
The other way (don't know if it is better or not) is to calculate your indicator value on each
next()
with your own method. -
thanks for this idea !
yes it can be done, but it takes more time and effort as i have to test my code for the EMAs and other indicators that i use.i finally used a strategy to do the above flowchart for EMA line of a particular period. then used optstrategy to iterate over the EMA periods.
Not exactly what i started to do, but it gets my job done.thanks for reaching out with help !