Last active
March 30, 2024 03:17
-
-
Save normanlmfung/851d7a23fc23e237c7121f9a91ef1c09 to your computer and use it in GitHub Desktop.
csharp_collections
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Text.RegularExpressions; | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
// Example 1. Simple List | |
List<float> floatList = new List<float> { 1.1f, 2.2f, 3.3f, 4.4f, 5.5f }; | |
foreach (float value in floatList) | |
{ | |
Console.WriteLine(value); | |
} | |
// Example 2. Dictionary vs SortedList | |
// Examples from CME BTC futures https://www.cmegroup.com/markets/cryptocurrencies/bitcoin/bitcoin.quotes.html#venue=globex | |
List<string> futureCodes = new List<string> | |
{ | |
"BTCU25", "BTCZ25", "BTCH24", "BTCQ24", "BTCU24", "BTCZ24", "BTCH25", "BTCJ24", "BTCK24", "BTCM24", "BTCN24" | |
}; | |
// https://www.tutorialsteacher.com/csharp/csharp-sortedlist | |
Dictionary<int, Future> futuresDictionary = new Dictionary<int, Future>(); | |
SortedList<int, Future> sortedFutures = new SortedList<int, Future>(); | |
foreach (string futureCode in futureCodes) | |
{ | |
Future future = new Future(futureCode); | |
futuresDictionary.Add(future.NumDaysToMaturity, future); | |
sortedFutures.Add(future.NumDaysToMaturity, future); | |
} | |
foreach (KeyValuePair<int, Future> kvp in futuresDictionary) | |
{ | |
Console.WriteLine($"NumDaysToMaturity: {kvp.Key}, Future: {kvp.Value.FutureCode}"); | |
} | |
foreach (KeyValuePair<int, Future> kvp in sortedFutures) | |
{ | |
Console.WriteLine($"NumDaysToMaturity: {kvp.Key}, Future: {kvp.Value.FutureCode}"); | |
} | |
} | |
} | |
class Future | |
{ | |
public string FutureCode { get; } | |
public string Token { get; private set; } | |
public string MonthCode { get; private set; } | |
public int Month { get; private set; } | |
public int Year { get; private set; } | |
public Future(string futureCode) | |
{ | |
FutureCode = futureCode; | |
string futureCodePattern = @"^(?<token>[A-Z]{3})(?<futureMonthCode>[A-Z])(?<year>\d{2})$"; | |
Match futureCodeMatch = Regex.Match(futureCode, futureCodePattern); | |
if (futureCodeMatch.Success) | |
{ | |
Token = futureCodeMatch.Groups["token"].Value; | |
MonthCode = futureCodeMatch.Groups["futureMonthCode"].Value; | |
Month = GetMonthFromCode(MonthCode); | |
Year = Convert.ToInt32(futureCodeMatch.Groups["year"].Value) + 2000; // Assuming years are in 20XX format | |
} | |
} | |
public int NumDaysToMaturity | |
{ | |
get | |
{ | |
DateTime today = DateTime.Today; | |
DateTime maturityDate = new DateTime(Year, Month, DateTime.DaysInMonth(Year, Month)); | |
return (int)(maturityDate - today).TotalDays; | |
} | |
} | |
private int GetMonthFromCode(string monthCode) | |
{ | |
switch (monthCode) | |
{ | |
case "F": return 1; // January | |
case "G": return 2; // February | |
case "H": return 3; // March | |
case "J": return 4; // April | |
case "K": return 5; // May | |
case "M": return 6; // June | |
case "N": return 7; // July | |
case "Q": return 8; // August | |
case "U": return 9; // September | |
case "V": return 10; // October | |
case "X": return 11; // November | |
case "Z": return 12; // December | |
default: throw new ArgumentException("Invalid month code"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment