Round stop loss price

I have a somewhat working Python script to my paper trade account and the issue im having has been addressed as a rounding issue but I cannot find a reference to how to fix the code itself.

I was able to round my Last_Price by doing this
def position_sizing(self):
cash = self.get_cash()
last_price = self.get_last_price(self.symbol)
last_price = round(last_price,00)
quantity = round(cash * self.cash_at_risk / last_price,0)
return cash, last_price, quantity

But that didnt actually round the end Stop Loss pirce as im still getting this error.
2024-06-04 11:27:02,323: root: INFO: market order of | 3.0 SPY sell | at price $499.7 of class bracket with status error did not go through. The following error occured: {“code”:42210000,“message”:“invalid stop_loss.stop_price 552.3000000000001. sub-penny increment does not fulfill minimum pricing criteria”}

Im sure its a simple bit of code to adjust this but any time I try and add a round into this bit of code it fails.

elif sentiment == “negative” and probability > .50:
if self.last_trade == “buy”:
self.sell_all()
order = self.create_order(
self.symbol,
quantity,
“sell”,
type=“bracket”,
take_profit_price=last_price*.95,
stop_loss_price=last_price*1.05,
)
self.submit_order(order)
self.last_trade = “sell”

To be honest, I am very familiar with coding with PowerShell logic but python is very new to me so if anyone can help that would be great.

All the forums I have read so far just say round to the nearest 2 decimal places but I cant figure that out with this code.

@Daniel_Sanders I believe the issue is, while you rounded the Last_Price, you didn’t actually round the take_profit and stop_loss prices in the actual order. Both of those values need to be specified with a max increment of $.01. So maybe something like this

order = self.create_order(
             self.symbol,
             quantity,
            “sell”,
            type=“bracket”,
            take_profit_price=round(last_price*.95, 2),
            stop_loss_price=round(last_price*1.05, 2),
)
1 Like

thank you, that makes sense, I will try it tomorrow and report back.

@Dan_Whitnable_Alpaca : That worked for me. thanks for your help!