Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mhamilton723/pytrading
A library for stock market analysis and automated trading, powered by sklearn.
https://github.com/mhamilton723/pytrading
Last synced: 16 days ago
JSON representation
A library for stock market analysis and automated trading, powered by sklearn.
- Host: GitHub
- URL: https://github.com/mhamilton723/pytrading
- Owner: mhamilton723
- License: other
- Created: 2015-11-04T04:59:51.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2015-12-17T22:56:20.000Z (almost 9 years ago)
- Last Synced: 2024-10-14T09:38:38.117Z (30 days ago)
- Language: Jupyter Notebook
- Homepage:
- Size: 44.9 MB
- Stars: 41
- Watchers: 8
- Forks: 17
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
#PyTrading
Python classes for automated stock trading.* Classes for portfolios and strategies
* Buy/hold, momentum, and linear/nonlinear regression strategy classes
* Backtesting functions defined for any strategy object
* example omega cluster scripts
* GPU enabled Recurrent Neural Nets, LSTMs for stock prediction##Install:
The current dependencies are mostly found in anaconda with the exception of theano and keras.
These are packages for running Neural Nets on GPUs and are not completely necessary for most of the files.
Theano comes with linux/mac anaconda but is a royal pain on windows. If you have trouble installing any of these feel free to shoot me an email.## Usage
PyTrading is a python package designed to facilitate the creation and testing of automated trading strategies.
The main object of the package is the strategy object.```
from Strategies import BuyAndHoldStrategy, backtesttickers = ['AAPL','VZ']
bs = BuyAndHoldStrategy(10000, tickers)
percent_gains = backtest(bs,start="2014-1-1", end="2015-11-02",correct=False)
print('Buy and hold strategy made a %{} return on investment'.format(round(percent_gains,1)) )
```
```
Buy and hold strategy made a %31.1 return on investment
```See the ipython notebook demo for more examples and usage!
##Needed:
* Unit tests
* More Stock Strategies
* tick data# TimeSeriesRegressor
A wrapper estimator that transforms any sklearn regressor into a time series predictor or sequence to sequence mapper.
The TSR internally transforms a regular dataset where the rows correspond to terms of a sequence into a sequence prediction dataset and
learns a sequence to sequence predictor.##Standalone
See https://github.com/mhamilton723/TimeSeriesRegressor for standalone files## Requires
Numpy, Pandas, SciKit-Learn,pickle## Usage
To make a predictor of the stock market that maps the previous two days of the s&p500 stock prices and
predicts the next day's price of AAPL stock try the following:
```
from TimeSeriesEstimator import TimeSeriesRegressor, time_series_split
from sklearn.linear_model import LinearRegression,Lasso
from utils import datasetsX = datasets('sp500')
y = X['AAPL']
X_train, X_test = time_series_split(X)
y_train, y_test = time_series_split(y)n_prev=2
tsr = TimeSeriesRegressor(Lasso(), n_prev=n_prev)
tsr.fit(X_train, y_train)
pred_train = tsr.predict(X_train) #outputs a numpy array of length: len(X_train)-n_prev
pred_test = tsr.predict(X_test)
```To forecast all stocks in the s&p500 100 days into the future use the forecast method:
```
tsr = TimeSeriesRegressor(LinearRegression(), n_prev=2)
tsr.fit(X_train)
fc = tsr.forecast(X_train, 100)
```
See the ipython notebook for a longer interactive example!## Install
Clone this repo and call directly as a module. Have not added automatic install support yet.##Mechanics
The TSR works by taking in a single (X) or two datasets (X,Y) of equal length.
In the single dataset case, the TSR assumes you would like to predict the next element in the dataset using the previous elements.
In either case it forms a dataset by taking the previous n timesteps and flattening them into a vector.Dataset X
Feature 1
Feature 21
1.52
2.53
3.54
4.55
5.5New X with n_prev = 2
Feature 1
Feature 2
Feature 3
Feature 41
1.5
2
2.52
2.5
3
3.53
3.5
4
4.5New Y with n_prev = 2
Feature 1
Feature 23
3.54
4.55
5.5Now the X and Y datasets can be fit by any regression technique in sklearn.
If the technique cannot handle vectors as outputs, use the "parallel_models" input to predict
each feature sequence with its own multi to single dim regressor.