我在交易策略中使用pyalgotrade,其中我想在一个列表中使用多个代码。
它现在的设置方式是,它为列表中的每个单独的滴头运行策略,但我希望它作为一个组合策略来运行它们。
,我该怎么做?
以下是代码:
from pyalgotrade.tools import yahoofinance
from pyalgotrade import strategy
from pyalgotrade.barfeed import yahoofeed
from pyalgotrade.technical import stoch
from pyalgotrade import dataseries
from pyalgotrade.technical import ma
from pyalgotrade import technical
from pyalgotrade.technical import highlow
from pyalgotrade import talibext
from pyalgotrade.talibext import indicator
import numpy as np
import talib
testlist = ['aapl', 'msft', 'z']
class MyStrategy( strategy.BacktestingStrategy ):
def __init__( self, feed, instrument ):
strategy.BacktestingStrategy.__init__( self, feed )
self.__position = []
self.__instrument = instrument
self.setUseAdjustedValues( True )
self.__prices = feed[instrument].getPriceDataSeries()
self.__stoch = stoch.StochasticOscillator( feed[instrument], 20, dSMAPeriod = 3, maxLen = 3 )
def onBars( self, bars ):
self.__PPO = talibext.indicator.PPO( self.__prices, len( self.__prices ), 12, 26, matype = 1 )
try: slope = talib.LINEARREG_SLOPE( self.__PPO, 3 )[-1]
except Exception: slope = np.nan
bar = bars[self.__instrument]
self.info( "%s,%s,%s" % ( bar.getClose(), self.__PPO[-1], slope ) )
if self.__PPO[-1] is None:
return
for inst in self.__instrument:
print inst
#INSERT STRATEGY HERE
def run_strategy():
# Load the yahoo feed from the CSV file
instruments = ['aapl', 'msft', 'z']
feed = yahoofinance.build_feed(instruments,2015,2016, ".")
# Evaluate the strategy with the feed.
myStrategy = MyStrategy(feed, instruments)
myStrategy.run()
print "Final portfolio value: $%.2f" % myStrategy.getBroker().getEquity()
run_strategy()发布于 2016-12-09 05:12:55
您可以使用此示例作为交易多个工具的指南:erniechan.html
发布于 2016-12-10 06:57:14
feed是pyalgotrade.feed.BaseFeed的实例。它包含一个或多个工具的BarDataSeries,它定义了一个价格序列。当打电话给feed[instrument]时,你会得到这个仪器的BarDataSeries。您需要一个for loop来处理每个单独的仪器。它将使您的代码干净,添加一个专门的类来处理每个工具。请参阅InstrumentManager类。我修复了很多程序错误。
class InstrumentManager():
def __init__(self, feed, instrument):
self.instrument = instrument
self.__prices = feed[instrument].getPriceDataSeries()
self.__stoch = stoch.StochasticOscillator( feed[instrument], 20, dSMAPeriod = 3, maxLen = 3 )
self.__signal = None
def onBars(self, bars):
bar = bars.getBar(self.instrument)
if bar:
self.__PPO = talibext.indicator.PPO( self.__prices, len( self.__prices ), 12, 26, matype = 1 )
try: slope = talib.LINEARREG_SLOPE( self.__PPO, 3 )[-1]
except Exception: slope = np.nan
print( "%s,%s,%s" % ( bar.getClose(), self.__PPO[-1], slope ) )
if self.__PPO[-1] is None:
return
# set signal in some conditions. eg self.__signal = 'buy'
def signal():
return self.__signal
class MyStrategy( strategy.BacktestingStrategy ):
def __init__( self, feed, instruments ):
strategy.BacktestingStrategy.__init__( self, feed )
self.__position = []
self.__feed = feed
self.__instruments = instruments
self.setUseAdjustedValues( True )
self.instManagers = {}
self.loadInstManagers()
def loadInstManagers(self):
for i in self.__instruments:
im = InstrumentManager(self.__feed, i)
self.instManagers[i] = im
def updateInstManagers(self, bars):
for im in self.instManagers.values():
im.onBars(bars)
def onBars( self, bars ):
self.updateInstManagers(bars)
for inst in self.__instruments:
instManager = self.instManagers[inst]
print inst
# Do something by instManager.signal()https://stackoverflow.com/questions/41007429
复制相似问题