0
私はpyalgotradeを使って取引戦略を作成しています。私はticker(testlist)のリストを調べて、get_score関数を使って得たスコアと一緒に辞書(list_large {})に追加します。最近の問題は、辞書(list_large {})の各ティッカーが同じスコアを取得していることです。どんな考え?異なる入力と同じ値を返す関数
コード:
from pyalgotrade import strategy
from pyalgotrade.tools import yahoofinance
import numpy as np
import pandas as pd
from collections import OrderedDict
from pyalgotrade.technical import ma
from talib import MA_Type
import talib
smaPeriod = 10
testlist = ['aapl','ddd','gg','z']
class MyStrategy(strategy.BacktestingStrategy):
def __init__(self, feed, instrument):
super(MyStrategy, self).__init__(feed, 1000)
self.__position = []
self.__instrument = instrument
self.setUseAdjustedValues(True)
self.__prices = feed[instrument].getPriceDataSeries()
self.__sma = ma.SMA(feed[instrument].getPriceDataSeries(), smaPeriod)
def get_score(self,slope):
MA_Score = self.__sma[-1] * slope
return MA_Score
def onBars(self, bars):
global bar
bar = bars[self.__instrument]
slope = 8
for instrument in bars.getInstruments():
list_large = {}
for tickers in testlist: #replace with real list when ready
list_large.update({tickers : self.get_score(slope)})
organized_list = OrderedDict(sorted(list_large.items(), key=lambda t: -t[1]))#organize the list from highest to lowest score
print list_large
def run_strategy(inst):
# Load the yahoo feed from the CSV file
feed = yahoofinance.build_feed([inst],2015,2016, ".") # feed = yahoofinance.build_feed([inst],2015,2016, ".")
# Evaluate the strategy with the feed.
myStrategy = MyStrategy(feed, inst)
myStrategy.run()
print "Final portfolio value: $%.2f" % myStrategy.getBroker().getEquity()
def main():
instruments = ['ddd','msft']
for inst in instruments:
run_strategy(inst)
if __name__ == '__main__':
main()
毎回のself.get_scoreは、それが同じ値を返すべきではないと呼ばれています。この関数はself .__ sma [-1]をとり、それを勾配で乗算します...各ティッカーの移動平均が異なる場合、辞書に入れた値は異なるはずはありませんか?私は少し混乱しています... – RageAgainstheMachine
@RageAgainstheMachine: 'self .__ sma [-1] * slope'のあなたの理解は何ですか? 'self .__ sma'から最後のエントリを取得し、それに' slope'を掛けます。それはいつも同じではありませんか? –
私は最近のsmaを取るという印象を受けましたが、私は、リスト内の個々のティッカーに対して最新のsmaを取るようにコードを書いたと思いました。どうすればこの仕事をすることができますか? – RageAgainstheMachine