18 using System.Collections;
19 using System.Collections.Generic;
34 private string _accountCurrency;
40 public event EventHandler<CashBookUpdatedEventArgs>
Updated;
47 get {
return _accountCurrency; }
53 if (!_accountCurrency.IsNullOrEmpty()
54 &&
TryGetValue(_accountCurrency, out accountCurrency))
56 amount = accountCurrency.
Amount;
61 _accountCurrency = value.LazyToUpper();
62 Add(_accountCurrency,
new Cash(_accountCurrency, amount, 1.0m));
70 private Dictionary<string, Cash> _currencies;
79 return this.Aggregate(0m, (d, pair) => d + pair.Value.ValueInAccountCurrency);
100 public Cash Add(
string symbol, decimal quantity, decimal conversionRate)
102 var cash =
new Cash(symbol, quantity, conversionRate);
104 return AddIternal(symbol, cash);
119 IReadOnlyDictionary<SecurityType, string> marketMap,
124 var addedSubscriptionDataConfigs =
new List<SubscriptionDataConfig>();
125 foreach (var kvp
in _currencies)
127 var cash = kvp.Value;
129 var subscriptionDataConfigs = cash.EnsureCurrencyDataFeed(
137 if (subscriptionDataConfigs !=
null)
139 foreach (var subscriptionDataConfig
in subscriptionDataConfigs)
141 addedSubscriptionDataConfigs.Add(subscriptionDataConfig);
145 return addedSubscriptionDataConfigs;
155 public decimal
Convert(decimal sourceQuantity,
string sourceCurrency,
string destinationCurrency)
157 if (sourceQuantity == 0)
162 var source =
this[sourceCurrency];
163 var destination =
this[destinationCurrency];
165 if (source.ConversionRate == 0)
170 if (destination.ConversionRate == 0)
175 var conversionRate = source.ConversionRate / destination.ConversionRate;
176 return sourceQuantity * conversionRate;
189 return sourceQuantity;
206 #region IDictionary Implementation
216 return _currencies.Count;
226 get {
return false; }
233 public void Add(KeyValuePair<string, Cash> item)
235 Add(item.Key, item.Value);
245 AddIternal(symbol, value);
263 return Remove(symbol, calledInternally:
false);
270 public bool Remove(KeyValuePair<string, Cash> item)
282 return _currencies.ContainsKey(symbol);
294 return _currencies.TryGetValue(symbol, out value);
301 public bool Contains(KeyValuePair<string, Cash> item)
303 return _currencies.Contains(item);
311 public void CopyTo(KeyValuePair<string, Cash>[] array,
int arrayIndex)
313 ((IDictionary<string, Cash>) _currencies).CopyTo(array, arrayIndex);
320 public Cash this[
string symbol]
329 if (!_currencies.TryGetValue(symbol, out cash))
345 public ICollection<string>
Keys => _currencies.Keys;
351 public ICollection<Cash>
Values => _currencies.Values;
359 return _currencies.GetEnumerator();
362 IEnumerator IEnumerable.GetEnumerator()
364 return _currencies.GetEnumerator();
369 #region ICurrencyConverter Implementation
389 private Cash AddIternal(
string symbol,
Cash value)
396 if (!_currencies.TryGetValue(symbol, out var cash))
401 var newCurrencies =
new Dictionary<string, Cash>(_currencies)
405 _currencies = newCurrencies;
416 cash.SetAmount(value.
Amount);
422 private bool Remove(
string symbol,
bool calledInternally)
425 var newCurrencies =
new Dictionary<string, Cash>(_currencies);
426 var removed = newCurrencies.Remove(symbol, out cash);
427 _currencies = newCurrencies;
430 if (!calledInternally)
432 Log.
Error(
"CashBook.Remove(): " + Messages.CashBook.FailedToRemoveRecord(symbol));
437 cash.Updated -= OnCashUpdate;
438 if (!calledInternally)
446 private void OnCashUpdate(
object sender, EventArgs eventArgs)
453 Updated?.Invoke(
this,
new CashBookUpdatedEventArgs(updateType, cash));