Skip to content

armgit / camel

Public Imported from GitHub
0

🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org

5.3 KB
Raw Blame
# ========= Copyright 2023-2026 @ CAMEL-AI.org. All Rights Reserved. =========
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ========= Copyright 2023-2026 @ CAMEL-AI.org. All Rights Reserved. =========

from camel.agents import ChatAgent
from camel.configs import NvidiaConfig
from camel.models import ModelFactory
from camel.types import ModelPlatformType, ModelType

model = ModelFactory.create(
    model_platform=ModelPlatformType.NVIDIA,
    model_type=ModelType.NVIDIA_LLAMA3_1_405B_INSTRUCT,
    model_config_dict=NvidiaConfig(temperature=0.2).as_dict(),
)

# Define system message
sys_msg = "You are a helpful assistant."

# Set agent
camel_agent = ChatAgent(system_message=sys_msg, model=model)

user_msg = """give me python code to develop a trading bot"""

# Get response information
response = camel_agent.step(user_msg)
print(response.msgs[0].content)

'''
===============================================================================
Here is a basic example of a trading bot in Python using the Binance API. This
bot will buy and sell a specified cryptocurrency based on a simple moving
average crossover strategy.

**Please note that this is a simplified example and should not be used for
actual trading without further development and testing. Trading with a bot
carries significant risks, including financial losses.**

**Required Libraries:**

* `ccxt` (CryptoCurrency eXchange Trading Library)
* `pandas` (for data manipulation)
* `numpy` (for numerical computations)

**Code:**
```python
import ccxt
import pandas as pd
import numpy as np

# Set up Binance API credentials
api_key = 'YOUR_API_KEY'
api_secret = 'YOUR_API_SECRET'

# Set up the exchange and API connection
exchange = ccxt.binance({
    'apiKey': api_key,
    'apiSecret': api_secret,
})

# Define the trading parameters
symbol = 'BTC/USDT'  # Trading pair
amount = 100  # Amount to trade (in USDT)
short_window = 20  # Short moving average window (in minutes)
long_window = 50  # Long moving average window (in minutes)

# Define the trading strategy
def strategy(data):
    short_ma = data['Close'].rolling(window=short_window).mean()
    long_ma = data['Close'].rolling(window=long_window).mean()

    if short_ma > long_ma:
        return 'BUY'
    elif short_ma < long_ma:
        return 'SELL'
    else:
        return 'HOLD'

# Define the trading function
def trade(exchange, symbol, amount, strategy):
    # Get the latest candlestick data
    data = exchange.fetch_ohlcv(symbol, timeframe='1m')
    df = pd.DataFrame(
        data,
        columns=['Time', 'Open', 'High', 'Low', 'Close', 'Volume']
    )

    # Apply the trading strategy
    signal = strategy(df)

    # Execute the trade
    if signal == 'BUY':
        exchange.place_order(
            symbol, 'limit', 'buy', amount, df['Close'].iloc[-1]
        )
        print(f'Buy {amount} {symbol} at {df["Close"].iloc[-1]}')
    elif signal == 'SELL':
        exchange.place_order(
            symbol, 'limit', 'sell', amount, df['Close'].iloc[-1]
        )
        print(f'Sell {amount} {symbol} at {df["Close"].iloc[-1]}')
    else:
        print('Hold')

# Run the trading bot
while True:
    trade(exchange, symbol, amount, strategy)
    time.sleep(60)  # Wait 1 minute before checking again

```

**Explanation:**

1. The code sets up a connection to the Binance API using the `ccxt` library.
2. It defines the trading parameters, including the trading pair, amount to
   trade, and moving average windows.
3. The `strategy` function calculates the short and long moving averages and
   returns a trading signal (BUY, SELL, or HOLD).
4. The `trade` function gets the latest candlestick data, applies the trading
   strategy, and executes the trade using the `place_order` method.
5. The code runs in an infinite loop, checking for trading signals every
   minute.

**Note:** This is a basic example and you should consider implementing
additional features, such as:

* Risk management (e.g., stop-loss, position sizing)
* Error handling (e.g., API errors, network issues)
* More sophisticated trading strategies
* Support for multiple trading pairs
* Integration with a database or logging system

I hope this helps! Let me know if you have any questions or need
further assistance.
===============================================================================
'''

model = ModelFactory.create(
    model_platform=ModelPlatformType.NVIDIA,
    model_type=ModelType.NVIDIA_LLAMA3_3_70B_INSTRUCT,
    model_config_dict=NvidiaConfig(temperature=0.2).as_dict(),
)

# Define system message
sys_msg = "You are a helpful assistant."

# Set agent
camel_agent = ChatAgent(system_message=sys_msg, model=model)

user_msg = """Say hi to CAMEL AI, one open-source community
    dedicated to the study of autonomous and communicative agents."""

# Get response information
response = camel_agent.step(user_msg)
print(response.msgs[0].content)