Simulation Broker Class

class algotradepy.brokers.sim_broker.SimulationBroker(sim_streamer, starting_funds, transaction_cost=0, starting_positions=None)

Implements the broker interface for simulated back-testing.

The simulation broker uses the SimulationDataStreamer to provide simulated brokerage functionality allowing to back-test trading algorithms before taking them to a live-trading environment.

Parameters
  • sim_streamer (SimulationDataStreamer) – The simulation data streamer object.

  • starting_funds (float) – The funds with which the simulation will begin.

  • transaction_cost (float) – The cost of each transaction.

  • starting_positions (dict, optional, default None) – A dictionary of the starting positions, mapping each account to a dictionary of AContract to Position.

property acc_cash

The total funds available across all accounts.

property datetime

Server date and time.

property trades

Returns all trades placed in the current session.

property open_trades

Returns all currently open trades placed in the ongoing session.

sleep(secs)

Allows the server to deliver data.

subscribe_to_new_trades(func, fn_kwargs=None)

Subscribe to being notified of all newly created orders.

The orders are transmitted only if they were successfully submitted.

Parameters
  • func (Callable) – The function to which to feed the bars. It must accept AContract and AnOrder as its sole positional arguments.

  • fn_kwargs (Dict) – Keyword arguments to feed to the callback function along with the bars.

subscribe_to_trade_updates(func, fn_kwargs=None)

Subscribe to receiving updates on orders’ status.

Parameters
  • func (Callable) – The callback function. It must accept a Trade and a TradeStatus as its sole positional arguments.

  • fn_kwargs (dict) – The keyword arguments to pass to the callback function along with the positional arguments.

subscribe_to_position_updates(func, fn_kwargs=None)

Subscribe to receiving position updates.

Parameters
  • func (Callable) – The callback function. It must accept a Position as its sole positional argument.

  • fn_kwargs (dict) – The keyword arguments to pass to the callback function along with the positional arguments.

place_trade(trade, *args, trade_execution_price=None, trade_execution_size=None, **kwargs)

Place a trade with specified details.

Parameters

trade (Trade) – The trade to execute.

Returns

The tuple indicates if the order has been successfully placed, whereas the int is the associated order-id.

Return type

tuple of bool and int

cancel_trade(trade)

Cancel placed traded.

Parameters

trade (Trade) – The trade to cancel.

get_position(contract, *args, account=None, **kwargs)

Request the currently held position for a given symbol.

Parameters

contract (AContract) – The contract definition for which the position is required.

Returns

The current position for the specified symbol.

Return type

float

get_transaction_fee()

Request the broker transaction cost.

Returns

The cost per transaction.

Return type

float