Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import pandas as pd
from prophet.orders import Orders
from prophet.data import DataGenerator
class BollingerEventStudy(DataGenerator):
name = "events"
def run(self, data, symbols, start, end, lookback, **kwargs):
bollinger_data = data['bollinger']
# Add an extra timestamp before close_data.index to be able
# to retrieve the prior day's data for the first day
start_index = bollinger_data.index.get_loc(start) - 1
timestamps = bollinger_data.index[start_index:]
# Find events that occur when the market is up more then 2%
bollinger_spy = bollinger_data['SPX'] >= 1.2 # Series
bollinger_today = bollinger_data.loc[timestamps[1:]] <= -2.0
bollinger_yesterday = bollinger_data.loc[timestamps[:-1]] >= -2.0
# When we look up a date in bollinger_yesterday,
# we want the data from the day before our input
def get_cache_filepath(self, name):
if not os.path.exists(self.CACHE_PATH):
os.makedirs(self.CACHE_PATH)
return os.path.join(self.CACHE_PATH, name)
def get_data_filepath(self, name):
if not os.path.exists(self.DATA_PATH):
os.makedirs(self.DATA_PATH)
return os.path.join(self.DATA_PATH, name)
def sanitize_name(self, name):
return name.replace(os.path.sep, '--')
class PandasDataGenerator(DataGenerator):
def __init__(self, cache_path=None, data_path=None):
super(PandasDataGenerator, self).__init__(cache_path=cache_path,
data_path=data_path)
def run(self, data, start, end, symbols, source, lookback=0):
data_start = self.get_data_start(start, lookback)
# Current caching implementation based on Zipline
symbols_data = dict()
for symbol in symbols:
symbol_path = self.sanitize_name(symbol)
cache_filename = "{stock}-{start}-{end}.csv".format(
stock=symbol_path, start=data_start, end=end
).replace(':', '-')
cache_filepath = self.get_cache_filepath(cache_filename)