Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
c52ccf3
README and tooling files
coccoinomane May 22, 2025
b6b53af
feat: exchange agnostic ask agent
coccoinomane May 22, 2025
0d1567f
feat: tool getBalance to get bybit unified balance
coccoinomane May 24, 2025
c968c48
feat: tool getCurrencyMarketsOfGivenType.ts
coccoinomane May 29, 2025
dc080a7
feat: tool getMarketInfo
coccoinomane May 29, 2025
8965b79
fix: force UTA account mode at exchange level
coccoinomane Jun 2, 2025
92f2144
feat: query open positions
coccoinomane Jun 2, 2025
7f55983
feat: tool setMarketLeverage
coccoinomane Jun 6, 2025
cba3c33
feat: tool setMarketLeverage
coccoinomane Jun 7, 2025
eb29f1f
docs: add spot commands to readme
coccoinomane Jun 7, 2025
48be5e3
ux: renamed set leverage tool
coccoinomane Jun 8, 2025
51c7a00
feat: tool getUserLeverageOnMarket
coccoinomane Jun 8, 2025
e756c83
feat: tool getUserMarginMode
coccoinomane Jun 8, 2025
64a26d0
feat: tool setUserMarginMode
coccoinomane Jun 8, 2025
6c6a381
feat: tools to add and reduce margin
coccoinomane Jun 9, 2025
fce8aea
feat: order fetching toolsTODO: handle buy/sell/long/short + type (de…
coccoinomane Jun 16, 2025
6c99669
feat: support for showing TP/SL
coccoinomane Jun 16, 2025
2546f3b
feat: cycle through all order types for order getter
coccoinomane Jun 16, 2025
2ba9ce8
ux: improve tool definition of futures tools
coccoinomane Jun 16, 2025
f888c86
feat: cancel order tools
coccoinomane Jun 16, 2025
bf23bfc
feat: tool createSimpleOrder
coccoinomane Jun 16, 2025
ef3df6c
ux: improve set leverage prompt
coccoinomane Jun 16, 2025
b049a3a
feat: tool closePosition
coccoinomane Jun 16, 2025
eff5cae
feat: Handle amount in quote currency (both future and spot)
coccoinomane Jun 18, 2025
e04ca9a
refac: minor prompt refactoring
coccoinomane Jun 18, 2025
be21350
feat: tool createPositionWithTakeProfitAndOrStopLossOrderAttached
coccoinomane Jun 21, 2025
a52a9c5
ux: optimized fetch max leverage speed
coccoinomane Jun 21, 2025
e9851c2
lint: remove redundant JSDocs types
coccoinomane Jun 21, 2025
bb5edd2
ux: optimized again fetch max leverage speed
coccoinomane Jun 21, 2025
828434b
ux: feature check on TP/SL attached to position
coccoinomane Jun 21, 2025
5ec5682
ux: removed redundant check
coccoinomane Jun 21, 2025
8173582
feat: tool attachTakeProfitAndOrStopLossOrderToExistingPosition
coccoinomane Jun 21, 2025
a0968c3
docs: minor README improvement
coccoinomane Jun 21, 2025
314286b
feat: tool createSpotEntryOrderWithTakeProfitAndOrStopLossAttached
coccoinomane Jun 22, 2025
da15354
feat: tool createConditionalOrder
coccoinomane Jun 22, 2025
fb485bd
ux: moved 'with 100 USDT' logic to tool implementation
coccoinomane Jun 22, 2025
5984d4a
ux: ensure spot OTOCO order executes as market order in absence of li…
coccoinomane Jun 23, 2025
6c034f2
feat: tool createSpotTakeProfitAndOrStopLossOrder
coccoinomane Jun 23, 2025
3b03258
fix: fixed ambiguity between OTOCO and TP/SL orders in spot
coccoinomane Jun 23, 2025
f8230d3
ux: moved TP and SL percentage computations to tool implementation
coccoinomane Jun 23, 2025
7af3b21
feat: attachTrailingStopToExistingPosition tool
coccoinomane Jun 24, 2025
15b79e7
feat: support for futures trailing stop as percentage
coccoinomane Jun 24, 2025
bad0759
ux: reduced limit order price tolerance
coccoinomane Jun 25, 2025
7f80ad2
ux: explain that trailing stop orders on SPOT markets are not supported
coccoinomane Jun 25, 2025
391d861
fix: cancelAllOrders
coccoinomane Jun 25, 2025
6d6458d
ux: show message if position does not exist
coccoinomane Jun 25, 2025
70786fa
docs: typo in README
coccoinomane Jun 25, 2025
5c91f62
ux: show 'close long'/'close short' as side in closePosition output
coccoinomane Jul 1, 2025
9f7ed7c
ux: add P&L to closePosition tool
coccoinomane Jul 1, 2025
e999f5f
ux: show TP, SL, TS info on position fetching tools
coccoinomane Jul 2, 2025
8a58651
fix: told the LLM to stop if setting margin mode failed
coccoinomane Jul 2, 2025
3f948fe
fix: make sure createSimpleOrder is called in absence of TP/SL
coccoinomane Jul 2, 2025
1e29dcc
feat: tool getUserDepositAddress
coccoinomane Jul 2, 2025
69a27b5
feat: tool transferFundsTo
coccoinomane Jul 4, 2025
5219c51
docs: minor update to readme
coccoinomane Jul 4, 2025
0ea1168
feat: tool withdraw
coccoinomane Jul 4, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions projects/bybit/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Variables for running the ask-bybit agent

# OpenAI API Key
OPENAI_API_KEY=

# DeepSeek API Key. If set, will be used instead of OpenAI.
DEEPSEEK_API_KEY=

# Bybit API key and secret key
# Get your key by Account > API
# The API key will expire automatically within
# 3 months unless you restrict the IP
BYBIT_API_KEY=
BYBIT_SECRET_KEY=

# Testnet configuration
# Get your key here: https://testnet.bybit.com
BYBIT_USE_TESTNET=false
BYBIT_TESTNET_API_KEY=
BYBIT_TESTNET_SECRET_KEY=
4 changes: 4 additions & 0 deletions projects/bybit/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
.vscode
.env
dist
6 changes: 6 additions & 0 deletions projects/bybit/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"singleQuote": true,
"trailingComma": "all",
"tabWidth": 4,
"printWidth": 180
}
98 changes: 98 additions & 0 deletions projects/bybit/CLAUDE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# CLAUDE.md

This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.

## Common Development Commands

### Build and Development
- `pnpm build` - Build the project using tsup
- `pnpm start` - Watch mode for development (rebuilds on changes)
- `pnpm format` - Format code with Prettier

### Testing the Integration
- `pnpm ask-bybit "YOUR_QUERY"` - Test the integration with a trading command
- `pnpm ask-bybit "YOUR_QUERY" --debug-llm` - Test with LLM request/response debugging
- `pnpm test` - Run tests with vitest (currently no tests implemented)

### Linting and Type Checking
- `pnpm lint` - Run linting with tsdx
- TypeScript compilation happens automatically during build

## High-Level Architecture

This is a Bybit cryptocurrency exchange integration for HeyAnon.ai that provides a conversational AI interface for trading on Bybit's spot and futures markets.

### Core Architecture Components

1. **HeyAnon SDK Integration** (`/src/index.ts`)
- Main export satisfies the `AdapterExport` interface from `@heyanon/sdk`
- Exposes trading functions and AI tools to the HeyAnon platform

2. **AI Tool Definitions** (`/src/tools.ts`)
- 23 AI tools defined for the LLM to understand and execute trading operations
- Each tool has detailed descriptions, parameters, and validation
- Tools are categorized: account info, market data, order management, position management

3. **Trading Functions** (`/src/functions/`)
- Each function implements a specific trading operation
- Functions use CCXT library for exchange communication
- Extended with custom Bybit-specific features via helper functions

4. **CCXT Extensions** (`/src/helpers/exchange.ts`)
- Custom functions that extend CCXT to cover Bybit-specific features not in the standard library:
- `attachTakeProfitAndOrStopLossOrderToExistingPosition`
- `getAccountMarginMode`
- `addOrReducePositionMargin`
- `getUserOpenOrders`
- `getOrderById`

5. **Test Agent** (`/src/agent/`)
- CLI tool for testing the integration locally
- Uses OpenAI or DeepSeek for natural language processing
- Simulates the HeyAnon environment for development

### Key Design Decisions

1. **Unified Trading Account (UTA 2.0 Pro)**
- No separate spot/futures wallets - single unified balance
- No fund transfers needed between account types
- `enableUnifiedAccount: true` must be set on exchange object

2. **Market Type Inference**
- LLM infers market type from context:
- "buy/sell" → spot market
- "long/short" → futures (perpetual by default)
- Delivery dates mentioned → delivery futures

3. **Account-Level vs Market-Level Settings**
- Margin mode (cross/isolated/portfolio) applies to entire account
- Leverage settings apply per market
- Critical distinction from other exchanges

4. **Order Type Mappings**
- Futures OCO: TP/SL on existing position
- Futures OTOCO: New position with TP/SL attached
- Spot OTOCO: Limit order with TP/SL (market orders not supported)
- Spot OCO: Simulated via two conditional orders

5. **Settlement Currency Support**
- Supports USDT and USDC as settlement currencies
- Required parameter for listing operations due to Bybit API requirements

### Environment Configuration

Required environment variables (see `.env.example`):
- `OPENAI_API_KEY` or `DEEPSEEK_API_KEY` - For test agent
- `BYBIT_API_KEY` and `BYBIT_SECRET_KEY` - Production credentials
- `BYBIT_USE_TESTNET` - Toggle testnet mode
- `BYBIT_TESTNET_API_KEY` and `BYBIT_TESTNET_SECRET_KEY` - Testnet credentials

Note: Bybit API keys expire after 3 months without IP whitelisting.

### Important Constants

- `MAX_MARKETS_IN_RESULTS`: 30 - Limit for market listings
- `MAX_ORDERS_IN_RESULTS`: 100 - Limit for order queries
- `MAX_POSITIONS_IN_RESULTS`: 100 - Limit for position queries
- `SUPPORTED_SETTLE_CURRENCIES`: ['USDT', 'USDC']
- `SUPPORTED_MARKET_TYPES`: ['spot', 'perpetual', 'delivery']
Loading