Merge branch 'dev'

This commit is contained in:
Viacheslav Demydiuk
2025-12-05 23:49:05 +02:00
8 changed files with 88 additions and 60 deletions
+30 -20
View File
@@ -123,14 +123,7 @@ namespace MtApi5
throw new Exception($"Connection to {host}:{port} failed. Error: {errorMessage}");
}
// Load quotes
Dictionary<int, Mt5Quote> quotes = [];
foreach (var handle in experts)
{
var quote = GetQuote(client, handle);
if (quote != null)
quotes[handle] = quote;
}
var quotes = LoadQuotes(client, experts);
lock (_locker)
{
@@ -160,6 +153,18 @@ namespace MtApi5
}
}
private Dictionary<int, Mt5Quote> LoadQuotes(MtRpcClient client, HashSet<int> experts)
{
Dictionary<int, Mt5Quote> quotes = [];
foreach (var handle in experts)
{
var quote = GetQuote(client, handle);
if (quote != null)
quotes[handle] = quote;
}
return quotes;
}
///<summary>
///Disconnect from MetaTrader API. Async method.
///</summary>
@@ -183,10 +188,22 @@ namespace MtApi5
///</summary>
public IEnumerable<Mt5Quote> GetQuotes()
{
MtRpcClient? client;
HashSet<int> experts;
lock (_locker)
{
return _quotes.Values.ToList();
client = _client;
experts = new HashSet<int>(_experts);
}
if (client == null)
{
Log.Warn("GetQuotes: No connection");
throw new Exception("No connection");
}
var quotes = LoadQuotes(client, experts);
return quotes.Values.ToList();
}
///<summary>
@@ -3402,11 +3419,6 @@ namespace MtApi5
var quote = GetQuote(Client, handle);
if (quote != null)
{
lock (_locker)
{
_quotes[handle] = quote;
}
QuoteAdded?.Invoke(this, new Mt5QuoteEventArgs(quote));
}
else
@@ -3421,19 +3433,18 @@ namespace MtApi5
{
Log.Debug($"ProcessExpertRemoved: {handle}");
Mt5Quote? quote = null;
Mt5Quote? quote;
lock (_locker)
{
_quotes.TryGetValue(handle, out quote);
_quotes.Remove(handle);
_experts.Remove(handle);
if (_quotes.TryGetValue(handle, out quote))
_quotes.Remove(handle);
if (_executorHandle == handle)
_executorHandle = (_experts.Count > 0) ? _experts.ElementAt(0) : 0;
}
if (quote != null)
QuoteRemoved?.Invoke(this, new Mt5QuoteEventArgs(quote));
QuoteRemoved?.Invoke(this, new Mt5QuoteEventArgs(new Mt5Quote { Instrument = quote.Instrument, ExpertHandle = quote.ExpertHandle }));
}
private Mt5Quote? GetQuote(MtRpcClient? client, int expertHandle)
@@ -3550,7 +3561,6 @@ namespace MtApi5
client = _client;
_client = null;
_quotes.Clear();
_experts.Clear();
_executorHandle = 0;
}