Order execution confusion?
-
I'm trying to make my bot "long only" and I'm having issues with the execution.
def __init__(self): self.dataclose = self.datas[0].close self.ind = AmaBands(plot=True, subplot=False) self.order = None def next(self): self.log('%0.2f' % (self.dataclose[0])) mean = self.ind[0] # Current mean(moving average) value size1 = cerebro.broker.get_cash() * 0.035 / self.data size2 = size1 * 2 size3 = size2 * 2 size4 = size3 * 2 pos = self.position.size stop = self.ind.bot[0] close = self.dataclose[0] # Current close buy1 = self.ind.bot[0] buy2 = self.ind.bot2[0] buy3 = self.ind.bot3[0] buy4 = self.ind.bot4[0] sell1 = self.ind.top[0] sell2 = self.ind.top2[0] sell3 = self.ind.top3[0] sell4 = self.ind.top4[0] """Trade logic""" if not self.position: if close < mean: self.buy_order = self.buy(price=buy1, size=size1, exectype=bt.Order.Limit) self.log('Going long step.. %.2f' % close) elif pos > 0 and close > mean: self.sell_order = self.close(price=sell1, size=-size1, exectype=bt.Order.Limit) self.log('Exiting long.. %.2f' % close)
So what I want is when price goes below my "mean" I want it to create one buy limit at my buy1 and only if price crosses above my mean and is in a position (ideally id put elif pos == size1 but this does not seem to work) I'll create a close position order.
It seems I'm creating more buy orders than intended:2020-02-01T02:25:00, 9443.81 2020-02-01T02:26:00, 9443.05 2020-02-01T02:27:00, 9442.35 2020-02-01T02:28:00, 9440.98 2020-02-01T02:29:00, 9437.60 2020-02-01T02:30:00, 9449.99 2020-02-01T02:31:00, 9447.88 2020-02-01T02:32:00, 9454.37 2020-02-01T02:33:00, 9450.49 2020-02-01T02:34:00, 9450.98 2020-02-01T02:35:00, 9457.76 2020-02-01T02:36:00, 9460.82 2020-02-01T02:37:00, 9462.72 2020-02-01T02:38:00, 9456.53 2020-02-01T02:39:00, 9443.56 2020-02-01T02:40:00, 9443.53 2020-02-01T02:41:00, 9444.83 2020-02-01T02:42:00, 9444.86 2020-02-01T02:43:00, 9447.26 2020-02-01T02:44:00, 9450.01 2020-02-01T02:45:00, 9443.65 2020-02-01T02:46:00, 9435.18 2020-02-01T02:46:00, Going long step.. 9435.18 2020-02-01T02:47:00, 9433.55 2020-02-01T02:47:00, Going long step.. 9433.55 2020-02-01T02:48:00, 9433.90 2020-02-01T02:48:00, Going long step.. 9433.90 2020-02-01T02:49:00, 9435.66 2020-02-01T02:49:00, Going long step.. 9435.66 2020-02-01T02:50:00, 9434.99 2020-02-01T02:50:00, Going long step.. 9434.99 -------------------------------- NOTIFY ORDER -------------------------------- Order Completed 2020-02-01, Status 4: Ref: 10, Size: 0.0370952117500673, Limit: 9424.85237, Close: 9426.18 Created: 2020-02-01 02:46:00 Price: 9424.852374567356 Size: 0.0370952117500673 -------------------------------------------------------------------------------- -------------------------------- NOTIFY ORDER -------------------------------- Order Completed 2020-02-01, Status 4: Ref: 11, Size: 0.037101621340852604, Limit: 9424.71338, Close: 9426.18 Created: 2020-02-01 02:47:00 Price: 9424.713382297963 Size: 0.037101621340852604 -------------------------------------------------------------------------------- -------------------------------- NOTIFY ORDER -------------------------------- Order Completed 2020-02-01, Status 4: Ref: 12, Size: 0.037100244861616095, Limit: 9424.62152, Close: 9426.18 Created: 2020-02-01 02:48:00 Price: 9424.621518023265 Size: 0.037100244861616095 -------------------------------------------------------------------------------- -------------------------------- NOTIFY ORDER -------------------------------- Order Completed 2020-02-01, Status 4: Ref: 13, Size: 0.03709332468528964, Limit: 9424.55865, Close: 9426.18 Created: 2020-02-01 02:49:00 Price: 9424.558650403575 Size: 0.03709332468528964 -------------------------------------------------------------------------------- -------------------------------- NOTIFY ORDER -------------------------------- Order Completed 2020-02-01, Status 4: Ref: 14, Size: 0.03709595876625201, Limit: 9424.45617, Close: 9426.18 Created: 2020-02-01 02:50:00 Price: 9424.456174110375 Size: 0.03709595876625201 --------------------------------------------------------------------------------
I've looked trough the forums for hours please help :D.
Once I figure out this part I can finish the logic.
And it won't exit properly, what am I doing wrong?Regards,
cept0r -
@cept0r said in Order execution confusion?:
mean = self.ind[0]
best way to understand what is going on while script running is to put more logging. what is the point of having only
close
values printed? how this can help? print yourclose
, andmean
, and all other data necessary to check the logic in thenext()
, in thenotify_order()
, etc.i think the issue is in the
mean
since you define set of values to it. but with what is shown it is the guess only. -
Changed the code abit and added a log for the mean I think there is a serious issue with my logging function.
class meanrevision(bt.Strategy): def log(self, txt, dt=None): """ Logging function fot this strategy""" dt = dt or self.datas[0].datetime.date(0) print('%s, %s' % (dt.isoformat(), txt)) def notify_order(self, order): if order.status in [order.Submitted, order.Accepted]: # Buy/Sell order submitted/accepted to/by broker - Nothing to do return # Check if an order has been completed # Attention: broker could reject order if not enough cash if order.status in [order.Completed]: if order.isbuy(): self.log( 'BUY EXECUTED, Price: %.2f, Cost: %.2f, Comm %.2f' % (order.executed.price, order.executed.value, order.executed.comm)) self.buyprice = order.executed.price self.buycomm = order.executed.comm else: # Sell self.log('SELL EXECUTED, Price: %.2f, Cost: %.2f, Comm %.2f' % (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') # Write down: no pending order self.order = None def __init__(self): self.dataclose = self.datas[0].close self.ind = AmaBands(plot=True, subplot=False) self.order = None self.trades_occured = False def next(self): self.log('Close %0.2f, Mean %0.2f' % (self.dataclose[0], self.ind[0])) if self.order: return mean = self.ind[0] # Current mean(moving average) value size1 = cerebro.broker.get_cash() * 0.035 / self.data size2 = size1 * 2 size3 = size2 * 2 size4 = size3 * 2 pos = self.position.size stop = self.ind.bot[0] close = self.dataclose[0] # Current close buy1 = self.ind.bot[0] buy2 = self.ind.bot2[0] buy3 = self.ind.bot3[0] buy4 = self.ind.bot4[0] sell1 = self.ind.top[0] sell2 = self.ind.top2[0] sell3 = self.ind.top3[0] sell4 = self.ind.top4[0] """Trade logic step 1""" if not self.position and close < mean and self.trades_occured: self.buy_order = self.buy(price=buy1, size=size1, exectype=bt.Order.Limit) self.log("First long position.. %.2f" % close) if pos < size2 and close > mean or close >= sell1: self.trades_occured = True self.sell_order = self.sell(price=sell1, size=-size1, exectype=bt.Order.Limit) self.log("Closing long position.. %.2f" % close) if not self.position and close < mean or close <= buy1: self.trades_occured = True self.buy_order = self.buy(price=buy1, size=size1, exectype=bt.Order.Limit) self.log("Going long.. %.2f" % close) def notify_trade(self, trade): if not trade.isclosed: return self.log("OPERATION PROFIT, GROSS %.2f, NET %.2f" % (trade.pnl, trade.pnlcomm))
output:
2020-02-01, Close 9356.33, Mean 9360.70 2020-02-01, Going long.. 9356.33 2020-02-01, Close 9357.21, Mean 9360.67 2020-02-01, First long position.. 9357.21 2020-02-01, Going long.. 9357.21 2020-02-01, Close 9358.41, Mean 9360.66 2020-02-01, First long position.. 9358.41 2020-02-01, Going long.. 9358.41 2020-02-01, Close 9358.30, Mean 9360.64 2020-02-01, First long position.. 9358.30 2020-02-01, Going long.. 9358.30 2020-02-01, Close 9355.65, Mean 9360.57 2020-02-01, First long position.. 9355.65 2020-02-01, Going long.. 9355.65 2020-02-01, Close 9355.65, Mean 9360.51 2020-02-01, First long position.. 9355.65 2020-02-01, Going long.. 9355.65 2020-02-01, Close 9359.29, Mean 9360.49 2020-02-01, First long position.. 9359.29 2020-02-01, Going long.. 9359.29 2020-02-01, Close 9363.69, Mean 9360.57 2020-02-01, Closing long position.. 9363.69 2020-02-01, Close 9374.21, Mean 9361.16 2020-02-01, Closing long position.. 9374.21 2020-02-01, Close 9370.20, Mean 9361.40 2020-02-01, Closing long position.. 9370.20 2020-02-01, SELL EXECUTED, Price: 9376.49, Cost: -350.48, Comm 0.88 2020-02-01, Close 9373.75, Mean 9361.53 2020-02-01, Closing long position.. 9373.75 2020-02-01, SELL EXECUTED, Price: 9377.07, Cost: -350.11, Comm 0.88 2020-02-01, SELL EXECUTED, Price: 9377.31, Cost: -350.27, Comm 0.88 2020-02-01, SELL EXECUTED, Price: 9377.45, Cost: -350.14, Comm 0.88 2020-02-01, Close 9381.68, Mean 9362.01 2020-02-01, Closing long position.. 9381.68 2020-02-01, SELL EXECUTED, Price: 9380.19, Cost: -349.94, Comm 0.87 2020-02-01, Close 9383.44, Mean 9362.45 2020-02-01, Closing long position.. 9383.44 2020-02-01, SELL EXECUTED, Price: 9383.67, Cost: -350.01, Comm 0.88 2020-02-01, Close 9387.57, Mean 9363.11 2020-02-01, Closing long position.. 9387.57 2020-02-01, SELL EXECUTED, Price: 9388.82, Cost: -350.05, Comm 0.88 2020-02-01, Close 9383.22, Mean 9363.38 2020-02-01, Closing long position.. 9383.22 2020-02-01, SELL EXECUTED, Price: 9383.77, Cost: -350.02, Comm 0.88 2020-02-01, Close 9388.64, Mean 9364.35 2020-02-01, Closing long position.. 9388.64 2020-02-01, SELL EXECUTED, Price: 9388.64, Cost: -350.00, Comm 0.88 2020-02-01, Close 9374.66, Mean 9364.48 2020-02-01, Closing long position.. 9374.66 2020-02-01, Close 9372.53, Mean 9364.54 2020-02-01, Closing long position.. 9372.53 2020-02-01, Close 9375.25, Mean 9364.67 2020-02-01, Closing long position.. 9375.25 2020-02-01, Close 9375.22, Mean 9364.78 2020-02-01, Closing long position.. 9375.22 2020-02-01, Close 9378.71, Mean 9364.97 2020-02-01, Closing long position.. 9378.71 2020-02-01, Close 9376.21, Mean 9365.08 2020-02-01, Closing long position.. 9376.21 2020-02-01, SELL EXECUTED, Price: 9380.40, Cost: -350.21, Comm 0.88 2020-02-01, SELL EXECUTED, Price: 9380.46, Cost: -350.30, Comm 0.88 2020-02-01, SELL EXECUTED, Price: 9380.59, Cost: -350.20, Comm 0.88 2020-02-01, SELL EXECUTED, Price: 9380.70, Cost: -350.20, Comm 0.88 2020-02-01, SELL EXECUTED, Price: 9380.89, Cost: -350.08, Comm 0.88 2020-02-01, SELL EXECUTED, Price: 9381.01, Cost: -350.18, Comm 0.88 2020-02-01, Close 9383.29, Mean 9365.44 2020-02-01, Closing long position.. 9383.29 2020-02-01, SELL EXECUTED, Price: 9382.99, Cost: -349.99, Comm 0.87 2020-02-01, Close 9385.85, Mean 9365.95 2020-02-01, Closing long position.. 9385.85 2020-02-01, SELL EXECUTED, Price: 9385.84, Cost: -350.00, Comm 0.87 2020-02-01, Close 9387.54, Mean 9366.64 2020-02-01, Closing long position.. 9387.54 2020-02-01, SELL EXECUTED, Price: 9385.88, Cost: -349.94, Comm 0.87 2020-02-01, Close 9384.05, Mean 9367.49 2020-02-01, Closing long position.. 9384.05 2020-02-01, SELL EXECUTED, Price: 9383.42, Cost: -349.98, Comm 0.87 2020-02-01, Close 9383.29, Mean 9368.15 2020-02-01, Closing long position.. 9383.29 2020-02-01, SELL EXECUTED, Price: 9384.08, Cost: -350.03, Comm 0.88 2020-02-01, Close 9387.60, Mean 9369.15 2020-02-01, Closing long position.. 9387.60 2020-02-01, SELL EXECUTED, Price: 9387.58, Cost: -350.00, Comm 0.87 2020-02-01, Close 9379.23, Mean 9369.65 2020-02-01, Closing long position.. 9379.23 2020-02-01, Close 9383.07, Mean 9370.36 2020-02-01, Closing long position.. 9383.07 2020-02-01, Close 9382.12, Mean 9370.84 2020-02-01, Closing long position.. 9382.12 2020-02-01, SELL EXECUTED, Price: 9385.58, Cost: -350.24, Comm 0.88 2020-02-01, SELL EXECUTED, Price: 9386.29, Cost: -350.12, Comm 0.88 2020-02-01, Close 9382.09, Mean 9371.29 2020-02-01, Closing long position.. 9382.09 2020-02-01, SELL EXECUTED, Price: 9386.77, Cost: -350.17, Comm 0.88 2020-02-01, SELL EXECUTED, Price: 9387.22, Cost: -350.19, Comm 0.88 2020-02-01, Close 9388.36, Mean 9372.11 2020-02-01, Closing long position.. 9388.36 2020-02-01, SELL EXECUTED, Price: 9388.13, Cost: -349.99, Comm 0.87 2020-02-01, Close 9397.93, Mean 9373.77 2020-02-01, Closing long position.. 9397.93 2020-02-01, SELL EXECUTED, Price: 9397.97, Cost: -350.00, Comm 0.88 2020-02-01, Close 9384.26, Mean 9374.15 2020-02-01, Closing long position.. 9384.26 2020-02-01, SELL EXECUTED, Price: 9390.08, Cost: -350.22, Comm 0.88 2020-02-01, Close 9392.70, Mean 9375.03 2020-02-01, Closing long position.. 9392.70 2020-02-01, SELL EXECUTED, Price: 9391.00, Cost: -349.94, Comm 0.87 2020-02-01, Close 9379.89, Mean 9375.13 2020-02-01, Closing long position.. 9379.89 2020-02-01, Close 9387.01, Mean 9375.41 2020-02-01, Closing long position.. 9387.01 2020-02-01, Close 9384.69, Mean 9375.52 2020-02-01, Closing long position.. 9384.69
I want it only to be long and massage the entries and exits of that long until it fully closes or stops out which will start a new cycle of laddering long buys. Looks like it opens several positions at the same time.
-
The script does what you programmed:
- each bar your
close
<mean
it issueslimit
order - after the first condition
close
<mean
met, it starts to issue one more buyinglimit
order each timeclose
<mean
sinceself.trades_occured = True
now for all further bars - it look s like these orders are hanging in the system waiting for execution, hard to say, no prices and indicator ranges are shown, probably price never get lower than
limit
buy price - at certain point your
close
>mean
and script starts to issue selllimit
order - it does it each bar when
close
>mean
sincepos
<size2
returns True always - no long position is open or short position (negativepos
is open) - at certain point price goes high enough and these accumulated sell order are executed
- hard to say if they are executed on the same abr or number of bars, since the date only is printed, but these intraday data
I would advice you to look on the prices and indicators on the price history, find short time period where your entry/exit signals happen, and orders are executed, and debug your code. If it is hard to find that time period, then split the task: find the short time period where prices and indicators generate your entries and debug them, then do the same for exits.
- each bar your
-
Ahaa I think I got it now, I just needs to make sure each step only issues one limit order and go from there. Thank you.
-
@ab_trader
Still stuck on this issue for a week now, anyway I could contact you on Discord/Telegram? In my head coming from a trading background this is so simple but I can't manually do the strategy fast enough for it to be viable..