19 using System.Threading;
21 using System.Globalization;
31 [ProtoContract(SkipConstructor =
true)]
35 private const decimal _scaleFactor = 1 / 10000m;
37 private int _initialized;
38 private decimal _open;
39 private decimal _high;
46 public virtual decimal
Volume {
get;
set; }
52 public virtual decimal
Open
66 public virtual decimal
High
80 public virtual decimal
Low
94 public virtual decimal
Close
107 public override DateTime
EndTime
117 public virtual TimeSpan
Period {
get;
set; }
146 Time =
new DateTime(original.
Time.Ticks);
169 public TradeBar(DateTime time,
Symbol symbol, decimal open, decimal high, decimal low, decimal close, decimal volume, TimeSpan? period =
null)
222 return ParseCfd(config, line, date);
237 catch (Exception err)
239 Log.
Error(Invariant($
"TradeBar.Reader(): Error parsing line: '{line}', Symbol: {config.Symbol.Value}, SecurityType: ") +
240 Invariant($
"{config.SecurityType}, Resolution: {config.Resolution}, Date: {date:yyyy-MM-dd}, Message: {err}")
259 if (stream ==
null || stream.EndOfStream)
288 return ParseCfd(config, stream, date);
300 catch (Exception err)
302 Log.
Error(Invariant($
"TradeBar.Reader(): Error parsing stream, Symbol: {config.Symbol.Value}, SecurityType: ") +
303 Invariant($
"{config.SecurityType}, Resolution: {config.Resolution}, Date: {date:yyyy-MM-dd}, Message: {err}")
330 return ParseCfd(config, line, baseDate);
372 StreamParseScale(config, streamReader, date, useScaleFactor:
true, tradeBar,
true);
379 LineParseScale(config, line, date, useScaleFactor:
true, tradeBar, hasVolume:
true);
416 LineParseNoScale(config, line, date, tradeBar, hasVolume:
false);
436 LineParseNoScale(config, line, date, tradeBar);
449 return LineParseNoScale(config, line, date);
460 return StreamParseNoScale(config, streamReader, date);
472 return LineParseNoScale(config, line, date, hasVolume:
false);
484 return StreamParseNoScale(config, streamReader, date, hasVolume:
false);
525 return ParseForex(config, streamReader, date);
541 Period = config.Increment,
562 Period = config.Increment,
583 Period = config.Increment,
586 StreamParseNoScale(config, streamReader, date, tradeBar);
604 Period = config.Increment,
607 LineParseNoScale(config, line, date, tradeBar);
617 return LineParseNoScale(config, line, date);
631 var csv = line.ToCsv(hasVolume ? 6 : 5);
642 tradeBar.Open = csv[1].ToDecimal();
643 tradeBar.High = csv[2].ToDecimal();
644 tradeBar.Low = csv[3].ToDecimal();
645 tradeBar.Close = csv[4].ToDecimal();
648 tradeBar.Volume = csv[5].ToDecimal();
656 private static TradeBar StreamParseNoScale(SubscriptionDataConfig config, StreamReader streamReader, DateTime date,
TradeBar bar =
null,
bool hasVolume =
true)
660 Period = config.Increment,
667 tradeBar.Time = streamReader.GetDateTime().ConvertTo(config.DataTimeZone, config.ExchangeTimeZone);
672 tradeBar.Time = date.Date.AddMilliseconds(streamReader.GetInt32()).ConvertTo(config.DataTimeZone, config.ExchangeTimeZone);
674 tradeBar.Open = streamReader.GetDecimal();
675 tradeBar.High = streamReader.GetDecimal();
676 tradeBar.Low = streamReader.GetDecimal();
677 tradeBar.Close = streamReader.GetDecimal();
680 tradeBar.Volume = streamReader.GetDecimal();
685 private static TradeBar LineParseScale(SubscriptionDataConfig config,
string line, DateTime date,
bool useScaleFactor,
TradeBar bar =
null,
bool hasVolume =
true)
689 Period = config.Increment,
693 LineParseNoScale(config, line, date, tradeBar, hasVolume);
696 tradeBar.Open *= _scaleFactor;
697 tradeBar.High *= _scaleFactor;
698 tradeBar.Low *= _scaleFactor;
699 tradeBar.Close *= _scaleFactor;
705 private static TradeBar StreamParseScale(SubscriptionDataConfig config, StreamReader streamReader, DateTime date,
bool useScaleFactor,
TradeBar bar =
null,
bool hasVolume =
true)
709 Period = config.Increment,
713 StreamParseNoScale(config, streamReader, date, tradeBar, hasVolume);
716 tradeBar.Open *= _scaleFactor;
717 tradeBar.High *= _scaleFactor;
718 tradeBar.Low *= _scaleFactor;
719 tradeBar.Close *= _scaleFactor;
730 return StreamParseNoScale(config, streamReader, date);
742 return ParseOption<TradeBar>(config, line, date);
754 return ParseOption<TradeBar>(config, streamReader, date);
766 return ParseFuture<TradeBar>(config, line, date);
778 return ParseFuture<TradeBar>(config, streamReader, date);
790 public override void Update(decimal lastTrade, decimal bidPrice, decimal askPrice, decimal volume, decimal bidSize, decimal askSize)
792 Initialize(lastTrade);
793 if (lastTrade >
High)
High = lastTrade;
794 if (lastTrade <
Low)
Low = lastTrade;
832 var clone = base.
Clone(fillForward);
857 return $
"{Symbol}: " +
858 $
"O: {Open.SmartRounding()} " +
859 $
"H: {High.SmartRounding()} " +
860 $
"L: {Low.SmartRounding()} " +
861 $
"C: {Close.SmartRounding()} " +
862 $
"V: {Volume.SmartRounding()}";
869 private void Initialize(decimal value)
871 if (Interlocked.CompareExchange(ref _initialized, 1, 0) == 0)