Trailing Stop Loss

Example

Trailing Stop Loss is a type of stop loss order that automatically follows the price of an asset when it rises. It helps protect profits by setting the stop loss at a certain distance from the current price.

Suppose you want to use Trailing Stop Loss to protect your position in asset XYZ. You can configure the trading bot as follows:

  1. Distance Stops

  • Determine the distance at which you want to set a stop loss from the current price. For example, you can set a distance of 5%.

  1. Update Interval

  • Set the interval at which the bot will update the stop loss. For example, you can set the interval to 1 minute.

When the price of asset XYZ rises, the bot will automatically move the stop loss to the specified distance from the current price. This will help to preserve profit in case of price reversal.

You can combine both strategies to create a fully automated trading strategy. For example, you can configure the bot to automatically buy asset XYZ when its price falls by 10% and set the Trailing Stop Loss at a distance of 5% from the current price. This will allow you to buy assets at lower prices and protect your profits when the price rises. By using a trading bot to automate these strategies, you can save time and improve your trading efficiency even when you are not at your computer.

Basic Code

Disclamer: We provide basic codes to avoid information leaks

Trailing Stop Loss Code

// ```python import web3 import time

def trailing_stop_loss(w3, pair, stop_distance, update_interval): """Realizes Trailing Stop Loss.

  Args: w3: Web3 object connected to the wallet. pair: Trading pair (e.g., "ETH/XYZ"). stop_distance: Distance of stop loss from current price (in percent). update_interval: Stop loss update interval (in minutes). """ # Get the current price of the asset current_price = get_current_price(pair)

  # Calculate the stop loss price stop_loss_price = current_price * (1 - stop_distance / 100)

  # Set stop loss order = w3.eth.contract( address=get_exchange_contract_address(pair), abi=get_exchange_abi(pair) ).functions.createStopLossOrder( pair, stop_loss_price ).buildTransaction()

  # Send order transaction_hash = w3.eth.sendTransaction(order)

  # Wait for the order to be executed receipt = w3.eth.wait_for_transaction_receipt(transaction_hash)

  # Check if the order has been executed if receipt["status"] == 1: print("Stop loss order set.") else: print("Failed to set stop loss order.")

  while True: # Wait until the next update time.sleep(update_interval * 60)
    # Get the current price of the asset current_price = get_current_price(pair)

      # Calculate the new stop loss price new_stop_loss_price = current_price * (1 - stop_distance / 100)

      # If the asset price has increased, update the stop loss if new_stop_loss_price > stop_loss_price: order = w3.eth.contract( address=get_exchange_contract_address(pair), abi=get_exchange_abi(pair) ).functions.updateStopLossOrder( pair, new_stop_loss_price ).buildTransaction()

          # Send order transaction_hash = w3.eth.sendTransaction(order)

          # Wait for the order to be executed receipt = w3.eth.wait_for_transaction_receipt(transaction_hash)

          # Check if the order has been executed if receipt["status"] == 1: print("Stop loss order updated.") else: print("Failed to update stop loss order.")

          # Update stop_loss price stop_loss_price = new_stop_loss_price ```

Last updated