Skip to content

Commit

Permalink
add stock price generator to DataGen
Browse files Browse the repository at this point in the history
  • Loading branch information
swharden committed Jul 27, 2019
1 parent 1a552fd commit 05f14a6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 15 deletions.
18 changes: 3 additions & 15 deletions demos/ScottPlotDemoCandlestick/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,10 @@ private void GenerateNewData()
Random rand = new Random();

int pointCount = 100;
ScottPlot.OHLC[] ohlcs = new ScottPlot.OHLC[pointCount];
double[] timestamps = new double[pointCount];
double[] volumes = new double[pointCount];
for (int i=0; i< ohlcs.Length; i++)
{
double open = rand.NextDouble() * 10 + 50;
double close = rand.NextDouble() * 10 + 50;
double high = Math.Max(open, close) + rand.NextDouble() * 10;
double low = Math.Min(open, close) - rand.NextDouble() * 10;
ohlcs[i] = new ScottPlot.OHLC(open, high, low, close, i);
timestamps[i] = i;
volumes[i] = 1000 + rand.NextDouble() * 500;
}
ScottPlot.OHLC[] ohlcs = ScottPlot.DataGen.RandomStockPrices(rand, pointCount);
double[] timestamps = ScottPlot.DataGen.Consecutive(pointCount);
double[] volumes = ScottPlot.DataGen.Random(rand, pointCount, 500, 1000);

//scottPlotUC1.plt.Style(ScottPlot.Style.Black);
//scottPlotUC1.plt.Grid(false);
scottPlotUC1.plt.Clear();
scottPlotUC1.plt.YLabel("Share Price", fontSize: 10);
scottPlotUC1.plt.Title("ScottPlot Candlestick Demo");
Expand Down
26 changes: 26 additions & 0 deletions src/ScottPlot/DataGen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,31 @@ public static double[] RandomWalk(Random rand, int pointCount, double mult = 1,
double span = maxVal - minVal;
return data;
}

public static OHLC[] RandomStockPrices(Random rand, int pointCount, double mult = 10, double startingPrice = 123.45)
{

double[] basePrices = ScottPlot.DataGen.RandomWalk(rand, pointCount, mult, startingPrice);

OHLC[] ohlcs = new OHLC[pointCount];

for (int i = 0; i < ohlcs.Length; i++)
{
double open = rand.NextDouble() * 10 + 50;
double close = rand.NextDouble() * 10 + 50;
double high = Math.Max(open, close) + rand.NextDouble() * 10;
double low = Math.Min(open, close) - rand.NextDouble() * 10;

// offset prices by randomwalk
open += basePrices[i];
close += basePrices[i];
high += basePrices[i];
low += basePrices[i];

ohlcs[i] = new ScottPlot.OHLC(open, high, low, close, i);
}

return ohlcs;
}
}
}

0 comments on commit 05f14a6

Please sign in to comment.