Skip to content

Commit

Permalink
Addressed review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
JosueNina committed Jan 10, 2025
1 parent c8bfd98 commit 9fee55d
Showing 1 changed file with 20 additions and 32 deletions.
52 changes: 20 additions & 32 deletions Common/Util/CurrencyPairUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public static class CurrencyPairUtil
private static readonly Lazy<SymbolPropertiesDatabase> SymbolPropertiesDatabase =
new Lazy<SymbolPropertiesDatabase>(Securities.SymbolPropertiesDatabase.FromDataFolder);

private static readonly int[][] PotentialStableCoins = { [1, 3], [1, 2], [0, 3], [0, 2] };

/// <summary>
/// Tries to decomposes the specified currency pair into a base and quote currency provided as out parameters
/// </summary>
Expand Down Expand Up @@ -202,51 +204,37 @@ public enum Match
/// <returns>The <see cref="Match"/> member that represents the relation between the two pairs</returns>
public static Match ComparePair(this Symbol pairA, string baseCurrencyB, string quoteCurrencyB)
{
var pairAValue = "";

// Check for a stablecoin between the currencies
if (TryDecomposeCurrencyPair(pairA, out var baseCurrencyA, out var quoteCurrencyA))
if (!TryDecomposeCurrencyPair(pairA, out var baseCurrencyA, out var quoteCurrencyA))
{
pairAValue = string.Concat(baseCurrencyA, "||", quoteCurrencyA);
var currencies = new string[] { baseCurrencyA, quoteCurrencyA, baseCurrencyB, quoteCurrencyB };
var isThereAnyMatch = false;

// Compute all the potential stablecoins
var potentialStableCoins = new int[][]
{
new int[]{ 1, 3 },
new int[]{ 1, 2 },
new int[]{ 0, 3 },
new int[]{ 0, 2 }
};
return Match.NoMatch;
}

foreach (var pair in potentialStableCoins)
{
if (Currencies.IsStableCoinWithoutPair(currencies[pair[0]] + currencies[pair[1]], pairA.ID.Market)
|| Currencies.IsStableCoinWithoutPair(currencies[pair[1]] + currencies[pair[0]], pairA.ID.Market))
{
// If there's a stablecoin between them, assign to currency in pair A the value
// of the currency in pair B
currencies[pair[0]] = currencies[pair[1]];
isThereAnyMatch = true;
}
}
// Check for a stablecoin between the currencies
var currencies = new string[] { baseCurrencyA, quoteCurrencyA, baseCurrencyB, quoteCurrencyB };
var isThereAnyMatch = false;

// Update the value of pairAValue if there was a match
if (isThereAnyMatch)
// Compute all the potential stablecoins
foreach (var pair in PotentialStableCoins)
{
if (Currencies.IsStableCoinWithoutPair(currencies[pair[0]] + currencies[pair[1]], pairA.ID.Market)
|| Currencies.IsStableCoinWithoutPair(currencies[pair[1]] + currencies[pair[0]], pairA.ID.Market))
{
pairAValue = string.Concat(currencies[0], "||", currencies[1]);
// If there's a stablecoin between them, assign to currency in pair A the value
// of the currency in pair B
currencies[pair[0]] = currencies[pair[1]];
isThereAnyMatch = true;
}
}

var directPair = string.Concat(baseCurrencyB, "||", quoteCurrencyB);
var inversePair = string.Concat(quoteCurrencyB, "||", baseCurrencyB);
string pairAValue = isThereAnyMatch ? string.Concat(currencies[0], "||", currencies[1]) : string.Concat(baseCurrencyA, "||", quoteCurrencyA);

var directPair = string.Concat(baseCurrencyB, "||", quoteCurrencyB);
if (pairAValue == directPair)
{
return Match.ExactMatch;
}

var inversePair = string.Concat(quoteCurrencyB, "||", baseCurrencyB);
if (pairAValue == inversePair)
{
return Match.InverseMatch;
Expand Down

0 comments on commit 9fee55d

Please sign in to comment.