gpt4 book ai didi

python - 如何在 Pyalgotrade 中使用多种工具创建复合策略?

转载 作者:太空狗 更新时间:2023-10-29 21:11:52 27 4
gpt4 key购买 nike

我正在使用 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()

最佳答案

您可以使用此示例作为交易多种工具的指南:http://gbeced.github.io/pyalgotrade/docs/v0.18/html/sample_statarb_erniechan.html

关于python - 如何在 Pyalgotrade 中使用多种工具创建复合策略?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41007429/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com