> For the complete documentation index, see [llms.txt](https://docs.hipersnipe.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.hipersnipe.com/features/buy-sell.md).

# Buy and sell execution

## Buy flow

1. User taps a buy preset (e.g. **Buy 0.1**) or enters custom SOL amount.
2. `handlers/trading_buy_sell.py` validates balance and settings.
3. `handlers/trading_execution.py` resolves slippage, priority fee, execution mode.
4. `swap.py` selects route and sends transaction.
5. Success UI shows route used (Pump direct, Lightning, Jupiter) and Solscan link.
6. Trade appended to `users.trades` JSON; PnL recalculated.
7. Trading Shield may auto-arm if enabled in settings.

## Sell flow

1. User taps sell % (25/50/100), custom %, or **Sell Initials**.
2. Balance fetched from RPC (confirmed balance for 100% sells).
3. Swap routed through Jupiter or Pump depending on token type.
4. On success, position updated; shield removed if 100% sell.

## Sell Initials

Sells enough tokens to recover the initial SOL cost basis for the current cycle. Shows 🛑 when position is in loss (confirmation dialog) or 💰 when profitable.

## Execution parameters

Resolved per user from DB:

| Setting        | Source                                |
| -------------- | ------------------------------------- |
| Slippage       | `slippage_turbo`, `slippage_bonding`  |
| Priority fee   | `prio_fee_buy`, `prio_fee_sell`       |
| Execution mode | `priority_mode` (Standard/Fast/Turbo) |
| Anti-MEV       | Jito bundle when mode warrants        |

## Error handling

| Error                  | Behavior                             |
| ---------------------- | ------------------------------------ |
| RPC 429                | Rotate fallback RPC, retry once      |
| Insufficient balance   | User alert, no tx sent               |
| Pump slippage exceeded | Auto-retry with higher slippage      |
| Timeout                | Graceful message; bot loop continues |

## Manual sell hardening

100% sells prefer confirmed token balances and rotate RPC on rate limits to reduce failed exits during provider congestion.

## Fire-and-forget mode

Recent optimizations dispatch trade execution as background tasks so the Telegram callback returns quickly and avoids timeout errors on slow RPC paths.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://docs.hipersnipe.com/features/buy-sell.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
