Search code examples
c#algorithmstockstochastic-process

Modify code to get synthetic data that trends smoothly from bull to bear market cycles


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()),

Solution

  • 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