Thank you for sharing an amazing information. Currently I am in the learning stage and joined Python Training in Pune to become expert python developer. I would like to apply for this position after completion of training.
After some research, the problem has been resolved.
data feed should be in _load() rather than in start()
add islive() function
here is the modified code:
self.redis = redis.StrictRedis(host='localhost', port=6379, db=0)
self.ps = self.redis.pubsub()
for item in self.ps.listen():
if item['type'] == 'message':
self.d = item['data']
bar = json.loads(self.d)
self.lines.datetime = bt.date2num(pd.to_datetime(bar['date']))
self.lines.open = bar['open']
self.lines.close = bar['close']
self.lines.high = bar['high']
self.lines.low = bar['low']
self.lines.volume = bar['volume']
@mahaaveer I cannot tell for sure with this limited information. Print a log from orders and I suspect you will find your answer. You are adding the monthly cash to a value that already has the monthly cash when figuring out your size. Then you are likely not having enough money in your account to cover the trade, because it's too big.
target_value = self.broker.get_value() + self.p.monthly_cash # get right of self.p.monthly_cash
@bowguo You may think about the orders as instructions to change the current position. The order doesn't constitute the position itself.
The position is maintained by the broker per data ( not per order ). So if you buy order has completed - your position for particular data has increased and vice versa for sell order.
Regarding the multiple orders with different prices - once again the position is not maintained per order - so if you sell part of your position - the position will just decrease by the size of the sell order - regardless of any previous order history that brought your position to the current state.
Diving a little bit deeper however, the real broker maintains the base price for every single share/unit of the entity you are holding in your position - for various purposes ( tax calculation, average price calculation, margin requirements and so on ). Some brokers even allow you to select the tax calculation policy if multiple lots of the same entity with different prices are part of your position.