![]() |
VOOZH | about |
dotnet add package TimHanewich.Investing --version 5.1.0
NuGet\Install-Package TimHanewich.Investing -Version 5.1.0
<PackageReference Include="TimHanewich.Investing" Version="5.1.0" />
<PackageVersion Include="TimHanewich.Investing" Version="5.1.0" />Directory.Packages.props
<PackageReference Include="TimHanewich.Investing" />Project file
paket add TimHanewich.Investing --version 5.1.0
#r "nuget: TimHanewich.Investing, 5.1.0"
#:package TimHanewich.Investing@5.1.0
#addin nuget:?package=TimHanewich.Investing&version=5.1.0Install as a Cake Addin
#tool nuget:?package=TimHanewich.Investing&version=5.1.0Install as a Cake Tool
A lightweight .NET library for simulating stock portfolio trading.
Install from NuGet:
dotnet add package TimHanewich.Investing
using TimHanewich.Investing;
using TimHanewich.Investing.Simulation;
// Create a portfolio and fund it
Portfolio portfolio = new Portfolio();
portfolio.EditCash(100_000.00f);
// Buy and sell stocks
portfolio.Buy("AAPL", 10, 150.00f);
portfolio.Buy("AAPL", 20, 175.00f);
portfolio.Sell("AAPL", 5, 200.00f);
// Check current holdings (computed from transaction log)
foreach (Holding h in portfolio.Holdings())
{
Console.WriteLine($"{h.Symbol}: {h.Quantity} shares, cost basis per share ${h.CostBasisPerShare:F2}, total position cost ${h.CostBasisTotalPosition:F2}");
}
Portfolio portfolio = new Portfolio();
portfolio.TradeCost = 4.95f; // optional commission per trade
portfolio.EditCash(5000f); // deposit $5,000
portfolio.EditCash(-500f); // withdraw $500
Cash changes are logged as CashTransaction entries with type CashTransactionType.Edit.
portfolio.Buy("AAPL", 10, 150.00f);
This will:
HoldingTransaction (Buy)CashTransactionType.Transaction)CashTransactionType.Expense)portfolio.Sell("AAPL", 5, 200.00f);
This will:
HoldingTransaction (Sell)Holdings are computed on-the-fly from the transaction history. No stale state is stored.
Holding[] holdings = portfolio.Holdings();
foreach (Holding h in holdings)
{
Console.WriteLine($"{h.Symbol}: {h.Quantity} shares");
Console.WriteLine($" Cost basis per share: ${h.CostBasisPerShare:F2}");
Console.WriteLine($" Total position cost: ${h.CostBasisTotalPosition:F2}");
}
CostBasisPerShare โ weighted average price per share (total cost of all buys รท total shares bought)CostBasisTotalPosition โ total cost of the current position (CostBasisPerShare ร Quantity)// Holding (stock) transactions
foreach (HoldingTransaction ht in portfolio.HoldingTransactionLog)
{
Console.WriteLine($"[{ht.TransactedAt}] {ht.OrderType} {ht.Quantity} {ht.Symbol} @ ${ht.ExecutedPrice:F2}");
}
// Cash transactions
foreach (CashTransaction ct in portfolio.CashTransactionLog)
{
Console.WriteLine($"[{ct.TransactedAt}] {ct.ChangeType}: ${ct.CashChange:F2}");
}
using TimHanewich.Investing;
using TimHanewich.Investing.Simulation;
using Newtonsoft.Json;
Portfolio p = new Portfolio();
p.EditCash(100_000.00f);
p.Buy("MSFT", 10, 100.00f);
p.Buy("MSFT", 20, 200.00f);
Console.WriteLine(JsonConvert.SerializeObject(p.Holdings(), Formatting.Indented));
p.Sell("MSFT", 25, 300.00f);
Console.WriteLine(JsonConvert.SerializeObject(p.Holdings(), Formatting.Indented));
TimHanewich.Investing.Simulation| Class | Description |
|---|---|
Portfolio |
The main portfolio class. Manages cash, executes trades, computes holdings from transaction history. |
Holding |
A computed stock position: Symbol, Quantity, CostBasisPerShare, CostBasisTotalPosition. |
HoldingTransaction |
A recorded stock trade: Symbol, Quantity, OrderType, ExecutedPrice. Extends Transaction. |
CashTransaction |
A recorded cash movement: CashChange, ChangeType. Extends Transaction. |
Transaction |
Base class with TransactedAt (DateTimeOffset). |
TransactionType |
Enum: Buy, Sell |
CashTransactionType |
Enum: Edit, Transaction, Expense |
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 net10.0 is compatible. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
Showing the top 2 NuGet packages that depend on TimHanewich.Investing:
| Package | Downloads |
|---|---|
|
EarningsAlley
Engine behind the Earnings Alley twitter account, @EarningsAlley. |
|
|
SimulatedInvesting
Allows you to manage a fake investment portfolio while investing in stocks, bonds, and options. |
This package is not used by any popular GitHub repositories.