private void RWTUpdateCounts(double close) { if ((close != prices[0]) && (close != prices[1])) { if (close < prices[0]) { curdat.addLevel(-1); prices[0] = close; lastMoveUp = false; } else if (close > prices[1]) { curdat.addLevel(1); prices[1] = close; lastMoveUp = true; } else if (lastMoveUp) { prices[0] = close; } else { prices[1] = close; } } }
/// <summary> /// </summary> /// <param name="bars"></param> /// <param name="open"></param> /// <param name="high"></param> /// <param name="low"></param> /// <param name="close"></param> /// <param name="time"></param> /// <param name="volume"></param> /// <param name="isRealtime"></param> public override void Add(Data.Bars bars, double open, double high, double low, double close, DateTime time, long volume, bool isRealtime) { if ((curdat == null) || (time < curdat.dt)) { extdat.Clear(); extdat.TrimExcess(); curdat = null; tickSize = bars.Instrument.MasterInstrument.TickSize; } if (bars.Count == 0 || bars.IsNewSession(time, isRealtime)) { AddBar(bars, open, high, low, close, time, volume, isRealtime); lastBarTime = time; RWTAddExtDat(time, close, volume); } else { Data.Bar bar = (Bar)bars.Get(bars.Count - 1); //double tickSize = bars.Instrument.MasterInstrument.TickSize; double rangeValue = Math.Floor(10000000.0 * (double)bars.Period.Value * tickSize) / 10000000.0; if (curdat == null) { RWTAddExtDat(time, close, 0); } curdat.dt = time; if ((bars.Instrument.MasterInstrument.Compare(close, bar.Low + rangeValue) > 0) && (lastBarUP || ((time.Ticks - lastBarTime.Ticks) > 10000000L))) { bool isFirstNewBar = true; double newClose = bar.Low + rangeValue; // every bar closes either with high or low UpdateBar(bars, bar.Open, newClose, bar.Low, newClose, time, 0, isRealtime); curdat.addLevel(1); // if still gap, fill with phantom bars double newBarOpen = newClose + tickSize; while (bars.Instrument.MasterInstrument.Compare(close, newClose) > 0) { newClose = Math.Min(close, newBarOpen + rangeValue); AddBar(bars, newBarOpen, newClose, newBarOpen, newClose, time, isFirstNewBar ? volume : 1, isRealtime); RWTAddExtDat(time, newClose, (isFirstNewBar?volume:0)); newBarOpen = newClose + tickSize; isFirstNewBar = false; } prices[1] = newClose; lastMoveUp = true; lastBarUP = true; lastBarTime = time; } else if ((bars.Instrument.MasterInstrument.Compare(bar.High - rangeValue, close) > 0) && ((!lastBarUP) || ((time.Ticks - lastBarTime.Ticks) > 10000000L))) { bool isFirstNewBar = true; double newClose = bar.High - rangeValue; // every bar closes either with high or low UpdateBar(bars, bar.Open, bar.High, newClose, newClose, time, 0, isRealtime); curdat.addLevel(-1); // if still gap, fill with phantom bars double newBarOpen = newClose - tickSize; while (bars.Instrument.MasterInstrument.Compare(newClose, close) > 0) { newClose = Math.Max(close, newBarOpen - rangeValue); AddBar(bars, newBarOpen, newBarOpen, newClose, newClose, time, isFirstNewBar ? volume : 1, isRealtime); RWTAddExtDat(time, newClose, (isFirstNewBar?volume:0)); newBarOpen = newClose - tickSize; isFirstNewBar = false; } prices[0] = newClose; lastMoveUp = false; lastBarUP = false; lastBarTime = time; } else { UpdateBar(bars, open, (close > bar.High ? close : bar.High), (close < bar.Low ? close : bar.Low), close, time, volume, isRealtime); RWTUpdateCounts(close); RWTUpdateCurdat(close, volume); } } bars.LastPrice = close; // update the extended data... prevClose = close; }
/// <summary> /// </summary> /// <param name="bars"></param> /// <param name="open"></param> /// <param name="high"></param> /// <param name="low"></param> /// <param name="close"></param> /// <param name="time"></param> /// <param name="volume"></param> /// <param name="isRealtime"></param> public override void Add(Data.Bars bars, double open, double high, double low, double close, DateTime time, long volume, bool isRealtime) { if ((curdat == null) || (time < curdat.dt)) { extdat.Clear(); extdat.TrimExcess(); curdat = null; tickSize = bars.Instrument.MasterInstrument.TickSize; if (bars.Period.Value > 100) { tlb = true; period = bars.Period.Value - 100; } else { tlb = false; period = bars.Period.Value; } } if (bars.Count == 0 || bars.IsNewSession(time, isRealtime)) { AddBar(bars, open, high, low, close, time, volume, isRealtime); lastBarTime = time; RWTAddExtDat(time, close, volume); } else { Data.Bar bar = (Bar)bars.Get(bars.Count - 1); double rangeValue = Math.Floor(10000000.0 * period * tickSize) / 10000000.0; if (curdat == null) { RWTAddExtDat(time, close, 0); } curdat.dt = time; double reverseRange = rangeValue * (tlb?3.0:1.0); if ((bars.Instrument.MasterInstrument.Compare(close, bar.Open + (lastBarUP?rangeValue:reverseRange)) > 0) && (lastBarUP || ((time.Ticks - lastBarTime.Ticks) > 10000000L))) { bool isFirstNewBar = true; double newBarOpen = bar.Open + (lastBarUP?rangeValue:reverseRange); double newClose = Math.Min(close, newBarOpen + rangeValue); UpdateBar(bars, bar.Open, newBarOpen, bar.Low, newBarOpen, time, 0, isRealtime); curdat.addLevel(1); do { AddBar(bars, newBarOpen, newClose, newBarOpen, newClose, time, isFirstNewBar ? volume : 0, isRealtime); RWTAddExtDat(time, newClose, (isFirstNewBar ? volume : 0)); if (bars.Instrument.MasterInstrument.Compare(close, newClose) == 0) { break; } newBarOpen = newClose; newClose = Math.Min(close, newBarOpen + rangeValue); isFirstNewBar = false; }while (true); prices[1] = newClose; lastMoveUp = true; lastBarUP = true; lastBarTime = time; } else if ((bars.Instrument.MasterInstrument.Compare(bar.Open - (lastBarUP?reverseRange:rangeValue), close) > 0) && ((!lastBarUP) || ((time.Ticks - lastBarTime.Ticks) > 10000000L))) { bool isFirstNewBar = true; double newBarOpen = bar.Open - (lastBarUP?reverseRange:rangeValue); double newClose = Math.Max(close, newBarOpen - rangeValue); UpdateBar(bars, bar.Open, bar.High, newBarOpen, newBarOpen, time, 0, isRealtime); curdat.addLevel(-1); do { AddBar(bars, newBarOpen, newBarOpen, newClose, newClose, time, isFirstNewBar ? volume : 0, isRealtime); RWTAddExtDat(time, newClose, (isFirstNewBar ? volume : 0)); if (bars.Instrument.MasterInstrument.Compare(close, newClose) == 0) { break; } newBarOpen = newClose; newClose = Math.Max(close, newBarOpen - rangeValue); isFirstNewBar = false; }while (true); prices[0] = newClose; lastMoveUp = false; lastBarUP = false; lastBarTime = time; } else { UpdateBar(bars, bar.Open, Math.Max(bar.High, close), Math.Min(bar.Low, close), close, time, volume, isRealtime); RWTUpdateCounts(close); RWTUpdateCurdat(close, volume); } } bars.LastPrice = close; // update the extended data... prevClose = close; }