Skip to content

Instantly share code, notes, and snippets.

@NMZivkovic
Created October 25, 2018 10:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save NMZivkovic/8011799a4411b4a78dd3109f4a7f5a78 to your computer and use it in GitHub Desktop.
Save NMZivkovic/8011799a4411b4a78dd3109f4a7f5a78 to your computer and use it in GitHub Desktop.
import pandas as pd
class StockPredictor(object):
def __init__(self, company, n_latency_days=10):
self._init_logger()
self.company = company
self.n_latency_days = n_latency_days
self.data = pd.read_csv(
'data/company_data/{company}.csv'.format(company=self.company))
def _init_logger(self):
self._logger = logging.getLogger(__name__)
handler = logging.StreamHandler()
formatter = logging.Formatter(
'%(asctime)s %(name)-12s %(levelname)-8s %(message)s')
handler.setFormatter(formatter)
self._logger.addHandler(handler)
self._logger.setLevel(logging.DEBUG)
@staticmethod
def _extract_features(data):
open_price = np.array(data['open'])
close_price = np.array(data['close'])
high_price = np.array(data['high'])
low_price = np.array(data['low'])
# Compute the fraction change in close, high and low prices
# which would be used a feature
frac_change = (close_price - open_price) / open_price
frac_high = (high_price - open_price) / open_price
frac_low = (open_price - low_price) / open_price
return np.column_stack((frac_change, frac_high, frac_low))
# Predictor for GOOGL stocks
stock_predictor = StockPredictor(company='GOOGL')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment