Skip to content

Commit

Permalink
Improve 'Close' execution support and correct conflicting behavior wi…
Browse files Browse the repository at this point in the history
…th method checksubmit. Addresses #62
  • Loading branch information
mementum committed Mar 14, 2016
1 parent 9583427 commit f00fa1d
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions backtrader/broker.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,31 @@


class BrokerBack(with_metaclass(MetaParams, object)):
'''Broker Simulator
Parameters:
Note: use the setXXXX to set the value after instance creation
- ``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 same ``time`` 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
'''
params = (
('cash', 10000.0),
('commission', CommInfoBase(percabs=True)),
('checksubmit', True),
('eosbar', False),
)

def __init__(self):
Expand All @@ -57,6 +77,9 @@ def init(self):

self.submitted = collections.deque()

def seteosbar(self, eosbar):
self.p.eosbar = eosbar

def getcash(self):
return self.cash

Expand Down Expand Up @@ -121,6 +144,7 @@ def orderstatus(self, order):
return o.status

def submit(self, order):
order.plen = len(order.data)
if self.p.checksubmit:
order.submit()
self.submitted.append(order)
Expand Down Expand Up @@ -154,7 +178,6 @@ def check_submitted(self):

def submit_accept(self, order):
order.pannotated = None
order.plen = len(order.data)
order.accept()
self.pending.append(order)
self.notify(order)
Expand Down Expand Up @@ -298,11 +321,11 @@ def notify(self, order):

def _try_exec_close(self, order, pclose):
if len(order.data) > order.plen:

dt0 = order.data.datetime[0]

if dt0 > order.dteos:
if order.pannotated:
if dt0 > order.dteos or (self.p.eosbar and dt0 == order.dteos):
# past the end of session or right at it and eosbar is True
if order.pannotated and dt0 != order.dteos:
execdt = order.data.datetime[-1]
execprice = order.pannotated
else:
Expand Down

0 comments on commit f00fa1d

Please sign in to comment.