take_profit_request = TakeProfitRequest(limit_price=c + 0.1)
stop_loss_request = StopLossRequest(stop_price=c - 10)
hello, im trying to order a market order that take profit when my stock price moves up $0.1 and down when the price drops $10. my current order:
market_order_data = MarketOrderRequest(
symbol=x,
qty=B_qty,
side=OrderSide.SELL,
take_profit=take_profit_request,
stop_loss=stop_loss_request,
time_in_force=TimeInForce.DAY
)
# Market order
market_order = trading_client.submit_order(
order_data=market_order_data
)
the issue I’m having is the code just sells the position automatically and does not wait for the stop loss. I don’t want to submit a bracket order. any guidance in the right direction would be greatly appreciated. I just really so not think I’m understanding either profit or stop loss request.
@AASAAQWE What you want is a One-Cancels-Other (OTO) order. This actually submits two orders - a limit order (ie a ‘take profit’) and a stop loss order. It isn’t well documented but this is specified like this
from alpaca.trading.client import TradingClient
from alpaca.trading.requests import LimitOrderRequest, StopLossRequest, TakeProfitRequest
from alpaca.trading.enums import OrderSide, OrderClass, TimeInForce
ALPACA_API_KEY = 'xxxxx'
ALPACA_API_SECRET_KEY = 'xxxxx'
client = TradingClient(api_key=ALPACA_API_KEY, secret_key=ALPACA_API_SECRET_KEY)
# OCO order must have the limit order as the 'parent' order
my_symbol = 'SPY'
my_qty = 1
my_limit_price = c + .10
my_stop_price = c - 10.00
my_oco_order = client.submit_order(LimitOrderRequest(
order_class = OrderClass.OCO,
symbol=my_symbol,
qty=my_qty,
side=OrderSide.SELL,
limit_price=my_limit_price,
time_in_force=TimeInForce.DAY,
stop_loss=StopLossRequest(stop_price=my_stop_price),
take_profit=TakeProfitRequest(limit_price=my_limit_price)))
By convention the order type is specified as a limit order, but by indicating order_class = OrderClass.OCO
it will create the take profit and stop loss orders as desired. The reason your code immediately sold was 1) you specified a MarketOrderRequest and 2) you didn’t indicate OrderClass.OCO
so the stop loss order was never created.
1 Like
Interesting. I understand, Thank you.
trailing_price_data = TrailingStopOrderRequest(
symbol=x,
qty=B_qty,
side=OrderSide.SELL,
time_in_force=TimeInForce.GTC,
trail_price=trail_p, # hwm * 0.99
)
trailing_price_order = trading_client.submit_order(
order_data=trailing_price_data
)
could you also provide some insight into this order, if you dont mind? trail_p is 0.1* of the current price of the stock. this order has no stop loss, but the program keeps selling the order. I just want the order to sell when there is only a profit, hence me removing any stop loss risk management. any insight would be appreciated.