python在变量中添加多个项

python在变量中添加多个项,python,pyalgotrade,Python,Pyalgotrade,我试图在工具变量中添加多个股票报价(例如APPL、TSLA、AMZN),以便metaplotlib可以为多个股票报价生成bollinger带图。目前,它仅显示一只股票的图 我试着用这个工具作为字典,然后试着用if循环,但是没有用。请告知我是否必须以不同的方式或任何其他简单的方式来实现它 from pyalgotrade import strategy from pyalgotrade import plotter from pyalgotrade.tools import yahoofinanc

我试图在工具变量中添加多个股票报价(例如APPL、TSLA、AMZN),以便metaplotlib可以为多个股票报价生成bollinger带图。目前,它仅显示一只股票的图

我试着用这个工具作为字典,然后试着用if循环,但是没有用。请告知我是否必须以不同的方式或任何其他简单的方式来实现它

from pyalgotrade import strategy
from pyalgotrade import plotter
from pyalgotrade.tools import yahoofinance
from pyalgotrade.technical import bollinger
from pyalgotrade.stratanalyzer import sharpe


class BBands(strategy.BacktestingStrategy):
def __init__(self, feed, instrument, bBandsPeriod):
    strategy.BacktestingStrategy.__init__(self, feed)
    self.__instrument = instrument
    self.__bbands = bollinger.BollingerBands(feed[instrument].getCloseDataSeries(), bBandsPeriod, 2)

def getBollingerBands(self):
    return self.__bbands

def onBars(self, bars):
    lower = self.__bbands.getLowerBand()[-1]
    upper = self.__bbands.getUpperBand()[-1]
    if lower is None:
        return

    shares = self.getBroker().getShares(self.__instrument)
    bar = bars[self.__instrument]
    if shares == 0 and bar.getClose() < lower:
        sharesToBuy = int(self.getBroker().getCash(False) / bar.getClose())
        self.marketOrder(self.__instrument, sharesToBuy)
    elif shares > 0 and bar.getClose() > upper:
        self.marketOrder(self.__instrument, -1*shares)


def main(plot):
instrument = "AAPL"
bBandsPeriod = 40

# Download the bars.
feed = yahoofinance.build_feed([instrument], 2015, 2016, ".")

strat = BBands(feed, instrument, bBandsPeriod)
sharpeRatioAnalyzer = sharpe.SharpeRatio()
strat.attachAnalyzer(sharpeRatioAnalyzer)

if plot:
    plt = plotter.StrategyPlotter(strat, True, True, True)
    plt.getInstrumentSubplot(instrument).addDataSeries("upper", strat.getBollingerBands().getUpperBand())
    plt.getInstrumentSubplot(instrument).addDataSeries("middle", strat.getBollingerBands().getMiddleBand())
    plt.getInstrumentSubplot(instrument).addDataSeries("lower", strat.getBollingerBands().getLowerBand())

strat.run()
print "Sharpe ratio: %.2f" % sharpeRatioAnalyzer.getSharpeRatio(0.05)

if plot:
    plt.plot()


if __name__ == "__main__":
main(True)
来自pyalgotrade进口战略的

从pyalgotrade导入绘图仪
从pyalgotrade.tools导入yahoofinance
来自pyalgotrade.technical import bollinger
从pyalgotrade.stratanalyzer导入sharpe
B类策略(策略。回溯测试策略):
定义初始值(自身、馈送、仪器、BB和SPERIOD):
strategy.BacktestingStrategy.\uuuuu初始化(self,feed)
自身仪器=仪器
self.\uu bbands=bollinger.BollingerBands(提要[instrument].getCloseDataSeries(),bBandsPeriod,2)
def getBollingerBands(自身):
返回自我
def onBars(自,条):
lower=self.\uu bbands.getLowerBand()[-1]
upper=self.\uBBands.getUpperBand()[-1]
如果lower为None:
返回
shares=self.getBroker().getShares(self.\uu工具)
巴=巴[自身仪器]
如果shares==0且bar.getClose()0和bar.getClose()>上限:
自营市场订单(自营工具,-1*股)
def主(绘图):
instrument=“AAPL”
b和speriod=40
#下载酒吧。
feed=yahoofinance.build_feed([工具],2015年,2016年,“.”)
strat=BBands(进料、仪表、bBandsPeriod)
sharpeRatioAnalyzer=sharpe.SharpeRatio()
地层分析仪(sharpeRatioAnalyzer)
如果绘图:
plt=绘图仪。策略绘图仪(strat,True,True,True)
plt.getInstrumentSubplot(instrument).addDataSeries(“upper”,strat.getBollingerBand().getUpperBand())
plt.getInstrumentSubplot(instrument).addDataSeries(“中间”,strat.getBollingerBand().getMiddleBand())
plt.getInstrumentSubplot(instrument).addDataSeries(“下”,strat.getBollingerBand().getLowerBand())
strat.run()
打印“Sharpe比率:%.2f”%sharpeRatioAnalyzer.getSharpeRatio(0.05)
如果绘图:
plt.plot()
如果名称=“\uuuuu main\uuuuuuuu”:
主(真)
这将有助于:

from pyalgotrade import strategy
from pyalgotrade import plotter
from pyalgotrade.tools import yahoofinance
from pyalgotrade.technical import bollinger
from pyalgotrade.stratanalyzer import sharpe


class BBands(strategy.BacktestingStrategy):
    def __init__(self, feed, instruments, bBandsPeriod):
        strategy.BacktestingStrategy.__init__(self, feed)
        self.__instruments = instruments

        self.__bbands = {}
        for instrument in instruments:
            self.__bbands[instrument] = bollinger.BollingerBands(feed[instrument].getCloseDataSeries(), bBandsPeriod, 2)

    def getBollingerBands(self):
        return self.__bbands

    def onBarsPerInstrument(self, instrument, bars):
        bbands = self.__bbands[instrument]
        lower = bbands.getLowerBand()[-1]
        upper = bbands.getUpperBand()[-1]
        if lower is None:
            return

        shares = self.getBroker().getShares(instrument)
        bar = bars[instrument]
        if shares == 0 and bar.getClose() < lower:
            sharesToBuy = int(self.getBroker().getCash(False) / bar.getClose())
            self.marketOrder(instrument, sharesToBuy)
        elif shares > 0 and bar.getClose() > upper:
            self.marketOrder(instrument, -1*shares)

    def onBars(self, bars):
        for instrument in self.__instruments:
            self.onBarsPerInstrument(instrument, bars)


def main(plot):
    instruments = ["AAPL", "ORCL"]
    bBandsPeriod = 40

    # Download the bars.
    feed = yahoofinance.build_feed(instruments, 2015, 2016, ".")

    strat = BBands(feed, instruments, bBandsPeriod)
    sharpeRatioAnalyzer = sharpe.SharpeRatio()
    strat.attachAnalyzer(sharpeRatioAnalyzer)

    if plot:
        plt = plotter.StrategyPlotter(strat, True, True, True)
        for instrument in instruments:
            bbands = strat.getBollingerBands()[instrument]
            plt.getInstrumentSubplot(instrument).addDataSeries("upper", bbands.getUpperBand())
            plt.getInstrumentSubplot(instrument).addDataSeries("middle", bbands.getMiddleBand())
            plt.getInstrumentSubplot(instrument).addDataSeries("lower", bbands.getLowerBand())

    strat.run()
    print "Sharpe ratio: %.2f" % sharpeRatioAnalyzer.getSharpeRatio(0.05)

    if plot:
        plt.plot()


if __name__ == "__main__":
    main(True)
来自pyalgotrade进口战略的

从pyalgotrade导入绘图仪
从pyalgotrade.tools导入yahoofinance
来自pyalgotrade.technical import bollinger
从pyalgotrade.stratanalyzer导入sharpe
B类策略(策略。回溯测试策略):
定义初始值(自身、馈送、仪器、BB和SPERIOD):
strategy.BacktestingStrategy.\uuuuu初始化(self,feed)
自身仪器=仪器
self.u bbands={}
对于仪表中的仪表:
self.\uu bbands[instrument]=bollinger.BollingerBands(提要[instrument].getCloseDataSeries(),bBandsPeriod,2)
def getBollingerBands(自身):
返回自我
def OnBars冲洗液(自身、仪器、棒):
B波段=自身波段[仪器]
lower=bbands.getLowerBand()[-1]
upper=bbands.getUpperBand()[-1]
如果lower为None:
返回
shares=self.getBroker().getShares(工具)
巴=巴[仪器]
如果shares==0且bar.getClose()0和bar.getClose()>上限:
自我市场秩序(工具,-1*股)
def onBars(自,条):
对于自成一体的仪表:
自攻杆式仪表(仪表、杆)
def主(绘图):
仪器=[“AAPL”,“ORCL”]
b和speriod=40
#下载酒吧。
feed=yahoofinance.build_feed(工具,2015年、2016年,“.”)
strat=BBands(进料、仪器、bBandsPeriod)
sharpeRatioAnalyzer=sharpe.SharpeRatio()
地层分析仪(sharpeRatioAnalyzer)
如果绘图:
plt=绘图仪。策略绘图仪(strat,True,True,True)
对于仪表中的仪表:
bbands=strat.getBollingerBands()[仪器]
plt.getInstrumentSubplot(instrument.addDataSeries(“upper”,bband.getUpperBand())
plt.getInstrumentSubplot(instrument.addDataSeries(“middle”,bbands.getMiddleBand())
plt.getInstrumentSubplot(instrument.addDataSeries(“lower”,bband.getLowerBand())
strat.run()
打印“Sharpe比率:%.2f”%sharpeRatioAnalyzer.getSharpeRatio(0.05)
如果绘图:
plt.plot()
如果名称=“\uuuuu main\uuuuuuuu”:
主(真)
这将有助于:

from pyalgotrade import strategy
from pyalgotrade import plotter
from pyalgotrade.tools import yahoofinance
from pyalgotrade.technical import bollinger
from pyalgotrade.stratanalyzer import sharpe


class BBands(strategy.BacktestingStrategy):
    def __init__(self, feed, instruments, bBandsPeriod):
        strategy.BacktestingStrategy.__init__(self, feed)
        self.__instruments = instruments

        self.__bbands = {}
        for instrument in instruments:
            self.__bbands[instrument] = bollinger.BollingerBands(feed[instrument].getCloseDataSeries(), bBandsPeriod, 2)

    def getBollingerBands(self):
        return self.__bbands

    def onBarsPerInstrument(self, instrument, bars):
        bbands = self.__bbands[instrument]
        lower = bbands.getLowerBand()[-1]
        upper = bbands.getUpperBand()[-1]
        if lower is None:
            return

        shares = self.getBroker().getShares(instrument)
        bar = bars[instrument]
        if shares == 0 and bar.getClose() < lower:
            sharesToBuy = int(self.getBroker().getCash(False) / bar.getClose())
            self.marketOrder(instrument, sharesToBuy)
        elif shares > 0 and bar.getClose() > upper:
            self.marketOrder(instrument, -1*shares)

    def onBars(self, bars):
        for instrument in self.__instruments:
            self.onBarsPerInstrument(instrument, bars)


def main(plot):
    instruments = ["AAPL", "ORCL"]
    bBandsPeriod = 40

    # Download the bars.
    feed = yahoofinance.build_feed(instruments, 2015, 2016, ".")

    strat = BBands(feed, instruments, bBandsPeriod)
    sharpeRatioAnalyzer = sharpe.SharpeRatio()
    strat.attachAnalyzer(sharpeRatioAnalyzer)

    if plot:
        plt = plotter.StrategyPlotter(strat, True, True, True)
        for instrument in instruments:
            bbands = strat.getBollingerBands()[instrument]
            plt.getInstrumentSubplot(instrument).addDataSeries("upper", bbands.getUpperBand())
            plt.getInstrumentSubplot(instrument).addDataSeries("middle", bbands.getMiddleBand())
            plt.getInstrumentSubplot(instrument).addDataSeries("lower", bbands.getLowerBand())

    strat.run()
    print "Sharpe ratio: %.2f" % sharpeRatioAnalyzer.getSharpeRatio(0.05)

    if plot:
        plt.plot()


if __name__ == "__main__":
    main(True)
来自pyalgotrade进口战略的

从pyalgotrade导入绘图仪
从pyalgotrade.tools导入yahoofinance
来自pyalgotrade.technical import bollinger
从pyalgotrade.stratanalyzer导入sharpe
B类策略(策略。回溯测试策略):
定义初始值(自身、馈送、仪器、BB和SPERIOD):
strategy.BacktestingStrategy.\uuuuu初始化(self,feed)
自身仪器=仪器
self.u bbands={}
对于仪表中的仪表:
self.\uu bbands[instrument]=bollinger.BollingerBands(提要[instrument].getCloseDataSeries(),bBandsPeriod,2)
def getBollingerBands(自身):
返回自我
def OnBars冲洗液(自身、仪器、棒):
B波段=自身波段[仪器]
lower=bbands.getLowerBand()[-1]
upper=bbands.getUpperBand()[-1]
如果lower为None:
返回
shares=self.getBroker().getShares(工具)
巴=巴[仪器]
如果shares==0且bar.getClose()0和bar.getClose()>上限:
自我市场秩序(工具,-1*股)
def onBars(自我,ba