For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

Ask on the logic of notify order and next to execute strategy

• Hi all,
I have the logic inside the function next() as below.
if sme > sma then open to buy_bracket with 3 orders to open and close with limit and stop. As I understand, stopprice is stoploss and limitprice is the target profit.

``````def next(self):
if self.order:
return
if not self.position:
if self.ema[0] > self.sma[0]:
self.log('BUY at {}'. format(self.dataopen[0]))
self.order = self.buy_bracket(stopprice = self.dataopen[0] * (1 - 0.02),
limitprice = self.dataopen[0] * (1 + 0.04))
elif self.ema[0] == self.sma[0]:
self.order = self.close()
self.log('CLOSED at {}'. format(self.dataclose[0]))
else:
if self.ema[0] < self.sma[0]:
self.order = self.sell_bracket(stopprice = self.dataopen[0] * (1 + 0.02),
limitprice = self.dataopen[0] * (1 - 0.04))
elif self.ema[0] == self.sma[0]:
self.order = self.close()
self.log('CLOSED at {}'. format(self.dataclose[0]))
``````

and the notify order is the same as the quick guide

``````def notify_order(self, order):
if order.status in [order.Completed]:
self.log(
'BUY EXECUTED at {}, cost {}, com {}'.format(order.executed.price,
order.executed.value,
order.executed.comm)
)
else:
self.log(
'SELL EXECUTED at price {}, cost {}, com {}'.format(order.executed.price,
order.executed.value,
order.executed.comm)
)
self.bar_executed = len(self)
elif order.status in [order.Canceled, order.Margin, order.Rejected]:
self.log('Order Canceled/Margin/Rejected')
self.order = None

return
self.log('OPERATION PROFIT, GROSS {}, NET{}'.format(trade.pnl,
``````

However, the logger have a lot of trade without return the notify_trade() even I have detailed the condition to close the trade in next() function. How to solve this problem?

``````2019-10-24 12:58:00 BUY EXECUTED at 108.59200000000001, cost 108.59200000000001, com 0.0010859200000000002
2019-10-24 13:00:00 SELL EXECUTED at price 108.581, cost 108.59200000000001, com 0.0010858100000000002
2019-10-24 13:00:00 OPERATION PROFIT, GROSS -0.01100000000000989, NET-0.013171730000009891
...
2019-10-25 02:30:00 SELL EXECUTED at price 108.664, cost -108.664, com 0.00108664
2019-10-25 02:31:00 SELL EXECUTED at price 108.662, cost -108.662, com 0.0010866200000000002
2019-10-25 02:33:00 SELL EXECUTED at price 108.65100000000001, cost -108.65100000000001, com 0.0010865100000000002
2019-10-25 02:34:00 SELL EXECUTED at price 108.656, cost -108.656, com 0.00108656
2019-10-25 02:35:00 SELL EXECUTED at price 108.65299999999999, cost -108.65299999999999, com 0.00108653

``````

Where is the wrong here?

• Where is the wrong here?

I think it is obvious. Your logic sells several times in a row.

``````        else:
if self.ema[0] < self.sma[0]:
self.order = self.sell_bracket(stopprice = self.dataopen[0] * (1 + 0.02),
limitprice = self.dataopen[0] * (1 - 0.04))
``````

How to solve this problem?

Correct your logic to something which wont sell several times in a row.

});