def _generate_price_history(self): try: price_fbm = FractionalBrownianMotion(t=self._times_to_generate, hurst=self._hurst) volume_gen = GaussianNoise(t=self._times_to_generate) except: self._generate_price_history() start_date = pd.to_datetime(self._start_date, format=self._start_date_format) price_volatility = price_fbm.sample(self._times_to_generate, zero=False) prices = price_volatility + self._base_price volume_volatility = volume_gen.sample(self._times_to_generate) volumes = volume_volatility * price_volatility + self._base_volume price_frame = pd.DataFrame([], columns=['date', 'price'], dtype=float) volume_frame = pd.DataFrame( [], columns=['date', 'volume'], dtype=float) price_frame['date'] = pd.date_range( start=start_date, periods=self._times_to_generate, freq="1min") price_frame['price'] = abs(prices) volume_frame['date'] = price_frame['date'].copy() volume_frame['volume'] = abs(volumes) price_frame.set_index('date') price_frame.index = pd.to_datetime(price_frame.index, unit='m', origin=start_date) volume_frame.set_index('date') volume_frame.index = pd.to_datetime(volume_frame.index, unit='m', origin=start_date) data_frame = price_frame['price'].resample(self._timeframe).ohlc() data_frame['volume'] = volume_frame['volume'].resample(self._timeframe).sum() self.data_frame = data_frame.astype(self._dtype)
def generate(price_fn: Callable[[ModelParameters], np.array], base_price: int = 1, base_volume: int = 1, start_date: str = '2010-01-01', start_date_format: str = '%Y-%m-%d', times_to_generate: int = 1000, time_frame: str = '1h', params: ModelParameters = None): delta = get_delta(time_frame) times_to_generate = scale_times_to_generate(times_to_generate, time_frame) params = params or default(base_price, times_to_generate, delta) prices = price_fn(params) volume_gen = GaussianNoise(t=times_to_generate) volumes = volume_gen.sample(times_to_generate) + base_volume start_date = pd.to_datetime(start_date, format=start_date_format) price_frame = pd.DataFrame([], columns=['date', 'price'], dtype=float) volume_frame = pd.DataFrame([], columns=['date', 'volume'], dtype=float) price_frame['date'] = pd.date_range(start=start_date, periods=times_to_generate, freq="1min") price_frame['price'] = abs(prices) volume_frame['date'] = price_frame['date'].copy() volume_frame['volume'] = abs(volumes) price_frame.set_index('date') price_frame.index = pd.to_datetime(price_frame.index, unit='m', origin=start_date) volume_frame.set_index('date') volume_frame.index = pd.to_datetime(volume_frame.index, unit='m', origin=start_date) data_frame = price_frame['price'].resample(time_frame).ohlc() data_frame['volume'] = volume_frame['volume'].resample(time_frame).sum() return data_frame
def fbm(base_price: int = 1, base_volume: int = 1, start_date: str = '2010-01-01', start_date_format: str = '%Y-%m-%d', times_to_generate: int = 1000, hurst: float = 0.61, time_frame: str = '1h'): times_to_generate = scale_times_to_generate(times_to_generate, time_frame) price_fbm = FractionalBrownianMotion(t=times_to_generate, hurst=hurst) price_volatility = price_fbm.sample(times_to_generate, zero=False) prices = price_volatility + base_price volume_gen = GaussianNoise(times_to_generate) volume_volatility = volume_gen.sample(times_to_generate) volumes = volume_volatility * price_volatility + base_volume start_date = pd.to_datetime(start_date, format=start_date_format) price_frame = pd.DataFrame([], columns=['date', 'price'], dtype=float) volume_frame = pd.DataFrame([], columns=['date', 'volume'], dtype=float) price_frame['date'] = pd.date_range(start=start_date, periods=times_to_generate, freq="1min") price_frame['price'] = abs(prices) volume_frame['date'] = price_frame['date'].copy() volume_frame['volume'] = abs(volumes) price_frame.set_index('date') price_frame.index = pd.to_datetime(price_frame.index, unit='m', origin=start_date) volume_frame.set_index('date') volume_frame.index = pd.to_datetime(volume_frame.index, unit='m', origin=start_date) data_frame = price_frame['price'].resample(time_frame).ohlc() data_frame['volume'] = volume_frame['volume'].resample(time_frame).sum() return data_frame
def gbm(base_price: int = 1, base_volume: int = 1, start_date: str = '2010-01-01', start_date_format: str = '%Y-%m-%d', times_to_generate: int = 1000, time_frame: str = '1h', params: 'ModelParameters' = None) -> 'pd.DataFrame': """Generates price data from a GBM process. Parameters ---------- base_price : int, default 1 The base price to use for price generation. base_volume : int, default 1 The base volume to use for volume generation. start_date : str, default '2010-01-01' The start date of the generated data start_date_format : str, default '%Y-%m-%d' The format for the start date of the generated data. times_to_generate : int, default 1000 The number of bars to make. time_frame : str, default '1h' The time frame. params : `ModelParameters`, optional The model parameters. Returns ------- `pd.DataFrame` The generated data frame containing the OHLCV bars. References ---------- [1] https://en.wikipedia.org/wiki/Geometric_Brownian_motion """ delta = get_delta(time_frame) times_to_generate = scale_times_to_generate(times_to_generate, time_frame) params = params or default(base_price, times_to_generate, delta) prices = geometric_brownian_motion_levels(params) volume_gen = GaussianNoise(t=times_to_generate) volumes = volume_gen.sample(times_to_generate) + base_volume start_date = pd.to_datetime(start_date, format=start_date_format) price_frame = pd.DataFrame([], columns=['date', 'price'], dtype=float) volume_frame = pd.DataFrame([], columns=['date', 'volume'], dtype=float) price_frame['date'] = pd.date_range(start=start_date, periods=times_to_generate, freq="1min") price_frame['price'] = abs(prices) volume_frame['date'] = price_frame['date'].copy() volume_frame['volume'] = abs(volumes) price_frame.set_index('date') price_frame.index = pd.to_datetime(price_frame.index, unit='m', origin=start_date) volume_frame.set_index('date') volume_frame.index = pd.to_datetime(volume_frame.index, unit='m', origin=start_date) data_frame = price_frame['price'].resample(time_frame).ohlc() data_frame['volume'] = volume_frame['volume'].resample(time_frame).sum() return data_frame
def test_gaussian_noise_sample(t, n, zero): instance = GaussianNoise(t) s = instance.sample(n) assert len(s) == n
def fbm(base_price: int = 1, base_volume: int = 1, start_date: str = '2010-01-01', start_date_format: str = '%Y-%m-%d', times_to_generate: int = 1000, hurst: float = 0.61, time_frame: str = '1h') -> 'pd.DataFrame': """Generates price data from the FBM process. Parameters ---------- base_price : int, default 1 The base price to use for price generation. base_volume : int, default 1 The base volume to use for volume generation. start_date : str, default '2010-01-01' The start date of the generated data start_date_format : str, default '%Y-%m-%d' The format for the start date of the generated data. times_to_generate : int, default 1000 The number of bars to make. hurst : float, default 0.61 The hurst parameter for the FBM process. time_frame : str, default '1h' The time frame. Returns ------- `pd.DataFrame` The generated data frame containing the OHLCV bars. References ---------- [1] https://en.wikipedia.org/wiki/Fractional_Brownian_motion """ times_to_generate = scale_times_to_generate(times_to_generate, time_frame) price_fbm = FractionalBrownianMotion(t=times_to_generate, hurst=hurst) price_volatility = price_fbm.sample(times_to_generate, zero=False) prices = price_volatility + base_price volume_gen = GaussianNoise(times_to_generate) volume_volatility = volume_gen.sample(times_to_generate) volumes = volume_volatility * price_volatility + base_volume start_date = pd.to_datetime(start_date, format=start_date_format) price_frame = pd.DataFrame([], columns=['date', 'price'], dtype=float) volume_frame = pd.DataFrame([], columns=['date', 'volume'], dtype=float) price_frame['date'] = pd.date_range(start=start_date, periods=times_to_generate, freq="1min") price_frame['price'] = abs(prices) volume_frame['date'] = price_frame['date'].copy() volume_frame['volume'] = abs(volumes) price_frame.set_index('date') price_frame.index = pd.to_datetime(price_frame.index, unit='m', origin=start_date) volume_frame.set_index('date') volume_frame.index = pd.to_datetime(volume_frame.index, unit='m', origin=start_date) data_frame = price_frame['price'].resample(time_frame).ohlc() data_frame['volume'] = volume_frame['volume'].resample(time_frame).sum() return data_frame
class VarianceGammaProcess(Continuous): r"""Variance Gamma process. .. image:: _static/variance_gamma_process.png :scale: 50% A variance gamma process has independent increments which follow the variance-gamma distribution. It can be represented as a Brownian motion with drift subordinated by a Gamma process: .. math:: \theta \Gamma(t; 1, \nu) + \sigma W(\Gamma(t; 1, \nu)) :param float t: the right hand endpoint of the time interval :math:`[0,t]` for the process :param float drift: the drift parameter of the Brownian motion, or :math:`\theta` above :param float variance: the variance parameter of the Gamma subordinator, or :math:`\nu` above :param float scale: the scale parameter of the Brownian motion, or :math:`\sigma` above """ def __init__(self, t=1, drift=0, variance=1, scale=1): super(VarianceGammaProcess, self).__init__(t) self.drift = drift self.variance = variance self.scale = scale self.gn = GaussianNoise(t) @property def drift(self): """Drift parameter.""" return self._drift @drift.setter def drift(self, value): self._check_number(value, "Drift") self._drift = value @property def variance(self): """Variance parameter.""" return self._variance @variance.setter def variance(self, value): self._check_positive_number(value, "Variance") self._variance = value @property def scale(self): """Scale parameter.""" return self._scale @scale.setter def scale(self, value): self._check_positive_number(value, "Scale") self._scale = value def _sample_variance_gamma_process(self, n, zero=True): """Generate a realization of a variance gamma process.""" self._check_increments(n) self._check_zero(zero) delta_t = 1.0 * self.t / n shape = delta_t / self.variance scale = self.variance gammas = np.random.gamma(shape=shape, scale=scale, size=n) gn = self.gn.sample(n) increments = self.drift * gammas + self.scale * np.sqrt(gammas) * gn samples = np.cumsum(increments) if zero: return np.concatenate(([0], samples)) else: return samples def _sample_variance_gamma_process_at(self, times): """Generate a realization of a variance gamma process.""" if times[0] != 0: zero = False times = np.array([0] + list(times)) else: zero = True shapes = np.diff(times) / self.variance scale = self.variance gammas = np.array([ np.random.gamma(shape=shape, scale=scale, size=1)[0] for shape in shapes ]) gn = self.gn.sample_at(times) increments = self.drift * gammas + self.scale * np.sqrt(gammas) * gn samples = np.cumsum(increments) if zero: samples = np.insert(samples, 0, [0]) return samples def sample(self, n, zero=True): """Generate a realization. :param int n: the number of increments to generate :param bool zero: if True, include :math:`t=0` """ return self._sample_variance_gamma_process(n, zero) def sample_at(self, times): """Generate a realization using specified times. :param times: a vector of increasing time values at which to generate the realization """ return self._sample_variance_gamma_process_at(times)
def generate(price_fn: 'Callable[[ModelParameters], np.array]', base_price: int = 1, base_volume: int = 1, start_date: str = '2010-01-01', start_date_format: str = '%Y-%m-%d', times_to_generate: int = 1000, time_frame: str = '1h', params: ModelParameters = None) -> 'pd.DataFrame': """Generates a data frame of OHLCV data based on the price model specified. Parameters ---------- price_fn : `Callable[[ModelParameters], np.array]` The price function generate the prices based on the chosen model. base_price : int, default 1 The base price to use for price generation. base_volume : int, default 1 The base volume to use for volume generation. start_date : str, default '2010-01-01' The start date of the generated data start_date_format : str, default '%Y-%m-%d' The format for the start date of the generated data. times_to_generate : int, default 1000 The number of bars to make. time_frame : str, default '1h' The time frame. params : `ModelParameters`, optional The model parameters. Returns ------- `pd.DataFrame` The data frame containing the OHLCV bars. """ delta = get_delta(time_frame) times_to_generate = scale_times_to_generate(times_to_generate, time_frame) params = params or default(base_price, times_to_generate, delta) prices = price_fn(params) volume_gen = GaussianNoise(t=times_to_generate) volumes = volume_gen.sample(times_to_generate) + base_volume start_date = pd.to_datetime(start_date, format=start_date_format) price_frame = pd.DataFrame([], columns=['date', 'price'], dtype=float) volume_frame = pd.DataFrame([], columns=['date', 'volume'], dtype=float) price_frame['date'] = pd.date_range(start=start_date, periods=times_to_generate, freq="1min") price_frame['price'] = abs(prices) volume_frame['date'] = price_frame['date'].copy() volume_frame['volume'] = abs(volumes) price_frame.set_index('date') price_frame.index = pd.to_datetime(price_frame.index, unit='m', origin=start_date) volume_frame.set_index('date') volume_frame.index = pd.to_datetime(volume_frame.index, unit='m', origin=start_date) data_frame = price_frame['price'].resample(time_frame).ohlc() data_frame['volume'] = volume_frame['volume'].resample(time_frame).sum() return data_frame