TimHanewich.Investing 5.1.0

dotnet add package TimHanewich.Investing --version 5.1.0
                    
NuGet\Install-Package TimHanewich.Investing -Version 5.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="TimHanewich.Investing" Version="5.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="TimHanewich.Investing" Version="5.1.0" />
                    
Directory.Packages.props
<PackageReference Include="TimHanewich.Investing" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add TimHanewich.Investing --version 5.1.0
                    
#r "nuget: TimHanewich.Investing, 5.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package TimHanewich.Investing@5.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=TimHanewich.Investing&version=5.1.0
                    
Install as a Cake Addin
#tool nuget:?package=TimHanewich.Investing&version=5.1.0
                    
Install as a Cake Tool

TimHanewich.Investing

A lightweight .NET library for simulating stock portfolio trading.

Features

  • Simulated Trading — Buy and sell stocks at specified prices
  • Transaction Logging — Every trade and cash movement is recorded with timestamps
  • On-the-Fly Holdings — Current positions and cost basis are computed from the transaction history (no stale state)
  • Trade Costs — Configurable per-trade commission that is automatically deducted

Installation

Install from NuGet:

dotnet add package TimHanewich.Investing

Quick Start

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}");
}

Usage

Creating a Portfolio

Portfolio portfolio = new Portfolio();
portfolio.TradeCost = 4.95f; // optional commission per trade

Depositing and Withdrawing Cash

portfolio.EditCash(5000f);  // deposit $5,000
portfolio.EditCash(-500f);  // withdraw $500

Cash changes are logged as CashTransaction entries with type CashTransactionType.Edit.

Buying Stocks

portfolio.Buy("AAPL", 10, 150.00f);

This will:

  1. Verify you have enough cash
  2. Log a HoldingTransaction (Buy)
  3. Deduct the cost from your cash balance (logged as CashTransactionType.Transaction)
  4. Deduct the trade cost/commission if set (logged as CashTransactionType.Expense)

Selling Stocks

portfolio.Sell("AAPL", 5, 200.00f);

This will:

  1. Verify you own enough shares to sell
  2. Log a HoldingTransaction (Sell)
  3. Credit the proceeds to your cash balance
  4. Deduct the trade cost/commission if set

Viewing Holdings

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)

Reviewing Transaction History

// 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}");
}

Full Example

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));

Class Reference

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 Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net10.0

    • No dependencies.

NuGet packages (2)

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.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
5.1.0 114 5/1/2026
5.0.0 105 4/26/2026
4.0.1 876 4/12/2021
4.0.0 846 6/8/2020
3.2.0 690 6/7/2020
3.1.0 654 6/7/2020
3.0.0 3,199 5/19/2020
2.0.0 688 5/19/2020
1.0.0 684 5/6/2020