├── LICENSE.txt ├── pystrat ├── test │ └── __init__.py ├── __init__.py ├── trading_strategies.py ├── backtester.py └── pyfinance.py ├── MANIFEST.in ├── CHANGES.txt ├── README.md ├── setup.py ├── .gitattributes └── .gitignore /LICENSE.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /pystrat/test/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | include *.txt 2 | recursive-include docs *.txt -------------------------------------------------------------------------------- /CHANGES.txt: -------------------------------------------------------------------------------- 1 | v0.0.1, August 8, 2013 -- Initial Creation (Incomplete) -------------------------------------------------------------------------------- /pystrat/__init__.py: -------------------------------------------------------------------------------- 1 | #tradingstrategies.py 2 | #Charles J. Lai 3 | #August 10, 2013 -------------------------------------------------------------------------------- /pystrat/trading_strategies.py: -------------------------------------------------------------------------------- 1 | #tradingstrategies.py 2 | #Charles J. Lai 3 | #August 10, 2013 -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | PyStrat 2 | ======= 3 | 4 | PyStrat is a light-weight trading strategy backtester for python. It 5 | also contains a built-in API into Yahoo Finance and compares historical 6 | backtests of trading strategy against a benchmark strategy of holding the 7 | S&P 500. 8 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from distutils.core import setup 2 | 3 | setup( 4 | name='PyStrat', 5 | version='0.0.1', 6 | author='Charles J. Lai', 7 | author_email='cjl223@cornell.edu', 8 | packages=['pystrat', 'pystrat.test'], 9 | scripts=[], 10 | url='', 11 | license='LICENSE.txt', 12 | description='A python trading strategy backtesting system.', 13 | long_description=open('README.txt').read(), 14 | install_requires=[ 15 | ], 16 | ) -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | *.sln merge=union 7 | *.csproj merge=union 8 | *.vbproj merge=union 9 | *.fsproj merge=union 10 | *.dbproj merge=union 11 | 12 | # Standard to msysgit 13 | *.doc diff=astextplain 14 | *.DOC diff=astextplain 15 | *.docx diff=astextplain 16 | *.DOCX diff=astextplain 17 | *.dot diff=astextplain 18 | *.DOT diff=astextplain 19 | *.pdf diff=astextplain 20 | *.PDF diff=astextplain 21 | *.rtf diff=astextplain 22 | *.RTF diff=astextplain 23 | -------------------------------------------------------------------------------- /pystrat/backtester.py: -------------------------------------------------------------------------------- 1 | #backtester.py 2 | #Charles J. Lai 3 | #August 26, 2013 4 | 5 | class Backtester(object): 6 | #Fields (Hidden) 7 | _field = 0 # Field Comment 8 | 9 | #Properties (Get and set fields) 10 | @property 11 | def field(self): 12 | return self._field 13 | 14 | @field.setter 15 | def field(self, value): 16 | #assert value >= 1, "Use assert statement to enforce preconditions" 17 | self._field = value 18 | 19 | #Immutable Properties 20 | 21 | #Built-in Functions 22 | def __init__(self, field): 23 | self.field = field 24 | 25 | def method(self, value): 26 | pass -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ################# 2 | ## Eclipse 3 | ################# 4 | 5 | *.pydevproject 6 | .project 7 | .metadata 8 | bin/ 9 | tmp/ 10 | *.tmp 11 | *.bak 12 | *.swp 13 | *~.nib 14 | local.properties 15 | .classpath 16 | .settings/ 17 | .loadpath 18 | 19 | # External tool builders 20 | .externalToolBuilders/ 21 | 22 | # Locally stored "Eclipse launch configurations" 23 | *.launch 24 | 25 | # CDT-specific 26 | .cproject 27 | 28 | # PDT-specific 29 | .buildpath 30 | 31 | 32 | ################# 33 | ## Visual Studio 34 | ################# 35 | 36 | ## Ignore Visual Studio temporary files, build results, and 37 | ## files generated by popular Visual Studio add-ons. 38 | 39 | # User-specific files 40 | *.suo 41 | *.user 42 | *.sln.docstates 43 | 44 | # Build results 45 | 46 | [Dd]ebug/ 47 | [Rr]elease/ 48 | x64/ 49 | build/ 50 | [Bb]in/ 51 | [Oo]bj/ 52 | 53 | # MSTest test Results 54 | [Tt]est[Rr]esult*/ 55 | [Bb]uild[Ll]og.* 56 | 57 | *_i.c 58 | *_p.c 59 | *.ilk 60 | *.meta 61 | *.obj 62 | *.pch 63 | *.pdb 64 | *.pgc 65 | *.pgd 66 | *.rsp 67 | *.sbr 68 | *.tlb 69 | *.tli 70 | *.tlh 71 | *.tmp 72 | *.tmp_proj 73 | *.log 74 | *.vspscc 75 | *.vssscc 76 | .builds 77 | *.pidb 78 | *.log 79 | *.scc 80 | 81 | # Visual C++ cache files 82 | ipch/ 83 | *.aps 84 | *.ncb 85 | *.opensdf 86 | *.sdf 87 | *.cachefile 88 | 89 | # Visual Studio profiler 90 | *.psess 91 | *.vsp 92 | *.vspx 93 | 94 | # Guidance Automation Toolkit 95 | *.gpState 96 | 97 | # ReSharper is a .NET coding add-in 98 | _ReSharper*/ 99 | *.[Rr]e[Ss]harper 100 | 101 | # TeamCity is a build add-in 102 | _TeamCity* 103 | 104 | # DotCover is a Code Coverage Tool 105 | *.dotCover 106 | 107 | # NCrunch 108 | *.ncrunch* 109 | .*crunch*.local.xml 110 | 111 | # Installshield output folder 112 | [Ee]xpress/ 113 | 114 | # DocProject is a documentation generator add-in 115 | DocProject/buildhelp/ 116 | DocProject/Help/*.HxT 117 | DocProject/Help/*.HxC 118 | DocProject/Help/*.hhc 119 | DocProject/Help/*.hhk 120 | DocProject/Help/*.hhp 121 | DocProject/Help/Html2 122 | DocProject/Help/html 123 | 124 | # Click-Once directory 125 | publish/ 126 | 127 | # Publish Web Output 128 | *.Publish.xml 129 | *.pubxml 130 | 131 | # NuGet Packages Directory 132 | ## TODO: If you have NuGet Package Restore enabled, uncomment the next line 133 | #packages/ 134 | 135 | # Windows Azure Build Output 136 | csx 137 | *.build.csdef 138 | 139 | # Windows Store app package directory 140 | AppPackages/ 141 | 142 | # Others 143 | sql/ 144 | *.Cache 145 | ClientBin/ 146 | [Ss]tyle[Cc]op.* 147 | ~$* 148 | *~ 149 | *.dbmdl 150 | *.[Pp]ublish.xml 151 | *.pfx 152 | *.publishsettings 153 | 154 | # RIA/Silverlight projects 155 | Generated_Code/ 156 | 157 | # Backup & report files from converting an old project file to a newer 158 | # Visual Studio version. Backup files are not needed, because we have git ;-) 159 | _UpgradeReport_Files/ 160 | Backup*/ 161 | UpgradeLog*.XML 162 | UpgradeLog*.htm 163 | 164 | # SQL Server files 165 | App_Data/*.mdf 166 | App_Data/*.ldf 167 | 168 | ############# 169 | ## Windows detritus 170 | ############# 171 | 172 | # Windows image file caches 173 | Thumbs.db 174 | ehthumbs.db 175 | 176 | # Folder config file 177 | Desktop.ini 178 | 179 | # Recycle Bin used on file shares 180 | $RECYCLE.BIN/ 181 | 182 | # Mac crap 183 | .DS_Store 184 | 185 | 186 | ############# 187 | ## Python 188 | ############# 189 | 190 | *.py[co] 191 | 192 | # Packages 193 | *.egg 194 | *.egg-info 195 | dist/ 196 | build/ 197 | eggs/ 198 | parts/ 199 | var/ 200 | sdist/ 201 | develop-eggs/ 202 | .installed.cfg 203 | 204 | # Installer logs 205 | pip-log.txt 206 | 207 | # Unit test / coverage reports 208 | .coverage 209 | .tox 210 | 211 | #Translations 212 | *.mo 213 | 214 | #Mr Developer 215 | .mr.developer.cfg 216 | -------------------------------------------------------------------------------- /pystrat/pyfinance.py: -------------------------------------------------------------------------------- 1 | #yfinanceapi.py 2 | #Charles J. Lai 3 | #September 30, 2013 4 | import urllib 5 | 6 | """ 7 | ============== 8 | yfinanceapi.py 9 | ============== 10 | 11 | The following is a module containing a Python API into Yahoo Finance RESTful 12 | API for getting stock ticker information in CSV format. It includes an umbrella 13 | function call that returns a hash table of various relevant stock information 14 | as well as individual methods that return a single integer or string of relevant 15 | information. This module does not provide support for simultaneously getting data 16 | for 2 or more stock tickers. 17 | 18 | Common Parameters 19 | ----------------- 20 | symbol = stock ticker symbol for a given security is string form (ex. 'goog') 21 | 22 | Methods 23 | ------- 24 | get_all(symbol) 25 | 26 | 27 | """ 28 | def _fetch(symbol, parameter): 29 | """ 30 | Returns: List of values parsed from the Yahoo finance csv object. 31 | 32 | First, the function reads from a Yahoo finance csv url using an 33 | http request from urllib.urlopen(). 34 | 35 | Values of the csv object are parsed and placed into a list in the 36 | order of the "f=parameter" given. For example if "f=ns" then the 37 | name of the security is placed into the list first and followed 38 | by the symbol. 39 | """ 40 | url = 'http://finance.yahoo.com/d/quotes.csv?s=%s&f=%s' % (symbol, parameter) 41 | #Parse the values an place into a list 42 | return urllib.urlopen(url).read().strip().strip('"') 43 | 44 | #========================================================================= 45 | # Commonly Retrieved Values Function 46 | #========================================================================= 47 | def get_all(symbol): 48 | """ 49 | Returns: A hash table/dictionary of values. 50 | 51 | Gets commonly used data for a specific security and places it into a 52 | hash table of values. Look up keys are strings such as 'symbol', 'name' 53 | or 'dividend_yield'. A full list of fetched information can be gleaned 54 | from the following source code. 55 | """ 56 | values = _fetch(symbol, 'snxopl1c1ghva2j1dyjkm3m4erj4').split(',') 57 | stock_data = {} 58 | stock_data['symbol'] = values[0] 59 | stock_data['name'] = values[1] 60 | stock_data['exchange'] = values[2] 61 | stock_data['open'] = values[3] 62 | stock_data['prev_close'] = values[4] 63 | stock_data['price'] = values[5] 64 | stock_data['change'] = values[6] 65 | stock_data['days_low'] = values[7] 66 | stock_data['days_high'] = values[8] 67 | stock_data['volume'] = values[9] 68 | stock_data['avg_daily_volume'] = values[10] 69 | stock_data['market_cap'] = values[11] 70 | stock_data['dividend_per_share'] = values[12] 71 | stock_data['dividend_yield'] = values[13] 72 | stock_data['52_week_low'] = values[14] 73 | stock_data['52_week_high'] = values[15] 74 | stock_data['50_day_ma'] = values[16] 75 | stock_data['200_day_ma'] = values[17] 76 | stock_data['earnings_per_share'] = values[18] 77 | stock_data['price_earnings_ratio'] = values[19] 78 | stock_data['ebitda'] = values[20] 79 | return stock_data 80 | 81 | #========================================================================= 82 | # Historical Price Functions 83 | #========================================================================= 84 | def get_historical_prices(symbol, start_date, end_date): 85 | """ 86 | Returns: A list of historical prices for the given ticker symbol and 87 | range of dates. 88 | 89 | Preconditions: start_date and end_date are in 'YYYYMMDD' format. 90 | """ 91 | url = 'http://ichart.yahoo.com/table.csv?s=%s&' % symbol + \ 92 | 'd=%s&' % str(int(end_date[4:6]) - 1) + \ 93 | 'e=%s&' % str(int(end_date[6:8])) + \ 94 | 'f=%s&' % str(int(end_date[0:4])) + \ 95 | 'g=d&' + \ 96 | 'a=%s&' % str(int(start_date[4:6]) - 1) + \ 97 | 'b=%s&' % str(int(start_date[6:8])) + \ 98 | 'c=%s&' % str(int(start_date[0:4])) + \ 99 | 'ignore=.csv' 100 | days = urllib.urlopen(url).readlines() 101 | data = [day[:-2].split(',') for day in days] 102 | return data 103 | 104 | def get_historical_ma(symbol, amt, start_date, end_date): 105 | """ 106 | Returns: A list of histroical moving averages for a given 107 | ticker symbol, start date, end date, and moving average amount. 108 | """ 109 | date_offset = amt 110 | pass 111 | #========================================================================= 112 | # Individual Data Mining Functions 113 | #========================================================================= 114 | def get_symbol(symbol): 115 | """ 116 | Returns: A one-dimensional list containing the string representation 117 | of the ticker symbol for a given security. 118 | 119 | (Kind of redundant but here in order for the Yahoo Finance API to be 120 | fairly complete) 121 | """ 122 | return _fetch(symbol, 's') 123 | 124 | 125 | def get_name(symbol): 126 | """ 127 | Returns: A one-dimensional list containing the string representation 128 | of the name for a given security. 129 | """ 130 | return _fetch(symbol, 'n') 131 | 132 | 133 | def get_open(symbol): 134 | """ 135 | Returns: A one-dimensional list containing the string representation 136 | of the opening price for a given security. 137 | """ 138 | return _fetch(symbol, 'o') 139 | 140 | 141 | def get_prev_close(symbol): 142 | return _fetch(symbol, 'p') 143 | 144 | def get_price(symbol): 145 | return _fetch(symbol, 'l1') 146 | 147 | 148 | def get_change(symbol): 149 | return _fetch(symbol, 'c1') 150 | 151 | 152 | def get_volume(symbol): 153 | return _fetch(symbol, 'v') 154 | 155 | 156 | def get_avg_daily_volume(symbol): 157 | return _fetch(symbol, 'a2') 158 | 159 | 160 | def get_stock_exchange(symbol): 161 | return _fetch(symbol, 'x') 162 | 163 | 164 | def get_market_cap(symbol): 165 | return _fetch(symbol, 'j1') 166 | 167 | 168 | def get_book_value(symbol): 169 | return _fetch(symbol, 'b4') 170 | 171 | 172 | def get_ebitda(symbol): 173 | return _fetch(symbol, 'j4') 174 | 175 | 176 | def get_dividend_per_share(symbol): 177 | return _fetch(symbol, 'd') 178 | 179 | 180 | def get_dividend_yield(symbol): 181 | return _fetch(symbol, 'y') 182 | 183 | 184 | def get_earnings_per_share(symbol): 185 | return _fetch(symbol, 'e') 186 | 187 | 188 | def get_52_week_high(symbol): 189 | return _fetch(symbol, 'k') 190 | 191 | 192 | def get_52_week_low(symbol): 193 | return _fetch(symbol, 'j') 194 | 195 | 196 | def get_50day_moving_avg(symbol): 197 | return _fetch(symbol, 'm3') 198 | 199 | 200 | def get_200day_moving_avg(symbol): 201 | return _fetch(symbol, 'm4') 202 | 203 | 204 | def get_price_earnings_ratio(symbol): 205 | return _fetch(symbol, 'r') 206 | 207 | 208 | def get_price_earnings_growth_ratio(symbol): 209 | return _fetch(symbol, 'r5') 210 | 211 | 212 | def get_price_sales_ratio(symbol): 213 | return _fetch(symbol, 'p5') 214 | 215 | 216 | def get_price_book_ratio(symbol): 217 | return _fetch(symbol, 'p6') 218 | 219 | 220 | def get_short_ratio(symbol): 221 | return _fetch(symbol, 's7') 222 | --------------------------------------------------------------------------------