经纪人#
参考资料#
- class backtrader.brokers.BackBroker(*args, **kwargs)#
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 theopen
price)Close
: meant for intraday in which the order is executed with the closing price of the last bar of the sessionLimit
: executes if the given limit price is seen during the sessionStop
: executes aMarket
order if the given stop price is seenStopLimit
: sets aLimit
order 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 = instance
to set the instance as the broker for therun
executionUse the
set_xxx
to set the value usingcerebro.broker.set_xxx
where`xxx
stands for the name of the parameter to set
备注
cerebro.broker
is a property supported by thegetbroker
andsetbroker
methods ofCerebro
Params:
cash
(default:10000
): starting cashcommission
(default:CommInfoBase(percabs=True)
) base commission scheme which applies to all assetschecksubmit
(default:True
) check margin/cash before accepting an order into the systemeosbar
(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 sessionfiller
(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
Order
documentation and reference for things available inside anOrder
instanceprice
the price at which the order is going to be executed in theago
barago
: index meant to be used withorder.data
for the extraction of the ohlc and volume 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 the executed size (a value >= 0)
The callable may of course be an object with
__call__
matching the aforementioned signatureWith the default
None
orders will be completely executed in a single shotslip_perc
(default:0.0
) Percentage in absolute termns (and positive) that should be used to slip prices up/down for buy/sell ordersNote:
0.01
is1%
0.001
is0.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_perc
is 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 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 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
True
the broker will offer a match by capping slippage athigh/low
prices in case they would be exceeded.If
False
the broker will not match the order with the current prices and will try execution during the next iterationslip_limit
(default:True
)Limit
orders, given the exact match price requested, will be matched even ifslip_match
isFalse
.This option controls that behavior.
If
True
, thenLimit
orders will be matched by capping prices to thelimit
/high/low
pricesIf
False
and slippage exceeds the cap, then there will be no matchslip_out
(default:False
)Provide slippage even if the price falls outside the
high
-low
range.coc
(default:False
)- Cheat-On-Close Setting this to
True
withset_coc
enables matching 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
- Cheat-On-Close Setting this to
coo
(default:False
)- Cheat-On-Open Setting this to
True
withset_coo
enables matching a
Market
order to the opening price, by for example using a timer withcheat
set toTrue
, because such a timer gets executed before the broker has evaluated
- Cheat-On-Open Setting this to
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
False
then the cash will be deducted as operation cost and the calculated value will be positive to end up with the same amountfundstartval
(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
True
analyzers likeTimeReturn
can 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 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 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 otherCommissionInfo
scheme can be found
Returns the current number of shares in the fund-like mode
- get_fundvalue()#
Returns the Fund-like share value
- cash is adjusted on each iteration for instruments like