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.
Let
...
Where:
The general term is:
Replacing (2) in (3)
If we now repeat the process to replace (1) in (4)
Repeating the process until
If
Making
If
With
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.
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. So for each new tick price, you need two values: Bid and Ask.
The difference between both is the Spread:
So to generate tick price it is needed to generate two time series.
The library generates Bid parice first (As described in Core Model), then calculates Ask as a function of Bid and Spread:
Where:
This calculation adds two new parameters to the model:
Price aggregations are data reductions by nean of applying functions on tick data for a period of time (candles or price bars), for a fix number of ticks (tick bars) or for a fix price change (renko bars).
So far only OHLC data is supported and is claculated using pandas library
tick.price_time_series["bid"].resample(<tick-data-time-series>).ohlc()
For more datails read here
TODO: add Renko and Tick Bars
pip install synthetick
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")
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)
- Improve documentation
- Produce ticks at random intervals.
- Change trend when price reaches zero level