In this code, the stop loss system is implemented using the Binance API. The stop loss order is placed when the signals variable is either 'buy' or 'sell'.
If the signals variable is 'buy', a market order to buy is executed using the client.new_order() method. Then, the stop loss price is calculated as stop_loss_price = mark_price - (mark_price * 0.002), which is 0.2% below the current market price. A stop loss order to sell is placed using the client.new_order() method with the type parameter set to 'STOP_MARKET'. The stopPrice parameter is set to the calculated stop loss price.
If the signals variable is 'sell', a market order to sell is executed using the client.new_order() method. Then, the stop loss price is calculated as stop_loss_price = mark_price + (mark_price * 0.002), which is 0.2% above the current market price. A stop loss order to buy is placed using the client.new_order() method with the type parameter set to 'STOP_MARKET'. The stopPrice parameter is set to the calculated stop loss price.
The time.sleep(1) statements are used to introduce delays between API calls to avoid exceeding rate limits.
If the stop loss orders are not being placed as expected, there could be several reasons including errors in the Binance API interaction, incorrect quantity or price calculations, or issues with the Binance account configuration. It would be helpful to check for any error messages or exceptions thrown during the execution of the code to further investigate the issue.