saksh-trading-system
v1.0.2
Published
A trading system package for managing stocks, traders, and trades.
Downloads
4
Maintainers
Readme
Saksh Trading System
Saksh Trading System is a trading system package for managing stocks, traders, and trades. It provides functionalities to add stocks, create traders, perform buy and sell trades, and generate profit/loss reports.
This package is for those who are leaning the trading and it strategies.
Installation
To install the package, use npm:
npm install saksh-trading-system
Usage
Example
Create a file named example.js
to demonstrate how to use the package:
const SakshTradingSystem = require('saksh-trading-system'); // Adjust the path as needed
const dbUri = 'your_mongodb_uri';
const tradingSystem = new SakshTradingSystem(dbUri);
tradingSystem.sakshConnect().then(async () => {
try {
// Add a new stock
const stock = await tradingSystem.sakshAddStock('AAPL', 'Apple Inc.', 150);
console.log('Stock added:', stock);
// Create a new trader
const trader = await tradingSystem.sakshCreateTrader('john_doe');
console.log('Trader created:', trader);
// Buy stock
const buyTrade = await tradingSystem.sakshBuyStock(trader._id, 'AAPL', 10);
console.log('Stock bought:', buyTrade);
// Update stock price
await tradingSystem.sakshUpdateStockPrice('AAPL', 160);
// Sell stock
const sellTrade = await tradingSystem.sakshSellStock(trader._id, 'AAPL', 5);
console.log('Stock sold:', sellTrade);
// Get portfolio
const portfolio = await tradingSystem.sakshGetPortfolio(trader._id);
console.log('Portfolio:', portfolio);
// Get trade history
const tradeHistory = await tradingSystem.sakshGetTradeHistory(trader._id);
console.log('Trade History:', tradeHistory);
// Get profit/loss report
const profitLossReport = await tradingSystem.sakshGetProfitLossReport(trader._id);
console.log('Profit/Loss Report:', profitLossReport);
} catch (error) {
console.error('Error:', error);
}
});
Running the Example
To run the example, execute the following command in your terminal:
node example.js
This will add a new stock, create a trader, perform buy and sell trades, update the stock price, and print the portfolio, trade history, and profit/loss report to the console.
REST API
You can also set up a REST API for this package using Express. Create a file named server.js
:
const express = require('express');
const mongoose = require('mongoose');
const SakshTradingSystem = require('saksh-trading-system'); // Adjust the path as needed
const app = express();
const port = 3000;
const dbUri = 'your_mongodb_uri';
const tradingSystem = new SakshTradingSystem(dbUri);
app.use(express.json());
tradingSystem.sakshConnect().then(() => {
console.log('Connected to MongoDB');
});
// Add a new stock
app.post('/stocks', async (req, res) => {
try {
const { symbol, companyName, currentPrice } = req.body;
const stock = await tradingSystem.sakshAddStock(symbol, companyName, currentPrice);
res.status(201).json(stock);
} catch (error) {
res.status(400).json({ error: error.message });
}
});
// Create a new trader
app.post('/traders', async (req, res) => {
try {
const { username } = req.body;
const trader = await tradingSystem.sakshCreateTrader(username);
res.status(201).json(trader);
} catch (error) {
res.status(400).json({ error: error.message });
}
});
// Buy stock
app.post('/traders/:traderId/buy', async (req, res) => {
try {
const { traderId } = req.params;
const { symbol, quantity } = req.body;
const trade = await tradingSystem.sakshBuyStock(traderId, symbol, quantity);
res.status(201).json(trade);
} catch (error) {
res.status(400).json({ error: error.message });
}
});
// Sell stock
app.post('/traders/:traderId/sell', async (req, res) => {
try {
const { traderId } = req.params;
const { symbol, quantity } = req.body;
const trade = await tradingSystem.sakshSellStock(traderId, symbol, quantity);
res.status(201).json(trade);
} catch (error) {
res.status(400).json({ error: error.message });
}
});
// Get portfolio
app.get('/traders/:traderId/portfolio', async (req, res) => {
try {
const { traderId } = req.params;
const portfolio = await tradingSystem.sakshGetPortfolio(traderId);
res.status(200).json(portfolio);
} catch (error) {
res.status(400).json({ error: error.message });
}
});
// Get trade history
app.get('/traders/:traderId/trades', async (req, res) => {
try {
const { traderId } = req.params;
const tradeHistory = await tradingSystem.sakshGetTradeHistory(traderId);
res.status(200).json(tradeHistory);
} catch (error) {
res.status(400).json({ error: error.message });
}
});
// Get profit/loss report
app.get('/traders/:traderId/profit-loss', async (req, res) => {
try {
const { traderId } = req.params;
const profitLossReport = await tradingSystem.sakshGetProfitLossReport(traderId);
res.status(200).json(profitLossReport);
} catch (error) {
res.status(400).json({ error: error.message });
}
});
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});
Running the Server
To run the server, execute the following command in your terminal:
node server.js
This will start the server on port 3000, and you can interact with the API using tools like Postman or curl.
License
This project is licensed under the ISC License.
support
susheel2339 @ gmail.com