StocksCoreApiSharp.SQLite
1.0.3
dotnet add package StocksCoreApiSharp.SQLite --version 1.0.3
NuGet\Install-Package StocksCoreApiSharp.SQLite -Version 1.0.3
<PackageReference Include="StocksCoreApiSharp.SQLite" Version="1.0.3" />
paket add StocksCoreApiSharp.SQLite --version 1.0.3
#r "nuget: StocksCoreApiSharp.SQLite, 1.0.3"
// Install StocksCoreApiSharp.SQLite as a Cake Addin #addin nuget:?package=StocksCoreApiSharp.SQLite&version=1.0.3 // Install StocksCoreApiSharp.SQLite as a Cake Tool #tool nuget:?package=StocksCoreApiSharp.SQLite&version=1.0.3
StocksCoreApiSharp
This is the core C# library to manage stocks for our StocksWatch application built with .NET MAUI.
Nuget
Usage
Depots & Watchlists
Depot
and WatchList
objects can hold Stock
items.
// Create a new depot
Depot myDepot = new("My depot")
{
DateOfCreation = DateTime.Now,
IsPrimaryDepot = true,
};
myDepot.Stocks.Add(new Stock("Daimler AG"));
// Create a new watchlist
WatchList myList = new("My watchlist")
{
DateOfCreation = DateTime.Now,
};
myList.Stocks.Add(new Stock("Daimler AG"));
Stocks
The Stock
object holds all necessary informaton about a stock or ETF.
// Create a new Stock item
Stock basf = new()
{
Name = "BASF",
ISIN = "DE000BASF111",
CurrentRate = 41.05,
};
// Add transactions to it
basf.Transactions.Add(new()
{
StockId = basf.Id,
DateOfCreation = DateTime.Now,
Amount = 100,
Price = 64.10,
Type = TransactionType.Buy,
});
basf.Transactions.Add(new()
{
StockId = basf.Id,
DateOfCreation = DateTime.Now,
Amount = 25,
Price = 60.10,
Type = TransactionType.Sell,
});
// Add dividends to it
basf.Dividends.Add(new()
{
StockId = basf.Id,
DateOfDividend = DateTime.Now,
AmountOfDividend = 3400.00,
Quantity = 100,
Tax = 300,
});
basf.Dividends.Add(new()
{
StockId = basf.Id,
DateOfDividend = DateTime.Now.AddYears(-1),
AmountOfDividend = 3400.00,
Quantity = 100,
Tax = 300,
});
Based on the Transaction
and Dividend
items, the Stock
class will automatically calculate the amount of stocks hold, the total worth and growth.
Database
The library supports SQLite by default. You either can create your own DatabaseHandler
or use the provided one.
try
{
string databasePath = "testdatabase.db";
// Start with a clear database
if (File.Exists(databasePath))
{
File.Delete(databasePath);
}
DatabaseHandler.Instance = new DatabaseHandler(databasePath);
if (DatabaseHandler.Instance.IsInitialized)
{
await DatabaseHandler.Instance.InitTablesAsync();
Depot myDepot = new("My depot")
{
DateOfCreation = DateTime.Now,
};
Stock basf = new()
{
DepotId = myDepot.Id,
Name = "BASF",
ISIN = "DE000BASF111",
CurrentRate = 41.05,
};
basf.Transactions.Add(new()
{
StockId = basf.Id,
DateOfCreation = DateTime.Now,
Amount = 100,
Price = 64.10,
Type = TransactionType.Buy,
});
basf.Transactions.Add(new()
{
StockId = basf.Id,
DateOfCreation = DateTime.Now,
Amount = 25,
Price = 60.10,
Type = TransactionType.Sell,
});
basf.Dividends.Add(new()
{
StockId = basf.Id,
DateOfDividend = DateTime.Now,
AmountOfDividend = 3400.00,
Quantity = 100,
Tax = 300,
});
basf.Dividends.Add(new()
{
StockId = basf.Id,
DateOfDividend = DateTime.Now.AddYears(-1),
AmountOfDividend = 3400.00,
Quantity = 100,
Tax = 300,
});
var total = basf.TotalCosts;
var entryPrice = basf.EntrancePrice;
var dividend = basf.TotalDividends;
var growth = basf.Growth;
myDepot.Stocks.Add(basf);
Stock daimler = new()
{
DepotId = myDepot.Id,
Name = "Mercedes Benz AG",
ISIN = "DE0007100000",
CurrentRate = 55.60,
};
daimler.Transactions.Add(new()
{
StockId = daimler.Id,
DateOfCreation = DateTime.Now,
Amount = 100,
Price = 58.10,
Type = TransactionType.Buy,
});
daimler.Transactions.Add(new()
{
StockId = daimler.Id,
DateOfCreation = DateTime.Now,
Amount = 30,
Price = 38.10,
Type = TransactionType.Buy,
});
daimler.Dividends.Add(new()
{
StockId = daimler.Id,
DateOfDividend = DateTime.Now,
AmountOfDividend = 5000,
Quantity = 100,
Tax = 1200,
});
myDepot.Stocks.Add(daimler);
var totalDepotWorth = myDepot.TotalWorth;
var overallDividends = myDepot.OverallDividends;
await DatabaseHandler.Instance.SetStocksWithChildrenAsync(myDepot.Stocks.ToList(), true);
await DatabaseHandler.Instance.SetDepotWithChildrenAsync(myDepot);
Depot? dbDepot = await DatabaseHandler.Instance.GetDepotWithChildrenAsync(myDepot.Id);
Assert.IsNotNull(dbDepot);
string jsonOriginal = JsonConvert.SerializeObject(myDepot, Formatting.Indented);
string jsonDatabase = JsonConvert.SerializeObject(dbDepot, Formatting.Indented);
//Assert.IsTrue(myDepot == dbDepot);
// Test WatchList
WatchList watchList = new("My Watchlist")
{
DateOfCreation = DateTime.Now,
};
basf.WatchListId = watchList.Id;
watchList.Stocks.Add(basf);
daimler.WatchListId = watchList.Id;
watchList.Stocks.Add(daimler);
await DatabaseHandler.Instance.SetStocksWithChildrenAsync(watchList.Stocks.ToList(), true);
await DatabaseHandler.Instance.SetWatchListWithChildrenAsync(watchList);
var loadedWatchLists = await DatabaseHandler.Instance.GetWatchListsWithChildrenAsync();
Assert.IsTrue(loadedWatchLists?.Count > 0);
WatchList list = loadedWatchLists?.FirstOrDefault(l => l.Id == watchList.Id);
Assert.IsNotNull(list);
Assert.IsTrue(list.Stocks?.Count == 2);
// Check if the updating works
var stocks = await DatabaseHandler.Instance.GetStocksWithChildrenAsync();
Assert.IsTrue(stocks?.Count == 2);
await DatabaseHandler.Instance.CloseDatabaseAsync();
}
}
catch (Exception exc)
{
Assert.Fail(exc.Message);
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net7.0
- CommunityToolkit.Mvvm (>= 8.2.1)
- SQLiteNetExtensions (>= 2.1.0)
- SQLiteNetExtensions.Async (>= 2.1.0)
- sqlite-net-pcl (>= 1.8.116)
- SQLitePCLRaw.provider.dynamic_cdecl (>= 2.1.5)
- StocksCoreApiSharp (>= 1.0.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.3 | 191 | 8/5/2023 |
Check GitHub releases for changelog.