npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

saksh-trading-system

v1.0.2

Published

A trading system package for managing stocks, traders, and trades.

Downloads

178

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