/// <summary> /// Generates random market candle stream /// </summary> public static CandleSample[] GenerateRandom(int count, DateTime startDate, int msInterval, int msIntervalDeviation, int priceDirChangeEvery, int priceChangeAccel, float currentMidPrice) { if (count <= 0) count = 1; if (msInterval == 0) msInterval = 1000; if (priceDirChangeEvery <= 0) priceDirChangeEvery = 11; if (priceChangeAccel == 0) priceChangeAccel = 8; var result = new CandleSample[count]; var dt = startDate; var deltaT = msInterval; var priceVelocity = -1.0f + (2.0f * (float)ExternalRandomGenerator.Instance.NextRandomDouble); var priceSteps = 0; var price = currentMidPrice; for (var i = 0; i < count; i++) { var sample = new CandleSample(dt); dt = dt.AddMilliseconds(deltaT); if (msIntervalDeviation != 0) { deltaT += ExternalRandomGenerator.Instance.NextScaledRandomInteger(-msIntervalDeviation, msIntervalDeviation); if (deltaT == 0) deltaT = msInterval; if (i % 8 == 0) deltaT = msInterval; } priceSteps++; if (priceSteps >= ExternalRandomGenerator.Instance.NextScaledRandomInteger(priceDirChangeEvery - 4, priceDirChangeEvery + 4)) { var accel = (float)ExternalRandomGenerator.Instance.NextScaledRandomInteger(1, priceChangeAccel); priceVelocity = -accel + (2.0f * accel * (float)ExternalRandomGenerator.Instance.NextRandomDouble); priceSteps = 0; } price += priceVelocity; var pSample = i > 0 ? result[i - 1] : null; sample.OpenPrice = pSample != null ? pSample.ClosePrice : price; sample.ClosePrice = price + (float)ExternalRandomGenerator.Instance.NextScaledRandomDouble(-0.08f * currentMidPrice, +0.08f * currentMidPrice); sample.LowPrice = Math.Min(sample.OpenPrice, sample.ClosePrice) - (float)ExternalRandomGenerator.Instance.NextScaledRandomDouble(0, +0.05f * currentMidPrice); sample.HighPrice = Math.Max(sample.OpenPrice, sample.ClosePrice) + (float)ExternalRandomGenerator.Instance.NextScaledRandomDouble(0, +0.05f * currentMidPrice); result[i] = sample; } return result; }
public void AggregateSample(CandleSample sample) { if (sample == null) return; this.TimeSpanMs += sample.TimeSpanMs; if (sample.LowPrice < this.LowPrice) this.LowPrice = sample.LowPrice; if (sample.HighPrice > this.HighPrice) this.HighPrice = sample.HighPrice; this.BuyVolume += sample.BuyVolume; this.SellVolume += sample.SellVolume; this.ClosePrice = sample.ClosePrice; }