Еще пара вещей по гидре [3.0.6]
1)
в SmartDepthSource
public void Start(IEnumerable<Security> securities)
{
_trader.Start();
securities.ForEach(_trader.Trader.RegisterQuotes);
}
_trader.Start вызывает Connect. Но Trader.Connected не ждет
В итоге RegisterQuotes вызовется до Trader.Connected.
Это разве правильно?
2)
Я захотел SmartDepthSource сделать помимо источника Quotes сделать источником Trade-ов.
Для этого добавил реализацию ITradeSource
И все можно сказать заработало, загрузился трейд, но при сохранении в базу в
DeleteTrades() все кончилось исключением
Code
Smart 20:06:44.1875577 Загружено 1 сделок.
Smart 20:06:44.1875577 Первая сделка 12281010 для RIH1@RTS за 22/02/2011 20:06:42.
Smart 20:06:44.1875577 Последняя сделка 12281010 для RIH1@RTS за 22/02/2011 20:06:42.
Smart 20:06:44.1875577 System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.
at System.ThrowHelper.ThrowKeyNotFoundException()
at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
at #=qN8oiXslVNgiUhuKs9C5uZgAkFATVDAsBWbPNhjTHTwcSqhF8Jp2W6wFkC8iwG3qR.#=qfOFdegjrBN$1UO2aK2Dd1g==(IEnumerable`1 #=q_ldcTM_jyhYkLszl5I2EKw==)
at Ecng.Trading.Algo.Storages.TradingStorage.DeleteTrades(Security security, IEnumerable`1 trades)
at Ecng.Trading.Hydra.Worker.<Download>b__10(IMarketDataSource source) in D:\SS\Sources\Hydra\Hydra\Worker.cs:line 136
Мой вариант SmartDepthSource.cs
Code
namespace Ecng.Trading.Hydra.Smart
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Net;
using Ecng.Collections;
using Ecng.Common;
using Ecng.Trading.BusinessEntities;
using Ecng.Trading.Hydra.Core;
using Ecng.Trading.Smart;
using System.Linq;
class SmartDepthSource : IMarketDepthSource, ISecuritySource, ITradeSource
{
private sealed class SmartSettings : MarketDataSourceSettings
{
public SmartSettings(MarketDataSourceSettings settings)
{
if (settings == null)
throw new ArgumentNullException("settings");
this.RealSettings = settings;
}
public override bool IsEnabled
{
get { return this.RealSettings.IsEnabled; }
set { this.RealSettings.IsEnabled = value; }
}
public override TimeSpan WorkingFrom
{
get { return this.RealSettings.WorkingFrom; }
set { this.RealSettings.WorkingFrom = value; }
}
public override TimeSpan WorkingTo
{
get { return this.RealSettings.WorkingTo; }
set { this.RealSettings.WorkingTo = value; }
}
public override TimeSpan Interval
{
get { return this.RealSettings.Interval; }
set { this.RealSettings.Interval = value; }
}
public override string DumpFolder
{
get { return this.RealSettings.DumpFolder; }
set { this.RealSettings.DumpFolder = value; }
}
[Category("Smart")]
[DisplayName("Логин")]
public string Login
{
get { return (string)this.RealSettings.ExtensionInfo["Login"]; }
set { this.RealSettings.ExtensionInfo["Login"] = value; }
}
[Category("Smart")]
[DisplayName("Пароль")]
public string Password
{
get { return (string)this.RealSettings.ExtensionInfo["Password"]; }
set { this.RealSettings.ExtensionInfo["Password"] = value; }
}
[Category("Smart")]
[DisplayName("Адрес")]
public string Address
{
get { return this.RealSettings.ExtensionInfo["Address"].To<string>(); }
set { this.RealSettings.ExtensionInfo["Address"] = value; }
}
internal MarketDataSourceSettings RealSettings { get; private set; }
}
private readonly Guid _id = "1ED92215-3E57-4EC9-89E5-22EC65786C67".To<Guid>();
private readonly HydraStorage _storage;
private readonly SmartSettings _settings;
private readonly MarketDataTrader _trader;
private IEnumerable<Security> _securities;
public SmartDepthSource(HydraStorage storage)
{
if (storage == null)
throw new ArgumentNullException("storage");
_storage = storage;
var settings = _storage.SourcesSettings.LoadBySourceId(_id);
if (settings == null)
{
settings = new MarketDataSourceSettings
{
SourceId = _id,
WorkingFrom = new TimeSpan(0, 0, 0),
WorkingTo = new TimeSpan(23, 59, 0),
ExtensionInfo = new Dictionary<object, object>(),
DumpFolder = string.Empty,
};
_settings = new SmartSettings(settings)
{
Login = string.Empty,
Password = string.Empty,
Address = SmartAddresses.Major.To<string>(),
};
SaveSettings();
}
else
_settings = new SmartSettings(settings);
_trader = new MarketDataTrader(new SecurityStorage(this),
CreateTrader);
}
ITrader CreateTrader()
{
SmartTrader st = new SmartTrader(_settings.Login, _settings.Password, _settings.Address.To<IPAddress>());
st.Connected += new Action(TraderConnected);
return st;
}
void TraderConnected()
{
if (_securities != null)
{
_securities.ForEach(_trader.Trader.RegisterQuotes);
_securities.ForEach(_trader.Trader.RegisterTrades);
}
}
Uri IMarketDataSource.Icon
{
get { return null; }
}
Guid IMarketDataSource.Id
{
get { return _id; }
}
public string Name
{
get { return "Smart"; }
}
HydraStorage IMarketDataSource.Storage
{
get { return _storage; }
}
MarketDataSourceSettings IMarketDataSource.Settings
{
get { return _settings; }
}
public void SaveSettings()
{
_storage.SourcesSettings.Save(_settings.RealSettings);
}
public void Start(IEnumerable<Security> securities)
{
_securities = securities;
_trader.Start();
//securities.ForEach(_trader.Trader.RegisterQuotes);
}
public void Stop()
{
_trader.Stop();
}
public IEnumerable<MarketDepth> Load(Security security)
{
if (_trader.LastError != null)
{
var copy = _trader.LastError;
_trader.LastError = null;
throw copy;
}
return _trader.GetMarketDepths(security);
}
IEnumerable<Trade> ITradeSource.Load(Security security)
{
if (_trader.LastError != null)
{
var copy = _trader.LastError;
_trader.LastError = null;
throw copy;
}
return _trader.GetTrades(security);
}
public void CommitLoad(Security security)
{
}
public IEnumerable<Security> GetNewSecurities()
{
return _trader.GetNewSecurities();
}
}
}