- ago
My strategy includes a Stop Loss, which works fine in backtesting but encounters an error during live trading. The issue appears to be that I can not call "ClosePosition(...)" while the stop loss is in place. Using the Alpaca extension, I get the following error in the Order manager:

QUOTE:
Error placing trade: insufficient qty available for order (requested: 24, available: 0)


So it sounds like I just need to cancel the stop loss before calling ClosePosition(...). Does anyone know of an example of the correct way to handle this in the strategy code?

Or, as an alternative solution, if I convert my code to override the new "AssignAutoStopTargetPrices" function, could that potentially fix the problem?
0
1,050
Solved
7 Replies

Reply

Bookmark

Sort
Glitch8
 ( 8.38% )
- ago
#1
what scale is this running? it sounds like the alpaca position has not updated by the time the ClosePosition is being called. 🤔
1
- ago
#2
This strategy is using 1 minute scale and "Streaming Data" with the Alpaca broker extension.
0
- ago
#3
I tried converting my strategy to override "AssignAutoStopTargetPrices(...)", but I still encounter the same problem. When logging in to the alpaca.markets website, it's easy to verify that no sell order will be accepted until the stop loss order has been manually cancelled. So I guess I just need to find a way for the strategy code to cancel the stop loss, and confirm success before attempting to close the position.
0
- ago
#4
Unfortunately, this error still occurs for me in the latest version of WealthLab and the latest version of the Alpaca extension. I'm not really sure what else to try at this point.
0
Cone8
 ( 26.65% )
- ago
#5
Can you provide a minimal strategy that demonstrates the issue? We can run it tomorrow to observe.

Is the exit logic something like this, and the error occurs when the market order is placed?

CODE:
               if (someCondition)                {                   ClosePosition(p, OrderType.Stop, StopPrice, "Stop Loss");                }                else                {                   ClosePosition(p, OrderType.Market);                }
0
- ago
#6
I don't have after-hours strategy code to test, but I can set up a minimal test case tomorrow during normal market hours.

My code is similar to your example, but like this:
CODE:
if (someCondition) { ClosePosition(p, OrderType.Market); } // Always called at the end of the Execute(...) function ClosePosition(p, OrderType.Stop, StopPrice, "Stop Loss");


I'm not certain which function call is responsible for the error. But maybe I just need to ensure that the OrderType.Stop is only called if no other ClosePosition(...) has been triggered inside the Execute(...) loop?
0
Cone8
 ( 26.65% )
- ago
#7
If you ClosePosition at Market, you should not try to enter a stop order on the same bar. If you add the else condition as I did, Wealth-Lab will cancel the existing stop order, and then sell the Position at Market. Should work!
1
Best Answer

Reply

Bookmark

Sort