Skip to content

Commit

Permalink
BasketSecuriry. Store internally SecurityId instead of Security insta…
Browse files Browse the repository at this point in the history
…nce.
  • Loading branch information
mikasoukhov committed Mar 1, 2017
1 parent c13f4aa commit 41bc52c
Show file tree
Hide file tree
Showing 12 changed files with 214 additions and 161 deletions.
1 change: 1 addition & 0 deletions Algo/Algo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
<Link>Properties\StockSharpAssemblyInfo.cs</Link>
</Compile>
<Compile Include="AssociatedSecurityAdapter.cs" />
<Compile Include="BasketSecurity.cs" />
<Compile Include="CollectionSecurityProvider.cs" />
<Compile Include="Commissions\CommissionMessageAdapter.cs" />
<Compile Include="DataType.cs" />
Expand Down
26 changes: 5 additions & 21 deletions BusinessEntities/BasketSecurity.cs → Algo/BasketSecurity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ Removal of this comment is a violation of the license agreement.
Copyright 2010 by StockSharp, LLC
*******************************************************************************************/
#endregion S# License
namespace StockSharp.BusinessEntities
namespace StockSharp.Algo
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;

using StockSharp.BusinessEntities;
using StockSharp.Messages;

/// <summary>
/// Instruments basket.
Expand All @@ -38,24 +40,6 @@ protected BasketSecurity()
/// Instruments, from which this basket is created.
/// </summary>
[Browsable(false)]
public abstract IEnumerable<Security> InnerSecurities { get; }

/// <summary>
/// To check whether specified instrument is used now.
/// </summary>
/// <param name="security">The instrument that should be checked.</param>
/// <returns><see langword="true" />, if specified instrument is used now, otherwise <see langword="false" />.</returns>
public virtual bool Contains(Security security)
{
return InnerSecurities.Any(innerSecurity =>
{
var basket = innerSecurity as BasketSecurity;

if (basket == null)
return innerSecurity == security;

return basket.Contains(security);
});
}
public abstract IEnumerable<SecurityId> InnerSecurityIds { get; }
}
}
3 changes: 2 additions & 1 deletion Algo/Candles/CandleManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ namespace StockSharp.Algo.Candles
using Ecng.Collections;
using Ecng.Common;
using Ecng.ComponentModel;
using Ecng.Configuration;

using MoreLinq;

Expand Down Expand Up @@ -362,7 +363,7 @@ public virtual void Start(CandleSeries series, DateTimeOffset from, DateTimeOffs
throw new ArgumentException(LocalizedStrings.Str650Params.Put(series), nameof(series));

enumerator = new CandleSourceEnumerator<ICandleManagerSource, Candle>(series, from, to,
series.Security is IndexSecurity ? (IEnumerable<ICandleManagerSource>)new[] { new IndexSecurityCandleManagerSource(this, from, to) } : Sources,
series.Security is IndexSecurity ? (IEnumerable<ICandleManagerSource>)new[] { new IndexSecurityCandleManagerSource(this, ConfigManager.GetService<ISecurityProvider>(), from, to) } : Sources,
c =>
{
Processing?.Invoke(series, c);
Expand Down
13 changes: 7 additions & 6 deletions Algo/Candles/IndexCandleBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ private Candle CreateFilledCandle(Candle candle)
private readonly Type _candleType;
private readonly CachedSynchronizedOrderedDictionary<DateTimeOffset, CandleBuffer> _buffers = new CachedSynchronizedOrderedDictionary<DateTimeOffset, CandleBuffer>();
private CandleBuffer _lastProcessBuffer;
private readonly SynchronizedDictionary<Security, int> _securityIndecies = new SynchronizedDictionary<Security, int>();
private readonly SynchronizedDictionary<SecurityId, int> _securityIndecies = new SynchronizedDictionary<SecurityId, int>();
private readonly int _bufferSize;
private CandleBuffer _sparseBuffer1;
private CandleBuffer _sparseBuffer2;
Expand All @@ -162,7 +162,7 @@ private void FillSecurityIndecies(BasketSecurity basketSecurity)
{
var index = 0;

foreach (var security in basketSecurity.InnerSecurities)
foreach (var security in basketSecurity.InnerSecurityIds)
{
_securityIndecies[security] = index;

Expand Down Expand Up @@ -276,7 +276,8 @@ private IEnumerable<CandleBuffer> GetFormedBuffers(Candle candle)
lock (_buffers.SyncRoot)
{
var buffer = _buffers.SafeAdd(candle.OpenTime, key => new CandleBuffer(_candleType, candle.OpenTime, candle.CloseTime, _bufferSize, false));
buffer.AddCandle(_securityIndecies[candle.Security], candle);
var secId = candle.Security.ToSecurityId();
buffer.AddCandle(_securityIndecies[secId], candle);

if (!buffer.IsFilled)
{
Expand All @@ -292,13 +293,13 @@ private IEnumerable<CandleBuffer> GetFormedBuffers(Candle candle)
_sparseBuffer1 = new CandleBuffer(_candleType, candle.OpenTime, candle.CloseTime, _bufferSize, true);

if (!_sparseBuffer1.IsFilled)
_sparseBuffer1.AddCandle(_securityIndecies[candle.Security], candle);
_sparseBuffer1.AddCandle(_securityIndecies[secId], candle);
else
{
if (_sparseBuffer2 == null)
_sparseBuffer2 = new CandleBuffer(_candleType, candle.OpenTime, candle.CloseTime, _bufferSize, true);

_sparseBuffer2.AddCandle(_securityIndecies[candle.Security], candle);
_sparseBuffer2.AddCandle(_securityIndecies[secId], candle);

// если первая свеча будет построена по размазанному буферу, то разница между временем эти буферов
// должна гарантировать, что между ними
Expand Down Expand Up @@ -395,7 +396,7 @@ private decimal Calculate(CandleBuffer buffer, Func<Candle, decimal> getPart)
}
catch (ArithmeticException excp)
{
throw new ArithmeticException("Build index candle {0} for {1} error.".Put(_security, _security.InnerSecurities.Zip(values, (s, v) => "{0}: {1}".Put(s, v)).Join(", ")), excp);
throw new ArithmeticException("Build index candle {0} for {1} error.".Put(_security, _security.InnerSecurityIds.Zip(values, (s, v) => "{0}: {1}".Put(s, v)).Join(", ")), excp);
}
}

Expand Down
13 changes: 11 additions & 2 deletions Algo/Candles/IndexSecurityCandleManagerSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ namespace StockSharp.Algo.Candles

using MoreLinq;

using StockSharp.BusinessEntities;
using StockSharp.Messages;

class IndexSecurityCandleManagerSource : Disposable, ICandleManagerSource
Expand Down Expand Up @@ -144,12 +145,20 @@ protected override void DisposeManaged()
}
}

private readonly ISecurityProvider _securityProvider;
private readonly DateTimeOffset _from;
private readonly DateTimeOffset _to;
private readonly SynchronizedDictionary<CandleSeries, IndexSeriesInfo> _info = new SynchronizedDictionary<CandleSeries, IndexSeriesInfo>();

public IndexSecurityCandleManagerSource(ICandleManager candleManager, DateTimeOffset from, DateTimeOffset to)
public IndexSecurityCandleManagerSource(ICandleManager candleManager, ISecurityProvider securityProvider, DateTimeOffset from, DateTimeOffset to)
{
if (candleManager == null)
throw new ArgumentNullException(nameof(candleManager));

if (securityProvider == null)
throw new ArgumentNullException(nameof(securityProvider));

_securityProvider = securityProvider;
_from = from;
_to = to;
CandleManager = candleManager;
Expand Down Expand Up @@ -184,7 +193,7 @@ void ICandleSource<Candle>.Start(CandleSeries series, DateTimeOffset from, DateT
var basketInfo = new IndexSeriesInfo(CandleManager,
series.CandleType,
indexSecurity
.InnerSecurities
.GetInnerSecurities(_securityProvider)
.Select(sec => new CandleSeries(series.CandleType, sec, IndexCandleBuilder.CloneArg(series.Arg, sec))
{
WorkingTime = series.WorkingTime.Clone(),
Expand Down
Loading

0 comments on commit 41bc52c

Please sign in to comment.