Class PaperTradingGateway

java.lang.Object
com.quantfinlib.trading.PaperTradingGateway
All Implemented Interfaces:
OrderGateway

public final class PaperTradingGateway extends Object implements OrderGateway
Quote-driven paper trading venue: closes the research-to-production loop by running real strategy + risk-gate code against simulated fills.
  • Feed top-of-book quotes with onQuote(java.lang.String, double, double); market orders fill at the touch, resting limit orders fill when the market crosses them.
  • Every order passes the optional PreTradeLimitChecker first — rejected orders never reach the market, exactly like production.
  • Full account tracking: signed positions with average cost, cash, realized/unrealized P&L, commission, and mark-to-market equity.
Thread-safe: all operations synchronize on the gateway, and snapshot() returns an internally consistent view of the account — safe to read from a dashboard thread while another thread trades.
  • Constructor Details

    • PaperTradingGateway

      public PaperTradingGateway(double initialCash)
    • PaperTradingGateway

      public PaperTradingGateway(double initialCash, double commissionRate, PreTradeLimitChecker limitChecker)
  • Method Details

    • onQuote

      public void onQuote(String symbol, double bid, double ask)
      Updates the top of book and fills any resting limit orders that now cross.
    • submitLimit

      public long submitLimit(String symbol, Side side, long quantity, double price)
      Description copied from interface: OrderGateway
      Submits a limit order; returns the order id (status may be REJECTED).
      Specified by:
      submitLimit in interface OrderGateway
    • submitMarket

      public long submitMarket(String symbol, Side side, long quantity)
      Description copied from interface: OrderGateway
      Submits a market order for immediate execution at the touch.
      Specified by:
      submitMarket in interface OrderGateway
    • cancel

      public boolean cancel(long orderId)
      Description copied from interface: OrderGateway
      Cancels a working order. Returns false if unknown or already terminal.
      Specified by:
      cancel in interface OrderGateway
    • status

      public OrderStatus status(long orderId)
      Specified by:
      status in interface OrderGateway
    • addExecutionListener

      public void addExecutionListener(OrderGateway.ExecutionListener listener)
      Specified by:
      addExecutionListener in interface OrderGateway
    • position

      public double position(String symbol)
    • cash

      public double cash()
    • realizedPnl

      public double realizedPnl()
    • equity

      public double equity()
      Mark-to-market equity at current mids.
    • rejectionLog

      public List<String> rejectionLog()
      The first 1000 rejection messages; the snapshot's rejectionCount keeps counting past the cap.
    • positionsSnapshot

      public Map<String,Double> positionsSnapshot()
      Snapshot of non-zero positions by symbol (for dashboards/monitoring).
    • snapshot