OpenTrader Documentation
repository·dev·Indexed 25 days ago
https://github.com/open-trader/opentraderA self-hosted cryptocurrency trading bot supporting high-frequency and cross-exchange trading. OpenTrader includes built-in strategies (Grid, DCA, RSI), backtesting capabilities, and a web-based UI. It provides a modular architecture with packages for exchange integration (@opentrader/exchanges), database management (@opentrader/db), and tRPC API integration for Next.js and Nest.js applications. Supported exchanges include OKX, BYBIT, BINANCE, KRAKEN, COINBASE, GATEIO, and BITGET.
What's inside OpenTrader
- In OpenTrader, an Entity is a Prisma model that has been normalized specifically to facilitate better integration with the User Interface (UI). While the underlying data is managed via Prisma, the Entity layer provides a structure optimized for consumption by frontend components and UI logic.
Use predefined TSConfigs from @opentrader/tsconfig
devThe
@opentrader/tsconfigpackage provides several base configurations to ensure consistent TypeScript settings across the project. You can inherit from these files depending on your environment:base.json: The foundational configuration used as a base for all other configs.module.json: A common configuration designed for every package located in the/packagesdirectory.nextjs.json: Optimized configuration for Next.js applications.nestjs.json: Optimized configuration for Nest.js applications.
Setup tRPC Backend with Next.js
devTo integrate the
@opentrader/trpcrouter into a Next.js application, create a route handler atsrc/app/api/trpc/[trpc]/route.ts. Use thefetchRequestHandlerfrom@trpc/server/adapters/fetchto handle requests, passing in theappRouterand acreateContextfunction.// src/app/api/trpc/[trpc]/route.ts import { appRouter, createContext } from "@opentrader/trpc"; import { fetchRequestHandler } from "@trpc/server/adapters/fetch"; const handler = (req: Request) => fetchRequestHandler({ endpoint: "/api/trpc", req, router: appRouter, createContext: () => ({ //... }), }); export { handler as GET, handler as POST };Configure exchange API keys
devTo connect an exchange, copy the
exchanges.sample.json5file toexchanges.json5and add your API keys.Supported exchanges include:
OKX,BYBIT,BINANCE,KRAKEN,COINBASE,GATEIO, andBITGET.Use @opentrader/exchanges on the Server
devTo use the exchanges package in a server-side environment (e.g., Next.js Server Components), you should configure a persistent cache provider using
PrismaCacheProviderfrom@opentrader/exchanges/server. This ensures that data like markets loaded viaexchange.loadMarkets()are cached in your database.import { ExchangeCode } from "@opentrader/types"; import { exchanges, cache } from "@opentrader/exchanges"; import { PrismaCacheProvider } from "@opentrader/exchanges/server"; // Configure the cache provider to use Prisma cache.setCacheProvider(new PrismaCacheProvider()); export default async function Page() { // Access a specific exchange using its ExchangeCode const exchange = exchanges[ExchangeCode.OKX](); // Load markets (this call will be cached in Prisma) const markets = await exchange.loadMarkets(); return <div>Markets: {Object.keys(markets).length}</div>; }Access Prisma Client and Zod types
devThe
@prisma/clientis exported via the@opentrader/dbpackage.If you specifically need to import Zod types, use the
ztexport from@opentrader/prisma.Access the OpenTrader UI
devOnce the application is running via
opentrader up, you can access the user interface to manage bots, view backtest results, and monitor live trading at:http://localhost:8000Setup tRPC Backend with Nest.js
devTo use
@opentrader/trpcin a Nest.js application, create a middleware class that uses@trpc/server/adapters/expressto mount theappRouterat the/api/trpcendpoint. Then, inject and apply this middleware in your application's bootstrap function.// src/trpc.middleware.ts import { INestApplication, Injectable } from "@nestjs/common"; import * as trpcExpress from "@trpc/server/adapters/express"; import { appRouter } from "@opentrader/trpc"; @Injectable() export class TrpcMiddleware { applyMiddleware(app: INestApplication) { app.use( `/api/trpc`, trpcExpress.createExpressMiddleware({ router: appRouter, createContext: () => ({ // ... }), }), ); } } // main.ts import { TrpcMiddleware } from "src/trpc.middleware"; async function bootstrap() { // ... const trpc = app.get(TrpcMiddleware); trpc.applyMiddleware(app); // ... } bootstrap();Use @opentrader/exchanges on the Client
devWhen using the exchanges package in a client-side environment (e.g., React Client Components), you can use
MemoryCacheProviderfrom@opentrader/exchanges/clientto manage caching in memory. Note thatMemoryCacheProvideris used by default, so explicitly setting it is optional."use client"; import { useEffect, useState } from "react"; import { ExchangeCode } from "@opentrader/types"; import { exchanges, cache } from "@opentrader/exchanges"; import { MemoryCacheProvider } from "@opentrader/exchanges/client"; // Configure memory cache (optional, as it is the default) cache.setCacheProvider(new MemoryCacheProvider()); const exchange = exchanges[ExchangeCode.OKX](); export default function Page() { const [markets, setMarkets] = useState<Awaited< ReturnType<typeof exchange.loadMarkets> > | null>(null); useEffect(() => { exchange.loadMarkets().then((data) => { setMarkets(data); }); }, []); if (markets === null) { return <div>Loading...</div>; } return <div>Markets: {Object.keys(markets).length}</div>; }Install and set up OpenTrader
devOpenTrader requires Node.js v22 or higher.
- Install the package globally via npm:
npm install -g opentrader- Set an admin password for the UI:
opentrader set-password <password>- Start the application:
opentrader upThe app starts an RPC server on port 8000. To run the app as a daemon, use
opentrader up -d. To stop the application, useopentrader down.npm install -g opentrader opentrader set-password <password> opentrader upConfigure a strategy with config.json5
devCreate a
config.json5file to define your strategy parameters. Below is an example configuration for thegridstrategy.{ // Grid strategy params settings: { highPrice: 70000, // upper price of the grid lowPrice: 60000, // lower price of the grid gridLevels: 20, // number of grid levels quantityPerGrid: 0.0001, // quantity in base currency per each grid }, pair: "BTC/USDT", exchange: "DEFAULT", }Use the @opentrader/exchanges package
devThe@opentrader/exchangespackage provides the core infrastructure for interacting with various cryptocurrency exchanges. It exports exchange implementations, common types, caching mechanisms, and the baseExchangeProviderinterface used to build or consume exchange integrations.