alpacahq/alpaca-backtrader-api

Updating stop order or profit of bracket orders - rejected

x777 opened this issue · 22 comments

x777 commented

@shlomikushchi can you show some real example of how to update the existing bracket order? I am receiving rejected status when trying to update the stop order price. Thx!

you need to cancel the open order and then you could create a new one

x777 commented

you need to cancel the open order and then you could create a new one

When you cancel stop, also cancels and profit, and it's logical. But why new order rejecting...

you have open order on this position. you can't open another. that's the way the api works

x777 commented

you have open order on this position. you can't open another. that's the way the api works

If in backtrader documentation says that some parameter not required and will be used default value it also the same for live feed for Alpaca?

not sure I understand what you mean..
if a python object has a default argument then it's default in all modes. but you need to make sure that the alpaca objects (live/paper it doesn't matter) don't override those arguments

x777 commented

not sure I understand what you mean..
if a python object has a default argument then it's default in all modes. but you need to make sure that the alpaca objects (live/paper it doesn't matter) don't override those arguments

In this case default arguments can't override with some wrong values that could send bad order?
Yesterday, I couldn't send simple stop order for opened position. Only bracket order worked.

x777 commented

Why order is rejected? It's possible to get additional info about the error?

self.buy(data=self.data0, price=round(self.positionsbyname[self.symbol].price +1.70, 2), size=self.positionsbyname[self.symbol].size, exectype=bt.Order.Stop)

Order notification. statusRejected.
Order info. statusAutoOrderedDict().
Ref: 2
OrdType: 0
OrdType: Buy
Status: 8
Status: Rejected
Size: 100
Price: 119.17
Price Limit: None
TrailAmount: None
TrailPercent: None
ExecType: 3
ExecType: Stop
CommInfo: <backtrader.comminfo.CommInfoBase object at 0x7fdfc55d9320>
End of Session: 737721.1666666665
Info: AutoOrderedDict()
Broker: <alpaca_backtrader_api.alpacabroker.AlpacaBroker object at 0x7fdfc462b710>
Alive: False

you get the error message in the alpaca store.
to see it, implement notify_store(). e.g

    def notify_store(self, msg, *args, **kwargs):
        self.log(msg)
        super().notify_store(msg, *args, **kwargs)

in your case: stop orders require no limit price

x777 commented

you get the error message in the alpaca store.
to see it, implement notify_store(). e.g

    def notify_store(self, msg, *args, **kwargs):
        self.log(msg)
        super().notify_store(msg, *args, **kwargs)

in your case: stop orders require no limit price

Cool, thx! But why this message happens? If I change exectype for StopTrail or use bracket order it not rejecting. I don't see stopprice argument for buy()/sell(), only for buy_bracket()/sell_bracket().

it's not so common to buy with just a stop price (usually just a stop price is used for sell protection)

x777 commented

it's not so common to buy with just a stop price (usually just a stop price is used for sell protection)

What default arguments to send common stop order for the opened position?

x777 commented

you should read this: https://alpaca.markets/docs/trading-on-alpaca/orders/

In order to submit a stop order, you will need to specify the stop price parameter in the API.

In this code where is stop price parameter, it's price?

self.buy(data=self.data0, price=round(self.positionsbyname[self.symbol].price +1.70, 2), size=self.positionsbyname[self.symbol].size, exectype=bt.Order.Stop)

read the docs ;)
backtrader has docs as well

x777 commented

read the docs ;)
backtrader has docs as well

I am reading, but looks like all problems that Alpaca not support stop orders without limit (target) orders. I am right?)

of course a stop order is supported. I think it's something in the package side. I will test it.

but in any case, you should buy with stop-limit. buying with stop order turns to a limit order once price is hit, you might as well control the rate

x777 commented

but in any case, you should buy with stop-limit. buying with stop order turns to a limit order once price is hit, you might as well control the rate

I don't want to buy with stop order. I just want to set stop order for opened position.

x777 commented

of course a stop order is supported. I think it's something in the package side. I will test it.

Let me know please if something changes

added a fix for it, you can pull it from the master branch

x777 commented

added a fix for it, you can pull it from the master branch

Ok, tomorrow I will test. Thanks!

x777 commented

added a fix for it, you can pull it from the master branch

pip install -U git+https://github.com/alpacahq/alpaca-backtrader-api
self.orders = self.sell(price=self.new_stop_price, exectype=bt.Order.Stop)

And it's worked already!