hey guys does the IsSuccessful boolean for checking cancelled orders in live trading work with Alpaca? Just discovered they still don't support trailling stops so have had to implement my own. Planning to just do a check every (minute) bar to see if the security has reached a new high since a position was opened, but has anyone else produced a workaround for this?
I'm guessing the problem with myt current implementation is I need the algo to wait for a confirmed cancellation from the Alpaca servers before placing a new stop order, otherwise I'm getting an error.
Reference documentation and error log below:https://www.quantconnect.com/docs/algorithm-reference/trading-and-orders
# Create an order and save its ticket
ticket = self.LimitOrder("SPY", 100, 221.05, False, "SPY Trade to Cancel")# Tag order later
response = ticket.Cancel("Canceled SPY Trade")# Use order response object to read status
if response.IsSuccessful:
self.Debug("Order successfully canceled")
2020-07-27 13:31:01 : New Order Event: Time: 07/27/2020 13:31:01 OrderID: 2 EventID: 1 Symbol: NUGT Status: Submitted Quantity: -97 StopPrice: 104.22
2020-07-27 13:33:00 : New Order Event: Time: 07/27/2020 13:33:00 OrderID: 2 EventID: 2 Symbol: NUGT Status: CancelPending Quantity: -97 StopPrice: 104.22
2020-07-27 13:33:00 : Warning: To meet brokerage precision requirements, order StopPrice was rounded to 104.84 from 104.84159999999999
2020-07-27 13:33:00 : New Order Event: Time: 07/27/2020 13:33:00 OrderID: 3 EventID: 1 Symbol: NUGT Status: Submitted Quantity: -97 StopPrice: 104.84
2020-07-27 13:33:00 : New Order Event: Time: 07/27/2020 13:33:00 OrderID: 2 EventID: 3 Symbol: NUGT Status: Canceled Quantity: -97 StopPrice: 104.22 Message: Alpaca Canceled Order Event
2020-07-27 13:34:00 : New Order Event: Time: 07/27/2020 13:34:00 OrderID: 3 EventID: 2 Symbol: NUGT Status: CancelPending Quantity: -97 StopPrice: 104.84
2020-07-27 13:34:00 : Warning: To meet brokerage precision requirements, order StopPrice was rounded to 105.94 from 105.93599999999999
2020-07-27 13:34:00 : New Order Event: Time: 07/27/2020 13:34:00 OrderID: 4 EventID: 1 Symbol: NUGT Status: Submitted Quantity: -97 StopPrice: 105.94
2020-07-27 13:34:00 : New Order Event: Time: 07/27/2020 13:34:00 OrderID: 3 EventID: 3 Symbol: NUGT Status: Canceled Quantity: -97 StopPrice: 104.84 Message: Alpaca Canceled Order Event
2020-07-27 13:37:00 : New Order Event: Time: 07/27/2020 13:37:00 OrderID: 4 EventID: 2 Symbol: NUGT Status: CancelPending Quantity: -97 StopPrice: 105.94
2020-07-27 13:37:00 : Warning: To meet brokerage precision requirements, order StopPrice was rounded to 106.47 from 106.47359999999999
2020-07-27 13:37:00 : New Order Event: Time: 07/27/2020 13:37:00 OrderID: 5 EventID: 1 Symbol: NUGT Status: Invalid Quantity: -97 StopPrice: 106.47 Message: Error placing order: insufficient qty available for order (requested: 97, available: 0)
2020-07-27 13:37:00 : New Order Event: Time: 07/27/2020 13:37:00 OrderID: 4 EventID: 3 Symbol: NUGT Status: Canceled Quantity: -97 StopPrice: 105.94 Message: Alpaca Canceled Order Event
2020-07-27 13:37:01 : Brokerage Warning: Error placing order: insufficient qty available for order (requested: 97, available: 0)
P Chen
On closer review of my live trading logs, I am still getting the same 'insufficient qty' error, even after implementing a 'IsSuccess' check below:
def OnData(self, data): # vars for checking stop loss quantities longQ = -self.Portfolio[self.symbolLong].Quantity shortQ = -self.Portfolio[self.symbolShort].Quantity # Update trailling stop on still-open overnight long if self.OrderIsPlaced(self.symbolLong,longQ) and self.Securities[self.symbolLong].Close > self.longPriceHigh: self.longPriceHigh = self.Securities[self.symbolLong].Close self.alpacaTrail(self.symbolLong,self.longStpMktTkt) # Update trailling stop on intraday short if self.OrderIsPlaced(self.symbolShort,shortQ) and self.Securities[self.symbolShort].Close > self.shortPriceHigh: # Alpaca trailling stop self.shortPriceHigh = self.Securities[self.symbolShort].Close self.alpacaTrail(self.symbolShort,self.shortStpMktTkt) def getStop(self,symbol): goFlat = self.CalculateOrderQuantity(symbol, 0) return self.StopMarketOrder(symbol, goFlat, (1 - self.trail) * self.Securities[symbol].Close) def alpacaTrail(self,symbol,ticket): #self.Debug(ticket) # Cancel order and assign its confirmation to a variable #response = self.Transactions.CancelOpenOrders(symbol) response = ticket.Cancel() if response.IsSuccess: ticket = self.getStop(symbol)
2020-08-03 13:31:01 : New Order Event: Time: 08/03/2020 13:31:01 OrderID: 5 EventID: 2 Symbol: DUST Status: Filled Quantity: 724 FillQuantity: 724 FillPrice: 17.5199 USD Message: Alpaca Fill Event 2020-08-03 13:31:01 : New Order Event: Time: 08/03/2020 13:31:01 OrderID: 6 EventID: 1 Symbol: DUST Status: Submitted Quantity: -724 StopPrice: 16.88 2020-08-03 13:31:02 : Warning: To meet brokerage precision requirements, order StopPrice was rounded to 16.88 from 16.8768 2020-08-03 13:31:02 : Unrealised profits -0.014201970641833175 on 121.0 units of NUGT at open. Flipping short 2020-08-03 13:37:00 : New Order Event: Time: 08/03/2020 13:37:00 OrderID: 6 EventID: 2 Symbol: DUST Status: CancelPending Quantity: -724 StopPrice: 16.88 2020-08-03 13:37:00 : Warning: To meet brokerage precision requirements, order StopPrice was rounded to 16.93 from 16.9296 2020-08-03 13:37:00 : New Order Event: Time: 08/03/2020 13:37:00 OrderID: 7 EventID: 1 Symbol: DUST Status: Invalid Quantity: -724 StopPrice: 16.93 Message: Error placing order: insufficient qty available for order (requested: 724, available: 0) 2020-08-03 13:37:00 : New Order Event: Time: 08/03/2020 13:37:00 OrderID: 6 EventID: 3 Symbol: DUST Status: Canceled Quantity: -724 StopPrice: 16.88 Message: Alpaca Canceled Order Event 2020-08-03 13:37:01 : Brokerage Warning: Error placing order: insufficient qty available for order (requested: 724, available: 0)
Shile Wen
Hi P Chen,
Please open a support ticket addressed to support@quantconnect.com and add the project with the live issues.
Best,
Shile Wen
P Chen
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!