# Opening a Position

When the entry condition is met, we need to open a position using the solver's instant trading API. More details on this can be found [here](/exchange-builder-documentation/frontend-builder-technical-guidance/instant-trading/sending-a-quote-instant-open.md).

#### Fetching Price from Muon Oracle

For on-chain verification of price and solvency, we need to fetch the price and a signature from the Muon oracle:

```python
def fetch_muon_price():
    try:
        response = requests.get(MUON_URL)
        data = response.json()
        fetched_price_wei = data["result"]["data"]["result"]["price"]
        return Decimal(fetched_price_wei) / Decimal('1e18')
    except Exception as e:
        print(f"[ERROR] Failed to fetch Muon price: {e}")
        return None
```

#### Applying Slippage

Since we're going LONG, we increase the price by 1% to ensure our order gets filled:

```python
# For long positions, add 1% slippage
adjusted_price = fetched_price * Decimal('1.01')
```

#### Preparing Trade Parameters

We need to calculate collateral parameters based on the notional value and parameters returned from the `get_locked_params` solver endpoint.

```python
def fetch_locked_params():
    """Fetch locked parameters for the trade."""
    try:
        response = requests.get(LOCKED_PARAMS_URL)
        response.raise_for_status()
        data = response.json()
        if data.get("message") == "Success":
            return data
        else:
            raise ValueError("Failed to fetch locked parameters")
    except Exception as e:
        print(f"[ERROR] Failed to fetch locked parameters: {e}")
        return None

def calculate_normalized_locked_value(notional, locked_param, leverage, apply_leverage=True):
    """Compute normalized locked value for a given parameter."""
    notional = Decimal(str(notional))
    locked_param = Decimal(str(locked_param))
    leverage = Decimal(str(leverage))
    
    if apply_leverage:
        return str(notional * locked_param / (Decimal('100') * leverage))
    else:
        return str(notional * locked_param / Decimal('100'))
```

#### Sending the Trade Request

```python
trade_params = {
    "symbolId": CONFIG["SYMBOL_ID"],
    "positionType": CONFIG["POSITION_TYPE"],  # 0 for long, 1 for short
    "orderType": 1,  # 1 for market order (must be market since we're using instant actrions)
    "price": str(adjusted_price),
    "quantity": CONFIG["QUANTITY"],
    "cva": normalized_cva,
    "lf": normalized_lf,
    "partyAmm": normalized_party_amm,
    "partyBmm": '0',
    "maxFundingRate": CONFIG["MAX_FUNDING_RATE"],
    "deadline": deadline
}

response = requests.post(f"{HEDGER_URL}/instant_open", json=trade_params, headers=headers)
```

The API will return a temporary quote ID that we'll use to track our order.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.symm.io/trader-documentation/building-a-trading-bot/opening-a-position.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
