saksh-trading-bot
v2.0.2
Published
A modular Node.js trading bot for processing TradingView webhooks with custom trading, Open AI parsing, and event-based monitoring
Maintainers
Readme
Saksh Trading Bot
A modular Node.js trading bot for processing TradingView webhooks, executing trades via a custom callback, and storing data in MongoDB. Supports paper trading and Binance trading (Testnet or live) with event-based monitoring and JSON logging, inspired by Cornix’s API-driven functionality.
Features
- Webhook Processing: Parses JSON or text (e.g.,
AltAlgo) webhooks into a standard JSON format using a user-definedwebhookParserCallback. - Custom Trading: Executes trades via a
placeOrderCallback, supporting paper trading, Binance, or other exchanges. - Paper Trading: Simulates trades with mock prices and balance tracking.
- Binance Trading: Connects to Binance API (Testnet or live) using
ccxt. - Event Monitoring: Emits events (
tradeExecuted,stopLossTriggered, etc.) for real-time tracking. - Data Storage: Logs trades and webhooks to MongoDB and
logs.json. - No Internal Indicators: Relies on third-party webhook signals for trading decisions.
Installation
Clone or Create Project:
mkdir my-trading-bot cd my-trading-bot npm init -yInstall Dependencies:
npm install saksh-trading-bot express mongodb dotenv ccxtSet Up Project Structure:
- Copy
examples/paper_trading_example.js,examples/binance_trading_example.js, andexamples/webhook_parser.jsto anexamplesfolder. - Ensure
saksh-trading-botis installed withsrc/index.js.
- Copy
Configuration
Create
.env:touch .envAdd:
MONGO_URI=mongodb://localhost:27017 PORT=3000 BINANCE_API_KEY=your_testnet_or_live_api_key BINANCE_SECRET_KEY=your_testnet_or_live_secret_key BINANCE_TESTNET=true- For Binance trading, get keys from testnet.binance.vision (Testnet) or Binance (live).
- Set
BINANCE_TESTNET=falsefor live trading.
Set Up MongoDB:
- Local: Install MongoDB, run
mongod(instructions). - Atlas: Create a cluster, whitelist IP, get URI (mongodb.com).
- Local: Install MongoDB, run
Usage
Paper Trading
Run the paper trading example to simulate trades:
node examples/paper_trading_example.js- Output:
Connected to MongoDB [EVENT] Bot Initialized: {"symbol":"BTC/USDT","timestamp":"2025-07-03T08:28:00Z"} Webhook server running on port 3000
Binance Trading
Run the Binance trading example:
node examples/binance_trading_example.js- Requires valid
BINANCE_API_KEY,BINANCE_SECRET_KEYin.env. - Set
BINANCE_TESTNET=falsefor live trading.
Webhook Format
The bot expects webhooks to be parsed into this JSON format by webhookParserCallback:
{
"action": "buy" | "sell",
"symbol": "BTC/USDT",
"quantity": 0.001, // Optional, defaults to 0.001
"stopLoss": 0.03, // Optional, percentage (e.g., 3%)
"takeProfit": 0.05, // Optional, percentage (e.g., 5%)
"timestamp": "2025-07-03T08:28:00Z" // Optional, ISO format
}- The
webhookParserCallbackinexamples/webhook_parser.jshandles:- JSON payloads: Uses input with defaults for missing fields.
- Text payloads (e.g.,
AltAlgo (...): order buy @ 50000 filled on BTCUSD): Parses to JSON using regex.
- Extend
webhookParserCallbackwith an AI model (e.g., xAI API) for advanced parsing.
Event Monitoring
The bot emits events for real-time monitoring, logged to logs.json and MongoDB in examples/*.js:
botInitialized: Bot startup with config.webhookReceived,webhookParsed: Webhook processing.webhookParseFailed: Invalid payload format.webhookValidationFailed: Invalid action or symbol.tradeExecuted: Trade placement with details.tradeMonitored: Price monitoring for stop-loss/take-profit.stopLossTriggered: Stop-loss hit.takeProfitTriggered: Take-profit hit.error: Any issues with details.
Testing
Webhook Test:
- JSON:
curl -X POST http://carbonew.exchange:3000/webhook \ -H "Content-Type: application/json" \ -d '{"action":"buy","symbol":"BTC/USDT","quantity":0.001,"stopLoss":0.03,"takeProfit":0.05}' - Text (
AltAlgo):curl -X POST http://carbonew.exchange:3000/webhook \ -H "Content-Type: text/plain" \ -d 'AltAlgo (...): order buy @ 50000 filled on BTCUSD' - Output:
[EVENT] Webhook Received: {"payload":"AltAlgo (...): order buy @ 50000 filled on BTCUSD"} [EVENT] Webhook Parsed: {"parsedPayload":{"action":"buy","symbol":"BTC/USDT","quantity":0.001,"stopLoss":0.03,"takeProfit":0.05,...}} [EVENT] Trade Executed: {"type":"buy","price":50000,"quantity":0.001,"stopLoss":0.03,"takeProfit":0.05,"balance":950}
- JSON:
Check MongoDB:
const { MongoClient } = require('mongodb'); async function checkData() { const client = new MongoClient('mongodb://localhost:27017'); await client.connect(); const db = client.db('trading_bot'); const trades = await db.collection('trades').find().toArray(); console.log(trades); await client.close(); } checkData();- Expected:
{ "timestamp": "2025-07-03T08:28:00Z", "type": "buy", "price": 50000, "quantity": 0.001, "balance": 950, "stopLoss": 0.03, "takeProfit": 0.05 }
- Expected:
Check logs.json:
[ {"event":"Bot Initialized","symbol":"BTC/USDT","timestamp":"2025-07-03T08:28:00Z"}, {"event":"Webhook Received","payload":"AltAlgo (...): order buy @ 50000 filled on BTCUSD"}, {"event":"Trade Executed","type":"buy","price":50000,"quantity":0.001,"balance":950,"stopLoss":0.03,"takeProfit":0.05} ]
Dependencies
saksh-trading-bot: Core trading bot module.express: Web server for webhook endpoint.mongodb: MongoDB driver for data storage.dotenv: Environment variable management.ccxt: Binance API access (forbinance_trading_example.js).
Troubleshooting
- No Trades: Verify webhook URL, API keys, and Binance Testnet access (testnet.binance.vision).
- Webhook Errors: Ensure payloads are parsable by
webhookParserCallback. - MongoDB: Confirm
MONGO_URIand database connectivity.
Extending the Bot
- Custom Exchange: Replace
placeOrderCallbackinexamples/*.jswith logic for another exchange (e.g., Coinbase). - AI Parsing: Enhance
webhookParserCallbackinexamples/webhook_parser.jswith an AI model (e.g., xAI API). - Custom Logging: Modify event listeners in
examples/*.jsto log to other systems (e.g., MySQL, Redis).
License
MIT License. See LICENSE for details.
