Persistence System

Position Recovery

The trading engine includes a built-in persistence layer designed to survive crashes, restarts, and unexpected shutdowns without losing critical trading state.

All active trading data is automatically stored on disk using the custom PersistentMap component.

The system persistently stores:

  • Active positions
  • Pending orders
  • Symbol trading controls
  • Risk management states

When the bot restarts:

  1. 1.Persistent files are automatically loaded.
  2. 2.Previous trading state is restored into memory.
  3. 3.Active positions continue to be monitored immediately.
  4. 4.Pending orders continue tracking exchange execution status.
  5. 5.Risk controls remain preserved.

This allows the bot to continue operation without manual intervention after interruptions.

Purpose:

  • Prevent losing track of open positions
  • Preserve trading continuity
  • Maintain accurate risk management state
  • Avoid duplicate entries after restart

Position Recovery & State Storage Flow

Interactive architecture visualizer showing state persistence and automatic startup recovery

Trading EngineIn-Memory
Active Position ManagerLIVE
Order ManagerLIVE
Symbol ControlsLIVE
Risk ManagementLIVE
Persistent StoragePython Pickle
FILEactive_positions.pkl
FILEpending_orders.pkl
FILEsymbol_controls.pkl
FILErisk_states.pkl
Async Write Queue (Background Thread)
Restored Engine StateRestored
Loaded Active Positions
Recovered Pending Orders
Preserved Symbol Controls
Continued Stop Loss / Trailing
01

1. Active Engine Write-Back

Trading Engine continuously pushes updated positions, orders, and symbol rules to the persistent async queue.


Database Structure

The persistence layer uses lightweight local file-based storage built on top of Python pickle serialization.

Each persistent structure is stored in an independent file:

  • active_positions.pkl
  • pending_orders.pkl
  • symbol_controls.pkl

The architecture is intentionally minimalistic and optimized for low-latency trading systems.

Database Structure & Schema Inspector

Select a file to inspect stored attributes, field types, and persistence purpose

Stores currently open trades and position tracking parameters required for automated management.

Field NameTypeSample ValueDescription
symbolstr"BTCUSDT"Trading pair identifier
sidestr"LONG"Position direction
qtyfloat0.45Position contract size
entry_pricefloat64250.50Average entry price
stop_lossfloat63100.00Active stop loss trigger price
take_profitfloat66500.00Target take profit price
trailing_stopbooltrueTrailing stop activation flag
timestampint1722345600Position creation timestamp
Key Purpose & Continuity Role:
  • Resume position management instantly after bot restart
  • Continue automated stop loss and trailing stop calculations
  • Preserve exact entry prices and execution state across crashes

Active Positions

Stores currently open trades.

Each position contains:

  • Trading symbol
  • Position side
  • Quantity
  • Entry price
  • Stop loss price
  • Take profit price
  • Trailing stop status
  • Timestamp

Purpose:

  • Resume position management after restart
  • Continue stop loss and trailing logic
  • Preserve exact entry state

Pending Orders

Stores exchange orders waiting for execution confirmation.

Each order contains:

  • Symbol
  • Order side
  • Order type
  • Quantity
  • Exchange order ID
  • Current status

Purpose:

  • Prevent duplicate orders
  • Continue monitoring exchange execution state
  • Recover unfinished order flows

Symbol Controls

Stores runtime trading permissions and protection states.

Each symbol may contain:

  • Buying enabled status
  • Selling enabled status
  • Disable reasons
  • Risk lock states

Purpose:

  • Preserve automated risk restrictions
  • Prevent accidental reactivation after restart

Crash Recovery Logic

The persistence system is designed for automatic recovery during:

  • Process crashes
  • Server restarts
  • VPS failures
  • Power outages
  • Unexpected exceptions

Asynchronous Persistence

All writes are processed in a dedicated background thread.

Features:

  • Non-blocking trading execution
  • Continuous automatic saving
  • Queue-based save scheduling

Purpose:

  • Prevent trading delays caused by disk operations.

Atomic File Saving

The system uses atomic file replacement for maximum data safety.

Save flow:

  1. 1.Data is written into a temporary .tmp file.
  2. 2.Temporary file is fully completed.
  3. 3.File is atomically renamed into the final persistence file.

Purpose:

  • Prevent corrupted persistence files during crashes or interrupted writes.

Atomic File Saving & Crash Safety Visualizer

Interactive simulator of temporary file creation, validation, and atomic OS rename

1. Write to .tmp FileIn Progress
active_positions.pkl.tmp

Updated engine state is written to a separate temporary file on disk without touching the live file.

2. Validate File IntegrityVerified
active_positions.pkl.tmp

The persistence layer verifies checksum and payload completeness before proceeding.

3. Atomic OS RenameCompleted
active_positions.pkl

The OS atomically replaces active_positions.pkl with the validated .tmp file in a single operation.

🛡️ ATOMIC GUARANTEE:

Atomic file replacement guarantees that disk storage is either 100% updated or unchanged. Partial or corrupted saves are physically impossible.

Save Deduplication

The persistence queue automatically removes outdated save requests.

Behavior:

  • Only the newest state is persisted.
  • Redundant disk writes are skipped.

Purpose:

  • Reduce disk usage
  • Improve performance under heavy update frequency

Automatic State Reload

During startup:

  1. 1.Persistence files are detected.
  2. 2.Serialized objects are loaded back into memory.
  3. 3.Trading engine resumes monitoring immediately.

If persistence files do not exist:

  • New clean storage is automatically initialized.

Thread Safety

The persistence engine uses internal synchronization locks during save operations.

Features:

  • Concurrent-safe writes
  • Safe multi-threaded access
  • Protected file replacement

Purpose:

  • Ensure consistency between trading threads and persistence layer

Graceful Shutdown Protection

Before shutdown:

  1. 1.Final save operation is forced.
  2. 2.Save queue is flushed completely.
  3. 3.Background persistence thread is stopped safely.

Purpose:

  • Guarantee latest trading state is written to disk before exit.