Sponsored by Deepsite.site

MCP Upstox Trading Server

Created By
lonelypx8 months ago
Content

MCP Upstox Trading Server

Project Overview

The Most Connected Pivot (MCP) Upstox Trading Server is an automated trading application that identifies and trades based on high-probability support and resistance levels in the market. The system analyzes historical price data to identify pivot points where price has repeatedly interacted with specific levels, and executes trades when current prices approach these significant levels.

What is MCP (Most Connected Pivot)?

MCP is a technical analysis concept that identifies price levels that have acted as significant support or resistance in the past. These are points where:

  1. Price has crossed its average multiple times (pivots)
  2. Multiple price points have clustered around a specific level (connections)
  3. The level with the most connections becomes the MCP - a high-probability reversal zone

The strategy assumes that price is more likely to respect levels that have previously acted as support/resistance multiple times, giving traders a statistical edge.

Prerequisites

  • Node.js (v14 or higher)
  • NPM (Node Package Manager)
  • Upstox Developer Account with API access
  • Basic understanding of JavaScript and trading concepts

Installation Steps

  1. Create a project directory

    mkdir mcp-upstox-server
    cd mcp-upstox-server
    
  2. Initialize the project

    npm init -y
    
  3. Install required dependencies

    npm install express axios cors dotenv upstox-js-sdk
    
  4. Create server files

    • Copy the provided server code into a file named server.js
    • Create a .env file with the configuration provided
  5. Get Upstox API credentials

    • Log in to your Upstox Developer Account
    • Create a new application to get your API key and secret
    • Set the redirect URI as http://localhost:3000/callback or your custom domain
    • Update the .env file with your credentials

Environment Setup

Development Environment

Create a .env.development file with the following configuration:

PORT=3000
UPSTOX_API_KEY=your_development_api_key
UPSTOX_API_SECRET=your_development_api_secret
REDIRECT_URI=http://localhost:3000/callback
NODE_ENV=development

To run in development mode:

NODE_ENV=development node server.js

Testing Environment

Create a .env.test file:

PORT=3001
UPSTOX_API_KEY=your_test_api_key
UPSTOX_API_SECRET=your_test_api_secret
REDIRECT_URI=http://localhost:3001/callback
NODE_ENV=test

To run in test mode:

NODE_ENV=test node server.js

Production Environment

Create a .env.production file:

PORT=8080
UPSTOX_API_KEY=your_production_api_key
UPSTOX_API_SECRET=your_production_api_secret
REDIRECT_URI=https://your-domain.com/callback
NODE_ENV=production

To run in production mode:

NODE_ENV=production node server.js

Deployment Options

Local Deployment

For personal or development use:

node server.js

Cloud Deployment

Heroku

  1. Create a Procfile:

    web: node server.js
    
  2. Set up environment variables in Heroku dashboard

  3. Deploy:

    git push heroku main
    

AWS EC2

  1. Set up an EC2 instance
  2. Install Node.js and npm
  3. Clone repository
  4. Set up environment variables
  5. Use PM2 to manage the Node process:
    npm install -g pm2
    pm2 start server.js
    

Docker Deployment

Create a Dockerfile:

FROM node:16
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 8080
CMD ["node", "server.js"]

Build and run:

docker build -t mcp-upstox-server .
docker run -p 8080:8080 -d mcp-upstox-server

Running the Server

  1. Start the server

    node server.js
    
  2. Authenticate with Upstox

    • Open your browser and navigate to http://localhost:3000/auth
    • You will be redirected to Upstox login page
    • After logging in and authorizing the application, you'll be redirected back to your server
    • You should see a success message indicating authentication was successful
    • Your tokens are now securely stored and will automatically refresh when needed

Authentication Improvements

The server now includes the following authentication improvements:

  1. Token Persistence

    • Access and refresh tokens are stored securely in a local file (token.json)
    • Tokens are loaded on server startup, eliminating the need to re-authenticate
  2. Automatic Token Refresh

    • The system automatically detects when tokens are about to expire
    • Tokens are refreshed in the background 5 minutes before expiry
    • All API calls use the middleware that ensures a valid token is available
  3. Logout Functionality

    • A new /logout endpoint allows users to completely sign out
    • This clears all stored tokens from memory and disk

Upstox SDK Integration

The server now uses the official Upstox JavaScript SDK, providing:

  1. Simplified API Calls

    • Direct access to all Upstox API endpoints through a consistent interface
    • Type checking and better error handling for API requests
  2. Automatic Request Formatting

    • The SDK handles proper formatting of all API requests
    • Ensures compliance with Upstox API requirements
  3. Easier Maintenance

    • API changes are handled through SDK updates
    • Reduces the need to modify code when the Upstox API evolves

Using the MCP Trading API

Calculate MCP for a Symbol

Request:

GET /historical-data/NSE_FO%7CNIFTY-I/1D?from=2023-01-01&to=2023-04-01

Response:

{
  "symbol": "NSE_FO|NIFTY-I",
  "interval": "1D",
  "dataPoints": [...],
  "mcp": {
    "timestamp": "2023-02-15T09:15:00.000Z",
    "price": 17865.75,
    "connections": 12
  }
}

Calculate MCPs for Multiple Symbols

Request:

POST /mcp/calculate
Content-Type: application/json

{
  "symbols": ["NSE_FO|NIFTY-I", "NSE_FO|BANKNIFTY-I"],
  "interval": "1D",
  "from": "2023-01-01",
  "to": "2023-04-01"
}

Response:

{
  "NSE_FO|NIFTY-I": {
    "timestamp": "2023-02-15T09:15:00.000Z",
    "price": 17865.75,
    "connections": 12
  },
  "NSE_FO|BANKNIFTY-I": {
    "timestamp": "2023-02-18T09:15:00.000Z",
    "price": 41235.50,
    "connections": 9
  }
}

Execute a Trade

Request:

POST /trade
Content-Type: application/json

{
  "symbol": "NSE_FO|NIFTY-I",
  "quantity": 50,
  "side": "BUY",
  "orderType": "LIMIT",
  "price": 17850
}

Response:

{
  "order_id": "12345678",
  "status": "success",
  "message": "Order placed successfully"
}

Run Automated MCP Strategy

Request:

POST /strategy/mcp
Content-Type: application/json

{
  "symbol": "NSE_FO|NIFTY-I",
  "interval": "1D",
  "lookbackDays": 30,
  "investmentAmount": 100000
}

Response:

{
  "strategy": "MCP",
  "action": "BUY",
  "order": {
    "order_id": "12345678",
    "status": "success"
  },
  "mcp": {
    "timestamp": "2023-03-15T09:15:00.000Z",
    "price": 17865.75,
    "connections": 12
  },
  "currentPrice": 17870.25,
  "analysis": {
    "mcpDeviation": "0.03%",
    "connectionStrength": 12
  }
}

Security Considerations

Development Environment

  • Use test API keys with limited permissions
  • Store tokens in memory only (disable file persistence)
  • Enable detailed error logging

Production Environment

  • Use proper API key management with restricted permissions
  • Use a secure database for token storage instead of file system
  • Implement IP whitelisting for API access
  • Enable HTTPS for all connections
  • Use a reverse proxy like Nginx
  • Implement rate limiting

Add the following code to server.js for production mode:

if (process.env.NODE_ENV === 'production') {
  // Force HTTPS
  app.use((req, res, next) => {
    if (req.header('x-forwarded-proto') !== 'https') {
      res.redirect(`https://${req.header('host')}${req.url}`);
    } else {
      next();
    }
  });
  
  // Rate limiting
  const rateLimit = require('express-rate-limit');
  const limiter = rateLimit({
    windowMs: 15 * 60 * 1000, // 15 minutes
    max: 100 // limit each IP to 100 requests per windowMs
  });
  app.use(limiter);
}

Troubleshooting

  • Authentication Issues: Ensure API credentials are correct in .env file
  • Order Placement Errors: Check quantity meets minimum lot size requirements
  • Data Retrieval Issues: Verify symbol format is correct (NSE_FO|SYMBOL-I)
  • Token Refresh Failures: Check if your API key has expired or been revoked

Further Customization

The MCP calculation and trading logic can be customized by modifying:

  1. calculateMCP() function - Adjust connection criteria, pivot point identification
  2. Strategy endpoint - Modify deviation threshold, trend identification logic
  3. Order parameters - Change order types, validity, disclosure quantity

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License.

Recommend Servers
TraeBuild with Free GPT-4.1 & Claude 3.7. Fully MCP-Ready.
Context7Context7 MCP Server -- Up-to-date code documentation for LLMs and AI code editors
Zhipu Web SearchZhipu Web Search MCP Server is a search engine specifically designed for large models. It integrates four search engines, allowing users to flexibly compare and switch between them. Building upon the web crawling and ranking capabilities of traditional search engines, it enhances intent recognition capabilities, returning results more suitable for large model processing (such as webpage titles, URLs, summaries, site names, site icons, etc.). This helps AI applications achieve "dynamic knowledge acquisition" and "precise scenario adaptation" capabilities.
Tavily Mcp
Playwright McpPlaywright MCP server
EdgeOne Pages MCPAn MCP service designed for deploying HTML content to EdgeOne Pages and obtaining an accessible public URL.
DeepChatYour AI Partner on Desktop
CursorThe AI Code Editor
TimeA Model Context Protocol server that provides time and timezone conversion capabilities. This server enables LLMs to get current time information and perform timezone conversions using IANA timezone names, with automatic system timezone detection.
Serper MCP ServerA Serper MCP Server
Jina AI MCP ToolsA Model Context Protocol (MCP) server that integrates with Jina AI Search Foundation APIs.
MiniMax MCPOfficial MiniMax Model Context Protocol (MCP) server that enables interaction with powerful Text to Speech, image generation and video generation APIs.
Baidu Map百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
AiimagemultistyleA Model Context Protocol (MCP) server for image generation and manipulation using fal.ai's Stable Diffusion model.
MCP AdvisorMCP Advisor & Installation - Use the right MCP server for your needs
Visual Studio Code - Open Source ("Code - OSS")Visual Studio Code
WindsurfThe new purpose-built IDE to harness magic
ChatWiseThe second fastest AI chatbot™
Amap Maps高德地图官方 MCP Server
BlenderBlenderMCP connects Blender to Claude AI through the Model Context Protocol (MCP), allowing Claude to directly interact with and control Blender. This integration enables prompt assisted 3D modeling, scene creation, and manipulation.
Howtocook Mcp基于Anduin2017 / HowToCook (程序员在家做饭指南)的mcp server,帮你推荐菜谱、规划膳食,解决“今天吃什么“的世纪难题; Based on Anduin2017/HowToCook (Programmer's Guide to Cooking at Home), MCP Server helps you recommend recipes, plan meals, and solve the century old problem of "what to eat today"