Python 在PyAlgoTrade中使用多种仪器进行反向测试
嗨,我想使用一个数组(“仪器”)将策略从1个推广到10个可能的投资,以简化加载10个提要、创建10个SMA的任务,然后每天检查一个(或多个)仪器中是否发生信号交叉 我被困在这里面了。此外,绘图仪是单独绘制图形,但我想打印在一个图形的所有仪器的结果 这是我的代码:Python 在PyAlgoTrade中使用多种仪器进行反向测试,python,pyalgotrade,Python,Pyalgotrade,嗨,我想使用一个数组(“仪器”)将策略从1个推广到10个可能的投资,以简化加载10个提要、创建10个SMA的任务,然后每天检查一个(或多个)仪器中是否发生信号交叉 我被困在这里面了。此外,绘图仪是单独绘制图形,但我想打印在一个图形的所有仪器的结果 这是我的代码: from pyalgotrade import strategy, plotter from pyalgotrade.barfeed import yahoofeed from pyalgotrade.technical import
from pyalgotrade import strategy, plotter
from pyalgotrade.barfeed import yahoofeed
from pyalgotrade.technical import ma
from pyalgotrade.tools import yahoofinance
class MyStrategy(strategy.BacktestingStrategy):
def __init__(self, feed, instruments, smaPeriod):
strategy.BacktestingStrategy.__init__(self, feed, 1000)
self.__position = None
# We'll use adjusted close values instead of regular close values.
self.setUseAdjustedValues(True)
self.__sma = {}
self.__instruments = instruments
for instrument in instruments:
self.__sma[instrument] = ma.SMA(feed[instrument].getPriceDataSeries(), smaPeriod)
def onEnterOk(self, position):
execInfo = position.getEntryOrder().getExecutionInfo()
self.info("BUY at $%.2f" % (execInfo.getPrice()))
def onEnterCanceled(self, position):
execInfo = position.getEntryOrder().getExecutionInfo()
def onExitOk(self, position):
execInfo = position.getExitOrder().getExecutionInfo()
self.info("SELL at $%.2f" % (execInfo.getPrice()))
def onExitCanceled(self, position):
# If the exit was canceled, re-submit it.
self.__position[str(position.getEntryOrder().getInstrument())].exitMarket()
def onBars(self, bars):
# Wait for enough bars to be available to calculate a SMA.
if self.__sma[-1] is None:
return
bar = bars[self.__instrument]
# If a position was not opened, check if we should enter a long position.
if self.__position is None:
if bar.getPrice() > self.__sma[-1]:
# Enter a buy market order for 25 shares. The order is good till canceled.
self.__position = self.enterLong(self.__instrument, 25, True)
# Check if we have to exit the position.
elif bar.getPrice() < self.__sma[-1] and not self.__position.exitActive():
self.__position.exitMarket()
def run_strategy(smaPeriod):
# Load the yahoo feed from the CSV file
instruments = [
"AMZN",
"ADBE",
"C" ,
"BA" ,
"HOG" ,
"MMM" ,
"MS" ,
"MSFT" ,
"CVS" ,
"AXP"
]
#Download and Load yahoo feed from CSV files
#Change year range 2000 to 2001 to your desired one
feed = yahoofinance.build_feed(instruments, 2000,2001, ".")
# Evaluate the strategy with the feed.
myStrategy = MyStrategy(feed, instruments, smaPeriod)
# Attach a plotter to the strategy
plt = plotter.StrategyPlotter(myStrategy)
# Run the strategy
myStrategy.run()
print "Final portfolio value: $%.2f" % myStrategy.getBroker().getEquity()
# Plot the strategy.
plt.plot()
run_strategy(10)
来自pyalgotrade导入策略,绘图仪
从pyalgotrade.barfeed导入yahoofeed
来自pyalgotrade.technical import ma
从pyalgotrade.tools导入yahoofinance
类MyStrategy(strategy.BacktestingStrategy):
定义初始(自我、馈送、仪器、SMA周期):
strategy.BacktestingStrategy.\uuuuu初始化(self,feed,1000)
自身位置=无
#我们将使用调整后的关闭值,而不是常规的关闭值。
self.setUseAjustedValues(真)
self.u sma={}
自身仪器=仪器
对于仪表中的仪表:
self.\uu sma[instrument]=ma.sma(提要[instrument].getPriceDataSeries(),smaPeriod)
def ONNETROK(自身,位置):
execInfo=position.getEntryOrder().getExecutionInfo()
self.info(“以%.2f%的价格购买”(execInfo.getPrice())
def onEnterCanceled(自身,位置):
execInfo=position.getEntryOrder().getExecutionInfo()
def onExitOk(自身,位置):
execInfo=position.getExitOrder().getExecutionInfo()
self.info(“以%.2f%的价格出售”(execInfo.getPrice())
def ONEXIT已取消(自身、位置):
#如果退出被取消,请重新提交。
self.\u position[str(position.getEntryOrder().getInstrument())].exitMarket()
def onBars(自,条):
#等待足够的钢筋可用于计算SMA。
如果self.\u sma[-1]为无:
返回
巴=巴[自身仪器]
#如果一个仓位没有打开,检查我们是否应该进入多头仓位。
如果self.\u位置为无:
如果bar.getPrice()>self.\u sma[-1]:
#输入购买25股股票的市场订单。订单取消前一直有效。
self.\uuuu位置=self.enterLong(self.\uuuu仪器,25,真)
#检查我们是否必须退出该位置。
elif bar.getPrice()条的实例是来自不同仪器的条的集合,它们都具有相同的时间戳。因此,当调用onbar
事件时,实际上必须循环字典中的所有工具。需要在onbar
中进行循环,以检查每个工具的中断信号和提交顺序。有人能举个例子吗?