> ## Documentation Index
> Fetch the complete documentation index at: https://composio-27-feat-docs-revamp.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Crypto Trading Agent

> This project demonstrates how to use Composio to create an automated crypto trading agent that monitors and executes trades on Base Mainnet.

## Overview

The Crypto Trading Agent is an AI Agent that monitors cryptocurrency prices and automatically executes trades between USDC and ETH when favorable conditions are met on Base Mainnet.

## Getting Started

<Tabs>
  <Tab title="Python">
    <Steps>
      <Step title="Installation">
        ```bash install dependencies
        pip install composio-langchain python-dotenv cdp-langchain langchain-openai
        ```
      </Step>

      <Step title="Connecting to tools and models">
        ```bash connect to required tools
        composio add coinbase
        composio add exa

        export OPENAI_API_KEY="<your-openai-api-key>"
        ```
      </Step>

      <Step title="Importing the required libraries">
        ```python import required libraries
        from langchain_openai import ChatOpenAI
        from langchain.agents import create_openai_functions_agent, AgentExecutor
        from langchain import hub
        from composio_langchain import ComposioToolSet, App
        from cdp_langchain.agent_toolkits import CdpToolkit
        from cdp_langchain.utils import CdpAgentkitWrapper
        from dotenv import load_dotenv
        from pathlib import Path
        import time
        import os

        load_dotenv()
        ```
      </Step>

      <Step title="Setting up the Tools and Agent">
        ```python setup tools and agent
        llm = ChatOpenAI(model="gpt-4o")

        toolset = ComposioToolSet()
        tools = toolset.get_tools(apps=[
            App.COINBASE,
            App.EXA,
            App.FILETOOL
        ])

        prompt = hub.pull("hwchase17/openai-functions-agent")
        agent = create_openai_functions_agent(llm, tools, prompt)
        agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
        ```
      </Step>

      <Step title="Initializing Wallet">
        ```python initialize wallet
        # Find and store wallet data
        agent_executor.invoke({
            "input": 'Find existing Wallet on Base Mainnet (If not found, create a new one), Get its details including wallet_id, seed, and default_address_id, store it in the wallet_data.txt file in this format: {"wallet_id": "", "seed": "", "default_address_id": ""}'
        })

        # Get the current file's directory and read wallet data
        current_dir = Path(__file__).parent
        wallet_data_file = current_dir / "wallet_data.txt"

        try:
            with open(wallet_data_file) as f:
                wallet_data = f.read()
        except FileNotFoundError:
            print(f"Could not find wallet data file at: {wallet_data_file}")
            wallet_data = None
        ```
      </Step>

      <Step title="Setting up CDP Toolkit">
        ```python setup cdp toolkit
        os.environ['NETWORK_ID'] = 'base-mainnet'

        # Configure CDP Agentkit
        values = {}
        if wallet_data is not None:
            values = {"cdp_wallet_data": wallet_data}

        cdp = CdpAgentkitWrapper()
        toolkit = CdpToolkit.from_cdp_agentkit_wrapper(cdp)
        cdp_toolkit = toolkit.get_tools()
        cdp_toolkit.extend(tools)

        agent = create_openai_functions_agent(llm, cdp_toolkit, prompt)
        agent_executor = AgentExecutor(agent=agent, tools=cdp_toolkit, verbose=True)
        ```
      </Step>

      <Step title="Running the Trading Loop">
        ```python run trading loop
        POLLING_INTERVAL = 60  # Poll every 60 seconds

        print("Starting trading poll...")
        while True:
            try:
                print(f"Executing trade at {time.strftime('%Y-%m-%d %H:%M:%S')}")
                agent_executor.invoke({
                    "input": "Check the prices of USDC and ETH on Base Mainnet and perform a trade between 0.01 ETH and USDC or 0.01 USDC and ETH if the price is favorable on Base Mainnet"
                })
            except Exception as e:
                print(f"Error during trade execution: {e}")
            
            print(f"Waiting {POLLING_INTERVAL} seconds before next trade...")
            time.sleep(POLLING_INTERVAL)
        ```
      </Step>

      <Step title="Final Code">
        ```python final code
        from langchain_openai import ChatOpenAI
        from langchain.agents import create_openai_functions_agent, AgentExecutor
        from langchain import hub
        from composio_langchain import ComposioToolSet, App
        from cdp_langchain.agent_toolkits import CdpToolkit
        from cdp_langchain.utils import CdpAgentkitWrapper
        from dotenv import load_dotenv
        from pathlib import Path
        import time
        import os

        load_dotenv()

        llm = ChatOpenAI(model="gpt-4o")

        toolset = ComposioToolSet()
        tools = toolset.get_tools(apps=[
            App.COINBASE,
            App.EXA,
            App.FILETOOL
        ])

        prompt = hub.pull("hwchase17/openai-functions-agent")
        agent = create_openai_functions_agent(llm, tools, prompt)
        agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)

        agent_executor.invoke({
            "input": 'Find existing Wallet on Base Mainnet, Get its details including wallet_id, seed, and default_address_id, store it in the wallet_data.txt file in this format: {"wallet_id": "", "seed": "", "default_address_id": ""}'
        })

        current_dir = Path(__file__).parent
        wallet_data_file = current_dir / "wallet_data.txt"

        try:
            with open(wallet_data_file) as f:
                wallet_data = f.read()
        except FileNotFoundError:
            print(f"Could not find wallet data file at: {wallet_data_file}")
            wallet_data = None

        os.environ['NETWORK_ID'] = 'base-mainnet'

        values = {}
        if wallet_data is not None:
            values = {"cdp_wallet_data": wallet_data}

        cdp = CdpAgentkitWrapper()
        toolkit = CdpToolkit.from_cdp_agentkit_wrapper(cdp)
        cdp_toolkit = toolkit.get_tools()
        cdp_toolkit.extend(tools)

        agent = create_openai_functions_agent(llm, cdp_toolkit, prompt)
        agent_executor = AgentExecutor(agent=agent, tools=cdp_toolkit, verbose=True)

        POLLING_INTERVAL = 60  # Poll every 60 seconds

        print("Starting trading poll...")
        while True:
            try:
                print(f"Executing trade at {time.strftime('%Y-%m-%d %H:%M:%S')}")
                agent_executor.invoke({
                    "input": "Check the prices of USDC and ETH on Base Mainnet and perform a trade between 0.01 ETH and USDC or 0.01 USDC and ETH if the price is favorable on Base Mainnet"
                })
            except Exception as e:
                print(f"Error during trade execution: {e}")
            
            print(f"Waiting {POLLING_INTERVAL} seconds before next trade...")
            time.sleep(POLLING_INTERVAL)
        ```
      </Step>
    </Steps>
  </Tab>
</Tabs>
