Trader is a lightweight, pure-Python backtesting engine designed for testing long and short trading strategies with realistic commission modeling (including MOEX-style fixed + percentage fees). It tracks your account balance, position size, unrealized PnL, and trade history — all in a clean, testable class-based interface.
Perfect for prototyping algorithmic strategies, educational purposes, or integrating into larger quant pipelines.
- Long & Short support: Open, close, and manage both long and short positions.
- Realistic commissions: Configurable percentage + fixed fee (e.g.,
0.1% + 0 RUB). - Account tracking: Automatically logs
My_money,Stock_Cash, and totalAccount_moneyover time. - Profit calculation: Built-in methods for current PnL and total cash result.
- Test-driven: Comes with a full
unittestsuite for reliability. - No external dependencies beyond
pandasandnumpy.
pip install pandas numpyfrom Trader import Trader
# Initialize trader with 100,000 RUB, 0.1% commission + 0 fixed fee
trader = Trader(money=100_000, comis=[0.1, 0], printBool=True)
# Buy 5,000 units at price 10.0
trader.buy(orderVolume=5000, price=10.0)
# Sell 5,000 units at price 20.0 (close long)
trader.sell(orderVolume=5000, price=20.0)
# Get total profit
print("Total PnL:", trader.getCashResultFast(20.0))trader.quant_money(price=15.0, date_now="2023-06-01")
trader.quant_money(price=18.0, date_now="2023-06-02")
# View full account history
print(trader.table)The project includes a comprehensive test suite:
python -m pytest TestTrader.py
# or
python TestTrader.pyTests cover:
- Commission calculation
- Long/short entry & exit
- Account value consistency
- Profit tracking accuracy
.
├── Trader.py # Core Trader class (rename to Trader.py in real use)
├── TestTrader.py # Unit tests (rename to TestTrader.py)
└── CNY000000TOD_22-6-2022_18-7-2023_Test.txt # Sample market data (CSV format expected)
💡 Note: The
.txtextensions are likely placeholders. In a real repo, rename to.pyand.csvaccordingly.
my_money: Cash not invested.posVolume: Current position (positive = long, negative = short).Account_money: Total equity = cash + value of open positions (adjusted for short logic).moneyOnStartDeal: Snapshot of account value at position entry — used to compute trade-level PnL.- Commissions: Calculated as
price * volume / 100 * comis[0] + comis[1]
(e.g.,0.1% + 0 RUBper trade).
Short positions are modeled using a "borrowed asset" approach: selling short reduces cash (you must post collateral), and profits come from buying back cheaper.
- Adjust commission structure via the
comis=[percentage, fixed]parameter. - Disable verbose output with
printBool=False. - Use
getCashResultFast()for real-time PnL without logging. - Extend the class to integrate with live brokers or strategy logic.
MIT License — feel free to use, modify, and distribute.
Created by gekator — for traders, by a trader.
🔍 Tip: Pair this engine with historical OHLC data (e.g., from MOEX or Binance) to backtest your strategies end-to-end!