DEV Community

hmza
hmza

Posted on • Edited on

Stock Market Simulation? Pfft. This is MARKET CRASH SIMULATION! 🚨💥

Welcome to the Wildest Portfolio Ride of Your Life 🎢💥

  • Buckle up, because this rollercoaster was definitely designed by a caffeine-fueled dev with zero bug fixes and lots of chaos! Watch your portfolio dive faster than your morning coffee disappears and your code breaks after a midnight commit. Buy high, sell low? Nah, here we do buy panic, sell chaos — the market’s so buggy it’s basically a free fall simulator with random spikes and faceplants. 😎💸

Image description

I keep changing the price random change code but still:

The Code Saga: Take 4 🚀 (Or maybe the “Why is this still so wild?” edition)

DateTime now = DateTime.Now;

double timeFactor = 0.05 * Math.Sin(now.Second * Math.PI / 100);

int chance = rand.Next(100);
double percentChange = 0;

double baseDrift = 0.0005;

if (chance < 2)
    percentChange = 0.8 + rand.NextDouble() * 0.4;
else if (chance < 6)
    percentChange = 0.3 + rand.NextDouble() * 0.2;
else if (chance < 12)
    percentChange = -0.4 - rand.NextDouble() * 0.3;
else if (chance < 40)
    percentChange = -0.1 - rand.NextDouble() * 0.1;
else
    percentChange = (rand.NextDouble() - 0.5) * 0.02;

percentChange += baseDrift + timeFactor;

double price = lastPrice * (1 + percentChange);

if (price < 1)
    price = 1;

int index = chart1.Series["Series1"].Points.AddXY(now, price);

chart1.Series["Series1"].Points[index].Color = price >= lastPrice ? Color.Green : Color.Red;

lastPrice = price;

if (chart1.Series["Series1"].Points.Count > 30)
    chart1.Series["Series1"].Points.RemoveAt(0);

var area = chart1.ChartAreas["MainArea"];
area.AxisX.Minimum = chart1.Series["Series1"].Points[0].XValue;
area.AxisX.Maximum = chart1.Series["Series1"].Points[chart1.Series["Series1"].Points.Count - 1].XValue;

UpdateWorthLabel();
UpdateWalletLabel();

lbl_how_much_wortyh.Text = lastPrice.ToString("C2");
Enter fullscreen mode Exit fullscreen mode

Result

Image description


The Comeback: Take 6 or 7 — Smoother Chaos Edition 🎯

DateTime now = DateTime.Now;

// Smoothed time factor using total seconds with sine for more continuous oscillation
double timeFactor = 0.03 * Math.Sin(now.TimeOfDay.TotalSeconds * Math.PI / 30);

// Volatility factor that slightly varies over time to simulate market mood changes
double volatility = 0.01 + 0.02 * Math.Sin(now.TimeOfDay.TotalMinutes);

// Base drift to simulate general market trend (can be positive or negative)
double baseDrift = 0.0003; 

// Random chance for bigger jumps or drops
int chance = rand.Next(100);
double percentChange = 0;

if (chance < 3) // rare big jump up
    percentChange = 0.6 + rand.NextDouble() * 0.5;
else if (chance < 8) // moderate jump up
    percentChange = 0.2 + rand.NextDouble() * 0.3;
else if (chance < 15) // moderate drop
    percentChange = -0.4 - rand.NextDouble() * 0.25;
else if (chance < 45) // small drop
    percentChange = -0.12 - rand.NextDouble() * 0.08;
else // usual minor fluctuation
    percentChange = (rand.NextDouble() - 0.5) * volatility;

// Combine all factors with base drift and time oscillation for smooth random walk
percentChange += baseDrift + timeFactor;

// Calculate new price and ensure it doesn’t fall below 1
double price = lastPrice * (1 + percentChange);
if (price < 1)
    price = 1;

// Add new price point to chart
int index = chart1.Series["Series1"].Points.AddXY(now, price);

// Color the point green if price increased or red if decreased
chart1.Series["Series1"].Points[index].Color = price >= lastPrice ? Color.Green : Color.Red;

// Update lastPrice for next iteration
lastPrice = price;

// Keep only last 30 points to keep chart neat
if (chart1.Series["Series1"].Points.Count > 30)
    chart1.Series["Series1"].Points.RemoveAt(0);

// Update X-axis range dynamically
var area = chart1.ChartAreas["MainArea"];
area.AxisX.Minimum = chart1.Series["Series1"].Points[0].XValue;
area.AxisX.Maximum = chart1.Series["Series1"].Points[chart1.Series["Series1"].Points.Count - 1].XValue;

// Update UI labels
UpdateWorthLabel();
UpdateWalletLabel();

lbl_how_much_wortyh.Text = lastPrice.ToString("C2");

Enter fullscreen mode Exit fullscreen mode

Result

Image description

So... What’s Next? 🔮

  • Add news-driven volatility spikes: simulate “breaking news” that causes massive sudden jumps or drops.

  • Implement volume-based effects: bigger trades move the market more.

  • Try mean reversion: prices tend to bounce back to an average over time (for some sanity).

  • Add seasonality or periodic events — like earnings reports or holidays messing with the market mood.

  • And of course, more memes and chaos! Because if your portfolio isn't making you sweat, is it even real?

Last try!

Code

NSFW
Enter fullscreen mode Exit fullscreen mode

Result

Image description

SHIT

But then, I finally uncovered the secret ingredient

Image description
Image description
Image description

Image description

Top comments (3)

Collapse
 
hmzas profile image
hmza

Still random chaos

Image description
Image description

Collapse
 
hmzas profile image
hmza
  • After few minutes!

Image description

  • still crashes!
Collapse
 
hmzas profile image
hmza

PLEASE, I NEED HELP!

C# IS GETTING ONTO MY NERVES!