1

I have this class that generates synthetic looking (stock) data and it works fine. However, I want to modify it so that NewPrice generates smooth trending data for say n-bars.

I know that if I reduce the volatility, I get smoother prices. However, not sure how to guarantee that the data goes into alternating persistant trend either up/down. A sine wave looking thing, but with stock looking prices, i.e, no negative prices.

Price = Trend + Previous Price + Random Component I am missing the trend component in the implementation below.

Any suggestions?

class SyntheticData
{
    public static double previous = 1.0;

    public static double NewPrice(double volatility, double rnd)
    {               
        var change_percent = 2 * volatility * rnd;
        if (change_percent > volatility)
            change_percent -= (2 * volatility);

        var change_amount = previous * change_percent;
        var new_price = previous + change_amount;
        previous = new_price;

        return new_price;
    }
}

Trade.previous = 100.0;
Price = Trade.NewPrice(.03, rnd.NextDouble()),
Ivan
  • 7,448
  • 14
  • 69
  • 134
  • 1
    Just for what its worth, you are better off getting real data, there are tons of it free and will give you less (well slightly less) chance of writing indicators or trading software that is curve matched or over optimized – TheGeneral Jun 05 '19 at 20:30

2 Answers2

4

Exponential smoothing or exponential moving average will create the type of data you want. Ideally, you would have existing stock price data that represents the type of time series that you want to generate. You fit an exponential smoothing model to your data. This will determine a number of parameters for that model. You can then use the model and its parameters to generate similar time series with the same kind of trends, and you can control the volatility (standard deviation) of the random variable associated with the model.

As an example of what you can do, in the image below the blue and yellow parts are from real data, and the green part is synthetic data generated with a model that was fit to the real data.

Exponential smoothing example

Time series forecasting is a large topic. I do not know how familiar you are with that topic. See Time Series Analysis, it covers a large range of time series providing clear presentations and examples in Excel. See exponential smoothing for more theoretical background

Here is a specific example of how such a time series can be generated. I chose one of the 30 exponential smoothing models, one that has additive trend and volatility, and no seasonal component. The equations for generating the time series are:

enter image description here

The time index is t, an integer. The values of the time series are yt. lt and bt are respectively the offset and slope components of the time series. Alpha and beta are parameters, and l-1 and b-1 are initial values of the offset and slope components. et is the value of a random variable that follows some distribution, e.g. normal. Alpha and beta must satisfy the relations below for stability of the time series.

enter image description here

To generate different time series you choose values for alpha, beta, l-1, b-1, and the standard deviation of et assuming a normal law, and calculate the successive values of yt. I have done this in Excel for several combinations of values. Here are several time series generated with this model. Sigma is the standard deviation (volatility) of et.

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

Here are the equations for the 30 models. N means no trend / seasonal component. A means additive component. M means multiplicative component. The d subscript indicates a variant that is damped. You can get all of the details from the references above.

enter image description here

enter image description here

RobertBaron
  • 2,817
  • 1
  • 12
  • 19
  • Was this the kind of things you were looking for? – RobertBaron Jun 06 '19 at 19:14
  • No. I am looking to generate synthetic data programatically. – Ivan Jun 06 '19 at 19:47
  • Well, this is exactly what you will be able to do with what is explained in the first link that I gave you. The mathematical formulas behind the time series described are all easy to program. I think the difficult party in solving your problem is to decide what time series model you want to use for the type of data that you want to synthesize. I can give you a piece of code that generates a random time series with trend following one model, it is only a few lines of code, but you need to select parameters that represent the type of data you want. – RobertBaron Jun 06 '19 at 19:58
  • @Ivan I added an example that shows how to calculate a random time series with the exponential smoothing models. – RobertBaron Jun 07 '19 at 21:11
  • thanks but that example doesn't cycle from bull to bear to bull to bear... markets. – Ivan Jun 08 '19 at 14:50
  • Yes I know, for cycles you have to use one of the seasonal models! – RobertBaron Jun 08 '19 at 14:56
  • I just wanted to show how easy it is to generate random time series. – RobertBaron Jun 08 '19 at 15:02
0

Something like this is what I was looking for:

public static double[] Sine(int n)
{
    const int FS = 64; // sampling rate

    return MathNet.Numerics.Generate.Sinusoidal(n, FS, 1.0, 20.0);
}

Although, it is not intuitive for a person that wants to deal in prices and time-based periodicity and not in mathematical functions.

https://numerics.mathdotnet.com/Generate.html

Ivan
  • 7,448
  • 14
  • 69
  • 134