python 在变量中添加多项
python adding multiple items in variable
我正在尝试在工具变量中添加多个股票报价(e.g.APPL、TSLA、AMZN),以便 metaplotlib 可以为多个股票生成布林带图 quotes.For 现在它只显示一只股票的图表。
我尝试将仪器用作字典,然后尝试使用 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)
这应该有帮助:
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)
我正在尝试在工具变量中添加多个股票报价(e.g.APPL、TSLA、AMZN),以便 metaplotlib 可以为多个股票生成布林带图 quotes.For 现在它只显示一只股票的图表。
我尝试将仪器用作字典,然后尝试使用 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)
这应该有帮助:
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)