Skip to content

Commit

Permalink
Added a check to prevent calls to setShares once the strategy started…
Browse files Browse the repository at this point in the history
… executing
  • Loading branch information
gbeced committed Oct 20, 2017
1 parent 6a598a3 commit cda1072
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions pyalgotrade/broker/backtesting.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ def __init__(self, cash, barFeed, commission=None):
self.__barFeed = barFeed
self.__allowNegativeCash = False
self.__nextOrderId = 1
self.__started = False

def _getNextOrderId(self):
ret = self.__nextOrderId
Expand Down Expand Up @@ -290,13 +291,16 @@ def getInstrumentTraits(self, instrument):
def getShares(self, instrument):
return self.__shares.get(instrument, 0)

def setShares(self, instrument, value):
def setShares(self, instrument, quantity):
"""
This method should only works on asset init.
:param instrument: the security name.
:param value: how much you have of given instrument.
Set existing shares before the strategy starts executing.
:param instrument: Instrument identifier.
:param quantity: The number of shares for the given instrument.
"""
self.__shares[instrument] = value

assert not self.__started, "Can't setShares once the strategy started executing"
self.__shares[instrument] = quantity

def getPositions(self):
return self.__shares
Expand Down Expand Up @@ -461,6 +465,7 @@ def onBars(self, dateTime, bars):

def start(self):
super(Broker, self).start()
self.__started = True

def stop(self):
pass
Expand Down

0 comments on commit cda1072

Please sign in to comment.