For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
Calculating Indicators for Multiple Assets
-
Hello,
I am trying to calculate indicators for multiple assets. So, if I had 3 stocks AAPL, GOOG, and FB, I would want each of them to have a Short MA and a Long MA associated with them. Here is my current code for my strategy:
class MovingAverageCrossover(bt.Strategy): params = ( ('ma_short_length', 50), ('ma_long_length', 200), ) def __init__(self): self.ma_short = btind.ExponentialMovingAverage(self.data.close, period=self.params.ma_short_length) self.ma_long = btind.ExponentialMovingAverage(self.data.close, period=self.params.ma_long_length) def next(self): for data in self.datas: print(f"Stock: {data._name} Close: {round(data.close[0], 2)}")
Within the "next" method, I am currently just printing the close for each stock along with it's name. I would like to be able to print the ma_short and ma_long values instead.
Basically just trying to figure out how to assign and access indicator values for multiple assets! Not sure where I'm going wrong, but I'm sure it's pretty simple.
Thanks for the help!