# Place Limit Orders

## Basic <a href="#basic" id="basic"></a>

A trading bot can create limit orders by interacting with DEX exchange contracts. An exchange contract usually provides a `createOrder` function that allows users to place limit orders.

#### Advantages of Limit Orders <a href="#advantages-of-limit-orders" id="advantages-of-limit-orders"></a>

1. **Price Control**

* Limit orders allow traders to specify a desired price to buy or sell an asset, providing greater control over the execution price.

2. **Reducing Slippage**

* Limit orders help reduce slippage, which occurs when an order is executed at a price different from the desired price.

3. **Passive Income**

* Limit orders can be used to create passive income by placing buy orders below the market price and sell orders above the market price.

The bot can also use `createOrder` to place limit sell orders.

By using a trading bot to automate limit order creation, traders can optimize their trading, increase efficiency and respond to market opportunities 24/7.

## Basic Code <a href="#basic-code" id="basic-code"></a>

{% hint style="info" %}
<mark style="color:red;">Disclamer:</mark> We provide basic codes to avoid information leaks
{% endhint %}

**Limit Order Creation Function**

{% code overflow="wrap" %}
```javascript
// import web3

def create_limit_order(w3, pair, amount, price, side): """Creates a limit order.

  Args: w3: Web3 object connected to the wallet. pair: Trading pair (e.g. "ETH/XYZ"). amount: Order amount. price: Order price. side: Order side ("buy" or "sell"). """ # Create exchange contract exchange_contract = w3.eth.contract( address="YOUR_EXCHANGE_CONTRACT_ADDRESS", abi="YOUR_EXCHANGE_CONTRACT_ABI" )

  # Create limit order if side == "buy": order = exchange_contract.functions.createOrder( pair, amount, price ).buildTransaction() elif side == "sell": order = exchange_contract.functions.createLimitSellOrder( pair, amount, price ).buildTransaction() else: raise ValueError("Unknown side of order.")

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

  return transaction_hash
  
# Replace these variables with the desired values pair = "ETH/XYZ" amount = 1 price = 0.1 side = "buy"

# Connect to Web3 and wallet w3 = connect_to_wallet(private_key, network)

# Create a limit order transaction_hash = create_limit_order(w3, pair, amount, price, side)

# 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("Limit order created.") else: print("Failed to create limit order.")
```
{% endcode %}
