Skip to content

Commit

Permalink
Storage. Fix candles fractional prices http://stocksharp.com/forum/87…
Browse files Browse the repository at this point in the history
  • Loading branch information
mikasoukhov committed Nov 4, 2017
1 parent 5d575b0 commit 2d56dae
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 25 deletions.
13 changes: 9 additions & 4 deletions Algo/Storages/Binary/BinaryHelper.cs
Expand Up @@ -137,7 +137,12 @@ public static void WritePrice(this BitArrayWriter writer, decimal price, ref dec
if (useLong)
writer.WriteLong(stepCount);
else
{
if (stepCount.Abs() > int.MaxValue)
throw new InvalidOperationException("Range is overflow.");

writer.WriteInt((int)stepCount);
}

prevPrice = price;
}
Expand All @@ -147,7 +152,7 @@ public static void WritePrice(this BitArrayWriter writer, decimal price, ref dec
}
}

public static void WritePriceEx(this BitArrayWriter writer, decimal price, BinaryMetaInfo info, SecurityId securityId)
public static void WritePriceEx(this BitArrayWriter writer, decimal price, BinaryMetaInfo info, SecurityId securityId, bool useLong = false)
{
if (info.Version < MarketDataVersions.Version41)
{
Expand All @@ -166,7 +171,7 @@ public static void WritePriceEx(this BitArrayWriter writer, decimal price, Binar
info.FirstPrice = info.LastPrice = price;

var prevPrice = info.LastPrice;
writer.WritePrice(price, ref prevPrice, info, securityId);
writer.WritePrice(price, ref prevPrice, info, securityId, useLong);
info.LastPrice = price;
}
else
Expand Down Expand Up @@ -200,7 +205,7 @@ public static decimal ReadPrice(this BitArrayReader reader, ref decimal prevPric
}
}

public static decimal ReadPriceEx(this BitArrayReader reader, BinaryMetaInfo info)
public static decimal ReadPriceEx(this BitArrayReader reader, BinaryMetaInfo info, bool useLong = false)
{
if (info.Version < MarketDataVersions.Version41)
{
Expand All @@ -212,7 +217,7 @@ public static decimal ReadPriceEx(this BitArrayReader reader, BinaryMetaInfo inf
if (reader.Read())
{
var prevPrice = info.FirstPrice;
return info.FirstPrice = reader.ReadPrice(ref prevPrice, info);
return info.FirstPrice = reader.ReadPrice(ref prevPrice, info, useLong);
}
else
{
Expand Down
44 changes: 23 additions & 21 deletions Algo/Storages/Binary/CandleBinarySerializer.cs
Expand Up @@ -91,7 +91,7 @@ class CandleBinarySerializer<TCandleMessage> : BinaryMarketDataSerializer<TCandl
private readonly object _arg;

public CandleBinarySerializer(SecurityId securityId, object arg, IExchangeInfoProvider exchangeInfoProvider)
: base(securityId, 74, MarketDataVersions.Version57, exchangeInfoProvider)
: base(securityId, 74, MarketDataVersions.Version58, exchangeInfoProvider)
{
if (arg == null)
throw new ArgumentNullException(nameof(arg));
Expand Down Expand Up @@ -122,6 +122,7 @@ protected override void OnSave(BitArrayWriter writer, IEnumerable<TCandleMessage
var allowDiffOffsets = metaInfo.Version >= MarketDataVersions.Version53;
var bigRange = metaInfo.Version >= MarketDataVersions.Version57;
var isTickPrecision = bigRange;
var useLong = metaInfo.Version >= MarketDataVersions.Version58;

foreach (var candle in candles)
{
Expand Down Expand Up @@ -157,24 +158,24 @@ protected override void OnSave(BitArrayWriter writer, IEnumerable<TCandleMessage
}
else
{
writer.WritePriceEx(candle.LowPrice, metaInfo, SecurityId);
writer.WritePriceEx(candle.LowPrice, metaInfo, SecurityId, useLong);

if (candle.OpenPrice <= candle.ClosePrice)
{
writer.Write(true);

writer.WritePriceEx(candle.OpenPrice, metaInfo, SecurityId);
writer.WritePriceEx(candle.ClosePrice, metaInfo, SecurityId);
writer.WritePriceEx(candle.OpenPrice, metaInfo, SecurityId, useLong);
writer.WritePriceEx(candle.ClosePrice, metaInfo, SecurityId, useLong);
}
else
{
writer.Write(false);

writer.WritePriceEx(candle.ClosePrice, metaInfo, SecurityId);
writer.WritePriceEx(candle.OpenPrice, metaInfo, SecurityId);
writer.WritePriceEx(candle.ClosePrice, metaInfo, SecurityId, useLong);
writer.WritePriceEx(candle.OpenPrice, metaInfo, SecurityId, useLong);
}

writer.WritePriceEx(candle.HighPrice, metaInfo, SecurityId);
writer.WritePriceEx(candle.HighPrice, metaInfo, SecurityId, useLong);
}

if (!candle.CloseTime.IsDefault() && candle.OpenTime > candle.CloseTime)
Expand Down Expand Up @@ -429,6 +430,15 @@ public override TCandleMessage MoveNext(MarketDataEnumerator enumerator)
Arg = _arg
};

var prevTime = metaInfo.FirstTime;
var allowNonOrdered = metaInfo.Version >= MarketDataVersions.Version49;
var isUtc = metaInfo.Version >= MarketDataVersions.Version50;
var timeZone = metaInfo.GetTimeZone(isUtc, SecurityId, ExchangeInfoProvider);
var allowDiffOffsets = metaInfo.Version >= MarketDataVersions.Version53;
var bigRange = metaInfo.Version >= MarketDataVersions.Version57;
var isTickPrecision = bigRange;
var useLong = metaInfo.Version >= MarketDataVersions.Version58;

if (metaInfo.Version < MarketDataVersions.Version56)
{
var prevPrice = metaInfo.FirstPrice;
Expand All @@ -446,30 +456,22 @@ public override TCandleMessage MoveNext(MarketDataEnumerator enumerator)
}
else
{
candle.LowPrice = reader.ReadPriceEx(metaInfo);
candle.LowPrice = reader.ReadPriceEx(metaInfo, useLong);

if (reader.Read())
{
candle.OpenPrice = reader.ReadPriceEx(metaInfo);
candle.ClosePrice = reader.ReadPriceEx(metaInfo);
candle.OpenPrice = reader.ReadPriceEx(metaInfo, useLong);
candle.ClosePrice = reader.ReadPriceEx(metaInfo, useLong);
}
else
{
candle.ClosePrice = reader.ReadPriceEx(metaInfo);
candle.OpenPrice = reader.ReadPriceEx(metaInfo);
candle.ClosePrice = reader.ReadPriceEx(metaInfo, useLong);
candle.OpenPrice = reader.ReadPriceEx(metaInfo, useLong);
}

candle.HighPrice = reader.ReadPriceEx(metaInfo);
candle.HighPrice = reader.ReadPriceEx(metaInfo, useLong);
}

var prevTime = metaInfo.FirstTime;
var allowNonOrdered = metaInfo.Version >= MarketDataVersions.Version49;
var isUtc = metaInfo.Version >= MarketDataVersions.Version50;
var timeZone = metaInfo.GetTimeZone(isUtc, SecurityId, ExchangeInfoProvider);
var allowDiffOffsets = metaInfo.Version >= MarketDataVersions.Version53;
var bigRange = metaInfo.Version >= MarketDataVersions.Version57;
var isTickPrecision = bigRange;

var lastOffset = metaInfo.FirstServerOffset;
candle.OpenTime = reader.ReadTime(ref prevTime, allowNonOrdered, isUtc, timeZone, allowDiffOffsets, isTickPrecision, ref lastOffset);
metaInfo.FirstServerOffset = lastOffset;
Expand Down
1 change: 1 addition & 0 deletions _ReleaseNotes/CHANGE_LOG_API.md
Expand Up @@ -19,6 +19,7 @@ StockSharp API Change log
* (bug) MarketDepthControl. Binding fixes.
* (feature) Snapshot storage.
* (feature) Security. IssueDate, IssueSize and UnderlyingSecurityType properties.
* (bug) http://stocksharp.com/forum/8795/Binary-Storage-Corrupted/

## v4.3.28:
* (feature) SecurityGrid. PriceChartEditor. Provider is non mandatory.
Expand Down

0 comments on commit 2d56dae

Please sign in to comment.