Skip to content

Commit

Permalink
Updated Market Timing strategy.
Browse files Browse the repository at this point in the history
  • Loading branch information
gbeced committed Jul 26, 2017
1 parent 0c9f35b commit 22cfb65
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 15 deletions.
3 changes: 3 additions & 0 deletions pyalgotrade/barfeed/csvfeed.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,9 @@ def setColumnName(self, col, name):
self.__columnNames[col] = name

def setDateTimeFormat(self, dateTimeFormat):
"""
Set the format string to use with strptime to parse datetime column.
"""
self.__dateTimeFormat = dateTimeFormat

def setBarClass(self, barClass):
Expand Down
35 changes: 25 additions & 10 deletions samples/market_timing.output
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
2014-05-25 22:36:59,740 yahoofinance [INFO] Creating data directory
2014-05-25 22:36:59,740 yahoofinance [INFO] Downloading SPY 2007 to data/SPY-2007-yahoofinance.csv
2014-05-25 22:37:06,332 yahoofinance [INFO] Downloading VTI 2007 to data/VTI-2007-yahoofinance.csv
2014-05-25 22:37:10,666 yahoofinance [INFO] Downloading DBC 2007 to data/DBC-2007-yahoofinance.csv
2014-05-25 22:37:13,102 yahoofinance [INFO] Downloading IEF 2007 to data/IEF-2007-yahoofinance.csv
2014-05-25 22:37:19,394 yahoofinance [INFO] Downloading VEU 2007 to data/VEU-2007-yahoofinance.csv
2014-05-25 22:37:27,378 yahoofinance [INFO] Downloading VNQ 2007 to data/VNQ-2007-yahoofinance.csv
2014-05-25 22:37:32,771 yahoofinance [INFO] Downloading SPY 2008 to data/SPY-2008-yahoofinance.csv
2014-05-25 22:37:38,326 yahoofinance [INFO] Downloading VTI 2008 to data/VTI-2008-yahoofinance.csv
2014-05-25 22:37:42,262 yahoofinance [INFO] Downloading DBC 2008 to data/DBC-2008-yahoofinance.csv
Loading bars from SPY-2007-yahoofinance.csv
Loading bars from VTI-2007-yahoofinance.csv
Loading bars from DBC-2007-yahoofinance.csv
Loading bars from IEF-2007-yahoofinance.csv
Loading bars from VEU-2007-yahoofinance.csv
Loading bars from VNQ-2007-yahoofinance.csv
Loading bars from SPY-2008-yahoofinance.csv
Loading bars from VTI-2008-yahoofinance.csv
Loading bars from DBC-2008-yahoofinance.csv
Loading bars from IEF-2008-yahoofinance.csv
.
.
.
2013-10-01 00:00:00 strategy [INFO] Best for class US Stocks: VTI
2013-10-01 00:00:00 strategy [INFO] Best for class Commodities: None
2013-10-01 00:00:00 strategy [INFO] Best for class US 10 Year Government Bonds: None
2013-10-01 00:00:00 strategy [INFO] Best for class Foreign Stocks: VEU
2013-10-01 00:00:00 strategy [INFO] Best for class Real Estate: None
2013-10-01 00:00:00 strategy [INFO] Placing market order for -2 VEU shares
2013-11-01 00:00:00 strategy [INFO] Rebalancing
2013-11-01 00:00:00 strategy [INFO] Best for class US Stocks: VTI
2013-11-01 00:00:00 strategy [INFO] Best for class Commodities: None
2013-11-01 00:00:00 strategy [INFO] Best for class US 10 Year Government Bonds: None
2013-11-01 00:00:00 strategy [INFO] Best for class Foreign Stocks: VEU
2013-11-01 00:00:00 strategy [INFO] Best for class Real Estate: VNQ
2013-11-01 00:00:00 strategy [INFO] Placing market order for -1 VTI shares
2013-11-01 00:00:00 strategy [INFO] Placing market order for -1 VEU shares
2013-11-01 00:00:00 strategy [INFO] Placing market order for 39 VNQ shares
2013-12-02 00:00:00 strategy [INFO] Rebalancing
2013-12-02 00:00:00 strategy [INFO] Best for class US Stocks: VTI
2013-12-02 00:00:00 strategy [INFO] Best for class Commodities: None
Expand Down
Binary file modified samples/market_timing.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 10 additions & 3 deletions samples/market_timing.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from pyalgotrade import strategy
from pyalgotrade import plotter
from pyalgotrade.tools import yahoofinance
from pyalgotrade.barfeed import yahoofeed
from pyalgotrade.technical import ma
from pyalgotrade.technical import cumret
from pyalgotrade.stratanalyzer import sharpe
Expand Down Expand Up @@ -137,12 +137,19 @@ def main(plot):
"Commodities": ["DBC"],
}

# Download the bars.
# Load the bars. These files were manually downloaded from Yahoo Finance.
feed = yahoofeed.Feed()
instruments = ["SPY"]
for assetClass in instrumentsByClass:
instruments.extend(instrumentsByClass[assetClass])
feed = yahoofinance.build_feed(instruments, 2007, 2013, "data", skipErrors=True)

for year in range(2007, 2013+1):
for instrument in instruments:
fileName = "%s-%d-yahoofinance.csv" % (instrument, year)
print "Loading bars from %s" % fileName
feed.addBarsFromCSV(instrument, fileName)

# Build the strategy and attach some metrics.
strat = MarketTiming(feed, instrumentsByClass, initialCash)
sharpeRatioAnalyzer = sharpe.SharpeRatio()
strat.attachAnalyzer(sharpeRatioAnalyzer)
Expand Down
3 changes: 1 addition & 2 deletions testcases/doc_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,15 +296,14 @@ def testQuandl(self):
)

def testMarketTiming(self):
init_data_path()
files = []
instruments = ["VTI", "VEU", "IEF", "VNQ", "DBC", "SPY"]
for year in range(2007, 2013+1):
for symbol in instruments:
fileName = "%s-%d-yahoofinance.csv" % (symbol, year)
files.append(os.path.join("samples", "data", fileName))

with common.CopyFiles(files, "data"):
with common.CopyFiles(files, "."):
code = """import sys
sys.path.append('samples')
import market_timing
Expand Down

0 comments on commit 22cfb65

Please sign in to comment.