Skip to main content

Synthetic price production utility for historical and live data (WIP).

Project description

Synthetick Library

This is a Work In progress for a library to generate synthetic price time series at different levels of abstraction: tick, OHLC, etc.

At the bottom level the library generates tick data, on top of which it calculates price aggregations like OHLC or others.

So, essentially, tick price is at the core so the model how tick price is calculated is presented in the next section.

Tick Data Model

Core Model

Let $p_0$ be the initial price value for the time series $P$, with $n$ elements, then the next price elements of the series are calculated as follows:

$p_1 = p_0 + \Delta p_1$

$p_2 = p_1 + \Delta p_2$

...

$p_{n-2} = p_{n-3} + \Delta p_{n-2}$ (1)

$p_{n-1} = p_{n-2} + \Delta p_{n-1}$ (2)

$p_{n} = p_{n-1} + \Delta p_{n}$ (3)

Where:

$\Delta p_i$ Is the return or price change for period $i$

The general term is:

$p_{i} = p_{i-1} + \Delta p{i}, i \in [1, 2, ... n]$

Replacing (2) in (3)

$p_n = p_{n-2} + \Delta p_{n-1} + \Delta p_{n} (4)$

If we now repeat the process to replace (1) in (4)

$p_n = p_{n-3} + \Delta p_{n-2} + \Delta p_{n-1} + \Delta p_{n} (4)$

Repeating the process until $p_1$ is replaced in the resultant formula:

$p_n = p_{0} + \sum\limits_{i=1}^n \Delta p_i$

If $\Delta p_{i}$ is produced by an stochastic process, then the series has the characteristics of a Random Walk.

Making $\Delta p_{i} \approx N(\mu, \sigma)$ a normal distribution with mean $\mu$ and standard deviation $\sigma$

If $\mu$ is 0, then the price generation process is an unbiased random walk, but as will be shown later, using $\mu \neq 0$ (biased random walk) it is possible to control the price trend: up (long), range or down (short)

With $\sigma$ it is possible to control price volatility.

To wrap up this section, the tick price generation process has three parameters:

  • $p_0$: First price in the series, necessary to calculate the remaining $n-1$ elements, with $n$ being the number of elements in the series.
  • $\mu$: Mean for the distribution of returns or price change.
  • $\sigma$: Standard deviation for the returns distribution.

Bid, Ask, Spread

The price of a financial asset comes in pairs: the price at wich you buy or Ask price, and the price at wich you sell or Bid price. So for each new tick price, you need two values: Bid and Ask.

The difference between both is the Spread:

$spread_i = Ask_i - Bid_i$

So to generate tick price it is needed to generate two time series: bid and ask

The library generates Bid parice first (As described in Core Model), then calculates Ask as a function of Bid and Spread:

$Ask_i = Bid_i + Spread_i$

Where:

$Spread \in (SPREAD_{min}, SPREAD_{max})$ is calculated as a random value with unifoirm dsitribution between $SPREAD_{min}$ and $SPREAD_{max}$

This calculation adds two new parameters to the model:

$spread_{min}$: Minimum value for the spread

$spread_{max}$ Maximum value for the spread

Price Aggregations

Price aggregations are data reductions calaculated as a result of applying functions on tick data sampled as follows:

  • Fix period of time (candles bars or price bars),
  • Fix number of ticks (tick bars)
  • Fix price change (renko bars, pip bars).

So far only aggregatoions for fix periods of time are supported.

All the aggregation produce a structure named OHLC, which stands for Open, High, Low, Close. These aggregations can be represented as vectors:

$[open{-}time_i, close{-}time_i, open_i, high_i, low_i, close_i]$

tick.price_time_series["bid"].resample(<tick-data-time-series>).ohlc()

For more datails read here

TODO: add Renko and Tick Bars

How to Use

Install

pip install synthetick

How to use

Generating tick data.

This example generates a time series of tick data with a frequency of 1 socond, uptrending, with a volatility range of 10 pips, a spread range from 0.5 to 3 pips, with the pip position at the 4th decimal place.

from datetime import datetime
import pandas as pd
from synthetick.synthetick import Ticks

DATE_FROM: datetime = pd.to_datetime("2023-01-01 00:00:00")
DATE_TO: datetime = pd.to_datetime("2023-02-01 00:00:00")

tick_data_generator = Ticks(trend=0.01,
                            volatility_range=10,
                            spread_min=0.5,
                            spread_max=3,
                            pip_position=-4,
                            remove_weekend=True)

tick_data_generator._compute_date_range(date_from=DATE_FROM,
                                        date_to=DATE_TO,
                                        frequency="1s",
                                        init_value=1.1300)

tick_data_generator.price_time_series.to_csv("test_tick_happy_path.csv", index_label="date-time")

tick_data_generator.price_time_series[300:350][["bid", "ask"]].plot(figsize=(10, 3), marker=".", cmap="PiYG")

Generating OHLC Data
from datetime import datetime
import pandas as pd
from synthetick.synthetick import OHLC
import mplfinance as mpf

DATE_FROM: datetime = pd.to_datetime("2023-01-01 00:00:00")
DATE_TO: datetime = pd.to_datetime("2023-02-01 00:00:00")

ohlc: OHLC = OHLC(trend=0.0001,
                  volatility_range=10,
                  spread_min=0.5,
                  spread_max=3,
                  pip_position=-4,
                  remove_weekend=True,
                  tick_frequency="1s",
                  time_frame="H")

ohlc.produce(date_from=DATE_FROM, date_to=DATE_TO, init_value=1.300)
ohlc.ohlc_time_series["bid"].to_csv("ohlc_bid_1h.csv", index_label="date-time")

mc2 = mpf.make_marketcolors(up='blue', down='r')
s2 = mpf.make_mpf_style(marketcolors=mc2)
mpf.plot(ohlc.ohlc_time_series["bid"][200:400], type="candle", figsize=(15, 4), style=s2)

TODO's

  1. Improve documentation
  2. Produce ticks at random intervals.
  3. Change trend when price reaches zero level

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

synthetick-0.2.0.tar.gz (496.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

synthetick-0.2.0-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file synthetick-0.2.0.tar.gz.

File metadata

  • Download URL: synthetick-0.2.0.tar.gz
  • Upload date:
  • Size: 496.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.6

File hashes

Hashes for synthetick-0.2.0.tar.gz
Algorithm Hash digest
SHA256 27c2b5930878fb0976c2eeca610fd48b4ec881814ab41ff5dbc3a7ec8eaa089e
MD5 358fbb78291ea7686660c1ea08d9e836
BLAKE2b-256 6322b77a004118efada7b8eb3471dbe72a974f641921dd426b5a60c40dc79c9e

See more details on using hashes here.

File details

Details for the file synthetick-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: synthetick-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 8.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.6

File hashes

Hashes for synthetick-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9fbdfbda4c25a356a82725b369b0448700b3c63ff9da99529886420ef2f5cd6a
MD5 81bc592438c931cf5dfaa16d02f13803
BLAKE2b-256 2bf0a7005bee667d492b95ecc891aac4dc762b29e2063603e2964c06f2bbbc9c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page