19 using System.Threading;
21 using System.Globalization;
32 [ProtoContract(SkipConstructor =
true)]
36 private const decimal _scaleFactor = 1 / 10000m;
38 private int _initialized;
39 private decimal _open;
40 private decimal _high;
47 public virtual decimal
Volume {
get;
set; }
53 public virtual decimal
Open
67 public virtual decimal
High
81 public virtual decimal
Low
95 public virtual decimal
Close
109 public override DateTime
EndTime
120 public virtual TimeSpan
Period {
get;
set; }
149 Time =
new DateTime(original.
Time.Ticks);
172 public TradeBar(DateTime time,
Symbol symbol, decimal open, decimal high, decimal low, decimal close, decimal volume, TimeSpan? period =
null)
225 return ParseCfd(config, line, date);
240 catch (Exception err)
242 Log.
Error(Invariant($
"TradeBar.Reader(): Error parsing line: '{line}', Symbol: {config.Symbol.Value}, SecurityType: ") +
243 Invariant($
"{config.SecurityType}, Resolution: {config.Resolution}, Date: {date:yyyy-MM-dd}, Message: {err}")
262 if (stream ==
null || stream.EndOfStream)
291 return ParseCfd(config, stream, date);
303 catch (Exception err)
305 Log.
Error(Invariant($
"TradeBar.Reader(): Error parsing stream, Symbol: {config.Symbol.Value}, SecurityType: ") +
306 Invariant($
"{config.SecurityType}, Resolution: {config.Resolution}, Date: {date:yyyy-MM-dd}, Message: {err}")
333 return ParseCfd(config, line, baseDate);
375 StreamParseScale(config, streamReader, date, useScaleFactor:
true, tradeBar,
true);
382 LineParseScale(config, line, date, useScaleFactor:
true, tradeBar, hasVolume:
true);
419 LineParseNoScale(config, line, date, tradeBar, hasVolume:
false);
439 LineParseNoScale(config, line, date, tradeBar);
452 return LineParseNoScale(config, line, date);
463 return StreamParseNoScale(config, streamReader, date);
475 return LineParseNoScale(config, line, date, hasVolume:
false);
487 return StreamParseNoScale(config, streamReader, date, hasVolume:
false);
528 return ParseForex(config, streamReader, date);
544 Period = config.Increment,
565 Period = config.Increment,
586 Period = config.Increment,
589 StreamParseNoScale(config, streamReader, date, tradeBar);
607 Period = config.Increment,
610 LineParseNoScale(config, line, date, tradeBar);
620 return LineParseNoScale(config, line, date);
634 var csv = line.ToCsv(hasVolume ? 6 : 5);
645 tradeBar.Open = csv[1].ToDecimal();
646 tradeBar.High = csv[2].ToDecimal();
647 tradeBar.Low = csv[3].ToDecimal();
648 tradeBar.Close = csv[4].ToDecimal();
651 tradeBar.Volume = csv[5].ToDecimal();
659 private static TradeBar StreamParseNoScale(SubscriptionDataConfig config, StreamReader streamReader, DateTime date,
TradeBar bar =
null,
bool hasVolume =
true)
663 Period = config.Increment,
670 tradeBar.Time = streamReader.GetDateTime().ConvertTo(config.DataTimeZone, config.ExchangeTimeZone);
675 tradeBar.Time = date.Date.AddMilliseconds(streamReader.GetInt32()).ConvertTo(config.DataTimeZone, config.ExchangeTimeZone);
677 tradeBar.Open = streamReader.GetDecimal();
678 tradeBar.High = streamReader.GetDecimal();
679 tradeBar.Low = streamReader.GetDecimal();
680 tradeBar.Close = streamReader.GetDecimal();
683 tradeBar.Volume = streamReader.GetDecimal();
688 private static TradeBar LineParseScale(SubscriptionDataConfig config,
string line, DateTime date,
bool useScaleFactor,
TradeBar bar =
null,
bool hasVolume =
true)
692 Period = config.Increment,
696 LineParseNoScale(config, line, date, tradeBar, hasVolume);
699 tradeBar.Open *= _scaleFactor;
700 tradeBar.High *= _scaleFactor;
701 tradeBar.Low *= _scaleFactor;
702 tradeBar.Close *= _scaleFactor;
708 private static TradeBar StreamParseScale(SubscriptionDataConfig config, StreamReader streamReader, DateTime date,
bool useScaleFactor,
TradeBar bar =
null,
bool hasVolume =
true)
712 Period = config.Increment,
716 StreamParseNoScale(config, streamReader, date, tradeBar, hasVolume);
719 tradeBar.Open *= _scaleFactor;
720 tradeBar.High *= _scaleFactor;
721 tradeBar.Low *= _scaleFactor;
722 tradeBar.Close *= _scaleFactor;
733 return StreamParseNoScale(config, streamReader, date);
745 return ParseOption<TradeBar>(config, line, date);
757 return ParseOption<TradeBar>(config, streamReader, date);
769 return ParseFuture<TradeBar>(config, line, date);
781 return ParseFuture<TradeBar>(config, streamReader, date);
793 public override void Update(decimal lastTrade, decimal bidPrice, decimal askPrice, decimal volume, decimal bidSize, decimal askSize)
795 Initialize(lastTrade);
796 if (lastTrade >
High)
High = lastTrade;
797 if (lastTrade <
Low)
Low = lastTrade;
835 var clone = base.
Clone(fillForward);
860 return $
"{Symbol}: " +
861 $
"O: {Open.SmartRounding()} " +
862 $
"H: {High.SmartRounding()} " +
863 $
"L: {Low.SmartRounding()} " +
864 $
"C: {Close.SmartRounding()} " +
865 $
"V: {Volume.SmartRounding()}";
872 private void Initialize(decimal value)
874 if (Interlocked.CompareExchange(ref _initialized, 1, 0) == 0)