Broker
Reference
class backtrader.brokers.BackBroker()
Broker Simulator
The simulation supports different order types, checking a submitted order
cash requirements against current cash, keeping track of cash and value
for each iteration of cerebro and keeping the current position on
different datas.
cash is adjusted on each iteration for instruments like futures for
which a price change implies in real brokers the addition/substracion of
cash.
Supported order types:
-
Market: to be executed with the 1st tick of the next bar (namely theopenprice) -
Close: meant for intraday in which the order is executed with the closing price of the last bar of the session -
Limit: executes if the given limit price is seen during the session -
Stop: executes aMarketorder if the given stop price is seen -
StopLimit: sets aLimitorder in motion if the given stop price is seen
Because the broker is instantiated by Cerebro and there should be
(mostly) no reason to replace the broker, the params are not controlled
by the user for the instance. To change this there are two options:
-
Manually create an instance of this class with the desired params and use
cerebro.broker = instanceto set the instance as the broker for therunexecution -
Use the
set_xxxto set the value usingcerebro.broker.set_xxxwhere\xxx` stands for the name of the parameter to set
Note
cerebro.broker is a property supported by the getbroker
and setbroker methods of Cerebro
Params:
-
cash(default:10000): starting cash -
commission(default:CommInfoBase(percabs=True)) base commission scheme which applies to all assets -
checksubmit(default:True) check margin/cash before accepting an order into the system -
eosbar(default:False): With intraday bars consider a bar with the sametimeas the end of session to be the end of the session. This is not usually the case, because some bars (final auction) are produced by many exchanges for many products for a couple of minutes after the end of the session -
eosbar(default:False): With intraday bars consider a bar with the sametimeas the end of session to be the end of the session. This is not usually the case, because some bars (final auction) are produced by many exchanges for many products for a couple of minutes after the end of the session -
filler(default:None)A callable with signature:
callable(order, price, ago)-
order: obviously the order in execution. This provides access to the data (and with it the ohlc and volume values), the execution type, remaining size (order.executed.remsize) and others.Please check the
Orderdocumentation and reference for things available inside anOrderinstance -
pricethe price at which the order is going to be executed in theagobar -
ago: index meant to be used withorder.datafor the extraction of the ohlc and volume prices. In most cases this will be0but on a corner case forCloseorders, this will be-1.In order to get the bar volume (for example) do:
volume = order.data.voluume[ago]
The callable must return the executed size (a value >= 0)
The callable may of course be an object with
__call__matching the aforementioned signatureWith the default
Noneorders will be completely executed in a single shot -
-
slip_perc(default:0.0) Percentage in absolute termns (and positive) that should be used to slip prices up/down for buy/sell ordersNote:
-
0.01is1% -
0.001is0.1%
-
-
slip_fixed(default:0.0) Percentage in units (and positive) that should be used to slip prices up/down for buy/sell ordersNote: if
slip_percis non zero, it takes precendence over this. -
slip_open(default:False) whether to slip prices for order execution which would specifically used the opening price of the next bar. An example would beMarketorder which is executed with the next available tick, i.e: the opening price of the bar.This also applies to some of the other executions, because the logic tries to detect if the opening price would match the requested price/execution type when moving to a new bar.
-
slip_match(default:True)If
Truethe broker will offer a match by capping slippage athigh/lowprices in case they would be exceeded.If
Falsethe broker will not match the order with the current prices and will try execution during the next iteration -
slip_limit(default:True)Limitorders, given the exact match price requested, will be matched even ifslip_matchisFalse.This option controls that behavior.
If
True, thenLimitorders will be matched by capping prices to thelimit/high/lowpricesIf
Falseand slippage exceeds the cap, then there will be no match -
slip_out(default:False)Provide slippage even if the price falls outside the
high-lowrange. -
coc(default:False)Cheat-On-Close Setting this to
Truewithset_cocenablesmatching a `Market` order to the closing price of the bar in which the order was issued. This is actually *cheating*, because the bar is *closed* and any order should first be matched against the prices in the next bar -
coo(default:False)Cheat-On-Open Setting this to
Truewithset_cooenablesmatching a `Market` order to the opening price, by for example using a timer with `cheat` set to `True`, because such a timer gets executed before the broker has evaluated -
int2pnl(default:True)Assign generated interest (if any) to the profit and loss of operation that reduces a position (be it long or short). There may be cases in which this is undesired, because different strategies are competing and the interest would be assigned on a non-deterministic basis to any of them.
-
shortcash(default:True)If True then cash will be increased when a stocklike asset is shorted and the calculated value for the asset will be negative.
If
Falsethen the cash will be deducted as operation cost and the calculated value will be positive to end up with the same amount -
fundstartval(default:100.0)This parameter controls the start value for measuring the performance in a fund-like way, i.e.: cash can be added and deducted increasing the amount of shares. Performance is not measured using the net asset value of the porftoflio but using the value of the fund
-
fundmode(default:False)If this is set to
Trueanalyzers likeTimeReturncan automatically calculate returns based on the fund value and not on the total net asset value
set_cash(cash)
Sets the cash parameter (alias: setcash)
get_cash()
Returns the current cash (alias: getcash)
get_value(datas=None, mkt=False, lever=False)
Returns the portfolio value of the given datas (if datas is None, then
the total portfolio value will be returned (alias: getvalue)
set_eosbar(eosbar)
Sets the eosbar parameter (alias: seteosbar
set_checksubmit(checksubmit)
Sets the checksubmit parameter
set_filler(filler)
Sets a volume filler for volume filling execution
set_coc(coc)
Configure the Cheat-On-Close method to buy the close on order bar
set_coo(coo)
Configure the Cheat-On-Open method to buy the close on order bar
set_int2pnl(int2pnl)
Configure assignment of interest to profit and loss
set_fundstartval(fundstartval)
Set the starting value of the fund-like performance tracker
set_slippage_perc(perc, slip_open=True, slip_limit=True, slip_match=True, slip_out=False)
Configure slippage to be percentage based
set_slippage_fixed(fixed, slip_open=True, slip_limit=True, slip_match=True, slip_out=False)
Configure slippage to be fixed points based
get_orders_open(safe=False)
Returns an iterable with the orders which are still open (either not executed or partially executed
The orders returned must not be touched.
If order manipulation is needed, set the parameter safe to True
getcommissioninfo(data)
Retrieves the CommissionInfo scheme associated with the given
data
setcommission(commission=0.0, margin=None, mult=1.0, commtype=None, percabs=True, stocklike=False, interest=0.0, interest_long=False, leverage=1.0, automargin=False, name=None)
This method sets a `` CommissionInfo`` object for assets managed in
the broker with the parameters. Consult the reference for
CommInfoBase
If name is None, this will be the default for assets for which no
other CommissionInfo scheme can be found
addcommissioninfo(comminfo, name=None)
Adds a CommissionInfo object that will be the default for all assets if
name is None
getposition(data)
Returns the current position status (a Position instance) for
the given data
get_fundshares()
Returns the current number of shares in the fund-like mode
get_fundvalue()
Returns the Fund-like share value
add_cash(cash)
Add/Remove cash to the system (use a negative value to remove)