LEAN vs StockSharp for C# algorithmic trading
A C# team can start a strategy inside the LEAN algorithm model or build it into a .NET trading application around a connector. Both paths should first prove that fill messages, fee currencies and tradable quantities enter the same portfolio ledger. A one-sample fill trace is more specific than checking whether a venue appears in an integration list.
| Area | LEAN | StockSharp |
|---|---|---|
| Entry point | Strategy code runs in the algorithm model. Process fills in OnOrderEvent and tie them to the appropriate order ticket. |
Select a connector and build the strategy against the corresponding market-data and trading interfaces. |
| Historical rehearsal | Run the same strategy with a pinned dataset, brokerage model and security normalization mode. | Set up historical storage and replay through HistoryEmulationConnector. Verify the storage source and emulation settings. |
| Deployment check | Inspect the brokerage-specific order fields and cash handling in the intended deployment. | Inspect connector translation of symbol, quantity, commission and the records available after reconnection. |
LEAN order events · LEAN normalization modes · StockSharp connectors · Historical emulation
Log the units before converting the fees
Use a fill identity, order identity, exchange time, receipt time, signed size, price, fee amount and fee currency as the common record. Preserve the venue symbol and the internal mapping. A fee deducted from BTC reduces directly how much BTC can be placed into the next sell order. The quote-currency value of the fee is useful for reporting, while the ledger still needs the BTC debit. Use decimal types for fees and lot increments and assert the remaining quantity after each operation.
On an actual connector, run one trade in a test environment or replay saved messages. Build an export for each engine from its own execution events. Reconcile the order and position before comparing summaries.
Screenshots and examples
Run the same accounting check in Python and C#
Portfolio after a buy and an exit at 51,000 USD
Subtract any base-asset fee before rounding the sell size down. Equity adds marked inventory to cash.| Case | Available BTC | Sold BTC | Remaining BTC | Cash, USD | Marked P&L, USD |
|---|---|---|---|---|---|
| A | 0.1 | 0.1 | 0.0 | 10089.9 | 89.9 |
| B | 0.0999 | 0.0999 | 0.0 | 10089.8051 | 89.8051 |
| C | 0.0999 | 0.099 | 0.0009 | 10043.951 | 89.851 |
The fixture buys 0.10 BTC at 50,000 USD and marks the exit at 51,000. The fee rate is 0.10% and the exit fee uses USD in every case. In A the entry fee is 5 USD, so the full 0.10 BTC can be sold. In B the entry fee is 0.0001 BTC, so only 0.0999 BTC is available. Case C uses the same fee currency and a 0.001 BTC lot step. It sells 0.099 BTC and keeps 0.0009 BTC, worth 45.90 USD at the tested mark.
The row with the smaller sale can report a slightly higher marked result because the retained inventory did not incur the exit fee. That inventory remains part of the portfolio. Use a separate liquidation report if you are testing realized P&L or the ability to close all residual positions.
Reproduce the result in Python
import json
from decimal import Decimal as D
START, BUY, SELL = D(10_000), D(50_000), D(51_000)
BOUGHT, FEE = D("0.10"), D("0.001")
def replay(label, fee_in_base, step):
fee_base = BOUGHT * FEE if fee_in_base else D(0)
fee_quote = D(0) if fee_in_base else BOUGHT * BUY * FEE
balance = BOUGHT - fee_base
sold = (balance // step) * step
remainder = balance - sold
cash = START - BOUGHT * BUY - fee_quote + sold * SELL * (1 - FEE)
profit = cash + remainder * SELL - START
return [label, balance, sold, remainder, cash, profit]
def run():
rows = [
replay("A", False, D("0.0001")),
replay("B", True, D("0.0001")),
replay("C", True, D("0.001")),
]
assert rows[0][-1] == D("89.90")
assert rows[1][-1] == D("89.8051")
assert rows[2][3] == D("0.0009") and rows[2][-1] == D("89.851")
return [[row[0], *(float(value) for value in row[1:])] for row in rows]
if __name__ == "__main__":
print(json.dumps(run()))The C# version of this fixture uses decimal and asserts the same outcomes. Run it with dotnet run --file lean-vs-stocksharp-csharp.cs on .NET 10. Both scripts implement the ledger independently of the two platforms. Partial fill reconciliation · Software choices by strategy.