- 如何用30分鐘, 打造出自己獨特程式交易系統. 話不多說,我們就直接開始吧!!!!
- requirements
- Ta-lib(support finance indicators)
- bigdata(pandas, numpy, scipy, matplotlib...)
- Ta-lib Cython wrapper (wrapper Ta-lib to python)
- how to install in Mac
- install Ta-lib (port install ta-lib)
- install bigdata
- port install py27-pandas
- port install py27-numpy
- port install py27-matplotlib
- ...
- setup python search path for mac port
- add the below info to "~/.bashrc"
- "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages"
- source ~/.bashrc
- install Ta-lib(wrapper)
- https://github.com/funningboy/ta-lib
- example case
- 買點
- 利用5日平均線大於10日平均線
- 賣點
- 持有天數最多只能有三天
- 結論
- 還是實在賺錢比較重要
- ref:
- https://github.com/funningboy/ta-lib/blob/master/tools/tdr_jump.py
- results:
total profit is : -20.26
----------------------------------------------------------------------------------------------------
ma10 ma5 close high low open volume entry leave profit
2013-01-02 NaN NaN 723.25 727.00 716.55 719.42 2541300 0 0 0.00
2013-01-03 NaN NaN 723.67 731.93 720.72 724.93 2318200 0 0 0.00
import sys from datetime import datetime import numpy as np import talib import matplotlib.finance as fin from pylab import show from pandas import Index, DataFrame, Series from pandas.core.datetools import BMonthEnd from pandas import ols def getQuotes(symbol, start, end): """ get stock Quotes from Yahoo """ quotes = fin.quotes_historical_yahoo(symbol, start, end) dates, open, close, high, low, volume = zip(*quotes) data = { 'open': open, 'close': close, 'high': high, 'low': low, 'volume': volume } dates = Index([datetime.fromordinal(int(d)) for d in dates]) return DataFrame(data, index=dates) def getMA(quotes): """ get mv_avg indicator and update org quotes by joining two DataFrame """ ma5 = talib.MA(quotes['close'],5) ma10 = talib.MA(quotes['close'],10) data = { 'ma5' : ma5, 'ma10': ma10, } update = DataFrame(data, index=quotes.index) return update.join(quotes) def setEntryRule1(quotes): """ set entry rule when ma5 is cross-over ma10 """ def rule1(quotes, index): rst = 1 if quotes['ma5'][index] > quotes['ma10'][index] else 0 return rst quotes['entry'] = [rule1(quotes, index) for index in quotes.index] return quotes def setLeaveRule1(quotes): """ set leave rule when the holding day is large than 3 days""" def rule1(quotes, position): if position - 3 >= 0: if quotes['entry'][position-3] == 1: return [1, quotes['close'][position] - quotes['close'][position-3]] return [0, 0] quotes['leave'], quotes['profit'] = zip(*[rule1(quotes, position) for position, index in enumerate(quotes.index)]) return quotes def getProfit(quotes): """ get profit report """ print "total profit is : %s" %(sum(quotes['profit'])) print "-" * 100 print quotes def main(): # get stock info from start to end startDate = datetime(2013, 1, 1) endDate = datetime(2013, 1, 30) # get stock id goog = getQuotes('GOOG', startDate, endDate) goog = getMA(goog) goog = setEntryRule1(goog) goog = setLeaveRule1(goog) getProfit(goog) if __name__ == '__main__': main()
沒有留言:
張貼留言