Broker
Reference
class backtrader.brokers.BackBroker()
Broker Simulator
The simulation supports different order types, checking a submitted ordercash requirements against current cash, keeping track of cash and valuefor each iteration ofcerebro and keeping the current position ondifferent datas.
cash is adjusted on each iteration for instruments likefutures for
which a price change implies in real brokers the addition/substracion ofcash.
Supported order types:
Market: to be executed with the 1st tick of the next bar (namely theopen price)
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 aMarket order if the given stop price is seen
StopLimit: sets aLimit order in motion if the given stop price is seen
Because the broker is instantiated byCerebro and there should be(mostly) no reason to replace the broker, the params are not controlledby the user for the instance. To change this there are two options:
Manually create an instance of this class with the desired params and usecerebro.broker = instance to set the instance as the broker for therun execution
Use theset_xxx to set the value usingcerebro.broker.set_xxx where\xxx` stands for the name of the parameter to set
Note
cerebro.broker is aproperty supported by thegetbrokerandsetbroker methods ofCerebro
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 sametime as 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 sametime as 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 thedata (and with it theohlc andvolume values), theexecution type, remaining size (order.executed.remsize) and others.
Please check theOrder documentation and reference for thingsavailable inside anOrder instance
price the price at which the order is going to be executed in theago bar
ago: index meant to be used withorder.data for the extraction of theohlc andvolume prices. In most cases this will be0 but on a corner case forClose orders, this will be-1.
In order to get the bar volume (for example) do:volume =order.data.voluume[ago]
The callable must return theexecuted size (a value >= 0)
The callable may of course be an object with__call__ matchingthe aforementioned signature
With the defaultNone orders will be completely executed in asingle shot
slip_perc (default:0.0) Percentage in absolute termns (and positive) that should be used to slip prices up/down for buy/sell orders
Note:
slip_fixed (default:0.0) Percentage in units (and positive) that should be used to slip prices up/down for buy/sell orders
Note: ifslip_perc is non zero, it takes precendence over this.
slip_open (default:False) whether to slip prices for order execution which would specifically used theopening price of the next bar. An example would beMarket order 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 logictries to detect if theopening price would match the requestedprice/execution type when moving to a new bar.
slip_match (default:True)
IfTrue the broker will offer a match by capping slippage athigh/low prices in case they would be exceeded.
IfFalse the broker will not match the order with the currentprices and will try execution during the next iteration
slip_limit (default:True)
Limit orders, given the exact match price requested, will bematched even ifslip_match isFalse.
This option controls that behavior.
IfTrue, thenLimit orders will be matched by capping pricesto thelimit /high/low prices
IfFalse and slippage exceeds the cap, then there will be nomatch
slip_out (default:False)
Provideslippage even if the price falls outside thehigh -low range.
coc (default:False)
Cheat-On-Close Setting this toTrue withset_coc enables
matching a `Market` order to the closing price of the bar in whichthe order was issued. This is actually *cheating*, because the baris *closed* and any order should first be matched against the pricesin the next bar
coo (default:False)
Cheat-On-Open Setting this toTrue withset_coo enables
matching a `Market` order to the opening price, by for exampleusing a timer with `cheat` set to `True`, because such a timergets executed before the broker has evaluated
int2pnl (default:True)
Assign generated interest (if any) to the profit and loss ofoperation that reduces a position (be it long or short). There may becases in which this is undesired, because different strategies arecompeting and the interest would be assigned on a non-deterministicbasis to any of them.
shortcash (default:True)
If True then cash will be increased when a stocklike asset is shortedand the calculated value for the asset will be negative.
IfFalse then the cash will be deducted as operation cost and thecalculated value will be positive to end up with the same amount
fundstartval (default:100.0)
This parameter controls the start value for measuring the performancein a fund-like way, i.e.: cash can be added and deducted increasingthe amount of shares. Performance is not measured using the netasset value of the porftoflio but using the value of the fund
fundmode (default:False)
If this is set toTrue analyzers likeTimeReturn canautomatically calculate returns based on the fund value and not onthe 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 isNone, thenthe 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 notexecuted or partially executed
The orders returned must not be touched.
If order manipulation is needed, set the parametersafe to True
getcommissioninfo(data)
Retrieves theCommissionInfo scheme associated with the givendata
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 inthe broker with the parameters. Consult the reference forCommInfoBase
If name isNone, this will be the default for assets for which nootherCommissionInfo scheme can be found
addcommissioninfo(comminfo, name=None)
Adds aCommissionInfo object that will be the default for all assets ifname isNone
getposition(data)
Returns the current position status (aPosition instance) forthe givendata
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)