Problem with multiple datafeeds
-
I have the list of past data of two stocks, say A and B. I want to long the stocks of A and short the stocks of B. My strategy looks as follows. When I add the two datas and run cerebro, I see that the "Buy" order is taking place only in the very beginning and remaining all are "Sell" orders.
class LazyPrices(bt.Strategy): def log(self,txt,dt=None): dt = dt or self.datas[0].datetime.date(0) print('%s, %s' % (dt.isoformat(),txt)) def __init__(self): self.dataCloseLong = self.datas[0].close self.dataCloseShort = self.datas[1].close self.order = None def notify_order(self,order): if order.status in [order.Submitted, order.Accepted]: return if order.status in [order.Completed]: if order.isbuy(): self.log('Buy Executed, Price: %.2f, Cost: %.2f, Size: %d' % (order.executed.price, order.executed.value,order.executed.size)) elif order.issell(): self.log('Sell Executed, Price: %.2f, Cost: %.2f, Size: %d' % (order.executed.price,order.executed.value,order.executed.size)) self.order = None def notify_trade(self,trade): if not trade.isclosed: return self.log('Trade Completed, Operation Profit Gross: %.2f, Net: %.2f' % (trade.pnl,trade.pnlcomm)) def next(self): if self.order: return if not self.position: self.log('Buy Created, %.2f ' % (self.dataCloseLong[0])) self.order = self.buy(data = self.datas[0],size=1) else: self.log('Sell Created, %.2f' % self.dataCloseShort[0]) self.order = self.sell(data = self.datas[1],size=1) cerebro = bt.Cerebro() cerebro.broker.setcash(100000.0) data_0 = bt.feeds.PandasData(dataname=data_long) #A data_1 = bt.feeds.PandasData(dataname=data_short) #B cerebro.adddata(data_0) cerebro.adddata(data_1) cerebro.addstrategy(LazyPrices) print('Initial Value of the Portfolio: %.2f' % cerebro.broker.get_value()) results = cerebro.run() print('Final Value of the Portfolio: %.2f' % cerebro.broker.get_value())
The output log is as follows:
Initial Value of the Portfolio: 100000.00 2011-01-03, Buy Created, 153.93 2011-01-04, Buy Executed, Price: 154.84, Cost: 154.84, Size: 1 2011-01-04, Sell Created, 136.97 2011-01-05, Sell Executed, Price: 137.50, Cost: -137.50, Size: -1 2011-01-05, Sell Created, 136.28 2011-01-06, Sell Executed, Price: 136.96, Cost: -136.96, Size: -1 2011-01-06, Sell Created, 135.23 2011-01-07, Sell Executed, Price: 135.93, Cost: -135.93, Size: -1 2011-01-07, Sell Created, 132.38 2011-01-10, Sell Executed, Price: 132.67, Cost: -132.67, Size: -1 2011-01-10, Sell Created, 129.57 2011-01-11, Sell Executed, Price: 130.02, Cost: -130.02, Size: -1 2011-01-11, Sell Created, 128.81 2011-01-12, Sell Executed, Price: 129.12, Cost: -129.12, Size: -1 2011-01-12, Sell Created, 129.31 2011-01-13, Sell Executed, Price: 129.56, Cost: -129.56, Size: -1 2011-01-13, Sell Created, 129.06 2011-01-14, Sell Executed, Price: 130.30, Cost: -130.30, Size: -1 2011-01-14, Sell Created, 129.96 2011-01-17, Sell Executed, Price: 129.70, Cost: -129.70, Size: -1 2011-01-17, Sell Created, 127.88 2011-01-18, Sell Executed, Price: 128.68, Cost: -128.68, Size: -1 2011-01-18, Sell Created, 128.30 2011-01-19, Sell Executed, Price: 128.92, Cost: -128.92, Size: -1 2011-01-19, Sell Created, 127.98 2011-01-20, Sell Executed, Price: 127.06, Cost: -127.06, Size: -1 2011-01-20, Sell Created, 127.93 2011-01-21, Sell Executed, Price: 127.77, Cost: -127.77, Size: -1 2011-01-21, Sell Created, 127.22 2011-01-24, Sell Executed, Price: 128.43, Cost: -128.43, Size: -1 2011-01-24, Sell Created, 128.79 2011-01-25, Sell Executed, Price: 129.92, Cost: -129.92, Size: -1 2011-01-25, Sell Created, 128.89 2011-01-27, Sell Executed, Price: 128.15, Cost: -128.15, Size: -1 2011-01-27, Sell Created, 125.85 2011-01-28, Sell Executed, Price: 126.50, Cost: -126.50, Size: -1 2011-01-28, Sell Created, 123.76 2011-01-31, Sell Executed, Price: 123.52, Cost: -123.52, Size: -1 2011-01-31, Sell Created, 123.66 2011-02-01, Sell Executed, Price: 123.79, Cost: -123.79, Size: -1 2011-02-01, Sell Created, 121.80 2011-02-02, Sell Executed, Price: 121.50, Cost: -121.50, Size: -1 2011-02-02, Sell Created, 120.33 2011-02-03, Sell Executed, Price: 121.40, Cost: -121.40, Size: -1 2011-02-03, Sell Created, 122.76 2011-02-04, Sell Executed, Price: 123.77, Cost: -123.77, Size: -1 2011-02-04, Sell Created, 121.60 2011-02-07, Sell Executed, Price: 122.77, Cost: -122.77, Size: -1 2011-02-07, Sell Created, 121.33 2011-02-08, Sell Executed, Price: 120.85, Cost: -120.85, Size: -1 2011-02-08, Sell Created, 117.97 2011-02-09, Sell Executed, Price: 117.90, Cost: -117.90, Size: -1 2011-02-09, Sell Created, 114.36 2011-02-10, Sell Executed, Price: 113.98, Cost: -113.98, Size: -1 2011-02-10, Sell Created, 113.14 2011-02-11, Sell Executed, Price: 114.51, Cost: -114.51, Size: -1 2011-02-11, Sell Created, 116.11 2011-02-14, Sell Executed, Price: 117.68, Cost: -117.68, Size: -1 2011-02-14, Sell Created, 119.80 2011-02-15, Sell Executed, Price: 119.81, Cost: -119.81, Size: -1 2011-02-15, Sell Created, 119.40 2011-02-16, Sell Executed, Price: 119.18, Cost: -119.18, Size: -1 2011-02-16, Sell Created, 119.52 2011-02-17, Sell Executed, Price: 120.55, Cost: -120.55, Size: -1 2011-02-17, Sell Created, 121.04 2011-02-18, Sell Executed, Price: 121.96, Cost: -121.96, Size: -1 2011-02-18, Sell Created, 119.39 2011-02-21, Sell Executed, Price: 119.93, Cost: -119.93, Size: -1 2011-02-21, Sell Created, 119.94 2011-02-22, Sell Executed, Price: 119.62, Cost: -119.62, Size: -1 2011-02-22, Sell Created, 119.53 2011-02-23, Sell Executed, Price: 119.06, Cost: -119.06, Size: -1 2011-02-23, Sell Created, 118.70 2011-02-24, Sell Executed, Price: 118.84, Cost: -118.84, Size: -1 2011-02-24, Sell Created, 116.73 2011-02-25, Sell Executed, Price: 117.00, Cost: -117.00, Size: -1 2011-02-25, Sell Created, 116.20 2011-02-28, Sell Executed, Price: 116.76, Cost: -116.76, Size: -1 2011-02-28, Sell Created, 116.07 2011-03-01, Sell Executed, Price: 117.33, Cost: -117.33, Size: -1 2011-03-01, Sell Created, 118.94 2011-03-03, Sell Executed, Price: 118.95, Cost: -118.95, Size: -1 2011-03-03, Sell Created, 119.82 2011-03-04, Sell Executed, Price: 120.84, Cost: -120.84, Size: -1 2011-03-04, Sell Created, 120.17 2011-03-07, Sell Executed, Price: 118.88, Cost: -118.88, Size: -1 2011-03-07, Sell Created, 118.83 2011-03-08, Sell Executed, Price: 119.11, Cost: -119.11, Size: -1 2011-03-08, Sell Created, 119.48 2011-03-09, Sell Executed, Price: 120.19, Cost: -120.19, Size: -1 2011-03-09, Sell Created, 119.93 2011-03-10, Sell Executed, Price: 118.57, Cost: -118.57, Size: -1 2011-03-10, Sell Created, 118.74 2011-03-11, Sell Executed, Price: 118.91, Cost: -118.91, Size: -1 2011-03-11, Sell Created, 118.50 2011-03-14, Sell Executed, Price: 118.14, Cost: -118.14, Size: -1 2011-03-14, Sell Created, 119.17 2011-03-15, Sell Executed, Price: 117.22, Cost: -117.22, Size: -1 2011-03-15, Sell Created, 117.98 2011-03-16, Sell Executed, Price: 119.48, Cost: -119.48, Size: -1 2011-03-16, Sell Created, 119.81 2011-03-17, Sell Executed, Price: 119.04, Cost: -119.04, Size: -1 2011-03-17, Sell Created, 118.87 2011-03-18, Sell Executed, Price: 119.62, Cost: -119.62, Size: -1 2011-03-18, Sell Created, 117.98 2011-03-21, Sell Executed, Price: 119.14, Cost: -119.14, Size: -1 2011-03-21, Sell Created, 118.46 2011-03-22, Sell Executed, Price: 118.28, Cost: -118.28, Size: -1 2011-03-22, Sell Created, 118.48 2011-03-23, Sell Executed, Price: 120.09, Cost: -120.09, Size: -1 2011-03-23, Sell Created, 120.62 2011-03-24, Sell Executed, Price: 120.77, Cost: -120.77, Size: -1 2011-03-24, Sell Created, 121.21 2011-03-25, Sell Executed, Price: 121.87, Cost: -121.87, Size: -1 2011-03-25, Sell Created, 122.82 2011-03-28, Sell Executed, Price: 122.71, Cost: -122.71, Size: -1 2011-03-28, Sell Created, 122.59 2011-03-29, Sell Executed, Price: 122.67, Cost: -122.67, Size: -1 2011-03-29, Sell Created, 123.52 2011-03-30, Sell Executed, Price: 124.07, Cost: -124.07, Size: -1 2011-03-30, Sell Created, 124.88 2011-03-31, Sell Executed, Price: 125.12, Cost: -125.12, Size: -1 2011-03-31, Sell Created, 125.34 2011-04-01, Sell Executed, Price: 125.50, Cost: -125.50, Size: -1 2011-04-01, Sell Created, 125.82 2011-04-04, Sell Executed, Price: 127.61, Cost: -127.61, Size: -1 2011-04-04, Sell Created, 128.97 2011-04-05, Sell Executed, Price: 129.37, Cost: -129.37, Size: -1 2011-04-05, Sell Created, 129.81 2011-04-06, Sell Executed, Price: 130.22, Cost: -130.22, Size: -1 2011-04-06, Sell Created, 129.68 2011-04-07, Sell Executed, Price: 130.09, Cost: -130.09, Size: -1 2011-04-07, Sell Created, 131.76 2011-04-08, Sell Executed, Price: 131.71, Cost: -131.71, Size: -1 2011-04-08, Sell Created, 131.03 2011-04-11, Sell Executed, Price: 131.32, Cost: -131.32, Size: -1 2011-04-11, Sell Created, 130.29 2011-04-13, Sell Executed, Price: 130.77, Cost: -130.77, Size: -1 2011-04-13, Sell Created, 132.19 2011-04-15, Sell Executed, Price: 132.15, Cost: -132.15, Size: -1 2011-04-15, Sell Created, 131.33 2011-04-18, Sell Executed, Price: 132.04, Cost: -132.04, Size: -1 2011-04-18, Sell Created, 130.03 2011-04-19, Sell Executed, Price: 130.28, Cost: -130.28, Size: -1 2011-04-19, Sell Created, 129.45 2011-04-20, Sell Executed, Price: 130.14, Cost: -130.14, Size: -1 2011-04-20, Sell Created, 130.25 2011-04-21, Sell Executed, Price: 130.97, Cost: -130.97, Size: -1 2011-04-21, Sell Created, 130.40 2011-04-25, Sell Executed, Price: 131.16, Cost: -131.16, Size: -1 2011-04-25, Sell Created, 131.78 2011-04-26, Sell Executed, Price: 131.60, Cost: -131.60, Size: -1 2011-04-26, Sell Created, 131.94 2011-04-27, Sell Executed, Price: 132.90, Cost: -132.90, Size: -1 2011-04-27, Sell Created, 132.41 2011-04-28, Sell Executed, Price: 132.63, Cost: -132.63, Size: -1 2011-04-28, Sell Created, 131.38 2011-04-29, Sell Executed, Price: 134.06, Cost: -134.06, Size: -1 2011-04-29, Sell Created, 132.63 2011-05-02, Sell Executed, Price: 131.63, Cost: -131.63, Size: -1 2011-05-02, Sell Created, 129.58 2011-05-03, Sell Executed, Price: 128.44, Cost: -128.44, Size: -1 2011-05-03, Sell Created, 126.44 2011-05-04, Sell Executed, Price: 127.49, Cost: -127.49, Size: -1 2011-05-04, Sell Created, 126.28 2011-05-05, Sell Executed, Price: 127.70, Cost: -127.70, Size: -1 2011-05-05, Sell Created, 125.92 2011-05-06, Sell Executed, Price: 126.70, Cost: -126.70, Size: -1 2011-05-06, Sell Created, 126.60 2011-05-09, Sell Executed, Price: 126.95, Cost: -126.95, Size: -1 2011-05-09, Sell Created, 126.86 2011-05-10, Sell Executed, Price: 128.10, Cost: -128.10, Size: -1 2011-05-10, Sell Created, 127.52 2011-05-11, Sell Executed, Price: 128.63, Cost: -128.63, Size: -1 2011-05-11, Sell Created, 128.60 2011-05-12, Sell Executed, Price: 128.76, Cost: -128.76, Size: -1 2011-05-12, Sell Created, 127.32 2011-05-13, Sell Executed, Price: 125.81, Cost: -125.81, Size: -1 2011-05-13, Sell Created, 125.83 2011-05-16, Sell Executed, Price: 125.80, Cost: -125.80, Size: -1 2011-05-16, Sell Created, 125.29 2011-05-17, Sell Executed, Price: 128.01, Cost: -128.01, Size: -1 2011-05-17, Sell Created, 126.84 2011-05-18, Sell Executed, Price: 125.88, Cost: -125.88, Size: -1 2011-05-18, Sell Created, 125.11 2011-05-19, Sell Executed, Price: 124.37, Cost: -124.37, Size: -1 2011-05-19, Sell Created, 123.41 2011-05-20, Sell Executed, Price: 125.09, Cost: -125.09, Size: -1 2011-05-20, Sell Created, 125.38 2011-05-23, Sell Executed, Price: 123.98, Cost: -123.98, Size: -1 2011-05-23, Sell Created, 122.65 2011-05-24, Sell Executed, Price: 123.25, Cost: -123.25, Size: -1 2011-05-24, Sell Created, 123.76 2011-05-25, Sell Executed, Price: 122.95, Cost: -122.95, Size: -1 2011-05-25, Sell Created, 122.45 2011-05-26, Sell Executed, Price: 123.04, Cost: -123.04, Size: -1 2011-05-26, Sell Created, 123.09 2011-05-27, Sell Executed, Price: 124.35, Cost: -124.35, Size: -1 2011-05-27, Sell Created, 124.63 2011-05-30, Sell Executed, Price: 125.55, Cost: -125.55, Size: -1 2011-05-30, Sell Created, 125.33 2011-05-31, Sell Executed, Price: 126.15, Cost: -126.15, Size: -1 2011-05-31, Sell Created, 126.91 2011-06-01, Sell Executed, Price: 127.63, Cost: -127.63, Size: -1 2011-06-01, Sell Created, 128.41 2011-06-02, Sell Executed, Price: 126.55, Cost: -126.55, Size: -1 2011-06-02, Sell Created, 126.97 2011-06-03, Sell Executed, Price: 128.02, Cost: -128.02, Size: -1 2011-06-03, Sell Created, 126.94 2011-06-06, Sell Executed, Price: 127.44, Cost: -127.44, Size: -1 2011-06-06, Sell Created, 127.25 2011-06-07, Sell Executed, Price: 127.21, Cost: -127.21, Size: -1 2011-06-07, Sell Created, 127.89 2011-06-08, Sell Executed, Price: 127.88, Cost: -127.88, Size: -1 2011-06-08, Sell Created, 127.80 2011-06-09, Sell Executed, Price: 128.35, Cost: -128.35, Size: -1 2011-06-09, Sell Created, 128.20 2011-06-10, Sell Executed, Price: 127.99, Cost: -127.99, Size: -1 2011-06-10, Sell Created, 127.45 2011-06-13, Sell Executed, Price: 127.16, Cost: -127.16, Size: -1 2011-06-13, Sell Created, 127.28 2011-06-14, Sell Executed, Price: 127.53, Cost: -127.53, Size: -1 2011-06-14, Sell Created, 128.03 2011-06-15, Sell Executed, Price: 128.41, Cost: -128.41, Size: -1 2011-06-15, Sell Created, 127.88 2011-06-16, Sell Executed, Price: 126.07, Cost: -126.07, Size: -1 2011-06-16, Sell Created, 125.61 2011-06-17, Sell Executed, Price: 127.20, Cost: -127.20, Size: -1 2011-06-17, Sell Created, 125.12 2011-06-20, Sell Executed, Price: 124.76, Cost: -124.76, Size: -1 2011-06-20, Sell Created, 120.96 2011-06-21, Sell Executed, Price: 121.29, Cost: -121.29, Size: -1 2011-06-21, Sell Created, 120.87 2011-06-22, Sell Executed, Price: 122.58, Cost: -122.58, Size: -1 2011-06-22, Sell Created, 121.25 2011-06-23, Sell Executed, Price: 119.82, Cost: -119.82, Size: -1 2011-06-23, Sell Created, 120.20 2011-06-24, Sell Executed, Price: 121.85, Cost: -121.85, Size: -1 2011-06-24, Sell Created, 123.48 2011-06-27, Sell Executed, Price: 123.65, Cost: -123.65, Size: -1 2011-06-27, Sell Created, 124.98 2011-06-28, Sell Executed, Price: 126.27, Cost: -126.27, Size: -1 2011-06-28, Sell Created, 126.31 2011-06-29, Sell Executed, Price: 127.08, Cost: -127.08, Size: -1 2011-06-29, Sell Created, 127.35 2011-06-30, Sell Executed, Price: 127.45, Cost: -127.45, Size: -1 2011-06-30, Sell Created, 127.61 2011-07-01, Sell Executed, Price: 128.94, Cost: -128.94, Size: -1 2011-07-01, Sell Created, 128.18 2011-07-04, Sell Executed, Price: 129.20, Cost: -129.20, Size: -1 2011-07-04, Sell Created, 128.81 2011-07-05, Sell Executed, Price: 127.91, Cost: -127.91, Size: -1 2011-07-05, Sell Created, 127.94 2011-07-06, Sell Executed, Price: 130.13, Cost: -130.13, Size: -1 2011-07-06, Sell Created, 129.27 2011-07-07, Sell Executed, Price: 129.58, Cost: -129.58, Size: -1 2011-07-07, Sell Created, 130.61 2011-07-08, Sell Executed, Price: 130.20, Cost: -130.20, Size: -1 2011-07-08, Sell Created, 128.77 2011-07-11, Sell Executed, Price: 128.85, Cost: -128.85, Size: -1 2011-07-11, Sell Created, 128.43 2011-07-12, Sell Executed, Price: 127.39, Cost: -127.39, Size: -1 2011-07-12, Sell Created, 126.84 2011-07-13, Sell Executed, Price: 128.69, Cost: -128.69, Size: -1 2011-07-13, Sell Created, 129.12 2011-07-14, Sell Executed, Price: 128.43, Cost: -128.43, Size: -1 2011-07-14, Sell Created, 128.69 2011-07-15, Sell Executed, Price: 127.40, Cost: -127.40, Size: -1 2011-07-15, Sell Created, 128.94 2011-07-18, Sell Executed, Price: 129.75, Cost: -129.75, Size: -1 2011-07-18, Sell Created, 130.42 2011-07-19, Sell Executed, Price: 130.75, Cost: -130.75, Size: -1 2011-07-19, Sell Created, 131.07 2011-07-20, Sell Executed, Price: 131.94, Cost: -131.94, Size: -1 2011-07-20, Sell Created, 130.39 2011-07-21, Sell Executed, Price: 130.63, Cost: -130.63, Size: -1 2011-07-21, Sell Created, 129.95 2011-07-22, Sell Executed, Price: 131.42, Cost: -131.42, Size: -1 2011-07-22, Sell Created, 133.12 2011-07-25, Sell Executed, Price: 133.90, Cost: -133.90, Size: -1 2011-07-25, Sell Created, 133.75 2011-07-26, Sell Executed, Price: 134.40, Cost: -134.40, Size: -1 2011-07-26, Sell Created, 132.58 2011-07-27, Sell Executed, Price: 132.60, Cost: -132.60, Size: -1 2011-07-27, Sell Created, 131.60 2011-07-28, Sell Executed, Price: 130.20, Cost: -130.20, Size: -1 2011-07-28, Sell Created, 130.22 2011-07-29, Sell Executed, Price: 131.51, Cost: -131.51, Size: -1 2011-07-29, Sell Created, 129.95 2011-08-01, Sell Executed, Price: 131.65, Cost: -131.65, Size: -1 2011-08-01, Sell Created, 130.69 2011-08-02, Sell Executed, Price: 130.27, Cost: -130.27, Size: -1 2011-08-02, Sell Created, 128.92 2011-08-03, Sell Executed, Price: 128.29, Cost: -128.29, Size: -1 2011-08-03, Sell Created, 128.25 2011-08-04, Sell Executed, Price: 128.25, Cost: -128.25, Size: -1 2011-08-04, Sell Created, 126.71 2011-08-05, Sell Executed, Price: 123.73, Cost: -123.73, Size: -1 2011-08-05, Sell Created, 124.04 2011-08-08, Sell Executed, Price: 121.15, Cost: -121.15, Size: -1 2011-08-08, Sell Created, 121.77 2011-08-09, Sell Executed, Price: 118.73, Cost: -118.73, Size: -1 2011-08-09, Sell Created, 120.43 2011-08-10, Sell Executed, Price: 122.78, Cost: -122.78, Size: -1 2011-08-10, Sell Created, 123.72 2011-08-11, Sell Executed, Price: 122.43, Cost: -122.43, Size: -1 2011-08-11, Sell Created, 122.99 2011-08-12, Sell Executed, Price: 124.70, Cost: -124.70, Size: -1 2011-08-12, Sell Created, 123.35 2011-08-16, Sell Executed, Price: 123.60, Cost: -123.60, Size: -1 2011-08-16, Sell Created, 121.02 2011-08-17, Sell Executed, Price: 120.93, Cost: -120.93, Size: -1 2011-08-17, Sell Created, 119.82 2011-08-18, Sell Executed, Price: 120.76, Cost: -120.76, Size: -1 2011-08-18, Sell Created, 118.24 2011-08-19, Sell Executed, Price: 115.79, Cost: -115.79, Size: -1 2011-08-19, Sell Created, 116.75 2011-08-22, Sell Executed, Price: 117.24, Cost: -117.24, Size: -1 2011-08-22, Sell Created, 117.51 2011-08-23, Sell Executed, Price: 117.63, Cost: -117.63, Size: -1 2011-08-23, Sell Created, 118.59 2011-08-24, Sell Executed, Price: 119.03, Cost: -119.03, Size: -1 2011-08-24, Sell Created, 118.04 2011-08-25, Sell Executed, Price: 118.41, Cost: -118.41, Size: -1 2011-08-25, Sell Created, 117.67 2011-08-26, Sell Executed, Price: 118.02, Cost: -118.02, Size: -1 2011-08-26, Sell Created, 115.80 2011-08-29, Sell Executed, Price: 116.94, Cost: -116.94, Size: -1 2011-08-29, Sell Created, 117.85 2011-08-30, Sell Executed, Price: 119.10, Cost: -119.10, Size: -1 2011-08-30, Sell Created, 119.34 2011-09-02, Sell Executed, Price: 121.22, Cost: -121.22, Size: -1 2011-09-02, Sell Created, 120.10 2011-09-05, Sell Executed, Price: 119.92, Cost: -119.92, Size: -1 2011-09-05, Sell Created, 120.63 2011-09-06, Sell Executed, Price: 120.47, Cost: -120.47, Size: -1 2011-09-06, Sell Created, 121.43 2011-09-07, Sell Executed, Price: 122.51, Cost: -122.51, Size: -1 2011-09-07, Sell Created, 122.66 2011-09-08, Sell Executed, Price: 123.20, Cost: -123.20, Size: -1 2011-09-08, Sell Created, 123.08 2011-09-09, Sell Executed, Price: 124.15, Cost: -124.15, Size: -1 2011-09-09, Sell Created, 122.36 2011-09-12, Sell Executed, Price: 121.70, Cost: -121.70, Size: -1 2011-09-12, Sell Created, 120.01 2011-09-13, Sell Executed, Price: 119.72, Cost: -119.72, Size: -1 2011-09-13, Sell Created, 119.09 2011-09-14, Sell Executed, Price: 121.29, Cost: -121.29, Size: -1 2011-09-14, Sell Created, 121.32 2011-09-15, Sell Executed, Price: 121.47, Cost: -121.47, Size: -1 2011-09-15, Sell Created, 121.20 2011-09-16, Sell Executed, Price: 122.09, Cost: -122.09, Size: -1 2011-09-16, Sell Created, 121.68 2011-09-19, Sell Executed, Price: 122.22, Cost: -122.22, Size: -1 2011-09-19, Sell Created, 121.97 2011-09-20, Sell Executed, Price: 120.21, Cost: -120.21, Size: -1 2011-09-20, Sell Created, 121.34 2011-09-21, Sell Executed, Price: 122.92, Cost: -122.92, Size: -1 2011-09-21, Sell Created, 123.41 2011-09-22, Sell Executed, Price: 123.44, Cost: -123.44, Size: -1 2011-09-22, Sell Created, 121.43 2011-09-23, Sell Executed, Price: 119.99, Cost: -119.99, Size: -1 2011-09-23, Sell Created, 120.55 2011-09-26, Sell Executed, Price: 118.93, Cost: -118.93, Size: -1 2011-09-26, Sell Created, 116.53 2011-09-27, Sell Executed, Price: 117.45, Cost: -117.45, Size: -1 2011-09-27, Sell Created, 117.73 2011-09-28, Sell Executed, Price: 118.52, Cost: -118.52, Size: -1 2011-09-28, Sell Created, 117.05 2011-09-29, Sell Executed, Price: 117.01, Cost: -117.01, Size: -1 2011-09-29, Sell Created, 117.05 2011-09-30, Sell Executed, Price: 116.11, Cost: -116.11, Size: -1 2011-09-30, Sell Created, 115.44 2011-10-03, Sell Executed, Price: 115.78, Cost: -115.78, Size: -1 2011-10-03, Sell Created, 115.06 2011-10-04, Sell Executed, Price: 115.29, Cost: -115.29, Size: -1 2011-10-04, Sell Created, 114.39 2011-10-05, Sell Executed, Price: 113.50, Cost: -113.50, Size: -1 2011-10-05, Sell Created, 112.17 2011-10-07, Sell Executed, Price: 113.28, Cost: -113.28, Size: -1 2011-10-07, Sell Created, 112.38 2011-10-10, Sell Executed, Price: 113.62, Cost: -113.62, Size: -1 2011-10-10, Sell Created, 114.52 2011-10-11, Sell Executed, Price: 114.77, Cost: -114.77, Size: -1 2011-10-11, Sell Created, 114.05 2011-10-12, Sell Executed, Price: 115.93, Cost: -115.93, Size: -1 2011-10-12, Sell Created, 116.82 2011-10-13, Sell Executed, Price: 117.79, Cost: -117.79, Size: -1 2011-10-13, Sell Created, 117.44 2011-10-14, Sell Executed, Price: 117.33, Cost: -117.33, Size: -1 2011-10-14, Sell Created, 117.54 2011-10-17, Sell Executed, Price: 118.50, Cost: -118.50, Size: -1 2011-10-17, Sell Created, 118.02 2011-10-18, Sell Executed, Price: 117.45, Cost: -117.45, Size: -1 2011-10-18, Sell Created, 117.06 2011-10-19, Sell Executed, Price: 117.89, Cost: -117.89, Size: -1 2011-10-19, Sell Created, 117.78 2011-10-20, Sell Executed, Price: 117.87, Cost: -117.87, Size: -1 2011-10-20, Sell Created, 117.57 2011-10-21, Sell Executed, Price: 117.78, Cost: -117.78, Size: -1 2011-10-21, Sell Created, 117.21 2011-10-24, Sell Executed, Price: 117.51, Cost: -117.51, Size: -1 2011-10-24, Sell Created, 117.06 2011-10-25, Sell Executed, Price: 115.24, Cost: -115.24, Size: -1 2011-10-25, Sell Created, 115.65 2011-10-26, Sell Executed, Price: 119.33, Cost: -119.33, Size: -1 2011-10-26, Sell Created, 118.85 2011-10-28, Sell Executed, Price: 120.62, Cost: -120.62, Size: -1 2011-10-28, Sell Created, 120.08 2011-10-31, Sell Executed, Price: 120.47, Cost: -120.47, Size: -1 2011-10-31, Sell Created, 120.01 2011-11-01, Sell Executed, Price: 119.51, Cost: -119.51, Size: -1 2011-11-01, Sell Created, 119.16 2011-11-02, Sell Executed, Price: 118.89, Cost: -118.89, Size: -1 2011-11-02, Sell Created, 118.81 2011-11-03, Sell Executed, Price: 119.07, Cost: -119.07, Size: -1 2011-11-03, Sell Created, 119.23 2011-11-04, Sell Executed, Price: 119.96, Cost: -119.96, Size: -1 2011-11-04, Sell Created, 120.02 2011-11-08, Sell Executed, Price: 120.72, Cost: -120.72, Size: -1 2011-11-08, Sell Created, 120.34 2011-11-09, Sell Executed, Price: 121.09, Cost: -121.09, Size: -1 2011-11-09, Sell Created, 119.46 2011-11-11, Sell Executed, Price: 119.52, Cost: -119.52, Size: -1 2011-11-11, Sell Created, 118.43 2011-11-14, Sell Executed, Price: 119.03, Cost: -119.03, Size: -1 2011-11-14, Sell Created, 117.53 2011-11-15, Sell Executed, Price: 117.84, Cost: -117.84, Size: -1 2011-11-15, Sell Created, 115.73 2011-11-16, Sell Executed, Price: 115.47, Cost: -115.47, Size: -1 2011-11-16, Sell Created, 114.16 2011-11-17, Sell Executed, Price: 114.14, Cost: -114.14, Size: -1 2011-11-17, Sell Created, 112.88 2011-11-18, Sell Executed, Price: 111.52, Cost: -111.52, Size: -1 2011-11-18, Sell Created, 111.29 2011-11-21, Sell Executed, Price: 111.63, Cost: -111.63, Size: -1 2011-11-21, Sell Created, 110.45 2011-11-22, Sell Executed, Price: 110.80, Cost: -110.80, Size: -1 2011-11-22, Sell Created, 110.09 2011-11-23, Sell Executed, Price: 109.35, Cost: -109.35, Size: -1 2011-11-23, Sell Created, 107.45 2011-11-24, Sell Executed, Price: 107.71, Cost: -107.71, Size: -1 2011-11-24, Sell Created, 108.26 2011-11-25, Sell Executed, Price: 109.24, Cost: -109.24, Size: -1 2011-11-25, Sell Created, 108.81 2011-11-28, Sell Executed, Price: 109.67, Cost: -109.67, Size: -1 2011-11-28, Sell Created, 109.84 2011-11-29, Sell Executed, Price: 110.93, Cost: -110.93, Size: -1 2011-11-29, Sell Created, 110.33 2011-11-30, Sell Executed, Price: 109.10, Cost: -109.10, Size: -1 2011-11-30, Sell Created, 109.40 2011-12-01, Sell Executed, Price: 110.61, Cost: -110.61, Size: -1 2011-12-01, Sell Created, 109.77 2011-12-02, Sell Executed, Price: 110.05, Cost: -110.05, Size: -1 2011-12-02, Sell Created, 110.97 2011-12-05, Sell Executed, Price: 111.67, Cost: -111.67, Size: -1 2011-12-05, Sell Created, 111.76 2011-12-07, Sell Executed, Price: 111.86, Cost: -111.86, Size: -1 2011-12-07, Sell Created, 111.63 2011-12-08, Sell Executed, Price: 111.95, Cost: -111.95, Size: -1 2011-12-08, Sell Created, 110.32 2011-12-09, Sell Executed, Price: 109.76, Cost: -109.76, Size: -1 2011-12-09, Sell Created, 109.95 2011-12-12, Sell Executed, Price: 110.09, Cost: -110.09, Size: -1 2011-12-12, Sell Created, 108.18 2011-12-13, Sell Executed, Price: 108.18, Cost: -108.18, Size: -1 2011-12-13, Sell Created, 108.73 2011-12-14, Sell Executed, Price: 108.39, Cost: -108.39, Size: -1 2011-12-14, Sell Created, 107.59 2011-12-15, Sell Executed, Price: 107.11, Cost: -107.11, Size: -1 2011-12-15, Sell Created, 106.94 2011-12-16, Sell Executed, Price: 107.15, Cost: -107.15, Size: -1 2011-12-16, Sell Created, 105.47 2011-12-19, Sell Executed, Price: 104.73, Cost: -104.73, Size: -1 2011-12-19, Sell Created, 103.82 2011-12-20, Sell Executed, Price: 103.54, Cost: -103.54, Size: -1 2011-12-20, Sell Created, 102.50 2011-12-21, Sell Executed, Price: 103.78, Cost: -103.78, Size: -1 2011-12-21, Sell Created, 103.64 2011-12-22, Sell Executed, Price: 102.53, Cost: -102.53, Size: -1 2011-12-22, Sell Created, 103.92 2011-12-23, Sell Executed, Price: 105.11, Cost: -105.11, Size: -1 2011-12-23, Sell Created, 104.68 2011-12-26, Sell Executed, Price: 104.54, Cost: -104.54, Size: -1 2011-12-26, Sell Created, 105.31 2011-12-27, Sell Executed, Price: 105.21, Cost: -105.21, Size: -1 2011-12-27, Sell Created, 104.98 2011-12-28, Sell Executed, Price: 104.60, Cost: -104.60, Size: -1 2011-12-28, Sell Created, 103.76 2011-12-29, Sell Executed, Price: 104.60, Cost: -104.60, Size: -1 2011-12-29, Sell Created, 104.55 2011-12-30, Sell Executed, Price: 105.14, Cost: -105.14, Size: -1 2011-12-30, Sell Created, 104.78 Final Value of the Portfolio: 104134.18
Also the
notify_trade
is not being called. Please help!! Thank you in advance!!! -
Few things:
-
Positions are maintained by the broker per data.
-
position
property of the Strategy class just returns the position of thedata[0]
. If you need to get the position for the different data, thegetposition(data=...)
could be used.
Now the problem in the above code seems to be related to the following logic in the
next
method:if not self.position: self.log('Buy Created, %.2f ' % (self.dataCloseLong[0])) self.order = self.buy(data = self.datas[0],size=1) else: self.log('Sell Created, %.2f' % self.dataCloseShort[0]) self.order = self.sell(data = self.datas[1],size=1)
If we have no position in data[0] then the 'buy' will be issued. Once this 'buy' is completed - the position will be established in data[0].
The next time the
next
will be called, the position in data[0] is present - so the 'sell' is issued fordata[1]
( different data, right ). So the position indata[0]
will never be changed from now on.Please correct me if I'm wrong.
-
-
More about a position: https://www.backtrader.com/docu/position/
-
Hi @vladisld , thank you for pointing this out. Can you help me with the code? I am not able to code the logic correctly and I got confused a lot. Thank you in advance!!!!