A really dumb question that I can somehow not find the answer to
-
I'm building a list in my init of my strategy like so
for d in self.datas: self.stoch_lst.append(bt.ind.Stochastic(d, period=14, period_dfast=3, safediv=True))
Really simple and it's working.
Then in my next how do I get the d period? I'm accessing the k value like so
self.stoch_lst[i][0]
and then using it without getting the 0 index for comparisons, but how would I get the d value from the stochastic? Or any other indicator that produces more than one line.
I'm sure this is really simple but I can't find it on the docs and my google searches have came up empty somehow.
Thanks. -
@kfue here is a code snippet based on what I use to add multiple indicators to a list of stocks.
import backtrader as bt import collections class St(bt.Strategy): def __init__(self): # to hold the indicators self.inds = collections.defaultdict(dict) # Iterate over datafeeds and add any indicators for each datafeed for d in self.datas: # Add an indicator and use a string to name which indicator self.inds[d]['stoch'] = bt.ind.Stochastic(d, period=14, period_dfast=3, safediv=True) # You can add mutiple indicators to a datafeed self.inds[d]['movav'] = bt.ind.SMA(d, period=28) # to get the period for the stochastic indicator print(self.inds[self.data0]['stoch'].p.period) data = bt.feeds.BacktraderCSVData(dataname='./data/yourdata.csv') cerebro = bt.Cerebro() cerebro.adddata(data) cerebro.addstrategy(St) cerebro.run()
-
@kfue I think for you example instead of
self.stoch_list[i][0]
you could use
self.stoch_list{i].p.period
-
@techydoc typo you { brackets should all be []
(sux you cannot edit posts)
-
@techydoc That's almost what I want. In the stochastic it should contain a "slowD" and a "slowK" or something of that nature. A d and a k value. The k is the default, how do I get the k.
Also, is this documented somewhere? There's this documentation but it doesn't contain any details of what these objects actually contain which is very frustrating. https://www.backtrader.com/docu/indautoref/
-
@kfue yep, it is documented exactly by the reference you have in your post.
Stochastic
has two linespercD
andpercK
, which keeps the values the same way likedata
has linesopen
,close
etc. And can be accessed the same way. -
@kfue said in A really dumb question that I can somehow not find the answer to:
Also, is this documented somewhere? There's this documentation but it doesn't contain any details of what these objects actually contain which is very frustrating. https://www.backtrader.com/docu/indautoref/
It pretty well documented here. https://www.backtrader.com/docu/indautoref/#stochastic